Mercurial > repos > IBBoard
comparison Lang/Translation.cs @ 67:5fb2e5a2e7a8
* Remove errors on uninitialised translations - means that the translations just fall back to the provided string if they haven't been initialised
no-open-ticket
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Wed, 17 Feb 2010 20:10:27 +0000 |
parents | 980ebd49c40b |
children | b5d7e8b93205 |
comparison
equal
deleted
inserted
replaced
66:7e5f5cb0a9ea | 67:5fb2e5a2e7a8 |
---|---|
24 { | 24 { |
25 private static readonly string DEFAULT_LANGUAGE = "en"; | 25 private static readonly string DEFAULT_LANGUAGE = "en"; |
26 private static readonly string DIVIDER_STRING = "-"; | 26 private static readonly string DIVIDER_STRING = "-"; |
27 private static string lang = ""; | 27 private static string lang = ""; |
28 private static DirectoryInfo translationDir; | 28 private static DirectoryInfo translationDir; |
29 private static Dictionary<string, string> translationsLocal; | 29 private static Dictionary<string, string> translationsLocal = new Dictionary<string, string>(); |
30 private static Dictionary<string, string> translationsDefault; | 30 private static Dictionary<string, string> translationsDefault = new Dictionary<string, string>(); |
31 private static XmlReaderSettings settings; | 31 private static XmlReaderSettings settings; |
32 | 32 |
33 /// <summary> | 33 /// <summary> |
34 /// Initialises the translations for the language specified and the default translations so that the Translation class can be used. | 34 /// Initialises the translations for the language specified and the default translations so that the Translation class can be used. |
35 /// Throws a TranslationLoadException if a problem occurred while loading translations. If this occurs then the translation methods can | 35 /// Throws a TranslationLoadException if a problem occurred while loading translations. If this occurs then the translation methods can |
198 | 198 |
199 LoadTranslationForLanguage(translationLanguage); | 199 LoadTranslationForLanguage(translationLanguage); |
200 } | 200 } |
201 | 201 |
202 private static void LoadTranslationForLanguage(string translationLanguage) | 202 private static void LoadTranslationForLanguage(string translationLanguage) |
203 { | 203 { |
204 CheckInitialisation(); | |
205 | |
206 if (translationLanguage != DEFAULT_LANGUAGE && translationLanguage != "" && translationLanguage != null) | 204 if (translationLanguage != DEFAULT_LANGUAGE && translationLanguage != "" && translationLanguage != null) |
207 { | 205 { |
208 FileInfo file = GetTranslationFile(translationLanguage); | 206 FileInfo file = GetTranslationFile(translationLanguage); |
209 XmlDocument doc = LoadTranslationDocument(file); | 207 XmlDocument doc = LoadTranslationDocument(file); |
210 LoadTranslationsFromDocument(doc, translationsLocal); | 208 LoadTranslationsFromDocument(doc, translationsLocal); |
269 /// <returns> | 267 /// <returns> |
270 /// The translation, if one exists, or the supplied default with the placeholders replaced | 268 /// The translation, if one exists, or the supplied default with the placeholders replaced |
271 /// </returns> | 269 /// </returns> |
272 public static string GetTranslation(string translationID, string defaultTranslation, params object[] replacements) | 270 public static string GetTranslation(string translationID, string defaultTranslation, params object[] replacements) |
273 { | 271 { |
274 CheckInitialisation(); | |
275 string trans = GetTranslationFromTables(translationID); | 272 string trans = GetTranslationFromTables(translationID); |
276 | 273 |
277 if (trans == null) | 274 if (trans == null) |
278 { | 275 { |
279 trans = GetDefaultTranslation(translationID, defaultTranslation); | 276 trans = GetDefaultTranslation(translationID, defaultTranslation); |
317 { | 314 { |
318 translation = String.Format(translation, replacements); | 315 translation = String.Format(translation, replacements); |
319 } | 316 } |
320 | 317 |
321 return translation; | 318 return translation; |
322 } | |
323 | |
324 private static void CheckInitialisation() | |
325 { | |
326 if (translationDir==null) | |
327 { | |
328 throw new InvalidOperationException("Translation class has not been initialised"); | |
329 } | |
330 } | 319 } |
331 | 320 |
332 /// <summary> | 321 /// <summary> |
333 /// Translate an <see cref="ITranslatable"/> item, with optional string replacement. If the translation | 322 /// Translate an <see cref="ITranslatable"/> item, with optional string replacement. If the translation |
334 /// does not exist then a warning message will be used as the translated text. | 323 /// does not exist then a warning message will be used as the translated text. |