Skip to content

Commit 07ac386

Browse files
committed
Enhanced TextHelper to support different locales
1 parent 1c2ec9c commit 07ac386

File tree

2 files changed

+152
-18
lines changed

2 files changed

+152
-18
lines changed
Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,48 @@
11
namespace PluginCore.Localization
22
{
3-
public enum LocaleVersion
3+
public enum LocaleVersion : sbyte
44
{
5+
/// <summary>
6+
/// English - UNITED STATES (US)
7+
/// </summary>
58
[StringValue("en_US")]
69
en_US = 0,
710

11+
/// <summary>
12+
/// Japanese - JAPAN (JP)
13+
/// </summary>
814
[StringValue("ja_JP")]
915
ja_JP = 1,
1016

17+
/// <summary>
18+
/// German - GERMANY (DE)
19+
/// </summary>
1120
[StringValue("de_DE")]
1221
de_DE = 2,
1322

23+
/// <summary>
24+
/// Basque - SPAIN (ES)
25+
/// </summary>
1426
[StringValue("eu_ES")]
1527
eu_ES = 3,
1628

29+
/// <summary>
30+
/// Chinese - CHINA (CN)
31+
/// </summary>
1732
[StringValue("zh_CN")]
18-
zh_CN = 4
33+
zh_CN = 4,
1934

35+
/// <summary>
36+
/// Korean - KOREA, REPUBLIC OF (KR)
37+
/// </summary>
38+
[StringValue("ko_KR")]
39+
ko_KR = 5,
40+
41+
/// <summary>
42+
/// Represents an invalid locale.
43+
/// </summary>
44+
[System.ComponentModel.Browsable(false)]
45+
Invalid = -1
2046
}
2147

2248
}

PluginCore/PluginCore/Localization/TextHelper.cs

Lines changed: 124 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,126 @@
55

