Mercurial > repos > IBBoard
annotate Lang/Translation.cs @ 79:a70d89de1435
Re #32: Add staged loading of translations
* Add "extends" attribute to schema so that translation files can define what they extend
* Add "get parent language" method to extractor
* Move loader to using XML translation sets
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Fri, 09 Apr 2010 19:48:51 +0000 |
parents | da339d10c5fe |
children | 09f71d10c249 |
rev | line source |
---|---|
16 | 1 // This file (Translation.cs) is a part of the IBBoard library and is copyright 2009 IBBoard. |
2 // | |
3 // The file and the library/program it is in are licensed under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license. | |
4 | |
37 | 5 using System; |
6 using System.IO; | |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
7 using System.Xml; |
37 | 8 using System.Xml.Schema; |
9 using System.Collections.Generic; | |
10 using System.Reflection; | |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
11 using System.ComponentModel; |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
12 using IBBoard.IO; |
37 | 13 using IBBoard.Logging; |
14 using IBBoard.Xml; | |
15 | |
16 namespace IBBoard.Lang | |
17 { | |
18 /// <summary> | |
71
40c09e57d213
Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
19 /// A basic string translator that a specified language and returns translated strings that correspond to translation IDs. |
40c09e57d213
Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
20 /// If the string doesn't exist in the specified language then the translator falls back defined 'super' languages. |
40c09e57d213
Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
21 /// If the translation doesn't exist in the hierarchy of languages then either a supplied value, null or a "no validation available" |
40c09e57d213
Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
22 /// message is returned, depending on the parameters supplied to the method. |
40c09e57d213
Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
23 /// |
40c09e57d213
Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
24 /// Loaded languages are referenced by two-character language code (e.g. "en" or "it") |
37 | 25 /// </summary> |
26 public class Translation | |
71
40c09e57d213
Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
27 { |
37 | 28 private static readonly string DIVIDER_STRING = "-"; |
73
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
29 private static AbstractTranslationSet currentTranslations; |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
30 private static Dictionary<string, AbstractTranslationSet> langToTranslationMap; |
74
726731c78414
Re #37: Add event to indicate change of translation
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
31 public static event MethodInvoker TranslationChanged; |
73
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
32 |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
33 static Translation() |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
34 { |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
35 langToTranslationMap = new Dictionary<string, AbstractTranslationSet>(); |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
36 } |
6 | 37 |
38 /// <summary> | |
71
40c09e57d213
Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
39 /// Initialises the translations and loads the specified language so that the Translation class can be used. |
21
c8d74202182a
Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents:
16
diff
changeset
|
40 /// Throws a TranslationLoadException if a problem occurred while loading translations. If this occurs then the translation methods can |
c8d74202182a
Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents:
16
diff
changeset
|
41 /// still be called but no translation will be performed. |
6 | 42 /// </summary> |
43 /// <param name="appPath"> | |
71
40c09e57d213
Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
44 /// The full path that the contains the "translations" folder - normally the application directory path. |
6 | 45 /// </param> |
46 /// <param name="language"> | |
71
40c09e57d213
Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
47 /// The language to use as the loaded language |
37 | 48 /// </param> |
49 public static void InitialiseTranslations(string appPath, string language) | |
6 | 50 { |
71
40c09e57d213
Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
51 InitialiseTranslations(appPath); |
40c09e57d213
Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
52 LoadTranslationForLanguage(language); |
40c09e57d213
Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
53 } |
40c09e57d213
Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
54 |
40c09e57d213
Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
55 /// <summary> |
40c09e57d213
Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
56 /// Initialises the translation class for an application or source. |
40c09e57d213
Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
57 /// </summary> |
40c09e57d213
Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
58 /// <param name="appPath"> |
40c09e57d213
Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
59 /// The full path that the contains the "translations" folder - normally the application directory path. |
40c09e57d213
Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
60 /// </param> |
40c09e57d213
Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
61 public static void InitialiseTranslations(string appPath) |
40c09e57d213
Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
62 { |
6 | 63 InitialiseDefaults(appPath); |
64 } | |
65 | |
66 private static void InitialiseDefaults(string appPath) | |
67 { | |
73
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
68 Reset(); |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
69 LoadTranslations(appPath); |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
70 } |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
71 |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
72 private static void LoadTranslations(string appPath) |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
73 { |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
74 DirectoryInfo dir = new DirectoryInfo(Path.Combine(appPath, "translations")); |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
75 |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
76 if (!dir.Exists) |
6 | 77 { |
73
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
78 throw new TranslationLoadException("Translation path not found (" + dir.FullName + ")"); |
37 | 79 } |
73
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
80 |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
81 TranslationXmlLoader loader = new TranslationXmlLoader(Path.Combine(appPath, "schemas/translation.xsd")); |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
82 |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
83 foreach (FileInfo file in dir.GetFiles("*.translation")) |
37 | 84 { |
73
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
85 AbstractTranslationSet translations = loader.LoadTranslations(file.FullName); |
78
da339d10c5fe
Re #32: Add staged loading of translations
IBBoard <dev@ibboard.co.uk>
parents:
75
diff
changeset
|
86 AddTranslationSet(translations); |
6 | 87 } |
88 } | |
89 | |
22
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
90 /// <summary> |
78
da339d10c5fe
Re #32: Add staged loading of translations
IBBoard <dev@ibboard.co.uk>
parents:
75
diff
changeset
|
91 /// Adds the supplied translation set to the list of available translations |
da339d10c5fe
Re #32: Add staged loading of translations
IBBoard <dev@ibboard.co.uk>
parents:
75
diff
changeset
|
92 /// </summary> |
da339d10c5fe
Re #32: Add staged loading of translations
IBBoard <dev@ibboard.co.uk>
parents:
75
diff
changeset
|
93 /// <param name="translations"> |
da339d10c5fe
Re #32: Add staged loading of translations
IBBoard <dev@ibboard.co.uk>
parents:
75
diff
changeset
|
94 /// The translation set to add |
da339d10c5fe
Re #32: Add staged loading of translations
IBBoard <dev@ibboard.co.uk>
parents:
75
diff
changeset
|
95 /// </param> |
da339d10c5fe
Re #32: Add staged loading of translations
IBBoard <dev@ibboard.co.uk>
parents:
75
diff
changeset
|
96 public static void AddTranslationSet(AbstractTranslationSet translations) |
da339d10c5fe
Re #32: Add staged loading of translations
IBBoard <dev@ibboard.co.uk>
parents:
75
diff
changeset
|
97 { |
da339d10c5fe
Re #32: Add staged loading of translations
IBBoard <dev@ibboard.co.uk>
parents:
75
diff
changeset
|
98 langToTranslationMap[translations.LanguageCode] = translations; |
da339d10c5fe
Re #32: Add staged loading of translations
IBBoard <dev@ibboard.co.uk>
parents:
75
diff
changeset
|
99 } |
da339d10c5fe
Re #32: Add staged loading of translations
IBBoard <dev@ibboard.co.uk>
parents:
75
diff
changeset
|
100 |
da339d10c5fe
Re #32: Add staged loading of translations
IBBoard <dev@ibboard.co.uk>
parents:
75
diff
changeset
|
101 /// <summary> |
71
40c09e57d213
Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
102 /// Resets the loaded translations and reverts to no translations. |
69
b5d7e8b93205
Re #30: Improve Translations API
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
103 /// </summary> |
b5d7e8b93205
Re #30: Improve Translations API
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
104 public static void Reset() |
b5d7e8b93205
Re #30: Improve Translations API
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
105 { |
74
726731c78414
Re #37: Add event to indicate change of translation
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
106 SetCurrentTranslations(null); |
73
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
107 langToTranslationMap.Clear(); |
69
b5d7e8b93205
Re #30: Improve Translations API
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
108 } |
74
726731c78414
Re #37: Add event to indicate change of translation
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
109 |
726731c78414
Re #37: Add event to indicate change of translation
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
110 private static void SetCurrentTranslations(AbstractTranslationSet newTranslations) |
726731c78414
Re #37: Add event to indicate change of translation
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
111 { |
726731c78414
Re #37: Add event to indicate change of translation
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
112 if (currentTranslations != newTranslations || (currentTranslations != null && !currentTranslations.Equals(newTranslations))) |
726731c78414
Re #37: Add event to indicate change of translation
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
113 { |
726731c78414
Re #37: Add event to indicate change of translation
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
114 currentTranslations = newTranslations; |
726731c78414
Re #37: Add event to indicate change of translation
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
115 |
726731c78414
Re #37: Add event to indicate change of translation
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
116 if (TranslationChanged != null) |
726731c78414
Re #37: Add event to indicate change of translation
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
117 { |
726731c78414
Re #37: Add event to indicate change of translation
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
118 TranslationChanged(); |
726731c78414
Re #37: Add event to indicate change of translation
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
119 } |
726731c78414
Re #37: Add event to indicate change of translation
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
120 } |
726731c78414
Re #37: Add event to indicate change of translation
IBBoard <dev@ibboard.co.uk>
parents:
73
diff
changeset
|
121 } |
6 | 122 |
123 /// <summary> | |
71
40c09e57d213
Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
124 /// Loads translations for a given language and sets them as the current language. |
40c09e57d213
Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
125 /// Throws a TranslationLoadException if a problem occurred while loading translations. If this occurs then the translation methods can |
21
c8d74202182a
Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents:
16
diff
changeset
|
126 /// still be called but all translations will fall back to the default translation. |
6 | 127 /// </summary> |
128 /// <param name="translationLang"> | |
129 /// The new local language to load | |
37 | 130 /// </param> |
131 public static void LoadTranslation(string translationLanguage) | |
73
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
132 { |
7
f4da31cb09d9
* Add translation DTD to utils project
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
133 if (translationLanguage == "" || translationLanguage == null) |
6 | 134 { |
10
3b7a321e7c4c
Fixes #4 - unexpected exception in translations
IBBoard <dev@ibboard.co.uk>
parents:
9
diff
changeset
|
135 throw new ArgumentException("Translation language cannot be null or empty"); |
37 | 136 } |
137 | |
138 LoadTranslationForLanguage(translationLanguage); | |
7
f4da31cb09d9
* Add translation DTD to utils project
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
139 } |
f4da31cb09d9
* Add translation DTD to utils project
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
140 |
f4da31cb09d9
* Add translation DTD to utils project
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
141 private static void LoadTranslationForLanguage(string translationLanguage) |
67
5fb2e5a2e7a8
* Remove errors on uninitialised translations - means that the translations just fall back to the provided string if they haven't been initialised
IBBoard <dev@ibboard.co.uk>
parents:
65
diff
changeset
|
142 { |
75
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
74
diff
changeset
|
143 SetCurrentTranslations(GetTranslationSet(translationLanguage)); |
37 | 144 } |
6 | 145 |
146 /// <summary> | |
147 /// Gets a translation for a given ID, falling back to a "missing translation" message if none can be found. Also optionally replaces any placeholders with the supplied values. | |
148 /// </summary> | |
149 /// <param name="translationID"> | |
150 /// The ID to look up the translation for | |
151 /// </param> | |
152 /// <param name="replacements"> | |
153 /// A collection of <see cref="System.Object"/>s to replace placeholders with | |
154 /// </param> | |
155 /// <returns> | |
156 /// The translation with the placeholders replaced or a "missing translation" message | |
37 | 157 /// </returns> |
158 public static string GetTranslation(string translationID, params object[] replacements) | |
159 { | |
160 return GetTranslation(translationID, false, replacements); | |
161 } | |
6 | 162 |
163 /// <summary> | |
164 /// Gets a translation for a given ID, falling back to null or a warning message if a translation cannot be found. Also optionally replaces any placeholders with the supplied values. | |
165 /// </summary> | |
166 /// <param name="translationID"> | |
167 /// The ID to look up the translation for | |
168 /// </param> | |
169 /// <param name="returnNullOnFail"> | |
170 /// TRUE if null should be returned when no translation can be found, or FALSE if a "missing translation" message should be returned | |
171 /// </param> | |
172 /// <param name="replacements"> | |
173 /// A collection of <see cref="System.Object"/>s to replace placeholders with | |
174 /// </param> | |
175 /// <returns> | |
176 /// The translation with the placeholders replaced, or a "missing translation" message or null depending on <param name="returnNullOnFail"> | |
37 | 177 /// </returns> |
178 public static string GetTranslation(string translationID, bool returnNullOnFail, params object[] replacements) | |
179 { | |
180 return GetTranslation(translationID, returnNullOnFail ? null : "", replacements); | |
181 } | |
6 | 182 |
183 /// <summary> | |
184 /// Gets a translation for a given ID, falling back to a supplied default if a translation cannot be found. Also optionally replaces any placeholders with the supplied values. | |
185 /// </summary> | |
186 /// <param name="translationID"> | |
187 /// The ID to look up the translation for | |
188 /// </param> | |
189 /// <param name="defaultTranslation"> | |
190 /// The string to return if no translation can be found. Can be null or any string. | |
191 /// </param> | |
192 /// <param name="replacements"> | |
193 /// A collection of <see cref="System.Object"/>s to replace placeholders with | |
194 /// </param> | |
195 /// <returns> | |
196 /// The translation, if one exists, or the supplied default with the placeholders replaced | |
37 | 197 /// </returns> |
198 public static string GetTranslation(string translationID, string defaultTranslation, params object[] replacements) | |
199 { | |
71
40c09e57d213
Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
200 string trans = GetTranslationFromTranslationSet(translationID); |
37 | 201 |
202 if (trans == null) | |
203 { | |
204 trans = GetDefaultTranslation(translationID, defaultTranslation); | |
205 } | |
206 | |
207 trans = AddVariablesToTranslation(trans, replacements); | |
208 | |
209 return trans; | |
6 | 210 } |
211 | |
71
40c09e57d213
Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
212 private static string GetTranslationFromTranslationSet(string translationID) |
6 | 213 { |
214 string translation = null; | |
215 | |
73
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
216 if (currentTranslations!=null) |
37 | 217 { |
73
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
218 translation = currentTranslations[translationID]; |
6 | 219 } |
220 | |
221 return translation; | |
222 } | |
223 | |
224 private static string GetDefaultTranslation(string translationID, string defaultTranslation) | |
225 { | |
69
b5d7e8b93205
Re #30: Improve Translations API
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
226 return (defaultTranslation != "" || defaultTranslation == null) ? defaultTranslation : GetMissingTranslationMessage(translationID); |
37 | 227 } |
6 | 228 |
229 private static string GetMissingTranslationMessage(string translationID) | |
230 { | |
231 return "++ Missing Translation "+translationID+" ++"; | |
232 } | |
233 | |
234 private static string AddVariablesToTranslation(string translation, object[] replacements) | |
235 { | |
37 | 236 if (translation != null && replacements != null && replacements.Length > 0) |
237 { | |
238 translation = String.Format(translation, replacements); | |
6 | 239 } |
240 | |
241 return translation; | |
242 } | |
243 | |
244 /// <summary> | |
36
c949727ec0e0
Re #22 - Make failing control translation cleaner for normal use
IBBoard <dev@ibboard.co.uk>
parents:
35
diff
changeset
|
245 /// Translate an <see cref="ITranslatable"/> item, with optional string replacement. If the translation |
c949727ec0e0
Re #22 - Make failing control translation cleaner for normal use
IBBoard <dev@ibboard.co.uk>
parents:
35
diff
changeset
|
246 /// does not exist then a warning message will be used as the translated text. |
6 | 247 /// </summary> |
248 /// <param name="item"> | |
249 /// A <see cref="ITranslatable"/> to set the text for | |
250 /// </param> | |
251 /// <param name="replacements"> | |
252 /// A collection of <see cref="System.Object"/>s that will be used to fill place-holders | |
37 | 253 /// </param> |
254 public static void Translate(ITranslatable item, params object[] replacements) | |
255 { | |
69
b5d7e8b93205
Re #30: Improve Translations API
IBBoard <dev@ibboard.co.uk>
parents:
67
diff
changeset
|
256 Translate(item, GetMissingTranslationMessage(item.Name), replacements); |
37 | 257 } |
258 | |
259 /// <summary> | |
260 /// Translate an <see cref="ITranslatable"/> item, with optional string replacement. The <code>defaultText</code> | |
261 /// can be used to specify an alternate translation. Passing <code>null</code> will result in a warning message | |
262 /// about a missing translation ID. | |
263 /// </summary> | |
264 /// <param name="item"> | |
265 /// A <see cref="ITranslatable"/> to set the text for | |
266 /// </param> | |
267 /// <param name="defaultText"> | |
268 /// The default string to display if no translation could be found. | |
269 /// </param> | |
270 /// <param name="replacements"> | |
271 /// A collection of <see cref="System.Object"/>s that will be used to fill place-holders | |
272 /// </param> | |
273 public static void Translate(ITranslatable item, string defaultText, params object[] replacements) | |
274 { | |
275 if (item.Text == "" || item.Text == DIVIDER_STRING) | |
276 { | |
277 //it doesn't need translating - either there is no text from the developer or it's a hyphen for a divider | |
278 return; | |
279 } | |
280 | |
281 item.Text = GetTranslation(item.Name, defaultText, replacements); | |
6 | 282 } |
283 | |
284 /// <summary> | |
285 /// Get the current local translation language. This is an arbitrary string used in the translation file's name and will not necessarily match the ISO language code. | |
286 /// </summary> | |
287 /// <returns> | |
288 /// The string used in the file name of the current local translation | |
289 /// </returns> | |
290 public static string GetTranslationLanguage() | |
291 { | |
73
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
292 return (currentTranslations != null ? currentTranslations.LanguageCode : ""); |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
293 } |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
294 |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
295 public static ICollection<TranslationLanguage> GetLanguages() |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
296 { |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
297 ICollection<TranslationLanguage> langs = new List<TranslationLanguage>(); |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
298 |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
299 foreach (AbstractTranslationSet translations in langToTranslationMap.Values) |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
300 { |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
301 langs.Add(translations.Language); |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
302 } |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
303 |
091bfa54d6c7
Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents:
71
diff
changeset
|
304 return langs; |
37 | 305 } |
75
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
74
diff
changeset
|
306 |
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
74
diff
changeset
|
307 public static AbstractTranslationSet GetTranslationSet(string translationLanguage) |
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
74
diff
changeset
|
308 { |
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
74
diff
changeset
|
309 AbstractTranslationSet translations = null; |
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
74
diff
changeset
|
310 |
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
74
diff
changeset
|
311 if (translationLanguage != null) |
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
74
diff
changeset
|
312 { |
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
74
diff
changeset
|
313 translations = DictionaryUtils.GetValue(langToTranslationMap, translationLanguage); |
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
74
diff
changeset
|
314 |
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
74
diff
changeset
|
315 if (translations == null) |
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
74
diff
changeset
|
316 { |
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
74
diff
changeset
|
317 translations = new ModifiableTranslationSet(translationLanguage); |
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
74
diff
changeset
|
318 } |
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
74
diff
changeset
|
319 } |
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
74
diff
changeset
|
320 |
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
74
diff
changeset
|
321 return translations; |
b1ae6fce2e3f
Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents:
74
diff
changeset
|
322 } |
37 | 323 } |
324 } |