Mercurial > repos > IBBoard
annotate Lang/Translation.cs @ 111:9832caa89140
* Make the SimpleSet a proper collection
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sat, 28 Jan 2012 16:52:48 +0000 |
parents | 13f0ffb012cb |
children | e32b5ccda410 |
rev | line source |
---|---|
16 | 1 // This file (Translation.cs) is a part of the IBBoard library and is copyright 2009 IBBoard. |
2 // | |
3 // The file and the library/program it is in are licensed under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license. | |
4 | |
37 | 5 using System; |
6 using System.IO; | |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
7 using System.Xml; |
37 | 8 using System.Xml.Schema; |
9 using System.Collections.Generic; | |
10 using System.Reflection; | |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
11 using System.ComponentModel; |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
12 using IBBoard.IO; |
37 | 13 using IBBoard.Logging; |
14 using IBBoard.Xml; | |
15 | |
16 namespace IBBoard.Lang | |
17 { | |
18 /// <summary> | |
71
40c09e57d213
Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
19 /// A basic string translator that a specified language and returns translated strings that correspond to translation IDs. |
40c09e57d213
Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
20 /// If the string doesn't exist in the specified language then the translator falls back defined 'super' languages. |
40c09e57d213
Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
21 /// If the translation doesn't exist in the hierarchy of languages then either a supplied value, null or a "no validation available" |
40c09e57d213
Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
22 /// message is returned, depending on the parameters supplied to the method. |
40c09e57d213
Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
23 /// |
40c09e57d213
Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
24 /// Loaded languages are referenced by two-character language code (e.g. "en" or "it") |
37 | 25 /// </summary> |
26 public class Translation | |
71
40c09e57d213
Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
27 { |
73
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
28 private static AbstractTranslationSet currentTranslations; |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
29 private static Dictionary<string, AbstractTranslationSet> langToTranslationMap; |
74
726731c78414
Re #37: Add event to indicate change of translation
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
30 public static event MethodInvoker TranslationChanged; |
73
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
31 |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
32 static Translation() |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
33 { |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
34 langToTranslationMap = new Dictionary<string, AbstractTranslationSet>(); |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
35 } |
6 | 36 |
37 /// <summary> | |
71
40c09e57d213
Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
38 /// Initialises the translations and loads the specified language so that the Translation class can be used. |
21
c8d74202182a
Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents:
16
diff
changeset
|
39 /// Throws a TranslationLoadException if a problem occurred while loading translations. If this occurs then the translation methods can |
c8d74202182a
Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents:
16
diff
changeset
|
40 /// still be called but no translation will be performed. |
6 | 41 /// </summary> |
42 /// <param name="appPath"> | |
71
40c09e57d213
Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
43 /// The full path that the contains the "translations" folder - normally the application directory path. |
6 | 44 /// </param> |
45 /// <param name="language"> | |
71
40c09e57d213
Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
46 /// The language to use as the loaded language |
37 | 47 /// </param> |
48 public static void InitialiseTranslations(string appPath, string language) | |
6 | 49 { |
71
40c09e57d213
Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
50 InitialiseTranslations(appPath); |
83
13f0ffb012cb
Re warfoundry:#272 : Program errors on exit if language file fails to load.
IBBoard <dev@ibboard.co.uk>
parents:
82
diff
changeset
|
51 LoadTranslation(language); |
71
40c09e57d213
Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
52 } |
40c09e57d213
Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
53 |
40c09e57d213
Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
54 /// <summary> |
40c09e57d213
Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
55 /// Initialises the translation class for an application or source. |
40c09e57d213
Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
56 /// </summary> |
40c09e57d213
Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
57 /// <param name="appPath"> |
40c09e57d213
Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
58 /// The full path that the contains the "translations" folder - normally the application directory path. |
40c09e57d213
Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
59 /// </param> |
40c09e57d213
Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
60 public static void InitialiseTranslations(string appPath) |
40c09e57d213
Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
61 { |
6 | 62 InitialiseDefaults(appPath); |
63 } | |
64 | |
65 private static void InitialiseDefaults(string appPath) | |
66 { | |
73
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
67 Reset(); |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
68 LoadTranslations(appPath); |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
69 } |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
70 |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
71 private static void LoadTranslations(string appPath) |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
72 { |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
73 DirectoryInfo dir = new DirectoryInfo(Path.Combine(appPath, "translations")); |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
74 |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
75 if (!dir.Exists) |
6 | 76 { |
73
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
77 throw new TranslationLoadException("Translation path not found (" + dir.FullName + ")"); |
37 | 78 } |
73
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
79 |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
80 TranslationXmlLoader loader = new TranslationXmlLoader(Path.Combine(appPath, "schemas/translation.xsd")); |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
81 |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
82 foreach (FileInfo file in dir.GetFiles("*.translation")) |
37 | 83 { |
73
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
84 AbstractTranslationSet translations = loader.LoadTranslations(file.FullName); |
78
da339d10c5fe
Re #32: Add staged loading of translations
IBBoard <dev@ibboard.co.uk>
parents:
75
diff
changeset
|
85 AddTranslationSet(translations); |
6 | 86 } |
87 } | |
88 | |
22
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
89 /// <summary> |
78
da339d10c5fe
Re #32: Add staged loading of translations
IBBoard <dev@ibboard.co.uk>
parents:
75
diff
changeset
|
90 /// Adds the supplied translation set to the list of available translations |
da339d10c5fe
Re #32: Add staged loading of translations
IBBoard <dev@ibboard.co.uk>
parents:
75
diff
changeset
|
91 /// </summary> |
da339d10c5fe
Re #32: Add staged loading of translations
IBBoard <dev@ibboard.co.uk>
parents:
75
diff
changeset
|
92 /// <param name="translations"> |
da339d10c5fe
Re #32: Add staged loading of translations
IBBoard <dev@ibboard.co.uk>
parents:
75
diff
changeset
|
93 /// The translation set to add |
da339d10c5fe
Re #32: Add staged loading of translations
IBBoard <dev@ibboard.co.uk>
parents:
75
diff
changeset
|
94 /// </param> |
da339d10c5fe
Re #32: Add staged loading of translations
IBBoard <dev@ibboard.co.uk>
parents:
75
diff
changeset
|
95 public static void AddTranslationSet(AbstractTranslationSet translations) |
da339d10c5fe
Re #32: Add staged loading of translations
IBBoard <dev@ibboard.co.uk>
parents:
75
diff
changeset
|
96 { |
da339d10c5fe
Re #32: Add staged loading of translations
IBBoard <dev@ibboard.co.uk>
parents:
75
diff
changeset
|
97 langToTranslationMap[translations.LanguageCode] = translations; |
da339d10c5fe
Re #32: Add staged loading of translations
IBBoard <dev@ibboard.co.uk>
parents:
75
diff
changeset
|
98 } |
da339d10c5fe
Re #32: Add staged loading of translations
IBBoard <dev@ibboard.co.uk>
parents:
75
diff
changeset
|
99 |
da339d10c5fe
Re #32: Add staged loading of translations
IBBoard <dev@ibboard.co.uk>
parents:
75
diff
changeset
|
100 /// <summary> |
71
40c09e57d213
Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
101 /// Resets the loaded translations and reverts to no translations. |
69
b5d7e8b93205
Re #30: Improve Translations API
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
102 /// </summary> |
b5d7e8b93205
Re #30: Improve Translations API
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
103 public static void Reset() |
b5d7e8b93205
Re #30: Improve Translations API
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
104 { |
74
726731c78414
Re #37: Add event to indicate change of translation
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
105 SetCurrentTranslations(null); |
73
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
106 langToTranslationMap.Clear(); |
69
b5d7e8b93205
Re #30: Improve Translations API
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
107 } |
74
726731c78414
Re #37: Add event to indicate change of translation
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
108 |
726731c78414
Re #37: Add event to indicate change of translation
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
109 private static void SetCurrentTranslations(AbstractTranslationSet newTranslations) |
726731c78414
Re #37: Add event to indicate change of translation
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
110 { |
726731c78414
Re #37: Add event to indicate change of translation
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
111 if (currentTranslations != newTranslations || (currentTranslations != null && !currentTranslations.Equals(newTranslations))) |
726731c78414
Re #37: Add event to indicate change of translation
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
112 { |
726731c78414
Re #37: Add event to indicate change of translation
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
113 currentTranslations = newTranslations; |
726731c78414
Re #37: Add event to indicate change of translation
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
114 |
726731c78414
Re #37: Add event to indicate change of translation
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
115 if (TranslationChanged != null) |
726731c78414
Re #37: Add event to indicate change of translation
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
116 { |
726731c78414
Re #37: Add event to indicate change of translation
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
117 TranslationChanged(); |
726731c78414
Re #37: Add event to indicate change of translation
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
118 } |
726731c78414
Re #37: Add event to indicate change of translation
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
119 } |
726731c78414
Re #37: Add event to indicate change of translation
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
120 } |
6 | 121 |
122 /// <summary> | |
71
40c09e57d213
Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
123 /// Loads translations for a given language and sets them as the current language. |
40c09e57d213
Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
124 /// Throws a TranslationLoadException if a problem occurred while loading translations. If this occurs then the translation methods can |
21
c8d74202182a
Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents:
16
diff
changeset
|
125 /// still be called but all translations will fall back to the default translation. |
6 | 126 /// </summary> |
127 /// <param name="translationLang"> | |
128 /// The new local language to load | |
37 | 129 /// </param> |
130 public static void LoadTranslation(string translationLanguage) | |
83
13f0ffb012cb
Re warfoundry:#272 : Program errors on exit if language file fails to load.
IBBoard <dev@ibboard.co.uk>
parents:
82
diff
changeset
|
131 { |
13f0ffb012cb
Re warfoundry:#272 : Program errors on exit if language file fails to load.
IBBoard <dev@ibboard.co.uk>
parents:
82
diff
changeset
|
132 if (HasLanguage(translationLanguage)) |
13f0ffb012cb
Re warfoundry:#272 : Program errors on exit if language file fails to load.
IBBoard <dev@ibboard.co.uk>
parents:
82
diff
changeset
|
133 { |
13f0ffb012cb
Re warfoundry:#272 : Program errors on exit if language file fails to load.
IBBoard <dev@ibboard.co.uk>
parents:
82
diff
changeset
|
134 SetCurrentTranslations(GetTranslationSet(translationLanguage)); |
37 | 135 } |
136 } | |
6 | 137 |
138 /// <summary> | |
139 /// Gets a translation for a given ID, falling back to a "missing translation" message if none can be found. Also optionally replaces any placeholders with the supplied values. | |
140 /// </summary> | |
141 /// <param name="translationID"> | |
142 /// The ID to look up the translation for | |
143 /// </param> | |
144 /// <param name="replacements"> | |
145 /// A collection of <see cref="System.Object"/>s to replace placeholders with | |
146 /// </param> | |
147 /// <returns> | |
148 /// The translation with the placeholders replaced or a "missing translation" message | |
37 | 149 /// </returns> |
150 public static string GetTranslation(string translationID, params object[] replacements) | |
151 { | |
152 return GetTranslation(translationID, false, replacements); | |
153 } | |
6 | 154 |
155 /// <summary> | |
156 /// Gets a translation for a given ID, falling back to null or a warning message if a translation cannot be found. Also optionally replaces any placeholders with the supplied values. | |
157 /// </summary> | |
158 /// <param name="translationID"> | |
159 /// The ID to look up the translation for | |
160 /// </param> | |
161 /// <param name="returnNullOnFail"> | |
162 /// TRUE if null should be returned when no translation can be found, or FALSE if a "missing translation" message should be returned | |
163 /// </param> | |
164 /// <param name="replacements"> | |
165 /// A collection of <see cref="System.Object"/>s to replace placeholders with | |
166 /// </param> | |
167 /// <returns> | |
168 /// The translation with the placeholders replaced, or a "missing translation" message or null depending on <param name="returnNullOnFail"> | |
37 | 169 /// </returns> |
170 public static string GetTranslation(string translationID, bool returnNullOnFail, params object[] replacements) | |
171 { | |
172 return GetTranslation(translationID, returnNullOnFail ? null : "", replacements); | |
173 } | |
6 | 174 |
175 /// <summary> | |
176 /// Gets a translation for a given ID, falling back to a supplied default if a translation cannot be found. Also optionally replaces any placeholders with the supplied values. | |
177 /// </summary> | |
178 /// <param name="translationID"> | |
179 /// The ID to look up the translation for | |
180 /// </param> | |
181 /// <param name="defaultTranslation"> | |
182 /// The string to return if no translation can be found. Can be null or any string. | |
183 /// </param> | |
184 /// <param name="replacements"> | |
185 /// A collection of <see cref="System.Object"/>s to replace placeholders with | |
186 /// </param> | |
187 /// <returns> | |
188 /// The translation, if one exists, or the supplied default with the placeholders replaced | |
37 | 189 /// </returns> |
190 public static string GetTranslation(string translationID, string defaultTranslation, params object[] replacements) | |
191 { | |
71
40c09e57d213
Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
192 string trans = GetTranslationFromTranslationSet(translationID); |
37 | 193 |
194 if (trans == null) | |
195 { | |
196 trans = GetDefaultTranslation(translationID, defaultTranslation); | |
197 } | |
198 | |
199 trans = AddVariablesToTranslation(trans, replacements); | |
200 | |
201 return trans; | |
6 | 202 } |
203 | |
71
40c09e57d213
Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
204 private static string GetTranslationFromTranslationSet(string translationID) |
6 | 205 { |
206 string translation = null; | |
207 | |
73
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
208 if (currentTranslations!=null) |
37 | 209 { |
73
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
210 translation = currentTranslations[translationID]; |
6 | 211 } |
212 | |
213 return translation; | |
214 } | |
215 | |
216 private static string GetDefaultTranslation(string translationID, string defaultTranslation) | |
217 { | |
69
b5d7e8b93205
Re #30: Improve Translations API
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
218 return (defaultTranslation != "" || defaultTranslation == null) ? defaultTranslation : GetMissingTranslationMessage(translationID); |
37 | 219 } |
6 | 220 |
221 private static string GetMissingTranslationMessage(string translationID) | |
222 { | |
223 return "++ Missing Translation "+translationID+" ++"; | |
224 } | |
225 | |
226 private static string AddVariablesToTranslation(string translation, object[] replacements) | |
227 { | |
37 | 228 if (translation != null && replacements != null && replacements.Length > 0) |
229 { | |
230 translation = String.Format(translation, replacements); | |
6 | 231 } |
232 | |
233 return translation; | |
234 } | |
235 | |
236 /// <summary> | |
36
c949727ec0e0
Re #22 - Make failing control translation cleaner for normal use
IBBoard <dev@ibboard.co.uk>
parents:
35
diff
changeset
|
237 /// Translate an <see cref="ITranslatable"/> item, with optional string replacement. If the translation |
c949727ec0e0
Re #22 - Make failing control translation cleaner for normal use
IBBoard <dev@ibboard.co.uk>
parents:
35
diff
changeset
|
238 /// does not exist then a warning message will be used as the translated text. |
6 | 239 /// </summary> |
240 /// <param name="item"> | |
241 /// A <see cref="ITranslatable"/> to set the text for | |
242 /// </param> | |
243 /// <param name="replacements"> | |
244 /// A collection of <see cref="System.Object"/>s that will be used to fill place-holders | |
37 | 245 /// </param> |
246 public static void Translate(ITranslatable item, params object[] replacements) | |
247 { | |
69
b5d7e8b93205
Re #30: Improve Translations API
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
248 Translate(item, GetMissingTranslationMessage(item.Name), replacements); |
37 | 249 } |
250 | |
251 /// <summary> | |
252 /// Translate an <see cref="ITranslatable"/> item, with optional string replacement. The <code>defaultText</code> | |
253 /// can be used to specify an alternate translation. Passing <code>null</code> will result in a warning message | |
254 /// about a missing translation ID. | |
255 /// </summary> | |
256 /// <param name="item"> | |
257 /// A <see cref="ITranslatable"/> to set the text for | |
258 /// </param> | |
259 /// <param name="defaultText"> | |
260 /// The default string to display if no translation could be found. | |
261 /// </param> | |
262 /// <param name="replacements"> | |
263 /// A collection of <see cref="System.Object"/>s that will be used to fill place-holders | |
264 /// </param> | |
265 public static void Translate(ITranslatable item, string defaultText, params object[] replacements) | |
266 { | |
82
5182df00c558
Fixes #36: Remove specialist "divider" handling from Translations
IBBoard <dev@ibboard.co.uk>
parents:
81
diff
changeset
|
267 if (item.Text == "") |
37 | 268 { |
269 //it doesn't need translating - either there is no text from the developer or it's a hyphen for a divider | |
270 return; | |
271 } | |
272 | |
273 item.Text = GetTranslation(item.Name, defaultText, replacements); | |
6 | 274 } |
275 | |
276 /// <summary> | |
81
09f71d10c249
* Fix documentation - we're now using ISO codes (or should be, although we do handle invalid ones)
IBBoard <dev@ibboard.co.uk>
parents:
78
diff
changeset
|
277 /// Get the current local translation language. This is the "language code" used by the translation and should match the ISO code for the language. |
6 | 278 /// </summary> |
279 /// <returns> | |
81
09f71d10c249
* Fix documentation - we're now using ISO codes (or should be, although we do handle invalid ones)
IBBoard <dev@ibboard.co.uk>
parents:
78
diff
changeset
|
280 /// The string used as the language code of the current local translation |
6 | 281 /// </returns> |
282 public static string GetTranslationLanguage() | |
283 { | |
73
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
284 return (currentTranslations != null ? currentTranslations.LanguageCode : ""); |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
285 } |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
286 |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
287 public static ICollection<TranslationLanguage> GetLanguages() |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
288 { |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
289 ICollection<TranslationLanguage> langs = new List<TranslationLanguage>(); |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
290 |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
291 foreach (AbstractTranslationSet translations in langToTranslationMap.Values) |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
292 { |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
293 langs.Add(translations.Language); |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
294 } |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
295 |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
296 return langs; |
37 | 297 } |
75
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
74
diff
changeset
|
298 |
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
74
diff
changeset
|
299 public static AbstractTranslationSet GetTranslationSet(string translationLanguage) |
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
74
diff
changeset
|
300 { |
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
74
diff
changeset
|
301 AbstractTranslationSet translations = null; |
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
74
diff
changeset
|
302 |
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
74
diff
changeset
|
303 if (translationLanguage != null) |
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
74
diff
changeset
|
304 { |
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
74
diff
changeset
|
305 translations = DictionaryUtils.GetValue(langToTranslationMap, translationLanguage); |
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
74
diff
changeset
|
306 |
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
74
diff
changeset
|
307 if (translations == null) |
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
74
diff
changeset
|
308 { |
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
74
diff
changeset
|
309 translations = new ModifiableTranslationSet(translationLanguage); |
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
74
diff
changeset
|
310 } |
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
74
diff
changeset
|
311 } |
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
74
diff
changeset
|
312 |
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
74
diff
changeset
|
313 return translations; |
83
13f0ffb012cb
Re warfoundry:#272 : Program errors on exit if language file fails to load.
IBBoard <dev@ibboard.co.uk>
parents:
82
diff
changeset
|
314 } |
13f0ffb012cb
Re warfoundry:#272 : Program errors on exit if language file fails to load.
IBBoard <dev@ibboard.co.uk>
parents:
82
diff
changeset
|
315 |
13f0ffb012cb
Re warfoundry:#272 : Program errors on exit if language file fails to load.
IBBoard <dev@ibboard.co.uk>
parents:
82
diff
changeset
|
316 public static bool HasLanguage(string languageCode) |
13f0ffb012cb
Re warfoundry:#272 : Program errors on exit if language file fails to load.
IBBoard <dev@ibboard.co.uk>
parents:
82
diff
changeset
|
317 { |
13f0ffb012cb
Re warfoundry:#272 : Program errors on exit if language file fails to load.
IBBoard <dev@ibboard.co.uk>
parents:
82
diff
changeset
|
318 return langToTranslationMap.ContainsKey(languageCode); |
13f0ffb012cb
Re warfoundry:#272 : Program errors on exit if language file fails to load.
IBBoard <dev@ibboard.co.uk>
parents:
82
diff
changeset
|
319 } |
37 | 320 } |
321 } |