diff Lang/AbstractTranslationSet.cs @ 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 091bfa54d6c7
children f45d28dc1d6a
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;