Mercurial > repos > IBBoard
annotate Lang/AbstractTranslationSet.cs @ 76:f45d28dc1d6a
Re #35: Add multi-level cascading of translations
* Restructure translations so that implementations define how parent translations are set
TODO: Stop looping definition of parent (en-GB inherits from en inherits from en-GB, or more indirect)
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Wed, 07 Apr 2010 19:55:05 +0000 |
parents | b1ae6fce2e3f |
children | eb47e17ec824 |
rev | line source |
---|---|
69 | 1 // This file (AbstractTranslationSet.cs) is a part of the IBBoard project and is copyright 2010 IBBoard |
2 // | |
3 // 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. | |
4 | |
5 using System; | |
6 using System.Collections.Generic; | |
7 | |
8 namespace IBBoard.Lang | |
9 { | |
10 /// <summary> | |
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. | |
13 /// </summary> | |
75
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
14 public abstract class AbstractTranslationSet |
69 | 15 { |
73
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
72
diff
changeset
|
16 private TranslationLanguage language; |
69 | 17 protected Dictionary<string, string> translations; |
18 | |
76
f45d28dc1d6a
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
75
diff
changeset
|
19 public AbstractTranslationSet(string languageCode) |
69 | 20 { |
73
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
72
diff
changeset
|
21 language = new TranslationLanguage(languageCode); |
69 | 22 translations = new Dictionary<string, string>(); |
23 } | |
24 | |
75
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
25 |
69 | 26 /// <summary> |
27 /// Gets the language code that this translation claims to be for | |
28 /// </summary> | |
29 public string LanguageCode | |
30 { | |
73
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
72
diff
changeset
|
31 get { return language.Code; } |
69 | 32 } |
33 | |
34 /// <summary> | |
35 /// Gets a translation from the translation set, or <code>null</code> if the translation doesn't exist. | |
36 /// </summary> | |
37 /// <param name="key"> | |
38 /// The key (ID) of the translation to retrieve | |
39 /// </param> | |
40 public string this[string key] | |
41 { | |
42 get | |
43 { | |
75
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
44 string translation = DictionaryUtils.GetValue(translations, key); |
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
45 |
76
f45d28dc1d6a
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
75
diff
changeset
|
46 if (translation == null) |
75
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
47 { |
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
48 translation = GetParentTranslation(key); |
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
49 } |
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
50 |
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
51 return translation; |
69 | 52 } |
72
cec6c4c0892d
Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
53 } |
75
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
54 |
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
55 private string GetParentTranslation(string key) |
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
56 { |
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
57 AbstractTranslationSet parentTranslations = GetParentTranslations(); |
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
58 string parentTranslation = null; |
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
59 |
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
60 if (parentTranslations != null) |
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
61 { |
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
62 parentTranslation = parentTranslations[key]; |
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
63 } |
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
64 |
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
65 return parentTranslation; |
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
66 } |
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
67 |
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
68 protected abstract AbstractTranslationSet GetParentTranslations(); |
72
cec6c4c0892d
Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
69 |
cec6c4c0892d
Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
70 public string LanguageName |
cec6c4c0892d
Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
71 { |
cec6c4c0892d
Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
72 get |
cec6c4c0892d
Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
73 { |
73
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
72
diff
changeset
|
74 return language.Name; |
72
cec6c4c0892d
Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
75 } |
cec6c4c0892d
Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
76 } |
73
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
72
diff
changeset
|
77 |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
72
diff
changeset
|
78 public TranslationLanguage Language |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
72
diff
changeset
|
79 { |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
72
diff
changeset
|
80 get { return language; } |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
72
diff
changeset
|
81 } |
72
cec6c4c0892d
Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
82 |
75
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
83 public TranslationLanguage ParentLanguage |
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
84 { |
76
f45d28dc1d6a
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
75
diff
changeset
|
85 get |
f45d28dc1d6a
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
75
diff
changeset
|
86 { |
f45d28dc1d6a
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
75
diff
changeset
|
87 TranslationLanguage parentLang = null; |
f45d28dc1d6a
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
75
diff
changeset
|
88 AbstractTranslationSet parentTranslations = GetParentTranslations(); |
f45d28dc1d6a
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
75
diff
changeset
|
89 |
f45d28dc1d6a
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
75
diff
changeset
|
90 if (parentTranslations != null) |
f45d28dc1d6a
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
75
diff
changeset
|
91 { |
f45d28dc1d6a
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
75
diff
changeset
|
92 parentLang = parentTranslations.Language; |
f45d28dc1d6a
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
75
diff
changeset
|
93 } |
f45d28dc1d6a
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
75
diff
changeset
|
94 |
f45d28dc1d6a
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
75
diff
changeset
|
95 return parentLang; |
f45d28dc1d6a
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
75
diff
changeset
|
96 } |
75
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
97 } |
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
98 |
73
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
72
diff
changeset
|
99 public override bool Equals(object obj) |
72
cec6c4c0892d
Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
100 { |
73
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
72
diff
changeset
|
101 bool equal = true; |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
72
diff
changeset
|
102 |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
72
diff
changeset
|
103 if (obj == null || !obj.GetType().Equals(GetType())) |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
72
diff
changeset
|
104 { |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
72
diff
changeset
|
105 equal = false; |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
72
diff
changeset
|
106 } |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
72
diff
changeset
|
107 else |
72
cec6c4c0892d
Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
108 { |
73
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
72
diff
changeset
|
109 equal = LanguageCode.Equals(((AbstractTranslationSet)obj).LanguageCode); |
72
cec6c4c0892d
Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
110 } |
73
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
72
diff
changeset
|
111 |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
72
diff
changeset
|
112 return equal; |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
72
diff
changeset
|
113 } |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
72
diff
changeset
|
114 |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
72
diff
changeset
|
115 public override int GetHashCode () |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
72
diff
changeset
|
116 { |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
72
diff
changeset
|
117 return GetType().GetHashCode() + LanguageCode.GetHashCode(); |
72
cec6c4c0892d
Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
118 } |
69 | 119 } |
120 } |