forked from AssetRipper/AssetRipper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocalization.cs
46 lines (39 loc) · 1.35 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
namespace AssetRipper.GUI.Localizations;
public static partial class Localization
{
private static Dictionary<string, string> CurrentDictionary { get; set; }
/// <summary>
/// The (English) dictionary to use if <see cref="CurrentDictionary"/> doesn't have a key.
/// </summary>
private static Dictionary<string, string> FallbackDictionary { get; }
/// <summary>
/// <see href="https://www.iana.org/assignments/language-subtag-registry/language-subtag-registry" >IANA</see> language code
/// </summary>
public static string CurrentLanguageCode { get; private set; }
public static event Action OnLanguageChanged = () => { };
static Localization()
{
LoadLanguage("en-US");
FallbackDictionary = CurrentDictionary;
}
[MemberNotNull(nameof(CurrentDictionary), nameof(CurrentLanguageCode))]
public static void LoadLanguage(string code)
{
CurrentLanguageCode = LocalizationLoader.AsHyphenatedLanguageCode(code);
CurrentDictionary = LocalizationLoader.LoadLanguage(CurrentLanguageCode);
OnLanguageChanged();
}
private static string Get(string key)
{
if (CurrentDictionary.TryGetValue(key, out string? ret) && !string.IsNullOrEmpty(ret))
{
return ret;
}
if (FallbackDictionary.TryGetValue(key, out ret))
{
return ret;
}
//This should never happen unless I edit the json without running the source generator.
return $"__{key}__?";
}
}