Mercurial > repos > IBBoard
annotate Lang/Translation.cs @ 35:2b5e73cb83a3
Re #21 - Reduce specificity of translations file
* Add minOccurs="0" to XSD
* Remove check for zero translations returned
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Tue, 19 May 2009 19:35:34 +0000 |
parents | fb4fdab841db |
children | c949727ec0e0 |
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 | |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
5 using System; |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
6 using System.IO; |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
7 using System.Xml; |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
8 using System.Xml.Schema; |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
9 using System.Collections.Generic; |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
10 using System.Reflection; |
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; |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
13 using IBBoard.Logging; |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
14 using IBBoard.Xml; |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
15 |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
16 namespace IBBoard.Lang |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
17 { |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
18 /// <summary> |
6 | 19 /// A basic string translator that loads a default language and a specified language and returns translated strings that correspond to translation IDs. |
20 /// 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 | |
21 /// 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
|
22 /// </summary> |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
23 public class Translation |
6 | 24 { |
25 private static readonly string DEFAULT_LANGUAGE = "en"; | |
26 private static readonly string DIVIDER_STRING = "-"; | |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
27 private static string lang = ""; |
6 | 28 private static DirectoryInfo translationDir; |
29 private static Dictionary<string, string> translationsLocal; | |
22
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
30 private static Dictionary<string, string> translationsDefault; |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
31 private static XmlReaderSettings settings; |
6 | 32 |
33 /// <summary> | |
21
c8d74202182a
Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents:
16
diff
changeset
|
34 /// Initialises the translations for the language specified and the default translations so that the Translation class can be used. |
c8d74202182a
Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents:
16
diff
changeset
|
35 /// 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
|
36 /// still be called but no translation will be performed. |
6 | 37 /// </summary> |
38 /// <param name="appPath"> | |
39 /// The full path that the application is running from. Must contain the "translations" folder. | |
40 /// </param> | |
41 /// <param name="language"> | |
42 /// The language to use as the load language | |
43 /// </param> | |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
44 public static void InitialiseTranslations(string appPath, string language) |
6 | 45 { |
46 InitialiseDefaults(appPath); | |
47 FileInfo file = GetTranslationFile(DEFAULT_LANGUAGE); | |
48 XmlDocument doc = LoadTranslationDocument(file); | |
49 LoadTranslationsFromDocument(doc, translationsDefault); | |
10
3b7a321e7c4c
Fixes #4 - unexpected exception in translations
IBBoard <dev@ibboard.co.uk>
parents:
9
diff
changeset
|
50 LoadTranslationForLanguage(language); |
6 | 51 } |
52 | |
53 private static void InitialiseDefaults(string appPath) | |
54 { | |
55 string translationPath = appPath.TrimEnd(Constants.DirectoryChar) + Constants.DirectoryString + "translations"; | |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
56 |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
57 if (Directory.Exists(translationPath)) |
6 | 58 { |
59 translationsDefault = new Dictionary<string,string>(); | |
60 translationsLocal = new Dictionary<string,string>(); | |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
61 translationDir = new DirectoryInfo(translationPath); |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
62 } |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
63 else |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
64 { |
21
c8d74202182a
Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents:
16
diff
changeset
|
65 throw new TranslationLoadException("Translation path not found ("+translationPath+")"); |
6 | 66 } |
67 } | |
68 | |
69 private static XmlDocument LoadTranslationDocument(FileInfo file) | |
70 { | |
22
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
71 XmlDocument doc = new XmlDocument(); |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
72 XmlReader valReader = XmlReader.Create(file.FullName, GetReaderSettings()); |
21
c8d74202182a
Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents:
16
diff
changeset
|
73 |
c8d74202182a
Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents:
16
diff
changeset
|
74 try |
c8d74202182a
Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents:
16
diff
changeset
|
75 { |
c8d74202182a
Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents:
16
diff
changeset
|
76 doc.Load(valReader); |
c8d74202182a
Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents:
16
diff
changeset
|
77 } |
22
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
78 catch (DirectoryNotFoundException ex) |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
79 { |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
80 throw new TranslationLoadException("Problem validating schema for translation: " + ex.Message, ex); |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
81 } |
21
c8d74202182a
Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents:
16
diff
changeset
|
82 catch (XmlSchemaException ex) |
c8d74202182a
Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents:
16
diff
changeset
|
83 { |
c8d74202182a
Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents:
16
diff
changeset
|
84 throw new TranslationLoadException("Problem validating schema for translation: " + ex.Message, ex); |
c8d74202182a
Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents:
16
diff
changeset
|
85 } |
c8d74202182a
Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents:
16
diff
changeset
|
86 catch (XmlException ex) |
c8d74202182a
Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents:
16
diff
changeset
|
87 { |
c8d74202182a
Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents:
16
diff
changeset
|
88 throw new TranslationLoadException("Problem reading data for translation: " + ex.Message, ex); |
c8d74202182a
Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents:
16
diff
changeset
|
89 } |
c8d74202182a
Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents:
16
diff
changeset
|
90 finally |
c8d74202182a
Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents:
16
diff
changeset
|
91 { |
c8d74202182a
Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents:
16
diff
changeset
|
92 valReader.Close(); |
c8d74202182a
Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents:
16
diff
changeset
|
93 } |
c8d74202182a
Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents:
16
diff
changeset
|
94 |
6 | 95 return doc; |
96 } | |
97 | |
22
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
98 /// <summary> |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
99 /// Lazy-getter for XML reader settings. May throw a <see cref="TranslationLoadException"/> if there is a problem with the translation schema. |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
100 /// </summary> |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
101 /// <returns> |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
102 /// A <see cref="XmlReaderSettings"/> with the default values for validating the translation document against the translation schema |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
103 /// </returns> |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
104 private static XmlReaderSettings GetReaderSettings() |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
105 { |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
106 if (settings == null) |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
107 { |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
108 try |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
109 { |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
110 settings = new XmlReaderSettings(); |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
111 settings.XmlResolver = new IBBXmlResolver(translationDir.Parent.FullName); |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
112 settings.ValidationType = ValidationType.Schema; |
23
fb4fdab841db
* Ignore schema location attribute for translations
IBBoard <dev@ibboard.co.uk>
parents:
22
diff
changeset
|
113 settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings; |
22
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
114 settings.ValidationEventHandler+= new ValidationEventHandler(ValidationEventMethod); |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
115 XmlSchemaSet cache = new XmlSchemaSet(); |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
116 cache.Add("http://ibboard.co.uk/translation", translationDir.Parent.FullName + "/dtds/translation.xsd"); |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
117 settings.Schemas.Add(cache); |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
118 } |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
119 catch (DirectoryNotFoundException ex) |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
120 { |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
121 throw new TranslationLoadException("Problem validating schema for translation: " + ex.Message, ex); |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
122 } |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
123 catch (XmlSchemaException ex) |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
124 { |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
125 throw new TranslationLoadException("Problem validating schema for translation: " + ex.Message, ex); |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
126 } |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
127 catch (XmlException ex) |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
128 { |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
129 throw new TranslationLoadException("Problem reading data for schema: " + ex.Message, ex); |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
130 } |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
131 } |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
132 |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
133 return settings; |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
134 } |
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
135 |
6 | 136 private static FileInfo GetTranslationFile(string language) |
137 { | |
138 FileInfo file = new FileInfo(translationDir.FullName + Constants.DirectoryString + language + ".translation"); | |
139 | |
140 if (!file.Exists) | |
141 { | |
21
c8d74202182a
Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents:
16
diff
changeset
|
142 throw new TranslationLoadException(language + ".translation could not be found in "+translationDir.FullName); |
6 | 143 } |
144 | |
145 return file; | |
146 } | |
147 | |
148 private static void LoadTranslationsFromDocument(XmlDocument doc, Dictionary<string, string> translationTable) | |
149 { | |
150 try | |
151 { | |
35
2b5e73cb83a3
Re #21 - Reduce specificity of translations file
IBBoard <dev@ibboard.co.uk>
parents:
23
diff
changeset
|
152 XmlNodeList translations = doc.GetElementsByTagName("translation"); |
6 | 153 Dictionary<string, string> tempTranslationTable = new Dictionary<string,string>(); |
154 | |
155 foreach (XmlNode node in translations) | |
156 { | |
157 tempTranslationTable.Add(node.Attributes["id"].Value, node.InnerText); | |
158 } | |
159 | |
160 translationTable.Clear(); | |
161 | |
162 foreach (string key in tempTranslationTable.Keys) | |
163 { | |
164 string translation; | |
165 tempTranslationTable.TryGetValue(key, out translation); | |
166 translationTable.Add(key, translation); | |
167 } | |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
168 } |
6 | 169 catch(Exception ex) |
170 { | |
21
c8d74202182a
Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents:
16
diff
changeset
|
171 throw new TranslationLoadException("Error while parsing " + GetLanguageOfDocument(doc)+" translation: "+ex.Message, ex); |
6 | 172 } |
173 } | |
174 | |
175 private static string GetLanguageOfDocument(XmlDocument doc) | |
176 { | |
177 return doc != null ? doc.DocumentElement.GetAttribute("lang") : ""; | |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
178 } |
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 private static void ValidationEventMethod(object sender, ValidationEventArgs e) |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
181 { |
22
ea058f9ea9d4
Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
182 throw new TranslationLoadException("Problem validating schema for translation: " + e.Exception.Message, e.Exception); |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
183 } |
6 | 184 |
185 /// <summary> | |
21
c8d74202182a
Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents:
16
diff
changeset
|
186 /// Loads translations for a given language and sets them as the local language. |
c8d74202182a
Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents:
16
diff
changeset
|
187 /// hrows 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
|
188 /// still be called but all translations will fall back to the default translation. |
6 | 189 /// </summary> |
190 /// <param name="translationLang"> | |
191 /// The new local language to load | |
192 /// </param> | |
7
f4da31cb09d9
* Add translation DTD to utils project
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
193 public static void LoadTranslation(string translationLanguage) |
f4da31cb09d9
* Add translation DTD to utils project
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
194 { |
f4da31cb09d9
* Add translation DTD to utils project
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
195 if (translationLanguage == "" || translationLanguage == null) |
6 | 196 { |
10
3b7a321e7c4c
Fixes #4 - unexpected exception in translations
IBBoard <dev@ibboard.co.uk>
parents:
9
diff
changeset
|
197 throw new ArgumentException("Translation language cannot be null or empty"); |
6 | 198 } |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
199 |
8
d6ce8764e92b
Fix typo in method name reference
IBBoard <dev@ibboard.co.uk>
parents:
7
diff
changeset
|
200 LoadTranslationForLanguage(translationLanguage); |
7
f4da31cb09d9
* Add translation DTD to utils project
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
201 } |
f4da31cb09d9
* Add translation DTD to utils project
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
202 |
f4da31cb09d9
* Add translation DTD to utils project
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
203 private static void LoadTranslationForLanguage(string translationLanguage) |
f4da31cb09d9
* Add translation DTD to utils project
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
204 { |
21
c8d74202182a
Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents:
16
diff
changeset
|
205 CheckInitialisation(); |
7
f4da31cb09d9
* Add translation DTD to utils project
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
206 |
f4da31cb09d9
* Add translation DTD to utils project
IBBoard <dev@ibboard.co.uk>
parents:
6
diff
changeset
|
207 if (translationLanguage != DEFAULT_LANGUAGE && translationLanguage != "" && translationLanguage != null) |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
208 { |
9 | 209 FileInfo file = GetTranslationFile(translationLanguage); |
6 | 210 XmlDocument doc = LoadTranslationDocument(file); |
211 LoadTranslationsFromDocument(doc, translationsLocal); | |
212 } | |
213 else | |
214 { | |
215 translationsLocal.Clear(); | |
216 } | |
217 | |
9 | 218 lang = translationLanguage; |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
219 } |
6 | 220 |
221 /// <summary> | |
222 /// 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. | |
223 /// </summary> | |
224 /// <param name="translationID"> | |
225 /// The ID to look up the translation for | |
226 /// </param> | |
227 /// <param name="replacements"> | |
228 /// A collection of <see cref="System.Object"/>s to replace placeholders with | |
229 /// </param> | |
230 /// <returns> | |
231 /// The translation with the placeholders replaced or a "missing translation" message | |
232 /// </returns> | |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
233 public static string GetTranslation(string translationID, params object[] replacements) |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
234 { |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
235 return GetTranslation(translationID, false, replacements); |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
236 } |
6 | 237 |
238 /// <summary> | |
239 /// 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. | |
240 /// </summary> | |
241 /// <param name="translationID"> | |
242 /// The ID to look up the translation for | |
243 /// </param> | |
244 /// <param name="returnNullOnFail"> | |
245 /// TRUE if null should be returned when no translation can be found, or FALSE if a "missing translation" message should be returned | |
246 /// </param> | |
247 /// <param name="replacements"> | |
248 /// A collection of <see cref="System.Object"/>s to replace placeholders with | |
249 /// </param> | |
250 /// <returns> | |
251 /// The translation with the placeholders replaced, or a "missing translation" message or null depending on <param name="returnNullOnFail"> | |
252 /// </returns> | |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
253 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
|
254 { |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
255 return GetTranslation(translationID, returnNullOnFail ? null : "", replacements); |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
256 } |
6 | 257 |
258 /// <summary> | |
259 /// 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. | |
260 /// </summary> | |
261 /// <param name="translationID"> | |
262 /// The ID to look up the translation for | |
263 /// </param> | |
264 /// <param name="defaultTranslation"> | |
265 /// The string to return if no translation can be found. Can be null or any string. | |
266 /// </param> | |
267 /// <param name="replacements"> | |
268 /// A collection of <see cref="System.Object"/>s to replace placeholders with | |
269 /// </param> | |
270 /// <returns> | |
271 /// The translation, if one exists, or the supplied default with the placeholders replaced | |
272 /// </returns> | |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
273 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
|
274 { |
21
c8d74202182a
Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents:
16
diff
changeset
|
275 CheckInitialisation(); |
6 | 276 string trans = GetTranslationFromTables(translationID); |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
277 |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
278 if (trans == null) |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
279 { |
6 | 280 trans = GetDefaultTranslation(translationID, defaultTranslation); |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
281 } |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
282 |
6 | 283 trans = AddVariablesToTranslation(trans, replacements); |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
284 |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
285 return trans; |
6 | 286 } |
287 | |
288 private static string GetTranslationFromTables(string translationID) | |
289 { | |
290 string translation = null; | |
291 | |
292 if (translationsLocal!=null) | |
293 { | |
294 translationsLocal.TryGetValue(translationID, out translation); | |
295 } | |
296 | |
297 if (translation == null) | |
298 { | |
299 translationsDefault.TryGetValue(translationID, out translation); | |
300 } | |
301 | |
302 return translation; | |
303 } | |
304 | |
305 private static string GetDefaultTranslation(string translationID, string defaultTranslation) | |
306 { | |
307 return (defaultTranslation != "" && defaultTranslation != null) ? defaultTranslation : GetMissingTranslationMessage(translationID); | |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
308 } |
6 | 309 |
310 private static string GetMissingTranslationMessage(string translationID) | |
311 { | |
312 return "++ Missing Translation "+translationID+" ++"; | |
313 } | |
314 | |
315 private static string AddVariablesToTranslation(string translation, object[] replacements) | |
316 { | |
317 if (translation != null && replacements != null && replacements.Length > 0) | |
318 { | |
319 translation = String.Format(translation, replacements); | |
320 } | |
321 | |
322 return translation; | |
323 } | |
324 | |
21
c8d74202182a
Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents:
16
diff
changeset
|
325 private static void CheckInitialisation() |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
326 { |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
327 if (translationDir==null) |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
328 { |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
329 throw new InvalidOperationException("Translation class has not been initialised"); |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
330 } |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
331 } |
6 | 332 |
333 /// <summary> | |
334 /// Translate an <see cref="ITranslatable"/> item, with optional string replacement | |
335 /// </summary> | |
336 /// <param name="item"> | |
337 /// A <see cref="ITranslatable"/> to set the text for | |
338 /// </param> | |
339 /// <param name="replacements"> | |
340 /// A collection of <see cref="System.Object"/>s that will be used to fill place-holders | |
341 /// </param> | |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
342 public static void Translate(ITranslatable item, params object[] replacements) |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
343 { |
6 | 344 if (item.Text == "" || item.Text == DIVIDER_STRING) |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
345 { |
6 | 346 //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
|
347 return; |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
348 } |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
349 |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
350 item.Text = GetTranslation(item.Name, replacements); |
6 | 351 } |
352 | |
353 /// <summary> | |
354 /// 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. | |
355 /// </summary> | |
356 /// <returns> | |
357 /// The string used in the file name of the current local translation | |
358 /// </returns> | |
359 public static string GetTranslationLanguage() | |
360 { | |
361 return lang; | |
0
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
362 } |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
363 } |
961030992bd2
Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
364 } |