66
namespace PluginCore.Localization
77
{
8-
public class TextHelper
8+
/// <summary>
9+
/// Provides methods to retrieve and modify localized resource strings.
10+
/// </summary>
11+
public static class TextHelper
912
{
10-
private static ResourceManager resourceManager = null;
11-
private static LocaleVersion storedLocale = LocaleVersion.en_US;
13+
static ResourceManager resourceManager;
14+
static LocaleVersion storedLocale = LocaleVersion.Invalid;
1215

1316
/// <summary>
14-
/// Gets the specified localized string
17+
/// Gets the specified localized string.
18+
/// <para/>
19+
/// The calling assembly's name will be used as the default prefix for the specified key.
20+
/// If the key with the default prefix does not exist, the key without the default prefix
21+
/// will be used to retrieve the localized string.
22+
/// <see cref="String.Empty"/> is returned if the key does not exist.
1523
/// </summary>
24+
/// <param name="key">The key used to retrieve the localized string.</param>
1625
public static String GetString(String key)
1726
{
18-
String result; String prefix;
19-
if (PluginBase.MainForm == null || PluginBase.MainForm.Settings == null) return key;
20-
LocaleVersion localeSetting = PluginBase.MainForm.Settings.LocaleVersion;
21-
if (resourceManager == null || localeSetting != storedLocale)
27+
return GetStringInternal(key, Assembly.GetCallingAssembly());
28+
}
29+
30+
/// <summary>
31+
/// Gets the specified localized string with ampersand (&amp;) characters removed.
32+
/// <para/>
33+
/// Internally calls <see cref="TextHelper.RemoveMnemonics(String)"/> on the string
34+
/// returned from <see cref="TextHelper.GetString(String)"/>.
35+
/// </summary>
36+
/// <param name="key">The key used to retrieve the localized string.</param>
37+
public static String GetStringWithoutMnemonics(String key)
38+
{
39+
return RemoveMnemonics(GetStringInternal(key, Assembly.GetCallingAssembly()));
40+
}
41+
42+
/// <summary>
43+
/// Gets the specified localized string with trailing triple dots (...) removed.
44+
/// <para/>
45+
/// Internally calls <see cref="TextHelper.RemoveEllipsis(String)"/> on the string returned
46+
/// from <see cref="TextHelper.GetString(String)"/>.
47+
/// </summary>
48+
/// <param name="key">The key used to retrieve the localized string.</param>
49+
public static String GetStringWithoutEllipsis(String key)
50+
{
51+
return RemoveEllipsis(GetStringInternal(key, Assembly.GetCallingAssembly()));
52+
}
53+
54+
/// <summary>
55+
/// Gets the specified localized string with mnemonics and ellipsis removed.
56+
/// <para/>
57+
/// Internally calls <see cref="TextHelper.RemoveMnemonicsAndEllipsis(String)"/> on the
58+
/// string returned from <see cref="TextHelper.GetString(String)"/>.
59+
/// </summary>
60+
/// <param name="key">The key used to retrieve the localized string.</param>
61+
public static String GetStringWithoutMnemonicsOrEllipsis(String key)
62+
{
63+
return RemoveMnemonicsAndEllipsis(GetStringInternal(key, Assembly.GetCallingAssembly()));
64+
}
65+
66+
/// <summary>
67+
/// Removes mnemonics from the specified string.
68+
/// <para/>
69+
/// The string can be in two forms: <code>"&amp;Close"</code> or <code>"Close (&amp;C)"</code>.
70+
/// In both cases this method will return the string <code>"Close"</code>.
71+
/// </summary>
72+
/// <param name="text">A <see cref="String"/> instance to remove mnemonics from.</param>
73+
public static String RemoveMnemonics(String text)
74+
{
75+
if (String.IsNullOrEmpty(text)) return String.Empty;
76+
Int32 index = text.Length;
77+
if (index > 4 &&
78+
text[--index].Equals(')') &&
79+
Char.IsUpper(text[--index]) &&
80+
text[--index].Equals('&') &&
81+
text[--index].Equals('('))
2282
{
23-
storedLocale = localeSetting;
24-
String path = "PluginCore.PluginCore.Resources." + storedLocale;
25-
resourceManager = new ResourceManager(path, Assembly.GetExecutingAssembly());
83+
if (!text[--index].Equals(' ')) index++;
84+
return text.Remove(index);
2685
}
27-
prefix = Assembly.GetCallingAssembly().GetName().Name;
86+
return text.Replace("&", String.Empty);
87+
}
88+
89+
/// <summary>
90+
/// Removes trailing ellipsis (...) from the specified string.
91+
/// </summary>
92+
/// <param name="text">A <see cref="String"/> instance to remove ellipsis from.</param>
93+
public static String RemoveEllipsis(String text)
94+
{
95+
if (String.IsNullOrEmpty(text)) return String.Empty;
96+
Int32 index = text.LastIndexOf("...", StringComparison.Ordinal);
97+
return index == -1 ? text : text.Remove(index, 3);
98+
}
99+
100+
/// <summary>
101+
/// Removes mnemonics and ellipsis from the specified string.
102+
/// Note that Mnemonics are removed first due to strings like "Open... (&amp;O)".
103+
/// </summary>
104+
/// <param name="text">A <see cref="String"/> instance to remove mnemonics and ellipsis from.</param>
105+
public static String RemoveMnemonicsAndEllipsis(String text)
106+
{
107+
return RemoveMnemonics(RemoveEllipsis(text));
108+
}
109+
110+
/// <summary>
111+
/// Gets the specified localized string with the specified assembly's name as the default prefix.
112+
/// </summary>
113+
static String GetStringInternal(String key, Assembly assembly)
114+
{
115+
if (!RefreshStoredLocale()) return key ?? String.Empty;
116+
String prefix = assembly.GetName().Name;
28117
// On different distro we need to use FlashDevelop prefix
29118
if (prefix == DistroConfig.DISTRIBUTION_NAME) prefix = "FlashDevelop";
30-
result = resourceManager.GetString(prefix + "." + key);
31-
if (result == null) result = resourceManager.GetString(key);
119+
String result = resourceManager.GetString(prefix + "." + key);
32120
if (result == null)
33121
{
34-
TraceManager.Add("No localized string found: " + key);
35-
result = String.Empty;
122+
result = resourceManager.GetString(key);
123+
if (result == null)
124+
{
125+
TraceManager.Add("No localized string found: " + key);
126+
return String.Empty;
127+
}
36128
}
37129
// Replace FlashDevelop with distro name if needed
38130
if (DistroConfig.DISTRIBUTION_NAME != "FlashDevelop")
@@ -44,6 +136,22 @@ public static String GetString(String key)
44136
return result;
45137
}
46138

139+
/// <summary>
140+
/// Checks and updates the stored locale and resource manager if necessary.
141+
/// Returns whether the operation succeeded (<code>true</code>) or failed (<code>false</code>).
142+
/// </summary>
143+
static bool RefreshStoredLocale()
144+
{
145+
if (PluginBase.MainForm == null || PluginBase.MainForm.Settings == null) return false;
146+
LocaleVersion localeSetting = PluginBase.MainForm.Settings.LocaleVersion;
147+
if (localeSetting != storedLocale)
148+
{
149+
storedLocale = localeSetting;
150+
String path = "PluginCore.PluginCore.Resources." + storedLocale;
151+
resourceManager = new ResourceManager(path, Assembly.GetExecutingAssembly());
152+
}
153+
return true;
154+
}
47155
}
48156

49157
}

0 commit comments

Comments
 (0)