-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathLocalization.cs
145 lines (121 loc) · 4.68 KB
/
Localization.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using nadena.dev.modular_avatar.ui;
using nadena.dev.ndmf.localization;
using nadena.dev.ndmf.ui;
using Newtonsoft.Json;
using UnityEditor;
using UnityEngine;
namespace nadena.dev.modular_avatar.core.editor
{
[InitializeOnLoad] // ensure that we register languages with NDMF on domain load
internal static class Localization
{
public static event Action OnLangChange;
private const string FallbackLanguage = "en";
private const string localizationPathGuid = "488c994003974b3ab2796371cf627bca";
private static string localizationPathRoot = AssetDatabase.GUIDToAssetPath(localizationPathGuid);
private static ImmutableDictionary<string, string> SupportedLanguageDisplayNames
= ImmutableDictionary<string, string>.Empty
.Add("en-us", "English")
.Add("ja-jp", "日本語")
.Add("zh-hans", "简体中文")
.Add("ko-kr", "한국어");
private static ImmutableList<string>
SupportedLanguages = new string[] {"en-us", "ja-jp", "zh-hans", "ko-kr"}.ToImmutableList();
private static string[] DisplayNames = SupportedLanguages.Select(l =>
{
return SupportedLanguageDisplayNames.TryGetValue(l, out var displayName) ? displayName : l;
}).ToArray();
private static Dictionary<string, ImmutableDictionary<string, string>> Cache
= new Dictionary<string, ImmutableDictionary<string, string>>();
internal static string OverrideLanguage { get; set; } = null;
public static Localizer L { get; private set; }
public static UIElementLocalizer UI;
static Localization()
{
Localizer localizer = new Localizer(SupportedLanguages[0], () =>
{
List<(string, Func<string, string>)> languages = new List<(string, Func<string, string>)>();
foreach (var lang in SupportedLanguages)
{
languages.Add((lang, LanguageLookup(lang)));
}
return languages;
});
L = localizer;
UI = new UIElementLocalizer(L);
LanguagePrefs.RegisterLanguageChangeCallback(typeof(Localization), _ => OnLangChange?.Invoke());
}
private static Func<string,string> LanguageLookup(string lang)
{
var filename = localizationPathRoot + "/" + lang + ".json";
try
{
var langData = File.ReadAllText(filename);
var langMap = JsonConvert.DeserializeObject<Dictionary<string, string>>(langData);
// return langMap.GetValueOrDefault; - Unity 2019 doesn't have this extension method
return key =>
{
if (langMap.TryGetValue(key, out var val)) return val;
else return null;
};
}
catch (Exception e)
{
Debug.LogError("Failed to load language file " + filename);
Debug.LogException(e);
return (k) => null;
}
}
[MenuItem(UnityMenuItems.TopMenu_ReloadLocalizations, false, UnityMenuItems.TopMenu_ReloadLocalizationsOrder)]
public static void Reload()
{
Localizer.ReloadLocalizations();
Cache.Clear();
}
public static GUIContent G(string key)
{
var tooltip = S(key + ".tooltip", null);
return tooltip != null ? new GUIContent(S(key), tooltip) : new GUIContent(S(key));
}
public static string S(string key)
{
return S(key, key);
}
public static string S_f(string key, params string[] format)
{
try
{
return string.Format(S(key, key), format);
}
catch (FormatException)
{
return S(key, key) + "(" + string.Join(", ", format) + ")";
}
}
public static string S(string key, string defValue)
{
if (L.TryGetLocalizedString(key, out var val))
{
return val;
}
else
{
return defValue;
}
}
public static string GetSelectedLocalization()
{
return LanguagePrefs.Language;
}
public static void ShowLanguageUI()
{
EditorGUILayout.Separator();
LanguageSwitcher.DrawImmediate();
}
}
}