feat: use english localization for untranslated strings

This commit is contained in:
bd_ 2023-01-17 20:06:50 +09:00
parent 9f9c2af1bd
commit 305377c1bb
6 changed files with 66 additions and 2 deletions

View File

@ -0,0 +1,35 @@
using nadena.dev.modular_avatar.core.editor;
using NUnit.Framework;
namespace modular_avatar_tests
{
public class LocalizationTest
{
[SetUp]
public void Setup()
{
Localization.OverrideLanguage = null;
Localization.Reload();
}
[TearDown]
public void Teardown()
{
Localization.OverrideLanguage = null;
}
[Test]
public void TestLanguageFallback()
{
Localization.OverrideLanguage = "test";
Assert.AreEqual(Localization.S("test0.test_a"), "replaced");
Assert.AreEqual(Localization.S("test0.test_b"), "test_b");
Assert.AreEqual(Localization.S("test0.test_c"), "test0.test_c");
Localization.OverrideLanguage = "en";
Assert.AreEqual(Localization.S("test0.test_a"), "test_a");
Assert.AreEqual(Localization.S("test0.test_b"), "test_b");
Assert.AreEqual(Localization.S("test0.test_c"), "test0.test_c");
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: f029beaecb7c47b2889ce683b71b219f
timeCreated: 1673953287

View File

@ -11,6 +11,8 @@ namespace nadena.dev.modular_avatar.core.editor
{ {
internal static class Localization internal static class Localization
{ {
private const string FallbackLanguage = "en";
private const string localizationPathGuid = "488c994003974b3ab2796371cf627bca"; private const string localizationPathGuid = "488c994003974b3ab2796371cf627bca";
private static string localizationPathRoot = AssetDatabase.GUIDToAssetPath(localizationPathGuid); private static string localizationPathRoot = AssetDatabase.GUIDToAssetPath(localizationPathGuid);
@ -29,6 +31,8 @@ namespace nadena.dev.modular_avatar.core.editor
private static Dictionary<string, ImmutableDictionary<string, string>> Cache private static Dictionary<string, ImmutableDictionary<string, string>> Cache
= new Dictionary<string, ImmutableDictionary<string, string>>(); = new Dictionary<string, ImmutableDictionary<string, string>>();
internal static string OverrideLanguage { get; set; } = null;
[MenuItem("Tools/Modular Avatar/Reload localizations")] [MenuItem("Tools/Modular Avatar/Reload localizations")]
public static void Reload() public static void Reload()
{ {
@ -42,12 +46,26 @@ namespace nadena.dev.modular_avatar.core.editor
return info; return info;
} }
var fallback = lang == FallbackLanguage
? ImmutableDictionary<string, string>.Empty
: GetLocalization(FallbackLanguage);
var filename = localizationPathRoot + "/" + lang + ".json"; var filename = localizationPathRoot + "/" + lang + ".json";
try try
{ {
var langData = File.ReadAllText(filename); var langData = File.ReadAllText(filename);
info = JsonConvert.DeserializeObject<Dictionary<string, string>>(langData).ToImmutableDictionary(); var tmp = JsonConvert.DeserializeObject<Dictionary<string, string>>(langData);
foreach (var kvp in fallback)
{
if (!tmp.ContainsKey(kvp.Key))
{
tmp[kvp.Key] = kvp.Value;
}
}
info = tmp.ToImmutableDictionary();
Cache[lang] = info; Cache[lang] = info;
return info; return info;
} }
@ -86,7 +104,7 @@ namespace nadena.dev.modular_avatar.core.editor
public static string GetSelectedLocalization() public static string GetSelectedLocalization()
{ {
return EditorPrefs.GetString("nadena.dev.modularavatar.lang", "en"); return OverrideLanguage ?? EditorPrefs.GetString("nadena.dev.modularavatar.lang", "en");
} }
public static void ShowLanguageUI() public static void ShowLanguageUI()

View File

@ -1,4 +1,6 @@
{ {
"test0.test_a": "test_a",
"test0.test_b": "test_b",
"boneproxy.foldout.advanced": "Advanced", "boneproxy.foldout.advanced": "Advanced",
"boneproxy.target": "Target", "boneproxy.target": "Target",
"menuinstall.help.hint_set_menu": "This prefab will be installed to the root menu of your avatar by default. Select a different menu or uncheck the component's enabled checkbox to prevent this.", "menuinstall.help.hint_set_menu": "This prefab will be installed to the root menu of your avatar by default. Select a different menu or uncheck the component's enabled checkbox to prevent this.",

View File

@ -0,0 +1,3 @@
{
"test0.test_a": "replaced"
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: a851b660ad5443bf92884fdf8e872c4a
timeCreated: 1673953035