-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathLocalization.cs
87 lines (73 loc) · 2.81 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
using System.Text;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Unicode;
namespace ItemPrinterDeGacha.WinForms;
public sealed class Localization
{
public static Localization Instance { get; private set; } = new();
public string ErrorMinMax { get; init; } = "Min must be less than or equal to Max.";
public string ErrorNoItem { get; init; } = "No item specified";
public string ErrorInvalidSearchCriteria { get; init; } = "Invalid search criteria.";
public string F1_Count { get; init; } = "Count: {0}";
public string F1_Time { get; init; } = "Time: {0}";
public string F1_Seed { get; init; } = "Seed: {0}";
public string F2_CountItem { get; init; } = "x{0} {1}"; // x4 Master Ball
public string F3_ModeAtTimeSeed { get; init; } = "{0} @ {1} -- {2}";
public Dictionary<string, string> Others { get; init; } = [];
public static void Initialize(string lang)
{
try { Instance = Load(lang); }
catch { }
}
private static string GetResourceName(ReadOnlySpan<char> language) => $"message_{language}";
public static Localization Load(ReadOnlySpan<char> language)
{
var name = GetResourceName(language);
var text = ResourceUtil.GetString(name);
if (text is not { Length: > 0 })
return new Localization();
return JsonSerializer.Deserialize<Localization>(text, Options)!;
}
private static readonly JsonSerializerOptions Options = new() {
WriteIndented = true,
AllowTrailingCommas = true,
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All),
};
#if DEBUG
public void Save(string path, ReadOnlySpan<char> language)
{
var fileName = GetResourceName(language) + ".txt";
var text = JsonSerializer.Serialize(this, Options);
File.WriteAllText(Path.Combine(path, fileName), text, Encoding.UTF8);
}
#endif
public string[] LocalizeEnum<T>() where T : struct, Enum
{
var type = typeof(T);
var names = Enum.GetNames<T>();
var result = new string[names.Length];
for (int i = 0; i < result.Length; i++)
result[i] = Localize($"{type.Name}.{names[i]}", names[i]);
return result;
}
public string LocalizeEnum<T>(T value) where T : struct, Enum
{
var type = typeof(T);
var name = Enum.GetName(value) ?? $"{value}";
return Localize($"{type.Name}.{name}", name);
}
public void CacheEnum<T>() where T : struct, Enum
{
var type = typeof(T);
foreach (var name in Enum.GetNames<T>())
Localize($"{type.Name}.{name}", name);
}
private string Localize(string key, string fallback)
{
if (Others.TryGetValue(key, out var value))
return value;
Others.Add(key, fallback);
return fallback;
}
}