annotate Lang/AbstractTranslationSet.cs @ 76:f45d28dc1d6a

Re #35: Add multi-level cascading of translations * Restructure translations so that implementations define how parent translations are set TODO: Stop looping definition of parent (en-GB inherits from en inherits from en-GB, or more indirect)
author IBBoard <dev@ibboard.co.uk>
date Wed, 07 Apr 2010 19:55:05 +0000
parents b1ae6fce2e3f
children eb47e17ec824
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 //
b5d7e8b93205 Re #30: Improve Translations API
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
3 // The file and the library/program it is in are licensed and distributed, without warranty, under the GNU Affero GPL license, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license.
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>
b5d7e8b93205 Re #30: Improve Translations API
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
40 public string this[string key]
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 {
b1ae6fce2e3f Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 73
diff changeset
62 parentTranslation = parentTranslations[key];
b1ae6fce2e3f Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 73
diff changeset
63 }
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 return parentTranslation;
b1ae6fce2e3f Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 73
diff changeset
66 }
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 protected abstract AbstractTranslationSet GetParentTranslations();
72
cec6c4c0892d Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents: 69
diff changeset
69
cec6c4c0892d Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents: 69
diff changeset
70 public string LanguageName
cec6c4c0892d Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents: 69
diff changeset
71 {
cec6c4c0892d Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents: 69
diff changeset
72 get
cec6c4c0892d Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents: 69
diff changeset
73 {
73
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 72
diff changeset
74 return language.Name;
72
cec6c4c0892d Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents: 69
diff changeset
75 }
cec6c4c0892d Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents: 69
diff changeset
76 }
73
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 72
diff changeset
77
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 72
diff changeset
78 public TranslationLanguage Language
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 72
diff changeset
79 {
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 72
diff changeset
80 get { return language; }
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 72
diff changeset
81 }
72
cec6c4c0892d Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents: 69
diff changeset
82
75
b1ae6fce2e3f Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 73
diff changeset
83 public TranslationLanguage ParentLanguage
b1ae6fce2e3f Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 73
diff changeset
84 {
76
f45d28dc1d6a Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 75
diff changeset
85 get
f45d28dc1d6a Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 75
diff changeset
86 {
f45d28dc1d6a Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 75
diff changeset
87 TranslationLanguage parentLang = null;
f45d28dc1d6a Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 75
diff changeset
88 AbstractTranslationSet parentTranslations = GetParentTranslations();
f45d28dc1d6a Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 75
diff changeset
89
f45d28dc1d6a Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 75
diff changeset
90 if (parentTranslations != null)
f45d28dc1d6a Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 75
diff changeset
91 {
f45d28dc1d6a Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 75
diff changeset
92 parentLang = parentTranslations.Language;
f45d28dc1d6a Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 75
diff changeset
93 }
f45d28dc1d6a Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 75
diff changeset
94
f45d28dc1d6a Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 75
diff changeset
95 return parentLang;
f45d28dc1d6a Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 75
diff changeset
96 }
75
b1ae6fce2e3f Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 73
diff changeset
97 }
b1ae6fce2e3f Re #35: Add multi-level cascading of translations
IBBoard <dev@ibboard.co.uk>
parents: 73
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 public override bool Equals(object obj)
72
cec6c4c0892d Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents: 69
diff changeset
100 {
73
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 72
diff changeset
101 bool equal = true;
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 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
104 {
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 72
diff changeset
105 equal = false;
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 72
diff changeset
106 }
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 72
diff changeset
107 else
72
cec6c4c0892d Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents: 69
diff changeset
108 {
73
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 72
diff changeset
109 equal = LanguageCode.Equals(((AbstractTranslationSet)obj).LanguageCode);
72
cec6c4c0892d Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents: 69
diff changeset
110 }
73
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 72
diff changeset
111
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 72
diff changeset
112 return equal;
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 72
diff changeset
113 }
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 72
diff changeset
114
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 72
diff changeset
115 public override int GetHashCode ()
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 72
diff changeset
116 {
091bfa54d6c7 Re #33: Add method to get list of available translations
IBBoard <dev@ibboard.co.uk>
parents: 72
diff changeset
117 return GetType().GetHashCode() + LanguageCode.GetHashCode();
72
cec6c4c0892d Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents: 69
diff changeset
118 }
69
b5d7e8b93205 Re #30: Improve Translations API
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
119 }
b5d7e8b93205 Re #30: Improve Translations API
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
120 }