annotate Lang/Translation.cs @ 65:980ebd49c40b

* Remove unnecessary XML resolver (was used for DTD, now using schema) no-open-ticket
author IBBoard <dev@ibboard.co.uk>
date Sun, 20 Dec 2009 20:30:29 +0000
parents 70d6c2a5d99e
children 5fb2e5a2e7a8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
16
0352fa33ee8f Closes #8 - license code
IBBoard <dev@ibboard.co.uk>
parents: 10
diff changeset
1 // This file (Translation.cs) is a part of the IBBoard library and is copyright 2009 IBBoard.
0352fa33ee8f Closes #8 - license code
IBBoard <dev@ibboard.co.uk>
parents: 10
diff changeset
2 //
0352fa33ee8f Closes #8 - license code
IBBoard <dev@ibboard.co.uk>
parents: 10
diff changeset
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.
0352fa33ee8f Closes #8 - license code
IBBoard <dev@ibboard.co.uk>
parents: 10
diff changeset
4
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
5 using System;
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
6 using System.IO;
0
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
7 using System.Xml;
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
8 using System.Xml.Schema;
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
9 using System.Collections.Generic;
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
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
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
13 using IBBoard.Logging;
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
14 using IBBoard.Xml;
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
15
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
16 namespace IBBoard.Lang
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
17 {
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
18 /// <summary>
6
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
19 /// A basic string translator that loads a default language and a specified language and returns translated strings that correspond to translation IDs.
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
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
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
21 /// then either a supplied value or a "no validation available" message is returned.
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
22 /// </summary>
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
23 public class Translation
6
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
24 {
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
25 private static readonly string DEFAULT_LANGUAGE = "en";
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
26 private static readonly string DIVIDER_STRING = "-";
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
27 private static string lang = "";
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
28 private static DirectoryInfo translationDir;
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
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;
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
31 private static XmlReaderSettings settings;
6
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
32
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
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
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
37 /// </summary>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
38 /// <param name="appPath">
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
39 /// The full path that the application is running from. Must contain the "translations" folder.
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
40 /// </param>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
41 /// <param name="language">
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
42 /// The language to use as the load language
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
43 /// </param>
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
44 public static void InitialiseTranslations(string appPath, string language)
6
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
45 {
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
46 InitialiseDefaults(appPath);
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
47 FileInfo file = GetTranslationFile(DEFAULT_LANGUAGE);
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
48 XmlDocument doc = LoadTranslationDocument(file);
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
49 LoadTranslationsFromDocument(doc, translationsDefault);
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
50 LoadTranslationForLanguage(language);
6
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
51 }
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
52
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
53 private static void InitialiseDefaults(string appPath)
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
54 {
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
55 string translationPath = appPath.TrimEnd(Constants.DirectoryChar) + Constants.DirectoryString + "translations";
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
56
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
57 if (Directory.Exists(translationPath))
6
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
58 {
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
59 translationsDefault = new Dictionary<string,string>();
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
60 translationsLocal = new Dictionary<string,string>();
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
61 translationDir = new DirectoryInfo(translationPath);
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
62 }
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
63 else
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
64 {
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
65 throw new TranslationLoadException("Translation path not found ("+translationPath+")");
6
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
66 }
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
67 }
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
68
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
69 private static XmlDocument LoadTranslationDocument(FileInfo file)
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
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
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
75 {
21
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
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
91 {
21
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
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
95 return doc;
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
96 }
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
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.ValidationType = ValidationType.Schema;
23
fb4fdab841db * Ignore schema location attribute for translations
IBBoard <dev@ibboard.co.uk>
parents: 22
diff changeset
112 settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings;
22
ea058f9ea9d4 Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents: 21
diff changeset
113 settings.ValidationEventHandler+= new ValidationEventHandler(ValidationEventMethod);
ea058f9ea9d4 Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents: 21
diff changeset
114 XmlSchemaSet cache = new XmlSchemaSet();
64
70d6c2a5d99e * Move schema from "dtds" folder to "schemas" folder to match warfoundry:429
IBBoard <dev@ibboard.co.uk>
parents: 37
diff changeset
115 cache.Add("http://ibboard.co.uk/translation", translationDir.Parent.FullName + "/schemas/translation.xsd");
22
ea058f9ea9d4 Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents: 21
diff changeset
116 settings.Schemas.Add(cache);
ea058f9ea9d4 Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents: 21
diff changeset
117 }
ea058f9ea9d4 Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents: 21
diff changeset
118 catch (DirectoryNotFoundException ex)
ea058f9ea9d4 Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents: 21
diff changeset
119 {
ea058f9ea9d4 Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents: 21
diff changeset
120 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
121 }
ea058f9ea9d4 Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents: 21
diff changeset
122 catch (XmlSchemaException ex)
ea058f9ea9d4 Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents: 21
diff changeset
123 {
ea058f9ea9d4 Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents: 21
diff changeset
124 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
125 }
ea058f9ea9d4 Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents: 21
diff changeset
126 catch (XmlException ex)
ea058f9ea9d4 Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents: 21
diff changeset
127 {
ea058f9ea9d4 Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents: 21
diff changeset
128 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
129 }
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 return settings;
ea058f9ea9d4 Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents: 21
diff changeset
133 }
ea058f9ea9d4 Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents: 21
diff changeset
134
6
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
135 private static FileInfo GetTranslationFile(string language)
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
136 {
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
137 FileInfo file = new FileInfo(translationDir.FullName + Constants.DirectoryString + language + ".translation");
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
138
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
139 if (!file.Exists)
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
140 {
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
141 throw new TranslationLoadException(language + ".translation could not be found in "+translationDir.FullName);
6
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
142 }
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
143
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
144 return file;
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
145 }
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
146
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
147 private static void LoadTranslationsFromDocument(XmlDocument doc, Dictionary<string, string> translationTable)
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
148 {
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
149 try
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
150 {
35
2b5e73cb83a3 Re #21 - Reduce specificity of translations file
IBBoard <dev@ibboard.co.uk>
parents: 23
diff changeset
151 XmlNodeList translations = doc.GetElementsByTagName("translation");
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
152 Dictionary<string, string> tempTranslationTable = new Dictionary<string,string>();
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
153
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
154 foreach (XmlNode node in translations)
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
155 {
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
156 tempTranslationTable.Add(node.Attributes["id"].Value, node.InnerText);
6
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
157 }
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
158
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
159 translationTable.Clear();
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
160
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
161 foreach (string key in tempTranslationTable.Keys)
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
162 {
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
163 string translation;
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
164 tempTranslationTable.TryGetValue(key, out translation);
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
165 translationTable.Add(key, translation);
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
166 }
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
167 }
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
168 catch(Exception ex)
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
169 {
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
170 throw new TranslationLoadException("Error while parsing " + GetLanguageOfDocument(doc)+" translation: "+ex.Message, ex);
6
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
171 }
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
172 }
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
173
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
174 private static string GetLanguageOfDocument(XmlDocument doc)
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
175 {
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
176 return doc != null ? doc.DocumentElement.GetAttribute("lang") : "";
0
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
177 }
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 private static void ValidationEventMethod(object sender, ValidationEventArgs e)
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
180 {
22
ea058f9ea9d4 Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents: 21
diff changeset
181 throw new TranslationLoadException("Problem validating schema for translation: " + e.Exception.Message, e.Exception);
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
182 }
6
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
183
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
184 /// <summary>
21
c8d74202182a Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
185 /// 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
186 /// 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
187 /// still be called but all translations will fall back to the default translation.
6
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
188 /// </summary>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
189 /// <param name="translationLang">
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
190 /// The new local language to load
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
191 /// </param>
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
192 public static void LoadTranslation(string translationLanguage)
7
f4da31cb09d9 * Add translation DTD to utils project
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
193 {
f4da31cb09d9 * Add translation DTD to utils project
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
194 if (translationLanguage == "" || translationLanguage == null)
6
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
195 {
10
3b7a321e7c4c Fixes #4 - unexpected exception in translations
IBBoard <dev@ibboard.co.uk>
parents: 9
diff changeset
196 throw new ArgumentException("Translation language cannot be null or empty");
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
197 }
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
198
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
199 LoadTranslationForLanguage(translationLanguage);
7
f4da31cb09d9 * Add translation DTD to utils project
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
200 }
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 private static void LoadTranslationForLanguage(string translationLanguage)
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
203 {
21
c8d74202182a Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
204 CheckInitialisation();
7
f4da31cb09d9 * Add translation DTD to utils project
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
205
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
206 if (translationLanguage != DEFAULT_LANGUAGE && translationLanguage != "" && translationLanguage != null)
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
207 {
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
208 FileInfo file = GetTranslationFile(translationLanguage);
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
209 XmlDocument doc = LoadTranslationDocument(file);
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
210 LoadTranslationsFromDocument(doc, translationsLocal);
6
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
211 }
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
212 else
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
213 {
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
214 translationsLocal.Clear();
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
215 }
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
216
9
562390c8d618 Fix variable name errors
IBBoard <dev@ibboard.co.uk>
parents: 8
diff changeset
217 lang = translationLanguage;
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
218 }
6
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
219
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
220 /// <summary>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
221 /// 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.
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
222 /// </summary>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
223 /// <param name="translationID">
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
224 /// The ID to look up the translation for
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
225 /// </param>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
226 /// <param name="replacements">
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
227 /// A collection of <see cref="System.Object"/>s to replace placeholders with
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
228 /// </param>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
229 /// <returns>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
230 /// The translation with the placeholders replaced or a "missing translation" message
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
231 /// </returns>
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
232 public static string GetTranslation(string translationID, params object[] replacements)
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
233 {
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
234 return GetTranslation(translationID, false, replacements);
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
235 }
6
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
236
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
237 /// <summary>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
238 /// 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.
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
239 /// </summary>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
240 /// <param name="translationID">
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
241 /// The ID to look up the translation for
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
242 /// </param>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
243 /// <param name="returnNullOnFail">
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
244 /// TRUE if null should be returned when no translation can be found, or FALSE if a "missing translation" message should be returned
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
245 /// </param>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
246 /// <param name="replacements">
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
247 /// A collection of <see cref="System.Object"/>s to replace placeholders with
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
248 /// </param>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
249 /// <returns>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
250 /// The translation with the placeholders replaced, or a "missing translation" message or null depending on <param name="returnNullOnFail">
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
251 /// </returns>
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
252 public static string GetTranslation(string translationID, bool returnNullOnFail, params object[] replacements)
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
253 {
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
254 return GetTranslation(translationID, returnNullOnFail ? null : "", replacements);
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
255 }
6
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
256
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
257 /// <summary>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
258 /// 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.
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
259 /// </summary>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
260 /// <param name="translationID">
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
261 /// The ID to look up the translation for
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
262 /// </param>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
263 /// <param name="defaultTranslation">
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
264 /// The string to return if no translation can be found. Can be null or any string.
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
265 /// </param>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
266 /// <param name="replacements">
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
267 /// A collection of <see cref="System.Object"/>s to replace placeholders with
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
268 /// </param>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
269 /// <returns>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
270 /// The translation, if one exists, or the supplied default with the placeholders replaced
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
271 /// </returns>
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
272 public static string GetTranslation(string translationID, string defaultTranslation, params object[] replacements)
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
273 {
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
274 CheckInitialisation();
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
275 string trans = GetTranslationFromTables(translationID);
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
276
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
277 if (trans == null)
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
278 {
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
279 trans = GetDefaultTranslation(translationID, defaultTranslation);
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
280 }
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
281
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
282 trans = AddVariablesToTranslation(trans, replacements);
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
283
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
284 return trans;
6
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
285 }
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
286
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
287 private static string GetTranslationFromTables(string translationID)
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
288 {
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
289 string translation = null;
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
290
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
291 if (translationsLocal!=null)
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
292 {
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
293 translationsLocal.TryGetValue(translationID, out translation);
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
294 }
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
295
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
296 if (translation == null)
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
297 {
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
298 translationsDefault.TryGetValue(translationID, out translation);
6
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
299 }
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
300
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
301 return translation;
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
302 }
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
303
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
304 private static string GetDefaultTranslation(string translationID, string defaultTranslation)
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
305 {
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
306 return (defaultTranslation != "" && defaultTranslation != null) ? defaultTranslation : GetMissingTranslationMessage(translationID);
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
307 }
6
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
308
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
309 private static string GetMissingTranslationMessage(string translationID)
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
310 {
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
311 return "++ Missing Translation "+translationID+" ++";
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
312 }
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
313
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
314 private static string AddVariablesToTranslation(string translation, object[] replacements)
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
315 {
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
316 if (translation != null && replacements != null && replacements.Length > 0)
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
317 {
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
318 translation = String.Format(translation, replacements);
6
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
319 }
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
320
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
321 return translation;
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
322 }
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
323
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
324 private static void CheckInitialisation()
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
325 {
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
326 if (translationDir==null)
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
327 {
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
328 throw new InvalidOperationException("Translation class has not been initialised");
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
329 }
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
330 }
6
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
331
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
332 /// <summary>
36
c949727ec0e0 Re #22 - Make failing control translation cleaner for normal use
IBBoard <dev@ibboard.co.uk>
parents: 35
diff changeset
333 /// 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
334 /// does not exist then a warning message will be used as the translated text.
6
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
335 /// </summary>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
336 /// <param name="item">
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
337 /// A <see cref="ITranslatable"/> to set the text for
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
338 /// </param>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
339 /// <param name="replacements">
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
340 /// A collection of <see cref="System.Object"/>s that will be used to fill place-holders
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
341 /// </param>
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
342 public static void Translate(ITranslatable item, params object[] replacements)
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
343 {
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
344 Translate(item, (string)null, replacements);
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
345 }
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
346
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
347 /// <summary>
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
348 /// Translate an <see cref="ITranslatable"/> item, with optional string replacement. The <code>defaultText</code>
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
349 /// can be used to specify an alternate translation. Passing <code>null</code> will result in a warning message
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
350 /// about a missing translation ID.
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
351 /// </summary>
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
352 /// <param name="item">
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
353 /// A <see cref="ITranslatable"/> to set the text for
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
354 /// </param>
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
355 /// <param name="defaultText">
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
356 /// The default string to display if no translation could be found.
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
357 /// </param>
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
358 /// <param name="replacements">
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
359 /// A collection of <see cref="System.Object"/>s that will be used to fill place-holders
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
360 /// </param>
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
361 public static void Translate(ITranslatable item, string defaultText, params object[] replacements)
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
362 {
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
363 if (item.Text == "" || item.Text == DIVIDER_STRING)
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
364 {
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
365 //it doesn't need translating - either there is no text from the developer or it's a hyphen for a divider
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
366 return;
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
367 }
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
368
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
369 item.Text = GetTranslation(item.Name, defaultText, replacements);
6
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
370 }
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
371
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
372 /// <summary>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
373 /// 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.
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
374 /// </summary>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
375 /// <returns>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
376 /// The string used in the file name of the current local translation
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
377 /// </returns>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
378 public static string GetTranslationLanguage()
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
379 {
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
380 return lang;
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
381 }
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
382 }
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
383 }