annotate Lang/Translation.cs @ 73:091bfa54d6c7

Re #33: Add method to get list of available translations * Add "translation language" object to pair up code and name * Load all translations at initialisation time Also: * Remove rogue "set" property on abstract translation set
author IBBoard <dev@ibboard.co.uk>
date Tue, 06 Apr 2010 18:58:30 +0000
parents 40c09e57d213
children 726731c78414
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>
71
40c09e57d213 Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents: 69
diff changeset
19 /// A basic string translator that a specified language and returns translated strings that correspond to translation IDs.
40c09e57d213 Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents: 69
diff changeset
20 /// If the string doesn't exist in the specified language then the translator falls back defined 'super' languages.
40c09e57d213 Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents: 69
diff changeset
21 /// If the translation doesn't exist in the hierarchy of languages then either a supplied value, null or a "no validation available"
40c09e57d213 Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents: 69
diff changeset
22 /// message is returned, depending on the parameters supplied to the method.
40c09e57d213 Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents: 69
diff changeset
23 ///
40c09e57d213 Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents: 69
diff changeset
24 /// Loaded languages are referenced by two-character language code (e.g. "en" or "it")
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
25 /// </summary>
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
26 public class Translation
71
40c09e57d213 Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents: 69
diff changeset
27 {
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
28 private static readonly string DIVIDER_STRING = "-";
73
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
29 private static AbstractTranslationSet currentTranslations;
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
30 private static Dictionary<string, AbstractTranslationSet> langToTranslationMap;
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
31
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
32 static Translation()
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
33 {
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
34 langToTranslationMap = new Dictionary<string, AbstractTranslationSet>();
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
35 }
6
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
36
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
37 /// <summary>
71
40c09e57d213 Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents: 69
diff changeset
38 /// Initialises the translations and loads the specified language so that the Translation class can be used.
21
c8d74202182a Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
39 /// 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
40 /// still be called but no translation will be performed.
6
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
41 /// </summary>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
42 /// <param name="appPath">
71
40c09e57d213 Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents: 69
diff changeset
43 /// The full path that the contains the "translations" folder - normally the application directory path.
6
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
44 /// </param>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
45 /// <param name="language">
71
40c09e57d213 Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents: 69
diff changeset
46 /// The language to use as the loaded language
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
47 /// </param>
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
48 public static void InitialiseTranslations(string appPath, string language)
6
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
49 {
71
40c09e57d213 Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents: 69
diff changeset
50 InitialiseTranslations(appPath);
40c09e57d213 Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents: 69
diff changeset
51 LoadTranslationForLanguage(language);
40c09e57d213 Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents: 69
diff changeset
52 }
40c09e57d213 Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents: 69
diff changeset
53
40c09e57d213 Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents: 69
diff changeset
54 /// <summary>
40c09e57d213 Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents: 69
diff changeset
55 /// Initialises the translation class for an application or source.
40c09e57d213 Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents: 69
diff changeset
56 /// </summary>
40c09e57d213 Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents: 69
diff changeset
57 /// <param name="appPath">
40c09e57d213 Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents: 69
diff changeset
58 /// The full path that the contains the "translations" folder - normally the application directory path.
40c09e57d213 Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents: 69
diff changeset
59 /// </param>
40c09e57d213 Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents: 69
diff changeset
60 public static void InitialiseTranslations(string appPath)
40c09e57d213 Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents: 69
diff changeset
61 {
6
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
62 InitialiseDefaults(appPath);
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
63 }
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
64
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
65 private static void InitialiseDefaults(string appPath)
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
66 {
73
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
67 Reset();
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
68 LoadTranslations(appPath);
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
69 }
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
70
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
71 private static void LoadTranslations(string appPath)
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
72 {
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
73 DirectoryInfo dir = new DirectoryInfo(Path.Combine(appPath, "translations"));
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
74
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
75 if (!dir.Exists)
6
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
76 {
73
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
77 throw new TranslationLoadException("Translation path not found (" + dir.FullName + ")");
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
78 }
73
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
79
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
80 TranslationXmlLoader loader = new TranslationXmlLoader(Path.Combine(appPath, "schemas/translation.xsd"));
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
81
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
82 foreach (FileInfo file in dir.GetFiles("*.translation"))
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
83 {
73
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
84 AbstractTranslationSet translations = loader.LoadTranslations(file.FullName);
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
85 langToTranslationMap[translations.LanguageCode] = translations;
6
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
86 }
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
87 }
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
88
22
ea058f9ea9d4 Closes #15 - Migrate to schema for translations
IBBoard <dev@ibboard.co.uk>
parents: 21
diff changeset
89 /// <summary>
71
40c09e57d213 Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents: 69
diff changeset
90 /// Resets the loaded translations and reverts to no translations.
69
b5d7e8b93205 Re #30: Improve Translations API
IBBoard <dev@ibboard.co.uk>
parents: 67
diff changeset
91 /// </summary>
b5d7e8b93205 Re #30: Improve Translations API
IBBoard <dev@ibboard.co.uk>
parents: 67
diff changeset
92 public static void Reset()
b5d7e8b93205 Re #30: Improve Translations API
IBBoard <dev@ibboard.co.uk>
parents: 67
diff changeset
93 {
73
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
94 currentTranslations = null;
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
95 langToTranslationMap.Clear();
69
b5d7e8b93205 Re #30: Improve Translations API
IBBoard <dev@ibboard.co.uk>
parents: 67
diff changeset
96 }
6
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
97
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
98 /// <summary>
71
40c09e57d213 Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents: 69
diff changeset
99 /// Loads translations for a given language and sets them as the current language.
40c09e57d213 Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents: 69
diff changeset
100 /// Throws a TranslationLoadException if a problem occurred while loading translations. If this occurs then the translation methods can
21
c8d74202182a Closes #14 - Throw specific exceptions from translations
IBBoard <dev@ibboard.co.uk>
parents: 16
diff changeset
101 /// 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
102 /// </summary>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
103 /// <param name="translationLang">
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
104 /// The new local language to load
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
105 /// </param>
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
106 public static void LoadTranslation(string translationLanguage)
73
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
107 {
7
f4da31cb09d9 * Add translation DTD to utils project
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
108 if (translationLanguage == "" || translationLanguage == null)
6
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
109 {
10
3b7a321e7c4c Fixes #4 - unexpected exception in translations
IBBoard <dev@ibboard.co.uk>
parents: 9
diff changeset
110 throw new ArgumentException("Translation language cannot be null or empty");
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
111 }
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
112
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
113 LoadTranslationForLanguage(translationLanguage);
7
f4da31cb09d9 * Add translation DTD to utils project
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
114 }
f4da31cb09d9 * Add translation DTD to utils project
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
115
f4da31cb09d9 * Add translation DTD to utils project
IBBoard <dev@ibboard.co.uk>
parents: 6
diff changeset
116 private static void LoadTranslationForLanguage(string translationLanguage)
67
5fb2e5a2e7a8 * Remove errors on uninitialised translations - means that the translations just fall back to the provided string if they haven't been initialised
IBBoard <dev@ibboard.co.uk>
parents: 65
diff changeset
117 {
73
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
118 currentTranslations = DictionaryUtils.GetValue(langToTranslationMap, translationLanguage);
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
119 }
6
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
120
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
121 /// <summary>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
122 /// 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
123 /// </summary>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
124 /// <param name="translationID">
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
125 /// The ID to look up the translation for
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
126 /// </param>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
127 /// <param name="replacements">
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
128 /// 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
129 /// </param>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
130 /// <returns>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
131 /// 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
132 /// </returns>
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
133 public static string GetTranslation(string translationID, params object[] replacements)
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
134 {
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
135 return GetTranslation(translationID, false, replacements);
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
136 }
6
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 /// <summary>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
139 /// 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
140 /// </summary>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
141 /// <param name="translationID">
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
142 /// The ID to look up the translation for
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
143 /// </param>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
144 /// <param name="returnNullOnFail">
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
145 /// 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
146 /// </param>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
147 /// <param name="replacements">
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
148 /// 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
149 /// </param>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
150 /// <returns>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
151 /// 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
152 /// </returns>
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
153 public static string GetTranslation(string translationID, bool returnNullOnFail, params object[] replacements)
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
154 {
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
155 return GetTranslation(translationID, returnNullOnFail ? null : "", replacements);
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
156 }
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 /// <summary>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
159 /// 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
160 /// </summary>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
161 /// <param name="translationID">
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
162 /// The ID to look up the translation for
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
163 /// </param>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
164 /// <param name="defaultTranslation">
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
165 /// 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
166 /// </param>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
167 /// <param name="replacements">
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
168 /// 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
169 /// </param>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
170 /// <returns>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
171 /// 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
172 /// </returns>
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
173 public static string GetTranslation(string translationID, string defaultTranslation, params object[] replacements)
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
174 {
71
40c09e57d213 Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents: 69
diff changeset
175 string trans = GetTranslationFromTranslationSet(translationID);
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
176
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
177 if (trans == null)
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
178 {
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
179 trans = GetDefaultTranslation(translationID, defaultTranslation);
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
180 }
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
181
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
182 trans = AddVariablesToTranslation(trans, replacements);
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
183
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
184 return trans;
6
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
185 }
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
186
71
40c09e57d213 Fixes #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents: 69
diff changeset
187 private static string GetTranslationFromTranslationSet(string translationID)
6
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
188 {
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
189 string translation = null;
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
190
73
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
191 if (currentTranslations!=null)
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
192 {
73
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
193 translation = currentTranslations[translationID];
6
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
194 }
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
195
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
196 return translation;
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
197 }
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
198
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
199 private static string GetDefaultTranslation(string translationID, string defaultTranslation)
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
200 {
69
b5d7e8b93205 Re #30: Improve Translations API
IBBoard <dev@ibboard.co.uk>
parents: 67
diff changeset
201 return (defaultTranslation != "" || defaultTranslation == null) ? defaultTranslation : GetMissingTranslationMessage(translationID);
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
202 }
6
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
203
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
204 private static string GetMissingTranslationMessage(string translationID)
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
205 {
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
206 return "++ Missing Translation "+translationID+" ++";
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
207 }
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
208
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
209 private static string AddVariablesToTranslation(string translation, object[] replacements)
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
210 {
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
211 if (translation != null && replacements != null && replacements.Length > 0)
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
212 {
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
213 translation = String.Format(translation, replacements);
6
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
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
216 return translation;
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
217 }
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
218
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
219 /// <summary>
36
c949727ec0e0 Re #22 - Make failing control translation cleaner for normal use
IBBoard <dev@ibboard.co.uk>
parents: 35
diff changeset
220 /// 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
221 /// 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
222 /// </summary>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
223 /// <param name="item">
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
224 /// A <see cref="ITranslatable"/> to set the text 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 that will be used to fill place-holders
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
228 /// </param>
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
229 public static void Translate(ITranslatable item, params object[] replacements)
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
230 {
69
b5d7e8b93205 Re #30: Improve Translations API
IBBoard <dev@ibboard.co.uk>
parents: 67
diff changeset
231 Translate(item, GetMissingTranslationMessage(item.Name), replacements);
37
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
232 }
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 /// <summary>
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
235 /// 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
236 /// 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
237 /// about a missing translation ID.
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
238 /// </summary>
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
239 /// <param name="item">
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
240 /// A <see cref="ITranslatable"/> to set the text for
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
241 /// </param>
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
242 /// <param name="defaultText">
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
243 /// The default string to display if no translation could be found.
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
244 /// </param>
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
245 /// <param name="replacements">
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
246 /// 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
247 /// </param>
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
248 public static void Translate(ITranslatable item, string defaultText, params object[] replacements)
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
249 {
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
250 if (item.Text == "" || item.Text == DIVIDER_STRING)
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
251 {
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
252 //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
253 return;
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
254 }
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
255
cc7fae81afec * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
256 item.Text = GetTranslation(item.Name, defaultText, replacements);
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
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 /// 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
261 /// </summary>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
262 /// <returns>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
263 /// 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
264 /// </returns>
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
265 public static string GetTranslationLanguage()
f269d8bcc152 Re #2 - Refactor API
IBBoard <dev@ibboard.co.uk>
parents: 4
diff changeset
266 {
73
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
267 return (currentTranslations != null ? currentTranslations.LanguageCode : "");
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
268 }
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
269
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
270 public static ICollection<TranslationLanguage> GetLanguages()
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
271 {
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
272 ICollection<TranslationLanguage> langs = new List<TranslationLanguage>();
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
273
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
274 foreach (AbstractTranslationSet translations in langToTranslationMap.Values)
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
275 {
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
276 langs.Add(translations.Language);
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
277 }
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
278
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 71
diff changeset
279 return langs;
37
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 }