fix: 翻译模糊匹配 - 规范化空白字符解决换行/空格变体问题

- GetSourceText: 精确匹配失败后尝试规范化空白匹配
- Translate: TryGetValue失败后尝试规范化空白匹配
- 所有换行符(\\r\\n/\\n)和空格变体都能匹配到翻译表
This commit is contained in:
ayuan9957 2026-07-16 17:10:52 +08:00
parent 1458908284
commit 7c0c52ac9f

View File

@ -704,6 +704,16 @@ public class LocalizationManager : MonoBehaviour
string[] values;
if (table.TryGetValue(source, out values) && values != null && language < values.Length && values[language] != null)
return values[language];
// Fuzzy: try normalized lookup
string normalized = System.Text.RegularExpressions.Regex.Replace(source, @"\s+", " ").Trim();
foreach (KeyValuePair<string, string[]> pair in table)
{
string nk = System.Text.RegularExpressions.Regex.Replace(pair.Key, @"\s+", " ").Trim();
if (nk == normalized && pair.Value != null && language < pair.Value.Length && pair.Value[language] != null)
return pair.Value[language];
}
return source;
}
@ -711,6 +721,7 @@ public class LocalizationManager : MonoBehaviour
{
if (string.IsNullOrEmpty(value) || table.ContainsKey(value)) return value;
// Exact match
foreach (KeyValuePair<string, string[]> pair in table)
{
if (pair.Value == null || pair.Value.Length <= 1) continue;
@ -721,6 +732,21 @@ public class LocalizationManager : MonoBehaviour
}
}
// Fuzzy match: normalize whitespace (newlines, tabs, multiple spaces → single space)
string normalizedValue = System.Text.RegularExpressions.Regex.Replace(value, @"\s+", " ").Trim();
foreach (KeyValuePair<string, string[]> pair in table)
{
if (pair.Value == null || pair.Value.Length <= 1) continue;
string normalizedKey = System.Text.RegularExpressions.Regex.Replace(pair.Key, @"\s+", " ").Trim();
if (normalizedKey == normalizedValue) return pair.Key;
for (int i = 1; i < pair.Value.Length; i++)
{
if (pair.Value[i] == null) continue;
string normalizedVal = System.Text.RegularExpressions.Regex.Replace(pair.Value[i], @"\s+", " ").Trim();
if (normalizedVal == normalizedValue) return pair.Key;
}
}
return value;
}