Mercurial > repos > IBBoard
annotate Lang/AbstractTranslationSet.cs @ 72:cec6c4c0892d
Fixes #34: Get name for translation
* Add code for translation name fetching (using native name)
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Tue, 06 Apr 2010 18:20:14 +0000 |
parents | b5d7e8b93205 |
children | 091bfa54d6c7 |
rev | line source |
---|---|
69 | 1 // This file (AbstractTranslationSet.cs) is a part of the IBBoard project and is copyright 2010 IBBoard |
2 // | |
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. | |
4 | |
5 using System; | |
6 using System.Collections.Generic; | |
72
cec6c4c0892d
Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
7 using System.Globalization; |
69 | 8 |
9 namespace IBBoard.Lang | |
10 { | |
11 /// <summary> | |
12 /// A collection of translations for a given language. The abstract class must be extended by implementations that | |
13 /// provide different ways of loading the data. | |
14 /// </summary> | |
15 public class AbstractTranslationSet | |
16 { | |
17 private string langCode; | |
72
cec6c4c0892d
Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
18 private string langName; |
69 | 19 protected Dictionary<string, string> translations; |
20 | |
21 public AbstractTranslationSet(string languageCode) | |
22 { | |
23 langCode = languageCode; | |
24 translations = new Dictionary<string, string>(); | |
25 } | |
26 | |
27 /// <summary> | |
28 /// Gets the language code that this translation claims to be for | |
29 /// </summary> | |
30 public string LanguageCode | |
31 { | |
32 get { return langCode; } | |
33 } | |
34 | |
35 /// <summary> | |
36 /// Gets a translation from the translation set, or <code>null</code> if the translation doesn't exist. | |
37 /// </summary> | |
38 /// <param name="key"> | |
39 /// The key (ID) of the translation to retrieve | |
40 /// </param> | |
41 public string this[string key] | |
42 { | |
43 set | |
44 { | |
45 translations[key] = value; | |
46 } | |
47 get | |
48 { | |
49 return DictionaryUtils.GetValue(translations, key); | |
50 } | |
72
cec6c4c0892d
Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
51 } |
cec6c4c0892d
Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
52 |
cec6c4c0892d
Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
53 public string LanguageName |
cec6c4c0892d
Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
54 { |
cec6c4c0892d
Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
55 get |
cec6c4c0892d
Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
56 { |
cec6c4c0892d
Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
57 if (langName == null) |
cec6c4c0892d
Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
58 { |
cec6c4c0892d
Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
59 LoadLangName(); |
cec6c4c0892d
Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
60 } |
cec6c4c0892d
Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
61 |
cec6c4c0892d
Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
62 return langName; |
cec6c4c0892d
Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
63 } |
cec6c4c0892d
Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
64 } |
cec6c4c0892d
Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
65 |
cec6c4c0892d
Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
66 private void LoadLangName() |
cec6c4c0892d
Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
67 { |
cec6c4c0892d
Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
68 try |
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 CultureInfo culture = CultureInfo.GetCultureInfo(langCode); |
cec6c4c0892d
Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
71 langName = culture.NativeName; |
cec6c4c0892d
Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
72 } |
cec6c4c0892d
Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
73 catch(ArgumentException) |
cec6c4c0892d
Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
74 { |
cec6c4c0892d
Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
75 langName = "Unknown ("+langCode+")"; |
cec6c4c0892d
Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
76 } |
cec6c4c0892d
Fixes #34: Get name for translation
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
77 } |
69 | 78 } |
79 } |