Mercurial > repos > IBBoard
annotate Lang/Translation.cs @ 64:70d6c2a5d99e
* Move schema from "dtds" folder to "schemas" folder to match warfoundry:429
no-open-ticket
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Wed, 16 Dec 2009 20:48:10 +0000 |
parents | cc7fae81afec |
children | 980ebd49c40b |
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> | |
6 | 19 /// A basic string translator that loads a default language and a specified language and returns translated strings that correspond to translation IDs. |
20 /// If the string doesn't exist in the specified language then the translator falls back to the default language. If the translation doesn't exist in the default language | |
37 | 21 /// then either a supplied value or a "no validation available" message is returned. |
22 /// </summary> | |
23 public class Translation | |
6 | 24 { |
25 private static readonly string DEFAULT_LANGUAGE = "en"; | |
37 | 26 private static readonly string DIVIDER_STRING = "-"; |
27 private static string lang = ""; | |
28 private static DirectoryInfo translationDir; | |
29 private static Dictionary<string, string> translationsLocal; | |
22
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
30 private static Dictionary<string, string> translationsDefault; |
37 | 31 private static XmlReaderSettings settings; |
6 | 32 |
33 /// <summary> | |
21
c8d74202182a
Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents:
16
diff
changeset
|
34 /// Initialises the translations for the language specified and the default translations so that the Translation class can be used. |
c8d74202182a
Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents:
16
diff
changeset
|
35 /// 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
|
36 /// still be called but no translation will be performed. |
6 | 37 /// </summary> |
38 /// <param name="appPath"> | |
39 /// The full path that the application is running from. Must contain the "translations" folder. | |
40 /// </param> | |
41 /// <param name="language"> | |
42 /// The language to use as the load language | |
37 | 43 /// </param> |
44 public static void InitialiseTranslations(string appPath, string language) | |
6 | 45 { |
46 InitialiseDefaults(appPath); | |
37 | 47 FileInfo file = GetTranslationFile(DEFAULT_LANGUAGE); |
48 XmlDocument doc = LoadTranslationDocument(file); | |
49 LoadTranslationsFromDocument(doc, translationsDefault); | |
50 LoadTranslationForLanguage(language); | |
6 | 51 } |
52 | |
53 private static void InitialiseDefaults(string appPath) | |
54 { | |
37 | 55 string translationPath = appPath.TrimEnd(Constants.DirectoryChar) + Constants.DirectoryString + "translations"; |
56 | |
57 if (Directory.Exists(translationPath)) | |
6 | 58 { |
59 translationsDefault = new Dictionary<string,string>(); | |
37 | 60 translationsLocal = new Dictionary<string,string>(); |
61 translationDir = new DirectoryInfo(translationPath); | |
62 } | |
63 else | |
64 { | |
65 throw new TranslationLoadException("Translation path not found ("+translationPath+")"); | |
6 | 66 } |
67 } | |
68 | |
69 private static XmlDocument LoadTranslationDocument(FileInfo file) | |
70 { | |
22
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
71 XmlDocument doc = new XmlDocument(); |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
72 XmlReader valReader = XmlReader.Create(file.FullName, GetReaderSettings()); |
21
c8d74202182a
Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents:
16
diff
changeset
|
73 |
c8d74202182a
Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents:
16
diff
changeset
|
74 try |
37 | 75 { |
21
c8d74202182a
Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents:
16
diff
changeset
|
76 doc.Load(valReader); |
c8d74202182a
Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents:
16
diff
changeset
|
77 } |
22
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
78 catch (DirectoryNotFoundException ex) |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
79 { |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
80 throw new TranslationLoadException("Problem validating schema for translation: " + ex.Message, ex); |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
81 } |
21
c8d74202182a
Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents:
16
diff
changeset
|
82 catch (XmlSchemaException ex) |
c8d74202182a
Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents:
16
diff
changeset
|
83 { |
c8d74202182a
Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents:
16
diff
changeset
|
84 throw new TranslationLoadException("Problem validating schema for translation: " + ex.Message, ex); |
c8d74202182a
Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents:
16
diff
changeset
|
85 } |
c8d74202182a
Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents:
16
diff
changeset
|
86 catch (XmlException ex) |
c8d74202182a
Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents:
16
diff
changeset
|
87 { |
c8d74202182a
Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents:
16
diff
changeset
|
88 throw new TranslationLoadException("Problem reading data for translation: " + ex.Message, ex); |
c8d74202182a
Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents:
16
diff
changeset
|
89 } |
c8d74202182a
Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents:
16
diff
changeset
|
90 finally |
37 | 91 { |
21
c8d74202182a
Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents:
16
diff
changeset
|
92 valReader.Close(); |
c8d74202182a
Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents:
16
diff
changeset
|
93 } |
c8d74202182a
Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents:
16
diff
changeset
|
94 |
6 | 95 return doc; |
96 } | |
97 | |
22
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
98 /// <summary> |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
99 /// Lazy-getter for XML reader settings. May throw a <see cref="TranslationLoadException"/> if there is a problem with the translation schema. |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
100 /// </summary> |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
101 /// <returns> |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
102 /// A <see cref="XmlReaderSettings"/> with the default values for validating the translation document against the translation schema |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
103 /// </returns> |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
104 private static XmlReaderSettings GetReaderSettings() |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
105 { |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
106 if (settings == null) |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
107 { |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
108 try |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
109 { |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
110 settings = new XmlReaderSettings(); |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
111 settings.XmlResolver = new IBBXmlResolver(translationDir.Parent.FullName); |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
112 settings.ValidationType = ValidationType.Schema; |
23
fb4fdab841db
* Ignore schema location attribute for translations
IBBoard <dev@ibboard.co.uk>
parents:
22
diff
changeset
|
113 settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings; |
22
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
114 settings.ValidationEventHandler+= new ValidationEventHandler(ValidationEventMethod); |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
115 XmlSchemaSet cache = new XmlSchemaSet(); |
64
70d6c2a5d99e
* Move schema from "dtds" folder to "schemas" folder to match warfoundry:429
IBBoard <dev@ibboard.co.uk>
parents:
37
diff
changeset
|
116 cache.Add("http://ibboard.co.uk/translation", translationDir.Parent.FullName + "/schemas/translation.xsd"); |
22
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
117 settings.Schemas.Add(cache); |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
118 } |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
119 catch (DirectoryNotFoundException ex) |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
120 { |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
121 throw new TranslationLoadException("Problem validating schema for translation: " + ex.Message, ex); |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
122 } |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
123 catch (XmlSchemaException ex) |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
124 { |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
125 throw new TranslationLoadException("Problem validating schema for translation: " + ex.Message, ex); |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
126 } |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
127 catch (XmlException ex) |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
128 { |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
129 throw new TranslationLoadException("Problem reading data for schema: " + ex.Message, ex); |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
130 } |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
131 } |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
132 |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
133 return settings; |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
134 } |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
135 |
6 | 136 private static FileInfo GetTranslationFile(string language) |
137 { | |
37 | 138 FileInfo file = new FileInfo(translationDir.FullName + Constants.DirectoryString + language + ".translation"); |
139 | |
140 if (!file.Exists) | |
141 { | |
142 throw new TranslationLoadException(language + ".translation could not be found in "+translationDir.FullName); | |
6 | 143 } |
144 | |
145 return file; | |
146 } | |
147 | |
148 private static void LoadTranslationsFromDocument(XmlDocument doc, Dictionary<string, string> translationTable) | |
149 { | |
37 | 150 try |
151 { | |
35
2b5e73cb83a3
Re #21 - Reduce specificity of translations file
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
152 XmlNodeList translations = doc.GetElementsByTagName("translation"); |
37 | 153 Dictionary<string, string> tempTranslationTable = new Dictionary<string,string>(); |
154 | |
155 foreach (XmlNode node in translations) | |
156 { | |
157 tempTranslationTable.Add(node.Attributes["id"].Value, node.InnerText); | |
6 | 158 } |
159 | |
160 translationTable.Clear(); | |
161 | |
162 foreach (string key in tempTranslationTable.Keys) | |
163 { | |
164 string translation; | |
165 tempTranslationTable.TryGetValue(key, out translation); | |
166 translationTable.Add(key, translation); | |
37 | 167 } |
168 } | |
169 catch(Exception ex) | |
170 { | |
171 throw new TranslationLoadException("Error while parsing " + GetLanguageOfDocument(doc)+" translation: "+ex.Message, ex); | |
6 | 172 } |
173 } | |
174 | |
175 private static string GetLanguageOfDocument(XmlDocument doc) | |
176 { | |
177 return doc != null ? doc.DocumentElement.GetAttribute("lang") : ""; | |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
178 } |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
179 |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
180 private static void ValidationEventMethod(object sender, ValidationEventArgs e) |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
181 { |
22
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
182 throw new TranslationLoadException("Problem validating schema for translation: " + e.Exception.Message, e.Exception); |
37 | 183 } |
6 | 184 |
185 /// <summary> | |
21
c8d74202182a
Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents:
16
diff
changeset
|
186 /// Loads translations for a given language and sets them as the local language. |
c8d74202182a
Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents:
16
diff
changeset
|
187 /// hrows 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
|
188 /// still be called but all translations will fall back to the default translation. |
6 | 189 /// </summary> |
190 /// <param name="translationLang"> | |
191 /// The new local language to load | |
37 | 192 /// </param> |
193 public static void LoadTranslation(string translationLanguage) | |
7
f4da31cb09d9
* Add translation DTD to utils project
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
194 { |
f4da31cb09d9
* Add translation DTD to utils project
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
195 if (translationLanguage == "" || translationLanguage == null) |
6 | 196 { |
10
3b7a321e7c4c
Fixes #4 - unexpected exception in translations
IBBoard <dev@ibboard.co.uk>
parents:
9
diff
changeset
|
197 throw new ArgumentException("Translation language cannot be null or empty"); |
37 | 198 } |
199 | |
200 LoadTranslationForLanguage(translationLanguage); | |
7
f4da31cb09d9
* Add translation DTD to utils project
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
201 } |
f4da31cb09d9
* Add translation DTD to utils project
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
202 |
f4da31cb09d9
* Add translation DTD to utils project
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
203 private static void LoadTranslationForLanguage(string translationLanguage) |
37 | 204 { |
21
c8d74202182a
Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents:
16
diff
changeset
|
205 CheckInitialisation(); |
7
f4da31cb09d9
* Add translation DTD to utils project
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
206 |
37 | 207 if (translationLanguage != DEFAULT_LANGUAGE && translationLanguage != "" && translationLanguage != null) |
208 { | |
209 FileInfo file = GetTranslationFile(translationLanguage); | |
210 XmlDocument doc = LoadTranslationDocument(file); | |
211 LoadTranslationsFromDocument(doc, translationsLocal); | |
6 | 212 } |
213 else | |
214 { | |
215 translationsLocal.Clear(); | |
216 } | |
37 | 217 |
9 | 218 lang = translationLanguage; |
37 | 219 } |
6 | 220 |
221 /// <summary> | |
222 /// 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. | |
223 /// </summary> | |
224 /// <param name="translationID"> | |
225 /// The ID to look up the translation for | |
226 /// </param> | |
227 /// <param name="replacements"> | |
228 /// A collection of <see cref="System.Object"/>s to replace placeholders with | |
229 /// </param> | |
230 /// <returns> | |
231 /// The translation with the placeholders replaced or a "missing translation" message | |
37 | 232 /// </returns> |
233 public static string GetTranslation(string translationID, params object[] replacements) | |
234 { | |
235 return GetTranslation(translationID, false, replacements); | |
236 } | |
6 | 237 |
238 /// <summary> | |
239 /// 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. | |
240 /// </summary> | |
241 /// <param name="translationID"> | |
242 /// The ID to look up the translation for | |
243 /// </param> | |
244 /// <param name="returnNullOnFail"> | |
245 /// TRUE if null should be returned when no translation can be found, or FALSE if a "missing translation" message should be returned | |
246 /// </param> | |
247 /// <param name="replacements"> | |
248 /// A collection of <see cref="System.Object"/>s to replace placeholders with | |
249 /// </param> | |
250 /// <returns> | |
251 /// The translation with the placeholders replaced, or a "missing translation" message or null depending on <param name="returnNullOnFail"> | |
37 | 252 /// </returns> |
253 public static string GetTranslation(string translationID, bool returnNullOnFail, params object[] replacements) | |
254 { | |
255 return GetTranslation(translationID, returnNullOnFail ? null : "", replacements); | |
256 } | |
6 | 257 |
258 /// <summary> | |
259 /// 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. | |
260 /// </summary> | |
261 /// <param name="translationID"> | |
262 /// The ID to look up the translation for | |
263 /// </param> | |
264 /// <param name="defaultTranslation"> | |
265 /// The string to return if no translation can be found. Can be null or any string. | |
266 /// </param> | |
267 /// <param name="replacements"> | |
268 /// A collection of <see cref="System.Object"/>s to replace placeholders with | |
269 /// </param> | |
270 /// <returns> | |
271 /// The translation, if one exists, or the supplied default with the placeholders replaced | |
37 | 272 /// </returns> |
273 public static string GetTranslation(string translationID, string defaultTranslation, params object[] replacements) | |
274 { | |
275 CheckInitialisation(); | |
276 string trans = GetTranslationFromTables(translationID); | |
277 | |
278 if (trans == null) | |
279 { | |
280 trans = GetDefaultTranslation(translationID, defaultTranslation); | |
281 } | |
282 | |
283 trans = AddVariablesToTranslation(trans, replacements); | |
284 | |
285 return trans; | |
6 | 286 } |
287 | |
288 private static string GetTranslationFromTables(string translationID) | |
289 { | |
290 string translation = null; | |
291 | |
37 | 292 if (translationsLocal!=null) |
293 { | |
294 translationsLocal.TryGetValue(translationID, out translation); | |
295 } | |
296 | |
297 if (translation == null) | |
298 { | |
299 translationsDefault.TryGetValue(translationID, out translation); | |
6 | 300 } |
301 | |
302 return translation; | |
303 } | |
304 | |
305 private static string GetDefaultTranslation(string translationID, string defaultTranslation) | |
306 { | |
307 return (defaultTranslation != "" && defaultTranslation != null) ? defaultTranslation : GetMissingTranslationMessage(translationID); | |
37 | 308 } |
6 | 309 |
310 private static string GetMissingTranslationMessage(string translationID) | |
311 { | |
312 return "++ Missing Translation "+translationID+" ++"; | |
313 } | |
314 | |
315 private static string AddVariablesToTranslation(string translation, object[] replacements) | |
316 { | |
37 | 317 if (translation != null && replacements != null && replacements.Length > 0) |
318 { | |
319 translation = String.Format(translation, replacements); | |
6 | 320 } |
321 | |
322 return translation; | |
323 } | |
324 | |
37 | 325 private static void CheckInitialisation() |
326 { | |
327 if (translationDir==null) | |
328 { | |
329 throw new InvalidOperationException("Translation class has not been initialised"); | |
330 } | |
331 } | |
6 | 332 |
333 /// <summary> | |
36
c949727ec0e0
Re #22 - Make failing control translation cleaner for normal use
IBBoard <dev@ibboard.co.uk>
parents:
35
diff
changeset
|
334 /// 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
|
335 /// does not exist then a warning message will be used as the translated text. |
6 | 336 /// </summary> |
337 /// <param name="item"> | |
338 /// A <see cref="ITranslatable"/> to set the text for | |
339 /// </param> | |
340 /// <param name="replacements"> | |
341 /// A collection of <see cref="System.Object"/>s that will be used to fill place-holders | |
37 | 342 /// </param> |
343 public static void Translate(ITranslatable item, params object[] replacements) | |
344 { | |
345 Translate(item, (string)null, replacements); | |
346 } | |
347 | |
348 /// <summary> | |
349 /// Translate an <see cref="ITranslatable"/> item, with optional string replacement. The <code>defaultText</code> | |
350 /// can be used to specify an alternate translation. Passing <code>null</code> will result in a warning message | |
351 /// about a missing translation ID. | |
352 /// </summary> | |
353 /// <param name="item"> | |
354 /// A <see cref="ITranslatable"/> to set the text for | |
355 /// </param> | |
356 /// <param name="defaultText"> | |
357 /// The default string to display if no translation could be found. | |
358 /// </param> | |
359 /// <param name="replacements"> | |
360 /// A collection of <see cref="System.Object"/>s that will be used to fill place-holders | |
361 /// </param> | |
362 public static void Translate(ITranslatable item, string defaultText, params object[] replacements) | |
363 { | |
364 if (item.Text == "" || item.Text == DIVIDER_STRING) | |
365 { | |
366 //it doesn't need translating - either there is no text from the developer or it's a hyphen for a divider | |
367 return; | |
368 } | |
369 | |
370 item.Text = GetTranslation(item.Name, defaultText, replacements); | |
6 | 371 } |
372 | |
373 /// <summary> | |
374 /// Get the current local translation language. This is an arbitrary string used in the translation file's name and will not necessarily match the ISO language code. | |
375 /// </summary> | |
376 /// <returns> | |
377 /// The string used in the file name of the current local translation | |
378 /// </returns> | |
379 public static string GetTranslationLanguage() | |
380 { | |
381 return lang; | |
37 | 382 } |
383 } | |
384 } |