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 wrap: on
line diff
--- 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 @@
     <Compile Include="Limits\IPercentageLimit.cs" />
     <Compile Include="Limits\INumericLimit.cs" />
     <Compile Include="EnumTools.cs" />
+    <Compile Include="Lang\ModifiableTranslationSet.cs" />
+    <Compile Include="Lang\TranslationXmlLoader.cs" />
+    <Compile Include="Lang\AbstractTranslationSet.cs" />
   </ItemGroup>
   <ItemGroup>
     <Content Include="libs\log4net.dll" />
@@ -151,6 +154,7 @@
     <None Include="COPYING.LGPL" />
     <None Include="schemas\translation.xsd">
       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+      <Gettext-ScanForTranslations>false</Gettext-ScanForTranslations>
     </None>
   </ItemGroup>
   <ItemGroup>
--- /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
+{
+	/// <summary>
+	/// A collection of translations for a given language. The abstract class must be extended by implementations that
+	/// provide different ways of loading the data.
+	/// </summary>
+	public class AbstractTranslationSet
+	{
+		private string langCode;
+		protected Dictionary<string, string> translations;
+		
+		public AbstractTranslationSet(string languageCode)
+		{
+			langCode = languageCode;
+			translations = new Dictionary<string, string>();
+		}
+		
+		/// <summary>
+		/// Gets the language code that this translation claims to be for
+		/// </summary>
+		public string LanguageCode
+		{
+			get { return langCode; }
+		}
+		
+		/// <summary>
+		/// Gets a translation from the translation set, or <code>null</code> if the translation doesn't exist.
+		/// </summary>
+		/// <param name="key">
+		/// The key (ID) of the translation to retrieve
+		/// </param>
+		public string this[string key]
+		{
+			set
+			{
+				translations[key] = value;
+			}
+			get
+			{
+				return DictionaryUtils.GetValue(translations, key);
+			}
+		}	
+	}
+}
--- /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
+{
+	/// <summary>
+	/// A set of translations in a given language that allow setting of translations as well as getting
+	/// </summary>
+	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];
+			}
+		}		
+	}
+}
--- 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);
 		}
+		
+		/// <summary>
+		/// Resets the loaded translations
+		/// </summary>
+		public static void Reset()
+		{
+			translationsLocal.Clear();
+			translationsDefault.Clear();
+		}
 
 		/// <summary>
 		/// 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 @@
 		/// </param>
 		public static void Translate(ITranslatable item, params object[] replacements)
 		{
-			Translate(item, (string)null, replacements);
+			Translate(item, GetMissingTranslationMessage(item.Name), replacements);
 		}
 
 		/// <summary>
--- /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 ()
+		{
+		}
+	}
+}