Add another translation dictionary that keeps CDATA

This commit is contained in:
Nick 2023-08-08 00:20:47 -04:00
parent fea15287c6
commit f765e07859
4 changed files with 35 additions and 6 deletions

View File

@ -22,7 +22,9 @@
"DEBUG_PLACE_TEXT": "Place Nomai Text",
"DEBUG_UNDO": "Undo",
"DEBUG_REDO": "Redo",
"Vessel": "Vessel",
"Vessel": "Vessel"
},
"OtherDictionary": {
"NOMAI_SHUTTLE_COMPUTER": "The <![CDATA[<color=orange>shuttle</color>]]> is currently resting at <![CDATA[<color=lightblue>{0}</color>]]>."
},
"AchievementTranslations": {

View File

@ -159,7 +159,7 @@ namespace NewHorizons.Builder.Props
var shuttle = ShuttleBuilder.Shuttles[id];
var planet = AstroObjectLocator.GetPlanetName(shuttle.GetComponentInParent<AstroObject>());
var displayText = TranslationHandler.GetTranslation("NOMAI_SHUTTLE_COMPUTER", TranslationHandler.TextType.UI).Replace("{0}", planet);
var displayText = TranslationHandler.GetTranslation("NOMAI_SHUTTLE_COMPUTER", TranslationHandler.TextType.OTHER).Replace("{0}", planet);
NHLogger.Log(displayText);
var xmlContent = $"<NomaiObject>\r\n <TextBlock>\r\n <ID>1</ID>\r\n <Text>{displayText}</Text>\r\n </TextBlock>\r\n</NomaiObject>";

View File

@ -23,6 +23,11 @@ namespace NewHorizons.External.Configs
/// </summary>
public Dictionary<string, string> UIDictionary;
/// <summary>
/// Translation table for other text, will be taken verbatim without correcting CDATA
/// </summary>
public Dictionary<string, string> OtherDictionary;
#region Achievements+
// This only exists for schema generation, Achievements+ handles the parsing
#pragma warning disable 0169
@ -63,6 +68,10 @@ namespace NewHorizons.External.Configs
UIDictionary =
(Dictionary<string, string>) (dict[nameof(UIDictionary)] as JObject).ToObject(
typeof(Dictionary<string, string>));
if (dict.ContainsKey(nameof(OtherDictionary)))
OtherDictionary =
(Dictionary<string, string>)(dict[nameof(OtherDictionary)] as JObject).ToObject(
typeof(Dictionary<string, string>));
}
}
}

View File

@ -11,12 +11,14 @@ namespace NewHorizons.Handlers
private static Dictionary<TextTranslation.Language, Dictionary<string, string>> _shipLogTranslationDictionary = new();
private static Dictionary<TextTranslation.Language, Dictionary<string, string>> _dialogueTranslationDictionary = new();
private static Dictionary<TextTranslation.Language, Dictionary<string, string>> _uiTranslationDictionary = new();
private static Dictionary<TextTranslation.Language, Dictionary<string, string>> _otherTranslationDictionary = new();
public enum TextType
{
SHIPLOG,
DIALOGUE,
UI
UI,
OTHER
}
public static string GetTranslation(string text, TextType type) => GetTranslation(text, type, true);
@ -36,6 +38,9 @@ namespace NewHorizons.Handlers
break;
case TextType.UI:
dictionary = _uiTranslationDictionary;
break;
case TextType.OTHER:
dictionary = _otherTranslationDictionary;
break;
default:
if (warn) NHLogger.LogVerbose($"Invalid TextType {type}");
@ -93,14 +98,27 @@ namespace NewHorizons.Handlers
if (!_uiTranslationDictionary.ContainsKey(language)) _uiTranslationDictionary.Add(language, new Dictionary<string, string>());
foreach (var originalKey in config.UIDictionary.Keys)
{
// Don't remove CDATA from UI
var key = originalKey.Replace("&lt;", "<").Replace("&gt;", ">");
var value = config.UIDictionary[originalKey].Replace("&lt;", "<").Replace("&gt;", ">");
var key = originalKey.Replace("&lt;", "<").Replace("&gt;", ">").Replace("<![CDATA[", "").Replace("]]>", "");
var value = config.UIDictionary[originalKey].Replace("&lt;", "<").Replace("&gt;", ">").Replace("<![CDATA[", "").Replace("]]>", "");
if (!_uiTranslationDictionary[language].ContainsKey(key)) _uiTranslationDictionary[language].Add(key, value);
else _uiTranslationDictionary[language][key] = value;
}
}
if (config.OtherDictionary != null && config.OtherDictionary.Count() > 0)
{
if (!_otherTranslationDictionary.ContainsKey(language)) _otherTranslationDictionary.Add(language, new Dictionary<string, string>());
foreach (var originalKey in config.OtherDictionary.Keys)
{
// Don't remove CDATA
var key = originalKey.Replace("&lt;", "<").Replace("&gt;", ">");
var value = config.OtherDictionary[originalKey].Replace("&lt;", "<").Replace("&gt;", ">");
if (!_otherTranslationDictionary[language].ContainsKey(key)) _otherTranslationDictionary[language].Add(key, value);
else _otherTranslationDictionary[language][key] = value;
}
}
}
public static void AddDialogue(string rawText, bool trimRawTextForKey = false, params string[] rawPreText)