changeset 75:b1ae6fce2e3f

Re #35: Add multi-level cascading of translations * Add "parent language" property * Make translation fetching request parent translation if none is found
author IBBoard <dev@ibboard.co.uk>
date Wed, 07 Apr 2010 19:34:38 +0000
parents 726731c78414
children f45d28dc1d6a
files Lang/AbstractTranslationSet.cs Lang/ModifiableTranslationSet.cs Lang/Translation.cs
diffstat 3 files changed, 82 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/Lang/AbstractTranslationSet.cs	Wed Apr 07 14:56:17 2010 +0000
+++ b/Lang/AbstractTranslationSet.cs	Wed Apr 07 19:34:38 2010 +0000
@@ -11,17 +11,30 @@
 	/// 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
+	public abstract class AbstractTranslationSet
 	{
 		private TranslationLanguage language;
 		protected Dictionary<string, string> translations;
+		private TranslationLanguage parentLang;
 		
-		public AbstractTranslationSet(string languageCode)
+		public AbstractTranslationSet(string languageCode) : this(languageCode, "")
+		{
+			//Do nothing extra
+		}
+		
+		public AbstractTranslationSet(string languageCode, string parentLanguageCode)
 		{
 			language = new TranslationLanguage(languageCode);
 			translations = new Dictionary<string, string>();
+			
+			if (parentLanguageCode != null && !parentLanguageCode.Trim().Equals(""))
+			{
+				ParentLanguage = new TranslationLanguage(parentLanguageCode);
+			}
+			
 		}
 		
+		
 		/// <summary>
 		/// Gets the language code that this translation claims to be for
 		/// </summary>
@@ -40,9 +53,31 @@
 		{
 			get
 			{
-				return DictionaryUtils.GetValue(translations, key);
+				string translation = DictionaryUtils.GetValue(translations, key);
+				
+				if (parentLang != null && translation == null)
+				{
+					translation = GetParentTranslation(key);
+				}
+				
+				return translation;
 			}
 		}
+
+		private string GetParentTranslation(string key)
+		{
+			AbstractTranslationSet parentTranslations = GetParentTranslations();
+			string parentTranslation = null;
+			
+			if (parentTranslations != null)
+			{
+				parentTranslation = parentTranslations[key];
+			}
+			
+			return parentTranslation;
+		}
+
+		protected abstract AbstractTranslationSet GetParentTranslations();
 		
 		public string LanguageName
 		{
@@ -57,6 +92,12 @@
 			get { return language; }
 		}
 		
+		public TranslationLanguage ParentLanguage
+		{
+			get { return parentLang; }
+			protected set { parentLang = value; }
+		}
+		
 		public override bool Equals(object obj)
 		{
 			bool equal = true;
--- a/Lang/ModifiableTranslationSet.cs	Wed Apr 07 14:56:17 2010 +0000
+++ b/Lang/ModifiableTranslationSet.cs	Wed Apr 07 19:34:38 2010 +0000
@@ -12,7 +12,14 @@
 	/// </summary>
 	public class ModifiableTranslationSet : AbstractTranslationSet
 	{		
-		public ModifiableTranslationSet(string languageCode) : base(languageCode)
+		private AbstractTranslationSet parentTranslationSet;
+		
+		public ModifiableTranslationSet(string languageCode) : this(languageCode, "")
+		{
+			//Do nothing extra
+		}
+		
+		public ModifiableTranslationSet(string languageCode, string parentLanguageCode) : base(languageCode, parentLanguageCode)
 		{
 			//Do nothing extra
 		}
@@ -21,5 +28,17 @@
 		{
 			translations[key] = translation;
 		}
+
+		public void SetParentTranslations(ModifiableTranslationSet parentTranslations)
+		{
+			ParentLanguage = parentTranslations.Language;
+			parentTranslationSet = parentTranslations;
+		}
+		
+		protected override AbstractTranslationSet GetParentTranslations()
+		{
+			return parentTranslationSet;
+		}
+
 	}
 }
--- a/Lang/Translation.cs	Wed Apr 07 14:56:17 2010 +0000
+++ b/Lang/Translation.cs	Wed Apr 07 19:34:38 2010 +0000
@@ -129,7 +129,7 @@
 		
 		private static void LoadTranslationForLanguage(string translationLanguage)
 		{			
-			SetCurrentTranslations(DictionaryUtils.GetValue(langToTranslationMap, translationLanguage));
+			SetCurrentTranslations(GetTranslationSet(translationLanguage));
 		}
 
 		/// <summary>
@@ -292,5 +292,22 @@
 			
 			return langs;
 		}
+
+		public static AbstractTranslationSet GetTranslationSet(string translationLanguage)
+		{
+			AbstractTranslationSet translations = null;
+			
+			if (translationLanguage != null)
+			{
+				translations = DictionaryUtils.GetValue(langToTranslationMap, translationLanguage);
+				
+				if (translations == null)
+				{
+					translations = new ModifiableTranslationSet(translationLanguage);
+				}
+			}
+			
+			return translations;
+		}
 	}
 }