Mercurial > repos > IBBoard
annotate Lang/AbstractTranslationSet.cs @ 83:13f0ffb012cb
Re warfoundry:#272 : Program errors on exit if language file fails to load.
* Make ID of translation required
* Add method to check if language exists
no-open-ticket
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sat, 15 May 2010 14:41:41 +0000 |
parents | da339d10c5fe |
children |
rev | line source |
---|---|
69 | 1 // This file (AbstractTranslationSet.cs) is a part of the IBBoard project and is copyright 2010 IBBoard |
2 // | |
78
da339d10c5fe
Re #32: Add staged loading of translations
IBBoard <dev@ibboard.co.uk>
parents:
77
diff
changeset
|
3 // The file and the library/program it is in are licensed and distributed, without warranty, under the GNU LGPL, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license. |
69 | 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> | |
78
da339d10c5fe
Re #32: Add staged loading of translations
IBBoard <dev@ibboard.co.uk>
parents:
77
diff
changeset
|
40 public virtual string this[string key] |
69 | 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 { |
77
eb47e17ec824
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
76
diff
changeset
|
62 CheckForLooping(parentTranslations); |
75
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
63 parentTranslation = parentTranslations[key]; |
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 |
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
66 return parentTranslation; |
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 |
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
69 protected abstract AbstractTranslationSet GetParentTranslations(); |
72
cec6c4c0892d
Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
70 |
77
eb47e17ec824
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
76
diff
changeset
|
71 private void CheckForLooping(AbstractTranslationSet translations) |
eb47e17ec824
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
76
diff
changeset
|
72 { |
eb47e17ec824
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
76
diff
changeset
|
73 bool hasLoop = false; |
eb47e17ec824
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
76
diff
changeset
|
74 AbstractTranslationSet slowLoop = translations; |
eb47e17ec824
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
76
diff
changeset
|
75 AbstractTranslationSet fastLoop1 = translations; |
eb47e17ec824
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
76
diff
changeset
|
76 AbstractTranslationSet fastLoop2 = translations; |
eb47e17ec824
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
76
diff
changeset
|
77 |
eb47e17ec824
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
76
diff
changeset
|
78 while (slowLoop != null && (fastLoop1 = fastLoop2.GetParentTranslations()) != null && (fastLoop2 = fastLoop1.GetParentTranslations()) != null) |
eb47e17ec824
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
76
diff
changeset
|
79 { |
eb47e17ec824
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
76
diff
changeset
|
80 if (slowLoop.Equals(fastLoop1) || slowLoop.Equals(fastLoop2)) |
eb47e17ec824
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
76
diff
changeset
|
81 { |
eb47e17ec824
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
76
diff
changeset
|
82 hasLoop = true; |
eb47e17ec824
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
76
diff
changeset
|
83 break; |
eb47e17ec824
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
76
diff
changeset
|
84 } |
eb47e17ec824
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
76
diff
changeset
|
85 |
eb47e17ec824
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
76
diff
changeset
|
86 slowLoop = slowLoop.GetParentTranslations(); |
eb47e17ec824
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
76
diff
changeset
|
87 } |
eb47e17ec824
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
76
diff
changeset
|
88 |
eb47e17ec824
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
76
diff
changeset
|
89 if (hasLoop) |
eb47e17ec824
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
76
diff
changeset
|
90 { |
eb47e17ec824
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
76
diff
changeset
|
91 throw new TranslationLoadException("Translations contained an inheritence loop"); |
eb47e17ec824
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
76
diff
changeset
|
92 } |
eb47e17ec824
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
76
diff
changeset
|
93 } |
eb47e17ec824
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
76
diff
changeset
|
94 |
72
cec6c4c0892d
Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
95 public string LanguageName |
cec6c4c0892d
Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
96 { |
cec6c4c0892d
Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
97 get |
cec6c4c0892d
Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents:
69
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 return language.Name; |
72
cec6c4c0892d
Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
100 } |
cec6c4c0892d
Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
101 } |
73
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 public TranslationLanguage Language |
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 get { return language; } |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
72
diff
changeset
|
106 } |
72
cec6c4c0892d
Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
107 |
75
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
108 public TranslationLanguage ParentLanguage |
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
109 { |
76
f45d28dc1d6a
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
75
diff
changeset
|
110 get |
f45d28dc1d6a
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
75
diff
changeset
|
111 { |
f45d28dc1d6a
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
75
diff
changeset
|
112 TranslationLanguage parentLang = null; |
f45d28dc1d6a
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
75
diff
changeset
|
113 AbstractTranslationSet parentTranslations = GetParentTranslations(); |
f45d28dc1d6a
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
75
diff
changeset
|
114 |
f45d28dc1d6a
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
75
diff
changeset
|
115 if (parentTranslations != null) |
f45d28dc1d6a
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
75
diff
changeset
|
116 { |
f45d28dc1d6a
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
75
diff
changeset
|
117 parentLang = parentTranslations.Language; |
f45d28dc1d6a
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
75
diff
changeset
|
118 } |
f45d28dc1d6a
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
75
diff
changeset
|
119 |
f45d28dc1d6a
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
75
diff
changeset
|
120 return parentLang; |
f45d28dc1d6a
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
75
diff
changeset
|
121 } |
75
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
122 } |
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
123 |
73
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
72
diff
changeset
|
124 public override bool Equals(object obj) |
72
cec6c4c0892d
Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
125 { |
73
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
72
diff
changeset
|
126 bool equal = true; |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
72
diff
changeset
|
127 |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
72
diff
changeset
|
128 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
|
129 { |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
72
diff
changeset
|
130 equal = false; |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
72
diff
changeset
|
131 } |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
72
diff
changeset
|
132 else |
72
cec6c4c0892d
Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
133 { |
73
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
72
diff
changeset
|
134 equal = LanguageCode.Equals(((AbstractTranslationSet)obj).LanguageCode); |
72
cec6c4c0892d
Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
135 } |
73
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
72
diff
changeset
|
136 |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
72
diff
changeset
|
137 return equal; |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
72
diff
changeset
|
138 } |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
72
diff
changeset
|
139 |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
72
diff
changeset
|
140 public override int GetHashCode () |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
72
diff
changeset
|
141 { |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
72
diff
changeset
|
142 return GetType().GetHashCode() + LanguageCode.GetHashCode(); |
72
cec6c4c0892d
Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
143 } |
69 | 144 } |
145 } |