comparison mde/lookup/Translation.d @ 74:cee261eba249

Minor tweaks.
author Diggory Hardy <diggory.hardy@gmail.com>
date Mon, 07 Jul 2008 15:54:47 +0100
parents 7fc0a8295c83
children 25cb7420dc91
comparison
equal deleted inserted replaced
73:08d3b6bcf891 74:cee261eba249
35 * is out of date. A tool should be made for checking for out of date entries to take advantage of 35 * is out of date. A tool should be made for checking for out of date entries to take advantage of
36 * this feature. Of course, out of date entries are still valid for use. 36 * this feature. Of course, out of date entries are still valid for use.
37 */ 37 */
38 module mde.lookup.Translation; 38 module mde.lookup.Translation;
39 39
40 import mde.options; 40 import mde.lookup.Options;
41 import mde.resource.paths; 41 import mde.setup.paths;
42 import mde.exception; 42 import mde.exception;
43 43
44 import mde.mergetag.DataSet; 44 import mde.mergetag.DataSet;
45 import mde.mergetag.Reader; 45 import mde.mergetag.Reader;
46 import mde.mergetag.exception; 46 import mde.mergetag.exception;
57 class Translation : IDataSection 57 class Translation : IDataSection
58 { 58 {
59 final char[] name; /// The module/package/... which the instance is for 59 final char[] name; /// The module/package/... which the instance is for
60 final char[] L10n; /// The localization loaded (e.g. en-GB) 60 final char[] L10n; /// The localization loaded (e.g. en-GB)
61 61
62 /** Get the translation for the given identifier, and optionally the description. 62 alias entry opCall; /// Convenience alias
63 * If no entry exists, the identifier will be returned. */ 63
64 char[] getEntry (char[] id, out char[] description) { 64 /** Get the translation for the given identifier.
65 * If no entry exists, the identifier will be returned.
66 *
67 * Optionally, the description can be returned. */
68 char[] entry (char[] id) {
65 Entry* p = id in entries; 69 Entry* p = id in entries;
66 if (p) { // FIXME: check: a SEGFAULT? 70 if (p) {
71 return p.str;
72 } else {
73 return id;
74 }
75 }
76 /** ditto */
77 char[] entry (char[] id, out char[] description) {
78 Entry* p = id in entries;
79 if (p) {
67 description = p.desc; 80 description = p.desc;
68 return p.str; 81 return p.str;
69 } else 82 } else {
70 return id; 83 return id;
71 } 84 }
72 /** ditto */ 85 }
73 char[] getEntry (char[] id) { 86
87 /** Alternative usage: return a Translation.Entry struct. */
88 Entry getStruct (char[] id) {
74 Entry* p = id in entries; 89 Entry* p = id in entries;
75 if (p !is null) { // FIXME: check: a SEGFAULT? 90 if (p) {
76 return p.str; 91 return *p;
77 } else 92 } else {
78 return id; 93 Entry ret;
94 ret.str = id;
95 return ret;
96 }
79 } 97 }
80 98
81 /** Load the translation for the requested module/package/... 99 /** Load the translation for the requested module/package/...
82 * 100 *
83 * Options (mde.options) must have been loaded before this is run. 101 * Options (mde.options) must have been loaded before this is run.
172 } 190 }
173 191
174 // This class is read-only and has no need of being saved. 192 // This class is read-only and has no need of being saved.
175 void writeAll (ItemDelg) {} 193 void writeAll (ItemDelg) {}
176 194
195 /** This struct is used to store each translation entry.
196 *
197 * Note that although each entry also has a version field, this is not loaded for general use.
198 */
199 struct Entry {
200 char[] str; // The translated string
201 char[] desc; // An optional description
202 }
203
177 private: 204 private:
178 /* Sets name and L10n. 205 /* Sets name and L10n.
179 * 206 *
180 * Also ensures only load() can create instances. */ 207 * Also ensures only load() can create instances. */
181 this (char[] n, char[] l) { 208 this (char[] n, char[] l) {
183 L10n = l; 210 L10n = l;
184 } 211 }
185 212
186 //BEGIN Data 213 //BEGIN Data
187 static Logger logger; 214 static Logger logger;
188
189 /* This struct is used to store each translation entry.
190 *
191 * Note that although each entry also has a version field, this is not loaded for general use.
192 */
193 struct Entry {
194 char[] str; // The translated string
195 char[] desc; // An optional description
196 }
197 215
198 Entry[char[]] entries; // all entries 216 Entry[char[]] entries; // all entries
199 217
200 ID[] depends; // dependancy sections (only used while loading) 218 ID[] depends; // dependancy sections (only used while loading)
201 //END Data 219 //END Data
219 miscOpts.L10n = "test-1"; 237 miscOpts.L10n = "test-1";
220 238
221 Translation transl = load ("i18nUnitTest"); 239 Translation transl = load ("i18nUnitTest");
222 240
223 // Simple get-string, check dependancy's entry doesn't override 241 // Simple get-string, check dependancy's entry doesn't override
224 assert (transl.getEntry ("Str1") == "Test 1"); 242 assert (transl.entry ("Str1") == "Test 1");
225 243
226 // Entry included from dependancy with description 244 // Entry included from dependancy with description
227 char[] desc; 245 char[] desc;
228 assert (transl.getEntry ("Str2", desc) == "Test 3"); 246 assert (transl.entry ("Str2", desc) == "Test 3");
229 assert (desc == "Description"); 247 assert (desc == "Description");
230 248
231 // No entry: fallback to identifier string 249 // No entry: fallback to identifier string
232 assert (transl.getEntry ("Str3") == "Str3"); 250 assert (transl.entry ("Str3") == "Str3");
233 251
234 // No checks for version info since it's not functionality of this module. 252 // No checks for version info since it's not functionality of this module.
235 // Only check extra entries are allowed but ignored. 253 // Only check extra entries are allowed but ignored.
236 254
237 // Restore 255 // Restore