# HG changeset patch # User IBBoard # Date 1270580310 0 # Node ID 091bfa54d6c7461f23e0f754af7ba456b0a1b4a9 # Parent cec6c4c0892d75fa6505b91d196832dead09acfb Re #33: Add method to get list of available translations * Add "translation language" object to pair up code and name * Load all translations at initialisation time Also: * Remove rogue "set" property on abstract translation set diff -r cec6c4c0892d -r 091bfa54d6c7 IBBoard.csproj --- a/IBBoard.csproj Tue Apr 06 18:20:14 2010 +0000 +++ b/IBBoard.csproj Tue Apr 06 18:58:30 2010 +0000 @@ -138,6 +138,7 @@ + diff -r cec6c4c0892d -r 091bfa54d6c7 Lang/AbstractTranslationSet.cs --- a/Lang/AbstractTranslationSet.cs Tue Apr 06 18:20:14 2010 +0000 +++ b/Lang/AbstractTranslationSet.cs Tue Apr 06 18:58:30 2010 +0000 @@ -4,7 +4,6 @@ using System; using System.Collections.Generic; -using System.Globalization; namespace IBBoard.Lang { @@ -14,13 +13,12 @@ /// public class AbstractTranslationSet { - private string langCode; - private string langName; + private TranslationLanguage language; protected Dictionary translations; public AbstractTranslationSet(string languageCode) { - langCode = languageCode; + language = new TranslationLanguage(languageCode); translations = new Dictionary(); } @@ -29,7 +27,7 @@ /// public string LanguageCode { - get { return langCode; } + get { return language.Code; } } /// @@ -40,10 +38,6 @@ /// public string this[string key] { - set - { - translations[key] = value; - } get { return DictionaryUtils.GetValue(translations, key); @@ -54,26 +48,34 @@ { get { - if (langName == null) - { - LoadLangName(); - } - - return langName; + return language.Name; } } + + public TranslationLanguage Language + { + get { return language; } + } - private void LoadLangName() + public override bool Equals(object obj) { - try + bool equal = true; + + if (obj == null || !obj.GetType().Equals(GetType())) + { + equal = false; + } + else { - CultureInfo culture = CultureInfo.GetCultureInfo(langCode); - langName = culture.NativeName; + equal = LanguageCode.Equals(((AbstractTranslationSet)obj).LanguageCode); } - catch(ArgumentException) - { - langName = "Unknown ("+langCode+")"; - } + + return equal; + } + + public override int GetHashCode () + { + return GetType().GetHashCode() + LanguageCode.GetHashCode(); } } } diff -r cec6c4c0892d -r 091bfa54d6c7 Lang/Translation.cs --- a/Lang/Translation.cs Tue Apr 06 18:20:14 2010 +0000 +++ b/Lang/Translation.cs Tue Apr 06 18:58:30 2010 +0000 @@ -26,9 +26,13 @@ public class Translation { private static readonly string DIVIDER_STRING = "-"; - private static DirectoryInfo translationDir; - private static AbstractTranslationSet translations; - private static TranslationXmlLoader loader; + private static AbstractTranslationSet currentTranslations; + private static Dictionary langToTranslationMap; + + static Translation() + { + langToTranslationMap = new Dictionary(); + } /// /// Initialises the translations and loads the specified language so that the Translation class can be used. @@ -60,17 +64,25 @@ private static void InitialiseDefaults(string appPath) { - string translationPath = Path.Combine(appPath, "translations"); - - if (Directory.Exists(translationPath)) + Reset(); + LoadTranslations(appPath); + } + + private static void LoadTranslations(string appPath) + { + DirectoryInfo dir = new DirectoryInfo(Path.Combine(appPath, "translations")); + + if (!dir.Exists) { - translations = null; - translationDir = new DirectoryInfo(translationPath); - loader = new TranslationXmlLoader(Path.Combine(appPath, "schemas/translation.xsd")); + throw new TranslationLoadException("Translation path not found (" + dir.FullName + ")"); } - else + + TranslationXmlLoader loader = new TranslationXmlLoader(Path.Combine(appPath, "schemas/translation.xsd")); + + foreach (FileInfo file in dir.GetFiles("*.translation")) { - throw new TranslationLoadException("Translation path not found ("+new FileInfo(translationPath).FullName+")"); + AbstractTranslationSet translations = loader.LoadTranslations(file.FullName); + langToTranslationMap[translations.LanguageCode] = translations; } } @@ -79,7 +91,8 @@ /// public static void Reset() { - translations = null; + currentTranslations = null; + langToTranslationMap.Clear(); } /// @@ -91,7 +104,7 @@ /// The new local language to load /// public static void LoadTranslation(string translationLanguage) - { + { if (translationLanguage == "" || translationLanguage == null) { throw new ArgumentException("Translation language cannot be null or empty"); @@ -102,27 +115,7 @@ private static void LoadTranslationForLanguage(string translationLanguage) { - if (translationLanguage != "" && translationLanguage != null) - { - translations = loader.LoadTranslations(GetTranslationFile(translationLanguage)); - } - else - { - translations = null; - } - } - - private static string GetTranslationFile(string language) - { - string translationFileName = language + ".translation"; - string path = Path.Combine(translationDir.FullName, translationFileName); - - if (!File.Exists(path)) - { - throw new TranslationLoadException(translationFileName +" could not be found in "+translationDir.FullName); - } - - return path; + currentTranslations = DictionaryUtils.GetValue(langToTranslationMap, translationLanguage); } /// @@ -195,9 +188,9 @@ { string translation = null; - if (translations!=null) + if (currentTranslations!=null) { - translation = translations[translationID]; + translation = currentTranslations[translationID]; } return translation; @@ -271,7 +264,19 @@ /// public static string GetTranslationLanguage() { - return (translations!=null ? translations.LanguageCode : ""); + return (currentTranslations != null ? currentTranslations.LanguageCode : ""); + } + + public static ICollection GetLanguages() + { + ICollection langs = new List(); + + foreach (AbstractTranslationSet translations in langToTranslationMap.Values) + { + langs.Add(translations.Language); + } + + return langs; } } } diff -r cec6c4c0892d -r 091bfa54d6c7 Lang/TranslationLanguage.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Lang/TranslationLanguage.cs Tue Apr 06 18:58:30 2010 +0000 @@ -0,0 +1,76 @@ +// This file (TranslationLanguage.cs) is a part of the IBBoard project and is copyright 2010 IBBoard +// +// The file and the library/program it is in are licensed and distributed, without warranty, under the GNU Affero GPL license, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license. + +using System; +using System.Globalization; + +namespace IBBoard.Lang +{ + /// + /// A simple object that holds the name and code pairing for a language + /// + public class TranslationLanguage + { + private string langCode; + private string langName; + + public TranslationLanguage(string languageCode) + { + langCode = languageCode; + } + + public string Name + { + get + { + if (langName == null) + { + LoadLangName(); + } + + return langName; + } + } + + public string Code + { + get { return langCode; } + } + + private void LoadLangName() + { + try + { + CultureInfo culture = CultureInfo.GetCultureInfo(langCode); + langName = culture.NativeName; + } + catch (ArgumentException) + { + langName = "Unknown (" + langCode + ")"; + } + } + + public override bool Equals(object obj) + { + bool equal = true; + + if (obj == null || !obj.GetType().Equals(GetType())) + { + equal = false; + } + + else + { + equal = Code.Equals(((TranslationLanguage)obj).Code); + } + + return equal; + } + + public override int GetHashCode() + { + return GetType().GetHashCode() + langCode.GetHashCode(); + } + } +}