mirror of
https://github.com/bdunderscore/modular-avatar.git
synced 2024-12-29 18:55:06 +08:00
feat: use english localization for untranslated strings
This commit is contained in:
parent
9f9c2af1bd
commit
305377c1bb
35
Assets/_ModularAvatar/EditModeTests/LocalizationTest.cs
Normal file
35
Assets/_ModularAvatar/EditModeTests/LocalizationTest.cs
Normal 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");
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f029beaecb7c47b2889ce683b71b219f
|
||||
timeCreated: 1673953287
|
@ -11,6 +11,8 @@ namespace nadena.dev.modular_avatar.core.editor
|
||||
{
|
||||
internal static class Localization
|
||||
{
|
||||
private const string FallbackLanguage = "en";
|
||||
|
||||
private const string localizationPathGuid = "488c994003974b3ab2796371cf627bca";
|
||||
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
|
||||
= new Dictionary<string, ImmutableDictionary<string, string>>();
|
||||
|
||||
internal static string OverrideLanguage { get; set; } = null;
|
||||
|
||||
[MenuItem("Tools/Modular Avatar/Reload localizations")]
|
||||
public static void Reload()
|
||||
{
|
||||
@ -42,12 +46,26 @@ namespace nadena.dev.modular_avatar.core.editor
|
||||
return info;
|
||||
}
|
||||
|
||||
var fallback = lang == FallbackLanguage
|
||||
? ImmutableDictionary<string, string>.Empty
|
||||
: GetLocalization(FallbackLanguage);
|
||||
|
||||
var filename = localizationPathRoot + "/" + lang + ".json";
|
||||
|
||||
try
|
||||
{
|
||||
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;
|
||||
return info;
|
||||
}
|
||||
@ -86,7 +104,7 @@ namespace nadena.dev.modular_avatar.core.editor
|
||||
|
||||
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()
|
||||
|
@ -1,4 +1,6 @@
|
||||
{
|
||||
"test0.test_a": "test_a",
|
||||
"test0.test_b": "test_b",
|
||||
"boneproxy.foldout.advanced": "Advanced",
|
||||
"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.",
|
||||
|
@ -0,0 +1,3 @@
|
||||
{
|
||||
"test0.test_a": "replaced"
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a851b660ad5443bf92884fdf8e872c4a
|
||||
timeCreated: 1673953035
|
Loading…
Reference in New Issue
Block a user