Repository navigation
Expand file tree
/
Copy pathLocalization.cs
More file actions
109 lines (98 loc) · 4.43 KB
/
Copy pathLocalization.cs
File metadata and controls
109 lines (98 loc) · 4.43 KB
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
using System.Globalization;
// UI copy stays next to each control so new dialogs can be translated without a resource loader.
// Unknown preferences and unsupported system languages use English.
internal static class Localization
{
internal const string English = "English";
internal const string German = "Deutsch";
internal const string SwissGerman = "Schwiizerdütsch";
internal const string System = "System";
internal static string Preference { get; private set; } = English;
// True for German and Swiss German: both use the German copy, Swiss German then maps it to dialect.
internal static bool IsGerman { get; private set; }
internal static bool IsSwissGerman { get; private set; }
private static readonly object Gate = new();
private static readonly Dictionary<string, string> EnglishToGerman = new(StringComparer.Ordinal);
private static readonly Dictionary<string, string> GermanToEnglish = new(StringComparer.Ordinal);
internal static void Configure(string? preference)
{
Preference = preference?.Trim() switch
{
German or "German" => German,
SwissGerman or "Swiss German" => SwissGerman,
System => System,
_ => English
};
IsSwissGerman = Preference == SwissGerman;
IsGerman = Preference is German or SwissGerman ||
(Preference == System && CultureInfo.CurrentUICulture.TwoLetterISOLanguageName == "de");
}
internal static string T(string english, string german)
{
Register(english, german);
return IsGerman ? Local(german) : english;
}
// German copy as shown in the current German variant: unchanged, or in Swiss German dialect.
internal static string Local(string german) => IsSwissGerman ? SwissGermanTranslations.Of(german) : german;
internal static void Register(string english, string german)
{
lock (Gate)
{
EnglishToGerman[english] = german;
GermanToEnglish[german] = english;
}
}
private static string Translate(string text)
{
lock (Gate)
{
// Bring any variant back to German first, then render it in the current language.
string german = SwissGermanTranslations.ToGerman(text);
if (EnglishToGerman.TryGetValue(german, out var fromEnglish)) german = fromEnglish;
if (IsGerman) return Local(german);
return GermanToEnglish.TryGetValue(german, out var english) ? english : german;
}
}
// Reapply registered static copy to an existing form after a language change.
// Text boxes and non-string list entries may contain user data and are left alone.
internal static void Apply(Control root)
{
ArgumentNullException.ThrowIfNull(root);
if (root is not TextBoxBase) root.Text = Translate(root.Text);
if (root.AccessibleName is { } accessible) root.AccessibleName = Translate(accessible);
if (root is ComboBox combo && combo.DataSource == null)
{
combo.BeginUpdate();
try
{
int selected = combo.SelectedIndex;
for (int i = 0; i < combo.Items.Count; i++)
if (combo.Items[i] is string item) combo.Items[i] = Translate(item);
if (selected >= 0 && selected < combo.Items.Count) combo.SelectedIndex = selected;
}
finally { combo.EndUpdate(); }
}
else if (root is ListBox list && root is not CheckedListBox && list.DataSource == null)
{
list.BeginUpdate();
try
{
for (int i = 0; i < list.Items.Count; i++)
if (list.Items[i] is string item) list.Items[i] = Translate(item);
}
finally { list.EndUpdate(); }
}
if (root is ToolStrip strip)
foreach (ToolStripItem item in strip.Items) Apply(item);
foreach (Control child in root.Controls) Apply(child);
if (root.ContextMenuStrip is { } menu)
foreach (ToolStripItem item in menu.Items) Apply(item);
}
private static void Apply(ToolStripItem item)
{
if (item.Text is { } text) item.Text = Translate(text);
if (item.AccessibleName is { } accessible) item.AccessibleName = Translate(accessible);
if (item is ToolStripDropDownItem dropdown)
foreach (ToolStripItem child in dropdown.DropDownItems) Apply(child);
}
}