5
5
6
6
namespace PluginCore . Localization
7
7
{
8
- public class TextHelper
8
+ /// <summary>
9
+ /// Provides methods to retrieve and modify localized resource strings.
10
+ /// </summary>
11
+ public static class TextHelper
9
12
{
10
- private static ResourceManager resourceManager = null ;
11
- private static LocaleVersion storedLocale = LocaleVersion . en_US ;
13
+ static ResourceManager resourceManager ;
14
+ static LocaleVersion storedLocale = LocaleVersion . Invalid ;
12
15
13
16
/// <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.
15
23
/// </summary>
24
+ /// <param name="key">The key used to retrieve the localized string.</param>
16
25
public static String GetString ( String key )
17
26
{
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 (&) 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>"&Close"</code> or <code>"Close (&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 ( '(' ) )
22
82
{
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 ) ;
26
85
}
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... (&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 ;
28
117
// On different distro we need to use FlashDevelop prefix
29
118
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 ) ;
32
120
if ( result == null )
33
121
{
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
+ }
36
128
}
37
129
// Replace FlashDevelop with distro name if needed
38
130
if ( DistroConfig . DISTRIBUTION_NAME != "FlashDevelop" )
@@ -44,6 +136,22 @@ public static String GetString(String key)
44
136
return result ;
45
137
}
46
138
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
+ }
47
155
}
48
156
49
157
}
0 commit comments