Mercurial > repos > IBBoard
diff Lang/Translation.cs @ 37:cc7fae81afec
* Fix line terminators
no-open-ticket
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sat, 27 Jun 2009 19:03:58 +0000 |
parents | c949727ec0e0 |
children | 70d6c2a5d99e |
line wrap: on
line diff
--- a/Lang/Translation.cs Tue May 19 19:55:21 2009 +0000 +++ b/Lang/Translation.cs Sat Jun 27 19:03:58 2009 +0000 @@ -2,33 +2,33 @@ // // 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. -using System; -using System.IO; +using System; +using System.IO; using System.Xml; -using System.Xml.Schema; -using System.Collections.Generic; -using System.Reflection; +using System.Xml.Schema; +using System.Collections.Generic; +using System.Reflection; using System.ComponentModel; using IBBoard.IO; -using IBBoard.Logging; -using IBBoard.Xml; - -namespace IBBoard.Lang -{ - /// <summary> +using IBBoard.Logging; +using IBBoard.Xml; + +namespace IBBoard.Lang +{ + /// <summary> /// A basic string translator that loads a default language and a specified language and returns translated strings that correspond to translation IDs. /// 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 - /// then either a supplied value or a "no validation available" message is returned. - /// </summary> - public class Translation + /// then either a supplied value or a "no validation available" message is returned. + /// </summary> + public class Translation { private static readonly string DEFAULT_LANGUAGE = "en"; - private static readonly string DIVIDER_STRING = "-"; - private static string lang = ""; - private static DirectoryInfo translationDir; - private static Dictionary<string, string> translationsLocal; + private static readonly string DIVIDER_STRING = "-"; + private static string lang = ""; + private static DirectoryInfo translationDir; + private static Dictionary<string, string> translationsLocal; private static Dictionary<string, string> translationsDefault; - private static XmlReaderSettings settings; + private static XmlReaderSettings settings; /// <summary> /// Initialises the translations for the language specified and the default translations so that the Translation class can be used. @@ -40,29 +40,29 @@ /// </param> /// <param name="language"> /// The language to use as the load language - /// </param> - public static void InitialiseTranslations(string appPath, string language) + /// </param> + public static void InitialiseTranslations(string appPath, string language) { InitialiseDefaults(appPath); - FileInfo file = GetTranslationFile(DEFAULT_LANGUAGE); - XmlDocument doc = LoadTranslationDocument(file); - LoadTranslationsFromDocument(doc, translationsDefault); - LoadTranslationForLanguage(language); + FileInfo file = GetTranslationFile(DEFAULT_LANGUAGE); + XmlDocument doc = LoadTranslationDocument(file); + LoadTranslationsFromDocument(doc, translationsDefault); + LoadTranslationForLanguage(language); } private static void InitialiseDefaults(string appPath) { - string translationPath = appPath.TrimEnd(Constants.DirectoryChar) + Constants.DirectoryString + "translations"; - - if (Directory.Exists(translationPath)) + string translationPath = appPath.TrimEnd(Constants.DirectoryChar) + Constants.DirectoryString + "translations"; + + if (Directory.Exists(translationPath)) { translationsDefault = new Dictionary<string,string>(); - translationsLocal = new Dictionary<string,string>(); - translationDir = new DirectoryInfo(translationPath); - } - else - { - throw new TranslationLoadException("Translation path not found ("+translationPath+")"); + translationsLocal = new Dictionary<string,string>(); + translationDir = new DirectoryInfo(translationPath); + } + else + { + throw new TranslationLoadException("Translation path not found ("+translationPath+")"); } } @@ -72,7 +72,7 @@ XmlReader valReader = XmlReader.Create(file.FullName, GetReaderSettings()); try - { + { doc.Load(valReader); } catch (DirectoryNotFoundException ex) @@ -88,7 +88,7 @@ throw new TranslationLoadException("Problem reading data for translation: " + ex.Message, ex); } finally - { + { valReader.Close(); } @@ -135,11 +135,11 @@ private static FileInfo GetTranslationFile(string language) { - FileInfo file = new FileInfo(translationDir.FullName + Constants.DirectoryString + language + ".translation"); - - if (!file.Exists) - { - throw new TranslationLoadException(language + ".translation could not be found in "+translationDir.FullName); + FileInfo file = new FileInfo(translationDir.FullName + Constants.DirectoryString + language + ".translation"); + + if (!file.Exists) + { + throw new TranslationLoadException(language + ".translation could not be found in "+translationDir.FullName); } return file; @@ -147,14 +147,14 @@ private static void LoadTranslationsFromDocument(XmlDocument doc, Dictionary<string, string> translationTable) { - try - { + try + { XmlNodeList translations = doc.GetElementsByTagName("translation"); - Dictionary<string, string> tempTranslationTable = new Dictionary<string,string>(); - - foreach (XmlNode node in translations) - { - tempTranslationTable.Add(node.Attributes["id"].Value, node.InnerText); + Dictionary<string, string> tempTranslationTable = new Dictionary<string,string>(); + + foreach (XmlNode node in translations) + { + tempTranslationTable.Add(node.Attributes["id"].Value, node.InnerText); } translationTable.Clear(); @@ -164,11 +164,11 @@ string translation; tempTranslationTable.TryGetValue(key, out translation); translationTable.Add(key, translation); - } - } - catch(Exception ex) - { - throw new TranslationLoadException("Error while parsing " + GetLanguageOfDocument(doc)+" translation: "+ex.Message, ex); + } + } + catch(Exception ex) + { + throw new TranslationLoadException("Error while parsing " + GetLanguageOfDocument(doc)+" translation: "+ex.Message, ex); } } @@ -180,7 +180,7 @@ private static void ValidationEventMethod(object sender, ValidationEventArgs e) { throw new TranslationLoadException("Problem validating schema for translation: " + e.Exception.Message, e.Exception); - } + } /// <summary> /// Loads translations for a given language and sets them as the local language. @@ -189,34 +189,34 @@ /// </summary> /// <param name="translationLang"> /// The new local language to load - /// </param> - public static void LoadTranslation(string translationLanguage) + /// </param> + public static void LoadTranslation(string translationLanguage) { if (translationLanguage == "" || translationLanguage == null) { throw new ArgumentException("Translation language cannot be null or empty"); - } - - LoadTranslationForLanguage(translationLanguage); + } + + LoadTranslationForLanguage(translationLanguage); } private static void LoadTranslationForLanguage(string translationLanguage) - { + { CheckInitialisation(); - if (translationLanguage != DEFAULT_LANGUAGE && translationLanguage != "" && translationLanguage != null) - { - FileInfo file = GetTranslationFile(translationLanguage); - XmlDocument doc = LoadTranslationDocument(file); - LoadTranslationsFromDocument(doc, translationsLocal); + if (translationLanguage != DEFAULT_LANGUAGE && translationLanguage != "" && translationLanguage != null) + { + FileInfo file = GetTranslationFile(translationLanguage); + XmlDocument doc = LoadTranslationDocument(file); + LoadTranslationsFromDocument(doc, translationsLocal); } else { translationsLocal.Clear(); } - + lang = translationLanguage; - } + } /// <summary> /// 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. @@ -229,11 +229,11 @@ /// </param> /// <returns> /// The translation with the placeholders replaced or a "missing translation" message - /// </returns> - public static string GetTranslation(string translationID, params object[] replacements) - { - return GetTranslation(translationID, false, replacements); - } + /// </returns> + public static string GetTranslation(string translationID, params object[] replacements) + { + return GetTranslation(translationID, false, replacements); + } /// <summary> /// 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. @@ -249,11 +249,11 @@ /// </param> /// <returns> /// The translation with the placeholders replaced, or a "missing translation" message or null depending on <param name="returnNullOnFail"> - /// </returns> - public static string GetTranslation(string translationID, bool returnNullOnFail, params object[] replacements) - { - return GetTranslation(translationID, returnNullOnFail ? null : "", replacements); - } + /// </returns> + public static string GetTranslation(string translationID, bool returnNullOnFail, params object[] replacements) + { + return GetTranslation(translationID, returnNullOnFail ? null : "", replacements); + } /// <summary> /// 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. @@ -269,34 +269,34 @@ /// </param> /// <returns> /// The translation, if one exists, or the supplied default with the placeholders replaced - /// </returns> - public static string GetTranslation(string translationID, string defaultTranslation, params object[] replacements) - { - CheckInitialisation(); - string trans = GetTranslationFromTables(translationID); - - if (trans == null) - { - trans = GetDefaultTranslation(translationID, defaultTranslation); - } - - trans = AddVariablesToTranslation(trans, replacements); - - return trans; + /// </returns> + public static string GetTranslation(string translationID, string defaultTranslation, params object[] replacements) + { + CheckInitialisation(); + string trans = GetTranslationFromTables(translationID); + + if (trans == null) + { + trans = GetDefaultTranslation(translationID, defaultTranslation); + } + + trans = AddVariablesToTranslation(trans, replacements); + + return trans; } private static string GetTranslationFromTables(string translationID) { string translation = null; - if (translationsLocal!=null) - { - translationsLocal.TryGetValue(translationID, out translation); - } - - if (translation == null) - { - translationsDefault.TryGetValue(translationID, out translation); + if (translationsLocal!=null) + { + translationsLocal.TryGetValue(translationID, out translation); + } + + if (translation == null) + { + translationsDefault.TryGetValue(translationID, out translation); } return translation; @@ -305,7 +305,7 @@ private static string GetDefaultTranslation(string translationID, string defaultTranslation) { return (defaultTranslation != "" && defaultTranslation != null) ? defaultTranslation : GetMissingTranslationMessage(translationID); - } + } private static string GetMissingTranslationMessage(string translationID) { @@ -314,21 +314,21 @@ private static string AddVariablesToTranslation(string translation, object[] replacements) { - if (translation != null && replacements != null && replacements.Length > 0) - { - translation = String.Format(translation, replacements); + if (translation != null && replacements != null && replacements.Length > 0) + { + translation = String.Format(translation, replacements); } return translation; } - private static void CheckInitialisation() - { - if (translationDir==null) - { - throw new InvalidOperationException("Translation class has not been initialised"); - } - } + private static void CheckInitialisation() + { + if (translationDir==null) + { + throw new InvalidOperationException("Translation class has not been initialised"); + } + } /// <summary> /// Translate an <see cref="ITranslatable"/> item, with optional string replacement. If the translation @@ -339,35 +339,35 @@ /// </param> /// <param name="replacements"> /// A collection of <see cref="System.Object"/>s that will be used to fill place-holders - /// </param> - public static void Translate(ITranslatable item, params object[] replacements) - { - Translate(item, (string)null, replacements); - } - - /// <summary> - /// Translate an <see cref="ITranslatable"/> item, with optional string replacement. The <code>defaultText</code> - /// can be used to specify an alternate translation. Passing <code>null</code> will result in a warning message - /// about a missing translation ID. - /// </summary> - /// <param name="item"> - /// A <see cref="ITranslatable"/> to set the text for - /// </param> - /// <param name="defaultText"> - /// The default string to display if no translation could be found. - /// </param> - /// <param name="replacements"> - /// A collection of <see cref="System.Object"/>s that will be used to fill place-holders - /// </param> - public static void Translate(ITranslatable item, string defaultText, params object[] replacements) - { - if (item.Text == "" || item.Text == DIVIDER_STRING) - { - //it doesn't need translating - either there is no text from the developer or it's a hyphen for a divider - return; - } - - item.Text = GetTranslation(item.Name, defaultText, replacements); + /// </param> + public static void Translate(ITranslatable item, params object[] replacements) + { + Translate(item, (string)null, replacements); + } + + /// <summary> + /// Translate an <see cref="ITranslatable"/> item, with optional string replacement. The <code>defaultText</code> + /// can be used to specify an alternate translation. Passing <code>null</code> will result in a warning message + /// about a missing translation ID. + /// </summary> + /// <param name="item"> + /// A <see cref="ITranslatable"/> to set the text for + /// </param> + /// <param name="defaultText"> + /// The default string to display if no translation could be found. + /// </param> + /// <param name="replacements"> + /// A collection of <see cref="System.Object"/>s that will be used to fill place-holders + /// </param> + public static void Translate(ITranslatable item, string defaultText, params object[] replacements) + { + if (item.Text == "" || item.Text == DIVIDER_STRING) + { + //it doesn't need translating - either there is no text from the developer or it's a hyphen for a divider + return; + } + + item.Text = GetTranslation(item.Name, defaultText, replacements); } /// <summary> @@ -379,6 +379,6 @@ public static string GetTranslationLanguage() { return lang; - } - } -} + } + } +}