comparison 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
comparison
equal deleted inserted replaced
74:726731c78414 75:b1ae6fce2e3f
9 { 9 {
10 /// <summary> 10 /// <summary>
11 /// A collection of translations for a given language. The abstract class must be extended by implementations that 11 /// A collection of translations for a given language. The abstract class must be extended by implementations that
12 /// provide different ways of loading the data. 12 /// provide different ways of loading the data.
13 /// </summary> 13 /// </summary>
14 public class AbstractTranslationSet 14 public abstract class AbstractTranslationSet
15 { 15 {
16 private TranslationLanguage language; 16 private TranslationLanguage language;
17 protected Dictionary<string, string> translations; 17 protected Dictionary<string, string> translations;
18 private TranslationLanguage parentLang;
18 19
19 public AbstractTranslationSet(string languageCode) 20 public AbstractTranslationSet(string languageCode) : this(languageCode, "")
21 {
22 //Do nothing extra
23 }
24
25 public AbstractTranslationSet(string languageCode, string parentLanguageCode)
20 { 26 {
21 language = new TranslationLanguage(languageCode); 27 language = new TranslationLanguage(languageCode);
22 translations = new Dictionary<string, string>(); 28 translations = new Dictionary<string, string>();
29
30 if (parentLanguageCode != null && !parentLanguageCode.Trim().Equals(""))
31 {
32 ParentLanguage = new TranslationLanguage(parentLanguageCode);
33 }
34
23 } 35 }
36
24 37
25 /// <summary> 38 /// <summary>
26 /// Gets the language code that this translation claims to be for 39 /// Gets the language code that this translation claims to be for
27 /// </summary> 40 /// </summary>
28 public string LanguageCode 41 public string LanguageCode
38 /// </param> 51 /// </param>
39 public string this[string key] 52 public string this[string key]
40 { 53 {
41 get 54 get
42 { 55 {
43 return DictionaryUtils.GetValue(translations, key); 56 string translation = DictionaryUtils.GetValue(translations, key);
57
58 if (parentLang != null && translation == null)
59 {
60 translation = GetParentTranslation(key);
61 }
62
63 return translation;
44 } 64 }
45 } 65 }
66
67 private string GetParentTranslation(string key)
68 {
69 AbstractTranslationSet parentTranslations = GetParentTranslations();
70 string parentTranslation = null;
71
72 if (parentTranslations != null)
73 {
74 parentTranslation = parentTranslations[key];
75 }
76
77 return parentTranslation;
78 }
79
80 protected abstract AbstractTranslationSet GetParentTranslations();
46 81
47 public string LanguageName 82 public string LanguageName
48 { 83 {
49 get 84 get
50 { 85 {
53 } 88 }
54 89
55 public TranslationLanguage Language 90 public TranslationLanguage Language
56 { 91 {
57 get { return language; } 92 get { return language; }
93 }
94
95 public TranslationLanguage ParentLanguage
96 {
97 get { return parentLang; }
98 protected set { parentLang = value; }
58 } 99 }
59 100
60 public override bool Equals(object obj) 101 public override bool Equals(object obj)
61 { 102 {
62 bool equal = true; 103 bool equal = true;