changeset 69:b5d7e8b93205

Re #30: Improve Translations API * Add translation sets * Add reset method to translations * Fix translations where API documentation (contract) didn't match behaviour
author IBBoard <dev@ibboard.co.uk>
date Tue, 06 Apr 2010 15:41:20 +0000
parents ecf3ce7bd6c5
children 753be4b6c3b0
files IBBoard.csproj Lang/AbstractTranslationSet.cs Lang/ModifiableTranslationSet.cs Lang/Translation.cs Lang/TranslationXmlLoader.cs
diffstat 5 files changed, 117 insertions(+), 3 deletions(-) [+]
line diff
     1.1 --- a/IBBoard.csproj	Sat Mar 06 19:59:37 2010 +0000
     1.2 +++ b/IBBoard.csproj	Tue Apr 06 15:41:20 2010 +0000
     1.3 @@ -135,6 +135,9 @@
     1.4      <Compile Include="Limits\IPercentageLimit.cs" />
     1.5      <Compile Include="Limits\INumericLimit.cs" />
     1.6      <Compile Include="EnumTools.cs" />
     1.7 +    <Compile Include="Lang\ModifiableTranslationSet.cs" />
     1.8 +    <Compile Include="Lang\TranslationXmlLoader.cs" />
     1.9 +    <Compile Include="Lang\AbstractTranslationSet.cs" />
    1.10    </ItemGroup>
    1.11    <ItemGroup>
    1.12      <Content Include="libs\log4net.dll" />
    1.13 @@ -151,6 +154,7 @@
    1.14      <None Include="COPYING.LGPL" />
    1.15      <None Include="schemas\translation.xsd">
    1.16        <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    1.17 +      <Gettext-ScanForTranslations>false</Gettext-ScanForTranslations>
    1.18      </None>
    1.19    </ItemGroup>
    1.20    <ItemGroup>
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/Lang/AbstractTranslationSet.cs	Tue Apr 06 15:41:20 2010 +0000
     2.3 @@ -0,0 +1,51 @@
     2.4 +// This file (AbstractTranslationSet.cs) is a part of the IBBoard project and is copyright 2010 IBBoard
     2.5 +// 
     2.6 +// 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.
     2.7 +
     2.8 +using System;
     2.9 +using System.Collections.Generic;
    2.10 +
    2.11 +namespace IBBoard.Lang
    2.12 +{
    2.13 +	/// <summary>
    2.14 +	/// A collection of translations for a given language. The abstract class must be extended by implementations that
    2.15 +	/// provide different ways of loading the data.
    2.16 +	/// </summary>
    2.17 +	public class AbstractTranslationSet
    2.18 +	{
    2.19 +		private string langCode;
    2.20 +		protected Dictionary<string, string> translations;
    2.21 +		
    2.22 +		public AbstractTranslationSet(string languageCode)
    2.23 +		{
    2.24 +			langCode = languageCode;
    2.25 +			translations = new Dictionary<string, string>();
    2.26 +		}
    2.27 +		
    2.28 +		/// <summary>
    2.29 +		/// Gets the language code that this translation claims to be for
    2.30 +		/// </summary>
    2.31 +		public string LanguageCode
    2.32 +		{
    2.33 +			get { return langCode; }
    2.34 +		}
    2.35 +		
    2.36 +		/// <summary>
    2.37 +		/// Gets a translation from the translation set, or <code>null</code> if the translation doesn't exist.
    2.38 +		/// </summary>
    2.39 +		/// <param name="key">
    2.40 +		/// The key (ID) of the translation to retrieve
    2.41 +		/// </param>
    2.42 +		public string this[string key]
    2.43 +		{
    2.44 +			set
    2.45 +			{
    2.46 +				translations[key] = value;
    2.47 +			}
    2.48 +			get
    2.49 +			{
    2.50 +				return DictionaryUtils.GetValue(translations, key);
    2.51 +			}
    2.52 +		}	
    2.53 +	}
    2.54 +}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/Lang/ModifiableTranslationSet.cs	Tue Apr 06 15:41:20 2010 +0000
     3.3 @@ -0,0 +1,32 @@
     3.4 +// This file (TranslationSet.cs) is a part of the IBBoard project and is copyright 2010 IBBoard
     3.5 +// 
     3.6 +// 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.
     3.7 +
     3.8 +using System;
     3.9 +using System.Collections.Generic;
    3.10 +
    3.11 +namespace IBBoard.Lang
    3.12 +{
    3.13 +	/// <summary>
    3.14 +	/// A set of translations in a given language that allow setting of translations as well as getting
    3.15 +	/// </summary>
    3.16 +	public class ModifiableTranslationSet : AbstractTranslationSet
    3.17 +	{		
    3.18 +		public ModifiableTranslationSet(string languageCode) : base(languageCode)
    3.19 +		{
    3.20 +			//Do nothing extra
    3.21 +		}
    3.22 +		
    3.23 +		public string this[string key]
    3.24 +		{
    3.25 +			set
    3.26 +			{
    3.27 +				translations[key] = value;
    3.28 +			}
    3.29 +			get
    3.30 +			{
    3.31 +				return base[key];
    3.32 +			}
    3.33 +		}		
    3.34 +	}
    3.35 +}
     4.1 --- a/Lang/Translation.cs	Sat Mar 06 19:59:37 2010 +0000
     4.2 +++ b/Lang/Translation.cs	Tue Apr 06 15:41:20 2010 +0000
     4.3 @@ -62,7 +62,7 @@
     4.4  			}
     4.5  			else
     4.6  			{
     4.7 -				throw new TranslationLoadException("Translation path not found ("+translationPath+")");
     4.8 +				throw new TranslationLoadException("Translation path not found ("+new FileInfo(translationPath).FullName+")");
     4.9  			}
    4.10  		}
    4.11  		
    4.12 @@ -180,6 +180,15 @@
    4.13  		{
    4.14  			throw new TranslationLoadException("Problem validating schema for translation: " + e.Exception.Message, e.Exception);
    4.15  		}
    4.16 +		
    4.17 +		/// <summary>
    4.18 +		/// Resets the loaded translations
    4.19 +		/// </summary>
    4.20 +		public static void Reset()
    4.21 +		{
    4.22 +			translationsLocal.Clear();
    4.23 +			translationsDefault.Clear();
    4.24 +		}
    4.25  
    4.26  		/// <summary>
    4.27  		/// Loads translations for a given language and sets them as the local language.
    4.28 @@ -300,7 +309,7 @@
    4.29  		
    4.30  		private static string GetDefaultTranslation(string translationID, string defaultTranslation)
    4.31  		{
    4.32 -			return (defaultTranslation != "" && defaultTranslation != null) ? defaultTranslation : GetMissingTranslationMessage(translationID);
    4.33 +			return (defaultTranslation != "" || defaultTranslation == null) ? defaultTranslation : GetMissingTranslationMessage(translationID);
    4.34  		}
    4.35  
    4.36  		private static string GetMissingTranslationMessage(string translationID)
    4.37 @@ -330,7 +339,7 @@
    4.38  		/// </param>
    4.39  		public static void Translate(ITranslatable item, params object[] replacements)
    4.40  		{
    4.41 -			Translate(item, (string)null, replacements);
    4.42 +			Translate(item, GetMissingTranslationMessage(item.Name), replacements);
    4.43  		}
    4.44  
    4.45  		/// <summary>
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/Lang/TranslationXmlLoader.cs	Tue Apr 06 15:41:20 2010 +0000
     5.3 @@ -0,0 +1,18 @@
     5.4 +// This file (TranslationXmlLoader.cs) is a part of the IBBoard project and is copyright 2010 IBBoard
     5.5 +// 
     5.6 +// 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.
     5.7 +
     5.8 +using System;
     5.9 +
    5.10 +namespace IBBoard.Lang
    5.11 +{
    5.12 +
    5.13 +
    5.14 +	public class TranslationXmlLoader
    5.15 +	{
    5.16 +
    5.17 +		public TranslationXmlLoader ()
    5.18 +		{
    5.19 +		}
    5.20 +	}
    5.21 +}