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
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
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
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
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
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
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";
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
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
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
28 private static DirectoryInfo translationDir;
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
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;
ea058f9ea9d4 Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents: 21
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
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
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
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);
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
47 FileInfo file = GetTranslationFile(DEFAULT_LANGUAGE);
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
48 XmlDocument doc = LoadTranslationDocument(file);
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
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
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 {
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
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
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>();
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
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
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
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
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.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
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
136 private static FileInfo GetTranslationFile(string language)
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
137 {
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
138 FileInfo file = new FileInfo(translationDir.FullName + Constants.DirectoryString + language + ".translation");
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
139
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
140 if (!file.Exists)
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
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
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
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
145 return file;
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
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
148 private static void LoadTranslationsFromDocument(XmlDocument doc, Dictionary<string, string> translationTable)
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
149 {
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
150 try
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
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
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
153 Dictionary<string, string> tempTranslationTable = new Dictionary<string,string>();
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
154
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
155 foreach (XmlNode node in translations)
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
156 {
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
157 tempTranslationTable.Add(node.Attributes["id"].Value, node.InnerText);
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
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
160 translationTable.Clear();
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
161
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
162 foreach (string key in tempTranslationTable.Keys)
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
163 {
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
164 string translation;
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
165 tempTranslationTable.TryGetValue(key, out translation);
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
166 translationTable.Add(key, translation);
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
167 }
0
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
168 }
6
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
169 catch(Exception ex)
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
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
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
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
175 private static string GetLanguageOfDocument(XmlDocument doc)
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
176 {
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
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
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
184
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
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
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
189 /// </summary>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
190 /// <param name="translationLang">
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
191 /// The new local language to load
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
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
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
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
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
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
562390c8d618 Fix variable name errors
IBBoard <dev@ibboard.co.uk>
parents: 8
diff changeset
209 FileInfo file = GetTranslationFile(translationLanguage);
6
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
210 XmlDocument doc = LoadTranslationDocument(file);
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
211 LoadTranslationsFromDocument(doc, translationsLocal);
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
212 }
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
213 else
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
214 {
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
215 translationsLocal.Clear();
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
216 }
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
217
9
562390c8d618 Fix variable name errors
IBBoard <dev@ibboard.co.uk>
parents: 8
diff changeset
218 lang = translationLanguage;
0
961030992bd2 Initial commit of IBBoard libraries
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
219 }
6
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
220
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
221 /// <summary>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
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.
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
223 /// </summary>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
224 /// <param name="translationID">
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
225 /// The ID to look up the translation for
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
226 /// </param>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
227 /// <param name="replacements">
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
228 /// 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
229 /// </param>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
230 /// <returns>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
231 /// The translation with the placeholders replaced or a "missing translation" message
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
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
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
237
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
238 /// <summary>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
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.
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
240 /// </summary>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
241 /// <param name="translationID">
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
242 /// The ID to look up the translation for
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
243 /// </param>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
244 /// <param name="returnNullOnFail">
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
245 /// 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
246 /// </param>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
247 /// <param name="replacements">
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
248 /// 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
249 /// </param>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
250 /// <returns>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
251 /// The translation with the placeholders replaced, or a "missing translation" message or null depending on <param name="returnNullOnFail">
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
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
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
257
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
258 /// <summary>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
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.
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
260 /// </summary>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
261 /// <param name="translationID">
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
262 /// The ID to look up the translation for
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
263 /// </param>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
264 /// <param name="defaultTranslation">
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
265 /// 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
266 /// </param>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
267 /// <param name="replacements">
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
268 /// 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
269 /// </param>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
270 /// <returns>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
271 /// The translation, if one exists, or the supplied default with the placeholders replaced
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
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
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
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
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
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
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
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
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
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
288 private static string GetTranslationFromTables(string translationID)
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
289 {
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
290 string translation = null;
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
291
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
292 if (translationsLocal!=null)
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
293 {
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
294 translationsLocal.TryGetValue(translationID, out translation);
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
295 }
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
296
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
297 if (translation == null)
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
298 {
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
299 translationsDefault.TryGetValue(translationID, out translation);
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
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
302 return translation;
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
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
305 private static string GetDefaultTranslation(string translationID, string defaultTranslation)
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
306 {
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
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
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
309
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
310 private static string GetMissingTranslationMessage(string translationID)
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
311 {
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
312 return "++ Missing Translation "+translationID+" ++";
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
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
315 private static string AddVariablesToTranslation(string translation, object[] replacements)
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
316 {
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
317 if (translation != null && replacements != null && replacements.Length > 0)
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
318 {
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
319 translation = String.Format(translation, replacements);
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
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
322 return translation;
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
323 }
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
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
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
332
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
333 /// <summary>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
334 /// Translate an <see cref="ITranslatable"/> item, with optional string replacement
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
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
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
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
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
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
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
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
351 }
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
352
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
353 /// <summary>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
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.
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
355 /// </summary>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
356 /// <returns>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
357 /// 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
358 /// </returns>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
359 public static string GetTranslationLanguage()
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
360 {
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
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 }