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 diff
     1.1 --- a/Lang/AbstractTranslationSet.cs	Wed Apr 07 14:56:17 2010 +0000
     1.2 +++ b/Lang/AbstractTranslationSet.cs	Wed Apr 07 19:34:38 2010 +0000
     1.3 @@ -11,17 +11,30 @@
     1.4  	/// A collection of translations for a given language. The abstract class must be extended by implementations that
     1.5  	/// provide different ways of loading the data.
     1.6  	/// </summary>
     1.7 -	public class AbstractTranslationSet
     1.8 +	public abstract class AbstractTranslationSet
     1.9  	{
    1.10  		private TranslationLanguage language;
    1.11  		protected Dictionary<string, string> translations;
    1.12 +		private TranslationLanguage parentLang;
    1.13  		
    1.14 -		public AbstractTranslationSet(string languageCode)
    1.15 +		public AbstractTranslationSet(string languageCode) : this(languageCode, "")
    1.16 +		{
    1.17 +			//Do nothing extra
    1.18 +		}
    1.19 +		
    1.20 +		public AbstractTranslationSet(string languageCode, string parentLanguageCode)
    1.21  		{
    1.22  			language = new TranslationLanguage(languageCode);
    1.23  			translations = new Dictionary<string, string>();
    1.24 +			
    1.25 +			if (parentLanguageCode != null && !parentLanguageCode.Trim().Equals(""))
    1.26 +			{
    1.27 +				ParentLanguage = new TranslationLanguage(parentLanguageCode);
    1.28 +			}
    1.29 +			
    1.30  		}
    1.31  		
    1.32 +		
    1.33  		/// <summary>
    1.34  		/// Gets the language code that this translation claims to be for
    1.35  		/// </summary>
    1.36 @@ -40,9 +53,31 @@
    1.37  		{
    1.38  			get
    1.39  			{
    1.40 -				return DictionaryUtils.GetValue(translations, key);
    1.41 +				string translation = DictionaryUtils.GetValue(translations, key);
    1.42 +				
    1.43 +				if (parentLang != null && translation == null)
    1.44 +				{
    1.45 +					translation = GetParentTranslation(key);
    1.46 +				}
    1.47 +				
    1.48 +				return translation;
    1.49  			}
    1.50  		}
    1.51 +
    1.52 +		private string GetParentTranslation(string key)
    1.53 +		{
    1.54 +			AbstractTranslationSet parentTranslations = GetParentTranslations();
    1.55 +			string parentTranslation = null;
    1.56 +			
    1.57 +			if (parentTranslations != null)
    1.58 +			{
    1.59 +				parentTranslation = parentTranslations[key];
    1.60 +			}
    1.61 +			
    1.62 +			return parentTranslation;
    1.63 +		}
    1.64 +
    1.65 +		protected abstract AbstractTranslationSet GetParentTranslations();
    1.66  		
    1.67  		public string LanguageName
    1.68  		{
    1.69 @@ -57,6 +92,12 @@
    1.70  			get { return language; }
    1.71  		}
    1.72  		
    1.73 +		public TranslationLanguage ParentLanguage
    1.74 +		{
    1.75 +			get { return parentLang; }
    1.76 +			protected set { parentLang = value; }
    1.77 +		}
    1.78 +		
    1.79  		public override bool Equals(object obj)
    1.80  		{
    1.81  			bool equal = true;
     2.1 --- a/Lang/ModifiableTranslationSet.cs	Wed Apr 07 14:56:17 2010 +0000
     2.2 +++ b/Lang/ModifiableTranslationSet.cs	Wed Apr 07 19:34:38 2010 +0000
     2.3 @@ -12,7 +12,14 @@
     2.4  	/// </summary>
     2.5  	public class ModifiableTranslationSet : AbstractTranslationSet
     2.6  	{		
     2.7 -		public ModifiableTranslationSet(string languageCode) : base(languageCode)
     2.8 +		private AbstractTranslationSet parentTranslationSet;
     2.9 +		
    2.10 +		public ModifiableTranslationSet(string languageCode) : this(languageCode, "")
    2.11 +		{
    2.12 +			//Do nothing extra
    2.13 +		}
    2.14 +		
    2.15 +		public ModifiableTranslationSet(string languageCode, string parentLanguageCode) : base(languageCode, parentLanguageCode)
    2.16  		{
    2.17  			//Do nothing extra
    2.18  		}
    2.19 @@ -21,5 +28,17 @@
    2.20  		{
    2.21  			translations[key] = translation;
    2.22  		}
    2.23 +
    2.24 +		public void SetParentTranslations(ModifiableTranslationSet parentTranslations)
    2.25 +		{
    2.26 +			ParentLanguage = parentTranslations.Language;
    2.27 +			parentTranslationSet = parentTranslations;
    2.28 +		}
    2.29 +		
    2.30 +		protected override AbstractTranslationSet GetParentTranslations()
    2.31 +		{
    2.32 +			return parentTranslationSet;
    2.33 +		}
    2.34 +
    2.35  	}
    2.36  }
     3.1 --- a/Lang/Translation.cs	Wed Apr 07 14:56:17 2010 +0000
     3.2 +++ b/Lang/Translation.cs	Wed Apr 07 19:34:38 2010 +0000
     3.3 @@ -129,7 +129,7 @@
     3.4  		
     3.5  		private static void LoadTranslationForLanguage(string translationLanguage)
     3.6  		{			
     3.7 -			SetCurrentTranslations(DictionaryUtils.GetValue(langToTranslationMap, translationLanguage));
     3.8 +			SetCurrentTranslations(GetTranslationSet(translationLanguage));
     3.9  		}
    3.10  
    3.11  		/// <summary>
    3.12 @@ -292,5 +292,22 @@
    3.13  			
    3.14  			return langs;
    3.15  		}
    3.16 +
    3.17 +		public static AbstractTranslationSet GetTranslationSet(string translationLanguage)
    3.18 +		{
    3.19 +			AbstractTranslationSet translations = null;
    3.20 +			
    3.21 +			if (translationLanguage != null)
    3.22 +			{
    3.23 +				translations = DictionaryUtils.GetValue(langToTranslationMap, translationLanguage);
    3.24 +				
    3.25 +				if (translations == null)
    3.26 +				{
    3.27 +					translations = new ModifiableTranslationSet(translationLanguage);
    3.28 +				}
    3.29 +			}
    3.30 +			
    3.31 +			return translations;
    3.32 +		}
    3.33  	}
    3.34  }