Keys are now much more forgiving (ignores whitespace casing why not)

This commit is contained in:
Nick 2024-03-08 10:59:25 -05:00
parent 9e8f8dd1df
commit ccd3e297e6
3 changed files with 20 additions and 3 deletions

View File

@ -1,8 +1,10 @@
using NewHorizons.External.Configs; using NewHorizons.External.Configs;
using NewHorizons.Utility;
using NewHorizons.Utility.OWML; using NewHorizons.Utility.OWML;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text.RegularExpressions;
namespace NewHorizons.Handlers namespace NewHorizons.Handlers
{ {
@ -49,8 +51,17 @@ namespace NewHorizons.Handlers
// Get the translated text // Get the translated text
if (dictionary.TryGetValue(language, out var table)) if (dictionary.TryGetValue(language, out var table))
{
if (table.TryGetValue(text, out var translatedText)) if (table.TryGetValue(text, out var translatedText))
{
return translatedText; return translatedText;
}
// Try without whitespace if its missing
else if (table.TryGetValue(text.TruncateWhitespace(), out translatedText))
{
return translatedText;
}
}
if (warn) NHLogger.LogVerbose($"Defaulting to english for {text}"); if (warn) NHLogger.LogVerbose($"Defaulting to english for {text}");
@ -85,8 +96,8 @@ namespace NewHorizons.Handlers
if (!_dialogueTranslationDictionary.ContainsKey(language)) _dialogueTranslationDictionary.Add(language, new Dictionary<string, string>()); if (!_dialogueTranslationDictionary.ContainsKey(language)) _dialogueTranslationDictionary.Add(language, new Dictionary<string, string>());
foreach (var originalKey in config.DialogueDictionary.Keys) foreach (var originalKey in config.DialogueDictionary.Keys)
{ {
var key = originalKey.Replace("&lt;", "<").Replace("&gt;", ">").Replace("<![CDATA[", "").Replace("]]>", ""); var key = originalKey.Replace("\\n", "\n").TruncateWhitespace().Replace("&lt;", "<").Replace("&gt;", ">").Replace("<![CDATA[", "").Replace("]]>", "");
var value = config.DialogueDictionary[originalKey].Replace("&lt;", "<").Replace("&gt;", ">").Replace("<![CDATA[", "").Replace("]]>", ""); var value = config.DialogueDictionary[originalKey].Replace("\\n", "\n").Replace("&lt;", "<").Replace("&gt;", ">").Replace("<![CDATA[", "").Replace("]]>", "");
if (!_dialogueTranslationDictionary[language].ContainsKey(key)) _dialogueTranslationDictionary[language].Add(key, value); if (!_dialogueTranslationDictionary[language].ContainsKey(key)) _dialogueTranslationDictionary[language].Add(key, value);
else _dialogueTranslationDictionary[language][key] = value; else _dialogueTranslationDictionary[language][key] = value;

View File

@ -350,5 +350,11 @@ namespace NewHorizons.Utility
{ {
return parentNode.ChildNodes.Cast<XmlNode>().First(node => node.LocalName == tagName); return parentNode.ChildNodes.Cast<XmlNode>().First(node => node.LocalName == tagName);
} }
public static string TruncateWhitespace(this string text)
{
// return Regex.Replace(text.Trim(), @"[^\S\r\n]+", "GUH");
return Regex.Replace(text.Trim(), @"\s+", " ").ToLowerInvariant();
}
} }
} }

View File

@ -4,7 +4,7 @@
"author": "xen, Bwc9876, JohnCorby, MegaPiggy, Clay, Trifid, and friends", "author": "xen, Bwc9876, JohnCorby, MegaPiggy, Clay, Trifid, and friends",
"name": "New Horizons", "name": "New Horizons",
"uniqueName": "xen.NewHorizons", "uniqueName": "xen.NewHorizons",
"version": "1.18.9", "version": "1.18.10",
"owmlVersion": "2.9.8", "owmlVersion": "2.9.8",
"dependencies": [ "JohnCorby.VanillaFix", "_nebula.MenuFramework", "xen.CommonCameraUtility", "dgarro.CustomShipLogModes" ], "dependencies": [ "JohnCorby.VanillaFix", "_nebula.MenuFramework", "xen.CommonCameraUtility", "dgarro.CustomShipLogModes" ],
"conflicts": [ "Raicuparta.QuantumSpaceBuddies", "PacificEngine.OW_CommonResources" ], "conflicts": [ "Raicuparta.QuantumSpaceBuddies", "PacificEngine.OW_CommonResources" ],