Mercurial > repos > IBBoard
annotate Lang/Translation.cs @ 15:043a1d8101f2
* Rename "Convert" method to "ToArray" now that the class is more generic
no-open-ticket
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sun, 25 Jan 2009 13:25:23 +0000 |
parents | 3b7a321e7c4c |
children | 0352fa33ee8f |
rev | line source |
---|---|
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
1 using System; |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
2 using System.IO; |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
3 using System.Xml; |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
4 using System.Xml.Schema; |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
5 using System.Collections.Generic; |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
6 using System.Reflection; |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
7 using System.ComponentModel; |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
8 using IBBoard.IO; |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
9 using IBBoard.Logging; |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
10 using IBBoard.Xml; |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
11 |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
12 namespace IBBoard.Lang |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
13 { |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
14 /// <summary> |
6 | 15 /// A basic string translator that loads a default language and a specified language and returns translated strings that correspond to translation IDs. |
16 /// If the string doesn't exist in the specified language then the translator falls back to the default language. If the translation doesn't exist in the default language | |
17 /// then either a supplied value or a "no validation available" message is returned. | |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
18 /// </summary> |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
19 public class Translation |
6 | 20 { |
21 private static readonly string DEFAULT_LANGUAGE = "en"; | |
22 private static readonly string DIVIDER_STRING = "-"; | |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
23 private static string lang = ""; |
6 | 24 private static DirectoryInfo translationDir; |
25 private static Dictionary<string, string> translationsLocal; | |
26 private static Dictionary<string, string> translationsDefault; | |
27 | |
28 /// <summary> | |
29 /// Initialises the translations for the language specified and the default translations so that the Translation class can be used | |
30 /// </summary> | |
31 /// <param name="appPath"> | |
32 /// The full path that the application is running from. Must contain the "translations" folder. | |
33 /// </param> | |
34 /// <param name="language"> | |
35 /// The language to use as the load language | |
36 /// </param> | |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
37 public static void InitialiseTranslations(string appPath, string language) |
6 | 38 { |
39 InitialiseDefaults(appPath); | |
40 FileInfo file = GetTranslationFile(DEFAULT_LANGUAGE); | |
41 XmlDocument doc = LoadTranslationDocument(file); | |
42 LoadTranslationsFromDocument(doc, translationsDefault); | |
10
3b7a321e7c4c
Fixes #4 - unexpected exception in translations
IBBoard <dev@ibboard.co.uk>
parents:
9
diff
changeset
|
43 LoadTranslationForLanguage(language); |
6 | 44 } |
45 | |
46 private static void InitialiseDefaults(string appPath) | |
47 { | |
48 string translationPath = appPath.TrimEnd(Constants.DirectoryChar) + Constants.DirectoryString + "translations"; | |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
49 |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
50 if (Directory.Exists(translationPath)) |
6 | 51 { |
52 translationsDefault = new Dictionary<string,string>(); | |
53 translationsLocal = new Dictionary<string,string>(); | |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
54 translationDir = new DirectoryInfo(translationPath); |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
55 } |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
56 else |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
57 { |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
58 throw new ArgumentException("Translation path must exist ("+translationPath+")"); |
6 | 59 } |
60 } | |
61 | |
62 private static XmlDocument LoadTranslationDocument(FileInfo file) | |
63 { | |
64 XmlDocument doc = new XmlDocument(); | |
65 XmlReaderSettings settings = new XmlReaderSettings(); | |
66 settings.XmlResolver = new IBBXmlResolver(translationDir.Parent.FullName); | |
67 settings.ValidationType = ValidationType.DTD; | |
68 settings.ProhibitDtd = false; | |
69 settings.ValidationEventHandler+= new ValidationEventHandler(ValidationEventMethod); | |
70 XmlReader valReader = XmlReader.Create(file.FullName, settings); | |
71 doc.Load(valReader); | |
72 valReader.Close(); | |
73 return doc; | |
74 } | |
75 | |
76 private static FileInfo GetTranslationFile(string language) | |
77 { | |
78 FileInfo file = new FileInfo(translationDir.FullName + Constants.DirectoryString + language + ".translation"); | |
79 | |
80 if (!file.Exists) | |
81 { | |
82 throw new FileNotFoundException(language + "translation could not be found in "+translationDir.FullName); | |
83 } | |
84 | |
85 return file; | |
86 } | |
87 | |
88 private static void LoadTranslationsFromDocument(XmlDocument doc, Dictionary<string, string> translationTable) | |
89 { | |
90 try | |
91 { | |
92 XmlNodeList translations = doc.GetElementsByTagName("translation"); | |
93 | |
94 if (translations.Count==0) | |
95 { | |
96 throw new InvalidFileException("No translations found in "+GetLanguageOfDocument(doc)+".translation"); | |
97 } | |
98 | |
99 Dictionary<string, string> tempTranslationTable = new Dictionary<string,string>(); | |
100 | |
101 foreach (XmlNode node in translations) | |
102 { | |
103 tempTranslationTable.Add(node.Attributes["id"].Value, node.InnerText); | |
104 } | |
105 | |
106 translationTable.Clear(); | |
107 | |
108 foreach (string key in tempTranslationTable.Keys) | |
109 { | |
110 string translation; | |
111 tempTranslationTable.TryGetValue(key, out translation); | |
112 translationTable.Add(key, translation); | |
113 } | |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
114 } |
6 | 115 catch(Exception ex) |
116 { | |
117 throw new XmlParseException("Error while parsing " + GetLanguageOfDocument(doc)+" translation: "+ex.Message); | |
118 } | |
119 } | |
120 | |
121 private static string GetLanguageOfDocument(XmlDocument doc) | |
122 { | |
123 return doc != null ? doc.DocumentElement.GetAttribute("lang") : ""; | |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
124 } |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
125 |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
126 private static void ValidationEventMethod(object sender, ValidationEventArgs e) |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
127 { |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
128 //TODO: Fire a validation failure event |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
129 LogNotifier.Error(typeof(Translation), "Validation Error", e.Exception); |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
130 } |
6 | 131 |
132 /// <summary> | |
133 /// Loads translations for a given language and sets them as the local language | |
134 /// </summary> | |
135 /// <param name="translationLang"> | |
136 /// The new local language to load | |
137 /// </param> | |
7
f4da31cb09d9
* Add translation DTD to utils project
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
138 public static void LoadTranslation(string translationLanguage) |
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 if (translationLanguage == "" || translationLanguage == null) |
6 | 141 { |
10
3b7a321e7c4c
Fixes #4 - unexpected exception in translations
IBBoard <dev@ibboard.co.uk>
parents:
9
diff
changeset
|
142 throw new ArgumentException("Translation language cannot be null or empty"); |
6 | 143 } |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
144 |
8
d6ce8764e92b
Fix typo in method name reference
IBBoard <dev@ibboard.co.uk>
parents:
7
diff
changeset
|
145 LoadTranslationForLanguage(translationLanguage); |
7
f4da31cb09d9
* Add translation DTD to utils project
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
146 } |
f4da31cb09d9
* Add translation DTD to utils project
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
147 |
f4da31cb09d9
* Add translation DTD to utils project
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
148 private static void LoadTranslationForLanguage(string translationLanguage) |
f4da31cb09d9
* Add translation DTD to utils project
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
149 { |
f4da31cb09d9
* Add translation DTD to utils project
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
150 checkInitialisation(); |
f4da31cb09d9
* Add translation DTD to utils project
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
151 |
f4da31cb09d9
* Add translation DTD to utils project
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
152 if (translationLanguage != DEFAULT_LANGUAGE && translationLanguage != "" && translationLanguage != null) |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
153 { |
9 | 154 FileInfo file = GetTranslationFile(translationLanguage); |
6 | 155 XmlDocument doc = LoadTranslationDocument(file); |
156 LoadTranslationsFromDocument(doc, translationsLocal); | |
157 } | |
158 else | |
159 { | |
160 translationsLocal.Clear(); | |
161 } | |
162 | |
9 | 163 lang = translationLanguage; |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
164 } |
6 | 165 |
166 /// <summary> | |
167 /// 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. | |
168 /// </summary> | |
169 /// <param name="translationID"> | |
170 /// The ID to look up the translation for | |
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 | |
177 /// </returns> | |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
178 public static string GetTranslation(string translationID, params object[] replacements) |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
179 { |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
180 return GetTranslation(translationID, false, replacements); |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
181 } |
6 | 182 |
183 /// <summary> | |
184 /// 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. | |
185 /// </summary> | |
186 /// <param name="translationID"> | |
187 /// The ID to look up the translation for | |
188 /// </param> | |
189 /// <param name="returnNullOnFail"> | |
190 /// TRUE if null should be returned when no translation can be found, or FALSE if a "missing translation" message should be returned | |
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 with the placeholders replaced, or a "missing translation" message or null depending on <param name="returnNullOnFail"> | |
197 /// </returns> | |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
198 public static string GetTranslation(string translationID, bool returnNullOnFail, params object[] replacements) |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
199 { |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
200 return GetTranslation(translationID, returnNullOnFail ? null : "", replacements); |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
201 } |
6 | 202 |
203 /// <summary> | |
204 /// 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. | |
205 /// </summary> | |
206 /// <param name="translationID"> | |
207 /// The ID to look up the translation for | |
208 /// </param> | |
209 /// <param name="defaultTranslation"> | |
210 /// The string to return if no translation can be found. Can be null or any string. | |
211 /// </param> | |
212 /// <param name="replacements"> | |
213 /// A collection of <see cref="System.Object"/>s to replace placeholders with | |
214 /// </param> | |
215 /// <returns> | |
216 /// The translation, if one exists, or the supplied default with the placeholders replaced | |
217 /// </returns> | |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
218 public static string GetTranslation(string translationID, string defaultTranslation, params object[] replacements) |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
219 { |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
220 checkInitialisation(); |
6 | 221 string trans = GetTranslationFromTables(translationID); |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
222 |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
223 if (trans == null) |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
224 { |
6 | 225 trans = GetDefaultTranslation(translationID, defaultTranslation); |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
226 } |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
227 |
6 | 228 trans = AddVariablesToTranslation(trans, replacements); |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
229 |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
230 return trans; |
6 | 231 } |
232 | |
233 private static string GetTranslationFromTables(string translationID) | |
234 { | |
235 string translation = null; | |
236 | |
237 if (translationsLocal!=null) | |
238 { | |
239 translationsLocal.TryGetValue(translationID, out translation); | |
240 } | |
241 | |
242 if (translation == null) | |
243 { | |
244 translationsDefault.TryGetValue(translationID, out translation); | |
245 } | |
246 | |
247 return translation; | |
248 } | |
249 | |
250 private static string GetDefaultTranslation(string translationID, string defaultTranslation) | |
251 { | |
252 return (defaultTranslation != "" && defaultTranslation != null) ? defaultTranslation : GetMissingTranslationMessage(translationID); | |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
253 } |
6 | 254 |
255 private static string GetMissingTranslationMessage(string translationID) | |
256 { | |
257 return "++ Missing Translation "+translationID+" ++"; | |
258 } | |
259 | |
260 private static string AddVariablesToTranslation(string translation, object[] replacements) | |
261 { | |
262 if (translation != null && replacements != null && replacements.Length > 0) | |
263 { | |
264 translation = String.Format(translation, replacements); | |
265 } | |
266 | |
267 return translation; | |
268 } | |
269 | |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
270 private static void checkInitialisation() |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
271 { |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
272 if (translationDir==null) |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
273 { |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
274 throw new InvalidOperationException("Translation class has not been initialised"); |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
275 } |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
276 } |
6 | 277 |
278 /// <summary> | |
279 /// Translate an <see cref="ITranslatable"/> item, with optional string replacement | |
280 /// </summary> | |
281 /// <param name="item"> | |
282 /// A <see cref="ITranslatable"/> to set the text for | |
283 /// </param> | |
284 /// <param name="replacements"> | |
285 /// A collection of <see cref="System.Object"/>s that will be used to fill place-holders | |
286 /// </param> | |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
287 public static void Translate(ITranslatable item, params object[] replacements) |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
288 { |
6 | 289 if (item.Text == "" || item.Text == DIVIDER_STRING) |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
290 { |
6 | 291 //it doesn't need translating - either there is no text from the developer or it's a hyphen for a divider |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
292 return; |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
293 } |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
294 |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
295 item.Text = GetTranslation(item.Name, replacements); |
6 | 296 } |
297 | |
298 /// <summary> | |
299 /// 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. | |
300 /// </summary> | |
301 /// <returns> | |
302 /// The string used in the file name of the current local translation | |
303 /// </returns> | |
304 public static string GetTranslationLanguage() | |
305 { | |
306 return lang; | |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
307 } |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
308 } |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
309 } |