annotate Lang/AbstractTranslationSet.cs @ 98:e260fce130eb

* Remove deprecated version of maths code no-open-ticket
author IBBoard <dev@ibboard.co.uk>
date Sun, 13 Feb 2011 16:17:34 +0000
parents da339d10c5fe
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
69
b5d7e8b93205 Re #30: Improve Translations API
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
1 // This file (AbstractTranslationSet.cs) is a part of the IBBoard project and is copyright 2010 IBBoard
b5d7e8b93205 Re #30: Improve Translations API
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
2 //
78
da339d10c5fe Re #32: Add staged loading of translations
IBBoard <dev@ibboard.co.uk>
parents: 77
diff changeset
3 // The file and the library/program it is in are licensed and distributed, without warranty, under the GNU LGPL, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license.
69
b5d7e8b93205 Re #30: Improve Translations API
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
4
b5d7e8b93205 Re #30: Improve Translations API
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
5 using System;
b5d7e8b93205 Re #30: Improve Translations API
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
6 using System.Collections.Generic;
b5d7e8b93205 Re #30: Improve Translations API
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
7
b5d7e8b93205 Re #30: Improve Translations API
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
8 namespace IBBoard.Lang
b5d7e8b93205 Re #30: Improve Translations API
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
9 {
b5d7e8b93205 Re #30: Improve Translations API
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
10 /// <summary>
b5d7e8b93205 Re #30: Improve Translations API
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
11 /// A collection of translations for a given language. The abstract class must be extended by implementations that
b5d7e8b93205 Re #30: Improve Translations API
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
12 /// provide different ways of loading the data.
b5d7e8b93205 Re #30: Improve Translations API
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
13 /// </summary>
75
b1ae6fce2e3f Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 73
diff changeset
14 public abstract class AbstractTranslationSet
69
b5d7e8b93205 Re #30: Improve Translations API
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
15 {
73
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 72
diff changeset
16 private TranslationLanguage language;
69
b5d7e8b93205 Re #30: Improve Translations API
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
17 protected Dictionary<string, string> translations;
b5d7e8b93205 Re #30: Improve Translations API
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
18
76
f45d28dc1d6a Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 75
diff changeset
19 public AbstractTranslationSet(string languageCode)
69
b5d7e8b93205 Re #30: Improve Translations API
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
20 {
73
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 72
diff changeset
21 language = new TranslationLanguage(languageCode);
69
b5d7e8b93205 Re #30: Improve Translations API
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
22 translations = new Dictionary<string, string>();
b5d7e8b93205 Re #30: Improve Translations API
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
23 }
b5d7e8b93205 Re #30: Improve Translations API
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
24
75
b1ae6fce2e3f Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 73
diff changeset
25
69
b5d7e8b93205 Re #30: Improve Translations API
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
26 /// <summary>
b5d7e8b93205 Re #30: Improve Translations API
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
27 /// Gets the language code that this translation claims to be for
b5d7e8b93205 Re #30: Improve Translations API
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
28 /// </summary>
b5d7e8b93205 Re #30: Improve Translations API
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
29 public string LanguageCode
b5d7e8b93205 Re #30: Improve Translations API
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
30 {
73
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 72
diff changeset
31 get { return language.Code; }
69
b5d7e8b93205 Re #30: Improve Translations API
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
32 }
b5d7e8b93205 Re #30: Improve Translations API
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
33
b5d7e8b93205 Re #30: Improve Translations API
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
34 /// <summary>
b5d7e8b93205 Re #30: Improve Translations API
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
35 /// Gets a translation from the translation set, or <code>null</code> if the translation doesn't exist.
b5d7e8b93205 Re #30: Improve Translations API
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
36 /// </summary>
b5d7e8b93205 Re #30: Improve Translations API
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
37 /// <param name="key">
b5d7e8b93205 Re #30: Improve Translations API
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
38 /// The key (ID) of the translation to retrieve
b5d7e8b93205 Re #30: Improve Translations API
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
39 /// </param>
78
da339d10c5fe Re #32: Add staged loading of translations
IBBoard <dev@ibboard.co.uk>
parents: 77
diff changeset
40 public virtual string this[string key]
69
b5d7e8b93205 Re #30: Improve Translations API
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
41 {
b5d7e8b93205 Re #30: Improve Translations API
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
42 get
b5d7e8b93205 Re #30: Improve Translations API
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
43 {
75
b1ae6fce2e3f Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 73
diff changeset
44 string translation = DictionaryUtils.GetValue(translations, key);
b1ae6fce2e3f Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 73
diff changeset
45
76
f45d28dc1d6a Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 75
diff changeset
46 if (translation == null)
75
b1ae6fce2e3f Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 73
diff changeset
47 {
b1ae6fce2e3f Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 73
diff changeset
48 translation = GetParentTranslation(key);
b1ae6fce2e3f Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 73
diff changeset
49 }
b1ae6fce2e3f Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 73
diff changeset
50
b1ae6fce2e3f Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 73
diff changeset
51 return translation;
69
b5d7e8b93205 Re #30: Improve Translations API
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
52 }
72
cec6c4c0892d Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents: 69
diff changeset
53 }
75
b1ae6fce2e3f Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 73
diff changeset
54
b1ae6fce2e3f Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 73
diff changeset
55 private string GetParentTranslation(string key)
b1ae6fce2e3f Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 73
diff changeset
56 {
b1ae6fce2e3f Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 73
diff changeset
57 AbstractTranslationSet parentTranslations = GetParentTranslations();
b1ae6fce2e3f Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 73
diff changeset
58 string parentTranslation = null;
b1ae6fce2e3f Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 73
diff changeset
59
b1ae6fce2e3f Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 73
diff changeset
60 if (parentTranslations != null)
b1ae6fce2e3f Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 73
diff changeset
61 {
77
eb47e17ec824 Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 76
diff changeset
62 CheckForLooping(parentTranslations);
75
b1ae6fce2e3f Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 73
diff changeset
63 parentTranslation = parentTranslations[key];
b1ae6fce2e3f Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 73
diff changeset
64 }
b1ae6fce2e3f Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 73
diff changeset
65
b1ae6fce2e3f Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 73
diff changeset
66 return parentTranslation;
b1ae6fce2e3f Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 73
diff changeset
67 }
b1ae6fce2e3f Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 73
diff changeset
68
b1ae6fce2e3f Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 73
diff changeset
69 protected abstract AbstractTranslationSet GetParentTranslations();
72
cec6c4c0892d Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents: 69
diff changeset
70
77
eb47e17ec824 Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 76
diff changeset
71 private void CheckForLooping(AbstractTranslationSet translations)
eb47e17ec824 Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 76
diff changeset
72 {
eb47e17ec824 Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 76
diff changeset
73 bool hasLoop = false;
eb47e17ec824 Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 76
diff changeset
74 AbstractTranslationSet slowLoop = translations;
eb47e17ec824 Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 76
diff changeset
75 AbstractTranslationSet fastLoop1 = translations;
eb47e17ec824 Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 76
diff changeset
76 AbstractTranslationSet fastLoop2 = translations;
eb47e17ec824 Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 76
diff changeset
77
eb47e17ec824 Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 76
diff changeset
78 while (slowLoop != null && (fastLoop1 = fastLoop2.GetParentTranslations()) != null && (fastLoop2 = fastLoop1.GetParentTranslations()) != null)
eb47e17ec824 Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 76
diff changeset
79 {
eb47e17ec824 Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 76
diff changeset
80 if (slowLoop.Equals(fastLoop1) || slowLoop.Equals(fastLoop2))
eb47e17ec824 Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 76
diff changeset
81 {
eb47e17ec824 Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 76
diff changeset
82 hasLoop = true;
eb47e17ec824 Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 76
diff changeset
83 break;
eb47e17ec824 Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 76
diff changeset
84 }
eb47e17ec824 Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 76
diff changeset
85
eb47e17ec824 Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 76
diff changeset
86 slowLoop = slowLoop.GetParentTranslations();
eb47e17ec824 Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 76
diff changeset
87 }
eb47e17ec824 Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 76
diff changeset
88
eb47e17ec824 Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 76
diff changeset
89 if (hasLoop)
eb47e17ec824 Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 76
diff changeset
90 {
eb47e17ec824 Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 76
diff changeset
91 throw new TranslationLoadException("Translations contained an inheritence loop");
eb47e17ec824 Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 76
diff changeset
92 }
eb47e17ec824 Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 76
diff changeset
93 }
eb47e17ec824 Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 76
diff changeset
94
72
cec6c4c0892d Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents: 69
diff changeset
95 public string LanguageName
cec6c4c0892d Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents: 69
diff changeset
96 {
cec6c4c0892d Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents: 69
diff changeset
97 get
cec6c4c0892d Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents: 69
diff changeset
98 {
73
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 72
diff changeset
99 return language.Name;
72
cec6c4c0892d Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents: 69
diff changeset
100 }
cec6c4c0892d Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents: 69
diff changeset
101 }
73
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 72
diff changeset
102
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 72
diff changeset
103 public TranslationLanguage Language
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 72
diff changeset
104 {
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 72
diff changeset
105 get { return language; }
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 72
diff changeset
106 }
72
cec6c4c0892d Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents: 69
diff changeset
107
75
b1ae6fce2e3f Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 73
diff changeset
108 public TranslationLanguage ParentLanguage
b1ae6fce2e3f Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 73
diff changeset
109 {
76
f45d28dc1d6a Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 75
diff changeset
110 get
f45d28dc1d6a Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 75
diff changeset
111 {
f45d28dc1d6a Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 75
diff changeset
112 TranslationLanguage parentLang = null;
f45d28dc1d6a Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 75
diff changeset
113 AbstractTranslationSet parentTranslations = GetParentTranslations();
f45d28dc1d6a Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 75
diff changeset
114
f45d28dc1d6a Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 75
diff changeset
115 if (parentTranslations != null)
f45d28dc1d6a Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 75
diff changeset
116 {
f45d28dc1d6a Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 75
diff changeset
117 parentLang = parentTranslations.Language;
f45d28dc1d6a Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 75
diff changeset
118 }
f45d28dc1d6a Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 75
diff changeset
119
f45d28dc1d6a Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 75
diff changeset
120 return parentLang;
f45d28dc1d6a Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 75
diff changeset
121 }
75
b1ae6fce2e3f Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 73
diff changeset
122 }
b1ae6fce2e3f Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 73
diff changeset
123
73
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 72
diff changeset
124 public override bool Equals(object obj)
72
cec6c4c0892d Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents: 69
diff changeset
125 {
73
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 72
diff changeset
126 bool equal = true;
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 72
diff changeset
127
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 72
diff changeset
128 if (obj == null || !obj.GetType().Equals(GetType()))
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 72
diff changeset
129 {
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 72
diff changeset
130 equal = false;
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 72
diff changeset
131 }
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 72
diff changeset
132 else
72
cec6c4c0892d Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents: 69
diff changeset
133 {
73
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 72
diff changeset
134 equal = LanguageCode.Equals(((AbstractTranslationSet)obj).LanguageCode);
72
cec6c4c0892d Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents: 69
diff changeset
135 }
73
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 72
diff changeset
136
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 72
diff changeset
137 return equal;
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 72
diff changeset
138 }
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 72
diff changeset
139
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 72
diff changeset
140 public override int GetHashCode ()
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 72
diff changeset
141 {
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 72
diff changeset
142 return GetType().GetHashCode() + LanguageCode.GetHashCode();
72
cec6c4c0892d Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents: 69
diff changeset
143 }
69
b5d7e8b93205 Re #30: Improve Translations API
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
144 }
b5d7e8b93205 Re #30: Improve Translations API
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
145 }