# HG changeset patch # User IBBoard # Date 1270568480 0 # Node ID b5d7e8b932057c4860904fc58924341ca57e3707 # Parent ecf3ce7bd6c598d274b06990ec5cf0e130595d9f Re #30: Improve Translations API * Add translation sets * Add reset method to translations * Fix translations where API documentation (contract) didn't match behaviour diff -r ecf3ce7bd6c5 -r b5d7e8b93205 IBBoard.csproj --- a/IBBoard.csproj Sat Mar 06 19:59:37 2010 +0000 +++ b/IBBoard.csproj Tue Apr 06 15:41:20 2010 +0000 @@ -135,6 +135,9 @@ + + + @@ -151,6 +154,7 @@ PreserveNewest + false diff -r ecf3ce7bd6c5 -r b5d7e8b93205 Lang/AbstractTranslationSet.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Lang/AbstractTranslationSet.cs Tue Apr 06 15:41:20 2010 +0000 @@ -0,0 +1,51 @@ +// This file (AbstractTranslationSet.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.Collections.Generic; + +namespace IBBoard.Lang +{ + /// + /// A collection of translations for a given language. The abstract class must be extended by implementations that + /// provide different ways of loading the data. + /// + public class AbstractTranslationSet + { + private string langCode; + protected Dictionary translations; + + public AbstractTranslationSet(string languageCode) + { + langCode = languageCode; + translations = new Dictionary(); + } + + /// + /// Gets the language code that this translation claims to be for + /// + public string LanguageCode + { + get { return langCode; } + } + + /// + /// Gets a translation from the translation set, or null if the translation doesn't exist. + /// + /// + /// The key (ID) of the translation to retrieve + /// + public string this[string key] + { + set + { + translations[key] = value; + } + get + { + return DictionaryUtils.GetValue(translations, key); + } + } + } +} diff -r ecf3ce7bd6c5 -r b5d7e8b93205 Lang/ModifiableTranslationSet.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Lang/ModifiableTranslationSet.cs Tue Apr 06 15:41:20 2010 +0000 @@ -0,0 +1,32 @@ +// This file (TranslationSet.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.Collections.Generic; + +namespace IBBoard.Lang +{ + /// + /// A set of translations in a given language that allow setting of translations as well as getting + /// + public class ModifiableTranslationSet : AbstractTranslationSet + { + public ModifiableTranslationSet(string languageCode) : base(languageCode) + { + //Do nothing extra + } + + public string this[string key] + { + set + { + translations[key] = value; + } + get + { + return base[key]; + } + } + } +} diff -r ecf3ce7bd6c5 -r b5d7e8b93205 Lang/Translation.cs --- a/Lang/Translation.cs Sat Mar 06 19:59:37 2010 +0000 +++ b/Lang/Translation.cs Tue Apr 06 15:41:20 2010 +0000 @@ -62,7 +62,7 @@ } else { - throw new TranslationLoadException("Translation path not found ("+translationPath+")"); + throw new TranslationLoadException("Translation path not found ("+new FileInfo(translationPath).FullName+")"); } } @@ -180,6 +180,15 @@ { throw new TranslationLoadException("Problem validating schema for translation: " + e.Exception.Message, e.Exception); } + + /// + /// Resets the loaded translations + /// + public static void Reset() + { + translationsLocal.Clear(); + translationsDefault.Clear(); + } /// /// Loads translations for a given language and sets them as the local language. @@ -300,7 +309,7 @@ private static string GetDefaultTranslation(string translationID, string defaultTranslation) { - return (defaultTranslation != "" && defaultTranslation != null) ? defaultTranslation : GetMissingTranslationMessage(translationID); + return (defaultTranslation != "" || defaultTranslation == null) ? defaultTranslation : GetMissingTranslationMessage(translationID); } private static string GetMissingTranslationMessage(string translationID) @@ -330,7 +339,7 @@ /// public static void Translate(ITranslatable item, params object[] replacements) { - Translate(item, (string)null, replacements); + Translate(item, GetMissingTranslationMessage(item.Name), replacements); } /// diff -r ecf3ce7bd6c5 -r b5d7e8b93205 Lang/TranslationXmlLoader.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Lang/TranslationXmlLoader.cs Tue Apr 06 15:41:20 2010 +0000 @@ -0,0 +1,18 @@ +// This file (TranslationXmlLoader.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; + +namespace IBBoard.Lang +{ + + + public class TranslationXmlLoader + { + + public TranslationXmlLoader () + { + } + } +}