-
-
Notifications
You must be signed in to change notification settings - Fork 250
/
Copy pathLoc.cs
130 lines (116 loc) · 4.54 KB
/
Loc.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
using System.Globalization;
using System.Resources;
using System.Threading;
using System.Windows.Forms;
namespace ARKBreedingStats
{
/// <summary>
/// static class for localizations
/// </summary>
internal static class Loc
{
private static ResourceManager _rm;
/// <summary>
/// Second language can be used in parts of the app.
/// </summary>
private static CultureInfo _secondaryCulture;
public static void LoadResourceFile(string language, string language2 = null)
{
CultureInfo LoadCultureInfo(string l)
{
if (string.IsNullOrEmpty(l))
{
return null;
}
try
{
return new CultureInfo(l);
}
catch (CultureNotFoundException)
{
return null;
}
}
if (!string.IsNullOrEmpty(language2) && language2 != language)
_secondaryCulture = LoadCultureInfo(language2);
else _secondaryCulture = null;
var culture = LoadCultureInfo(language) ?? CultureInfo.CurrentCulture;
Thread.CurrentThread.CurrentUICulture = culture;
if (_rm == null)
_rm = new ResourceManager("ARKBreedingStats.local.strings", typeof(Form1).Assembly);
}
public static bool UseSecondaryCulture => _secondaryCulture != null;
/// <summary>
/// Returns the localized string.
/// </summary>
public static string S(string key, bool returnKeyIfValueNa = true, bool secondaryCulture = false)
{
if (_rm == null) return null;
if (secondaryCulture && _secondaryCulture != null)
return S(key, _secondaryCulture, returnKeyIfValueNa);
var s = _rm.GetString(key);
//if (string.IsNullOrEmpty(s) && !key.EndsWith("TT")) System.Console.WriteLine("missing: " + key); // for debugging
return s ?? (returnKeyIfValueNa ? key : null);
}
/// <summary>
/// Returns the localized string of the given culture.
/// </summary>
public static string S(string key, CultureInfo culture, bool returnKeyIfValueNa = true)
{
if (_rm == null) return null;
string s = _rm.GetString(key, culture);
//if (string.IsNullOrEmpty(s) && !key.EndsWith("TT")) System.Console.WriteLine("missing: " + key); // for debugging
return s ?? (returnKeyIfValueNa ? key : null);
}
public static void ControlText(Control c) => c.Text = S(c.Name);
public static void ControlText(Control c, string key) => c.Text = S(key);
/// <summary>
/// Sets the Text property of the control to the localized string, using the control.Name as key.
/// If there is a key with an appended TT, a tooltip is set.
/// </summary>
public static void ControlText(Control c, ToolTip tt)
{
c.Text = S(c.Name);
tt.SetToolTip(c, S(c.Name + "TT", false));
}
/// <summary>
/// Sets the tooltip of the control using the localization key of the control name with TT appended.
/// </summary>
public static void SetToolTip(Control c, ToolTip tt)
{
tt.SetToolTip(c, S(c.Name + "TT", false));
}
/// <summary>
/// Sets the tooltip of the control using the custom key.
/// </summary>
public static void SetToolTip(Control c, string key, ToolTip tt)
{
tt.SetToolTip(c, S(key));
}
/// <summary>
/// Sets the Text of the control according to the key. Sets the tooltip according to the controlName
/// </summary>
/// <param name="c"></param>
/// <param name="key"></param>
/// <param name="tt"></param>
public static void ControlText(Control c, string key, ToolTip tt)
{
c.Text = S(key);
tt.SetToolTip(c, S(c.Name + "TT", false));
}
public static void ControlText(ToolStripItem i)
{
i.Text = S(i.Name);
string tt = S(i.Name + "TT", false);
if (!string.IsNullOrEmpty(tt))
i.ToolTipText = tt;
}
public static void ControlText(ToolStripItem i, string key)
{
i.Text = S(key);
string tt = S(key + "TT", false);
if (!string.IsNullOrEmpty(tt))
i.ToolTipText = tt;
}
}
}