Mercurial > repos > IBBoard
annotate Lang/Translation.cs @ 65:980ebd49c40b
* Remove unnecessary XML resolver (was used for DTD, now using schema)
no-open-ticket
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sun, 20 Dec 2009 20:30:29 +0000 |
parents | 70d6c2a5d99e |
children | 5fb2e5a2e7a8 |
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.ValidationType = ValidationType.Schema; |
23
fb4fdab841db
* Ignore schema location attribute for translations
IBBoard <dev@ibboard.co.uk>
parents:
22
diff
changeset
|
112 settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings; |
22
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
113 settings.ValidationEventHandler+= new ValidationEventHandler(ValidationEventMethod); |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
114 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
|
115 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
|
116 settings.Schemas.Add(cache); |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
117 } |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
118 catch (DirectoryNotFoundException ex) |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
119 { |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
120 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
|
121 } |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
122 catch (XmlSchemaException ex) |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
123 { |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
124 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
|
125 } |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
126 catch (XmlException ex) |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
127 { |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
128 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
|
129 } |
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 return settings; |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
133 } |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
134 |
6 | 135 private static FileInfo GetTranslationFile(string language) |
136 { | |
37 | 137 FileInfo file = new FileInfo(translationDir.FullName + Constants.DirectoryString + language + ".translation"); |
138 | |
139 if (!file.Exists) | |
140 { | |
141 throw new TranslationLoadException(language + ".translation could not be found in "+translationDir.FullName); | |
6 | 142 } |
143 | |
144 return file; | |
145 } | |
146 | |
147 private static void LoadTranslationsFromDocument(XmlDocument doc, Dictionary<string, string> translationTable) | |
148 { | |
37 | 149 try |
150 { | |
35
2b5e73cb83a3
Re #21 - Reduce specificity of translations file
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
151 XmlNodeList translations = doc.GetElementsByTagName("translation"); |
37 | 152 Dictionary<string, string> tempTranslationTable = new Dictionary<string,string>(); |
153 | |
154 foreach (XmlNode node in translations) | |
155 { | |
156 tempTranslationTable.Add(node.Attributes["id"].Value, node.InnerText); | |
6 | 157 } |
158 | |
159 translationTable.Clear(); | |
160 | |
161 foreach (string key in tempTranslationTable.Keys) | |
162 { | |
163 string translation; | |
164 tempTranslationTable.TryGetValue(key, out translation); | |
165 translationTable.Add(key, translation); | |
37 | 166 } |
167 } | |
168 catch(Exception ex) | |
169 { | |
170 throw new TranslationLoadException("Error while parsing " + GetLanguageOfDocument(doc)+" translation: "+ex.Message, ex); | |
6 | 171 } |
172 } | |
173 | |
174 private static string GetLanguageOfDocument(XmlDocument doc) | |
175 { | |
176 return doc != null ? doc.DocumentElement.GetAttribute("lang") : ""; | |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
177 } |
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 private static void ValidationEventMethod(object sender, ValidationEventArgs e) |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
180 { |
22
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
181 throw new TranslationLoadException("Problem validating schema for translation: " + e.Exception.Message, e.Exception); |
37 | 182 } |
6 | 183 |
184 /// <summary> | |
21
c8d74202182a
Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents:
16
diff
changeset
|
185 /// 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
|
186 /// 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
|
187 /// still be called but all translations will fall back to the default translation. |
6 | 188 /// </summary> |
189 /// <param name="translationLang"> | |
190 /// The new local language to load | |
37 | 191 /// </param> |
192 public static void LoadTranslation(string translationLanguage) | |
7
f4da31cb09d9
* Add translation DTD to utils project
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
193 { |
f4da31cb09d9
* Add translation DTD to utils project
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
194 if (translationLanguage == "" || translationLanguage == null) |
6 | 195 { |
10
3b7a321e7c4c
Fixes #4 - unexpected exception in translations
IBBoard <dev@ibboard.co.uk>
parents:
9
diff
changeset
|
196 throw new ArgumentException("Translation language cannot be null or empty"); |
37 | 197 } |
198 | |
199 LoadTranslationForLanguage(translationLanguage); | |
7
f4da31cb09d9
* Add translation DTD to utils project
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
200 } |
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 private static void LoadTranslationForLanguage(string translationLanguage) |
37 | 203 { |
21
c8d74202182a
Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents:
16
diff
changeset
|
204 CheckInitialisation(); |
7
f4da31cb09d9
* Add translation DTD to utils project
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
205 |
37 | 206 if (translationLanguage != DEFAULT_LANGUAGE && translationLanguage != "" && translationLanguage != null) |
207 { | |
208 FileInfo file = GetTranslationFile(translationLanguage); | |
209 XmlDocument doc = LoadTranslationDocument(file); | |
210 LoadTranslationsFromDocument(doc, translationsLocal); | |
6 | 211 } |
212 else | |
213 { | |
214 translationsLocal.Clear(); | |
215 } | |
37 | 216 |
9 | 217 lang = translationLanguage; |
37 | 218 } |
6 | 219 |
220 /// <summary> | |
221 /// 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. | |
222 /// </summary> | |
223 /// <param name="translationID"> | |
224 /// The ID to look up the translation for | |
225 /// </param> | |
226 /// <param name="replacements"> | |
227 /// A collection of <see cref="System.Object"/>s to replace placeholders with | |
228 /// </param> | |
229 /// <returns> | |
230 /// The translation with the placeholders replaced or a "missing translation" message | |
37 | 231 /// </returns> |
232 public static string GetTranslation(string translationID, params object[] replacements) | |
233 { | |
234 return GetTranslation(translationID, false, replacements); | |
235 } | |
6 | 236 |
237 /// <summary> | |
238 /// 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. | |
239 /// </summary> | |
240 /// <param name="translationID"> | |
241 /// The ID to look up the translation for | |
242 /// </param> | |
243 /// <param name="returnNullOnFail"> | |
244 /// TRUE if null should be returned when no translation can be found, or FALSE if a "missing translation" message should be returned | |
245 /// </param> | |
246 /// <param name="replacements"> | |
247 /// A collection of <see cref="System.Object"/>s to replace placeholders with | |
248 /// </param> | |
249 /// <returns> | |
250 /// The translation with the placeholders replaced, or a "missing translation" message or null depending on <param name="returnNullOnFail"> | |
37 | 251 /// </returns> |
252 public static string GetTranslation(string translationID, bool returnNullOnFail, params object[] replacements) | |
253 { | |
254 return GetTranslation(translationID, returnNullOnFail ? null : "", replacements); | |
255 } | |
6 | 256 |
257 /// <summary> | |
258 /// 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. | |
259 /// </summary> | |
260 /// <param name="translationID"> | |
261 /// The ID to look up the translation for | |
262 /// </param> | |
263 /// <param name="defaultTranslation"> | |
264 /// The string to return if no translation can be found. Can be null or any string. | |
265 /// </param> | |
266 /// <param name="replacements"> | |
267 /// A collection of <see cref="System.Object"/>s to replace placeholders with | |
268 /// </param> | |
269 /// <returns> | |
270 /// The translation, if one exists, or the supplied default with the placeholders replaced | |
37 | 271 /// </returns> |
272 public static string GetTranslation(string translationID, string defaultTranslation, params object[] replacements) | |
273 { | |
274 CheckInitialisation(); | |
275 string trans = GetTranslationFromTables(translationID); | |
276 | |
277 if (trans == null) | |
278 { | |
279 trans = GetDefaultTranslation(translationID, defaultTranslation); | |
280 } | |
281 | |
282 trans = AddVariablesToTranslation(trans, replacements); | |
283 | |
284 return trans; | |
6 | 285 } |
286 | |
287 private static string GetTranslationFromTables(string translationID) | |
288 { | |
289 string translation = null; | |
290 | |
37 | 291 if (translationsLocal!=null) |
292 { | |
293 translationsLocal.TryGetValue(translationID, out translation); | |
294 } | |
295 | |
296 if (translation == null) | |
297 { | |
298 translationsDefault.TryGetValue(translationID, out translation); | |
6 | 299 } |
300 | |
301 return translation; | |
302 } | |
303 | |
304 private static string GetDefaultTranslation(string translationID, string defaultTranslation) | |
305 { | |
306 return (defaultTranslation != "" && defaultTranslation != null) ? defaultTranslation : GetMissingTranslationMessage(translationID); | |
37 | 307 } |
6 | 308 |
309 private static string GetMissingTranslationMessage(string translationID) | |
310 { | |
311 return "++ Missing Translation "+translationID+" ++"; | |
312 } | |
313 | |
314 private static string AddVariablesToTranslation(string translation, object[] replacements) | |
315 { | |
37 | 316 if (translation != null && replacements != null && replacements.Length > 0) |
317 { | |
318 translation = String.Format(translation, replacements); | |
6 | 319 } |
320 | |
321 return translation; | |
322 } | |
323 | |
37 | 324 private static void CheckInitialisation() |
325 { | |
326 if (translationDir==null) | |
327 { | |
328 throw new InvalidOperationException("Translation class has not been initialised"); | |
329 } | |
330 } | |
6 | 331 |
332 /// <summary> | |
36
c949727ec0e0
Re #22 - Make failing control translation cleaner for normal use
IBBoard <dev@ibboard.co.uk>
parents:
35
diff
changeset
|
333 /// 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
|
334 /// does not exist then a warning message will be used as the translated text. |
6 | 335 /// </summary> |
336 /// <param name="item"> | |
337 /// A <see cref="ITranslatable"/> to set the text for | |
338 /// </param> | |
339 /// <param name="replacements"> | |
340 /// A collection of <see cref="System.Object"/>s that will be used to fill place-holders | |
37 | 341 /// </param> |
342 public static void Translate(ITranslatable item, params object[] replacements) | |
343 { | |
344 Translate(item, (string)null, replacements); | |
345 } | |
346 | |
347 /// <summary> | |
348 /// Translate an <see cref="ITranslatable"/> item, with optional string replacement. The <code>defaultText</code> | |
349 /// can be used to specify an alternate translation. Passing <code>null</code> will result in a warning message | |
350 /// about a missing translation ID. | |
351 /// </summary> | |
352 /// <param name="item"> | |
353 /// A <see cref="ITranslatable"/> to set the text for | |
354 /// </param> | |
355 /// <param name="defaultText"> | |
356 /// The default string to display if no translation could be found. | |
357 /// </param> | |
358 /// <param name="replacements"> | |
359 /// A collection of <see cref="System.Object"/>s that will be used to fill place-holders | |
360 /// </param> | |
361 public static void Translate(ITranslatable item, string defaultText, params object[] replacements) | |
362 { | |
363 if (item.Text == "" || item.Text == DIVIDER_STRING) | |
364 { | |
365 //it doesn't need translating - either there is no text from the developer or it's a hyphen for a divider | |
366 return; | |
367 } | |
368 | |
369 item.Text = GetTranslation(item.Name, defaultText, replacements); | |
6 | 370 } |
371 | |
372 /// <summary> | |
373 /// 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. | |
374 /// </summary> | |
375 /// <returns> | |
376 /// The string used in the file name of the current local translation | |
377 /// </returns> | |
378 public static string GetTranslationLanguage() | |
379 { | |
380 return lang; | |
37 | 381 } |
382 } | |
383 } |