forked from fattard/FlagsEditorEXPlugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LocalizedStrings.cs
147 lines (124 loc) · 4.76 KB
/
LocalizedStrings.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
146
147
namespace FlagsEditorEXPlugin
{
public static class LocalizedStrings
{
readonly static Dictionary<string, string> s_localizedStrings = [];
static string s_langCode = "en";
public static void Initialize(string langCode)
{
if (s_localizedStrings.Count == 0 || s_langCode != langCode)
{
s_localizedStrings.Clear();
s_langCode = langCode;
var res = ReadLangResFile(langCode);
using (System.IO.StringReader reader = new System.IO.StringReader(res))
{
string? s = reader.ReadLine();
while (s is not null)
{
if (!string.IsNullOrWhiteSpace(s))
{
var entry = s.Split('=');
s_localizedStrings[entry[0]] = entry[1];
}
s = reader.ReadLine();
}
if (s is null)
{
return;
}
}
}
}
public static string Find(string key, string current)
{
return s_localizedStrings.TryGetValue(key, out string? value) ? value : current;
}
public static void LocalizeForm(Control form)
{
form.SuspendLayout();
string formName = form.GetType().Name;
form.Text = Find($"{formName}.TitleName", form.Text);
var listOfControls = AssembleLocalizableControlsList(form);
foreach (var c in listOfControls)
{
if (c is DataGridView dgv)
{
var columns = dgv.Columns;
foreach (DataGridViewColumn dgv_c in columns)
{
var current = dgv_c.HeaderText;
var updated = Find($"{formName}.{dgv_c.Name}", current);
if (!ReferenceEquals(current, updated))
{
dgv_c.HeaderText = updated;
}
}
}
else if (c is Control r)
{
var current = r.Text;
if (!string.IsNullOrEmpty(current))
{
var updated = Find($"{formName}.{r.Name}", current);
if (!ReferenceEquals(current, updated))
{
r.Text = updated;
}
}
}
}
form.ResumeLayout();
}
static List<Control> AssembleLocalizableControlsList(Control formRoot)
{
List<Control> controlsList = [];
foreach (var c in formRoot.Controls)
{
if (c is ListControl or TextBoxBase or LinkLabel or NumericUpDown or ContainerControl or VScrollBar or HScrollBar)
continue;
else if (c is Control r)
{
if (r.HasChildren)
{
controlsList.AddRange(AssembleLocalizableControlsList(r));
}
controlsList.Add(r);
}
}
return controlsList;
}
static string ReadLangResFile(string langCode)
{
string? contentTxt = null;
var assembly = System.Reflection.Assembly.GetExecutingAssembly();
string resName = $"lang_{langCode}.txt";
// Try outside file first
var offResPath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(assembly.Location)!, resName);
if (!System.IO.File.Exists(offResPath))
{
try
{
resName = assembly.GetManifestResourceNames().Single(str => str.EndsWith(resName));
}
catch (InvalidOperationException)
{
// Sanity Fallback
return ReadLangResFile("en");
}
using (var stream = assembly.GetManifestResourceStream(resName))
{
using (var reader = new System.IO.StreamReader(stream!))
{
contentTxt = reader.ReadToEnd();
}
}
}
else
{
contentTxt = System.IO.File.ReadAllText(offResPath);
}
return contentTxt;
}
}
}