comparison mde/content/Items.d @ 110:6acd96f8685f

Translation reloading as far as AContent name/desc supported. Limited & crude support for updating gui. Gave AContent support for multiple callbacks. New locale: "en".
author Diggory Hardy <diggory.hardy@gmail.com>
date Fri, 05 Dec 2008 11:29:39 +0000
parents c9fc2d303178
children fe061009029d
comparison
equal deleted inserted replaced
109:2a1428ec5344 110:6acd96f8685f
33 } 33 }
34 } 34 }
35 35
36 /** Get a specific content item. 36 /** Get a specific content item.
37 * 37 *
38 * loadTranslation() $(B must) be called before this function.
39 *
38 * E.g. get ("Options.MiscOptions.L10n") returns miscOpts.L10n, 40 * E.g. get ("Options.MiscOptions.L10n") returns miscOpts.L10n,
39 * Items.get ("Options.MiscOptions") returns a ContentList of all misc options. */ 41 * Items.get ("Options.MiscOptions") returns a ContentList of all misc options. */
40 AContent get (char[] item) { 42 AContent get (char[] item) {
43 assert (currentL10n is miscOpts.L10n(), "must call loadTranslation (code error)");
44
41 char[] h = head (item); 45 char[] h = head (item);
42 if (h == "Options") { 46 if (h == "Options") {
43 if (item is null) { 47 if (item is null)
44 if (Options.allContentList is null) {
45 AContent[] list;
46 list.length = Options.optionsClasses.length;
47 size_t i;
48 foreach (n,opts; Options.optionsClasses) {
49 if (opts.contentList is null)
50 loadTransl (opts, n);
51 list[i++] = opts.contentList;
52 }
53 Options.allContentList = new ContentList (h, list);
54 Translation trl = Translation.get (h);
55 Translation.Entry trle = trl.getStruct (h);
56 Options.allContentList.name (trle.name, trle.desc);
57 }
58 return Options.allContentList; 48 return Options.allContentList;
59 } 49
60 h = head (item); 50 h = head (item);
61 auto p = h in Options.optionsClasses; 51 auto p = h in Options.optionsClasses;
62 if (p) { 52 if (p) {
63 if (p.contentList is null)
64 loadTransl (*p, h);
65
66 if (item == null) 53 if (item == null)
67 return p.contentList; 54 return p.contentList;
68 55
69 auto q = (h = head (item)) in p.content; 56 auto q = (h = head (item)) in p.content;
70 if (q && item is null) // enforce item is an exact match 57 if (q && item is null) // enforce item is an exact match
71 return *q; 58 return *q;
72 } 59 }
73 } else if (h == "imde") { 60 } else if (h == "imde") {
74 if (!imdeTransl) { 61 h = head (item);
75 Translation trl = Translation.get (h); 62 if (h == "quit" && item is null)
76 Translation.Entry trle = trl.getStruct ("quit"); 63 return quit;
77 quit.name (trle.name, trle.desc); 64 }
78 imdeTransl = true; 65 throw new ContentItemException (h);
66 }
67
68 /** Creates some content on first run (required by get()).
69 *
70 * If the correct translation strings are not loaded, this loads them. */
71 void loadTranslation () {
72 if (currentL10n is miscOpts.L10n()) return;
73
74 // Create Option classes' ContentLists if necessary:
75 if (Options.allContentList is null) {
76 AContent[] list;
77 list.length = Options.optionsClasses.length;
78 size_t i;
79 foreach (n,opts; Options.optionsClasses) {
80 opts.contentList = new ContentList (n, opts.content);
81 list[i++] = opts.contentList;
79 } 82 }
80 h = head (item); 83 Options.allContentList = new ContentList ("Options", list);
81 if (h == "quit" && item is null) { 84 }
82 quit.name ("Quit"); //FIXME 85
83 return quit; 86 // Translate Options:
87 Translation.Entry trle;
88 with (Options.allContentList) {
89 trle = Translation.get (symbol).getStruct (symbol);
90 name (trle.name, trle.desc);
91 }
92 foreach (n,opts; Options.optionsClasses) {
93 Translation trl;
94 trl = Translation.get (n);
95 trle = trl.getStruct (n);
96 opts.contentList.name (trle.name, trle.desc);
97 foreach (s, v; opts.content) {
98 trle = trl.getStruct (s);
99 v.name (trle.name, trle.desc);
84 } 100 }
85 } 101 }
86 throw new ContentItemException (h); 102
103 // Translate imde:
104 trle = Translation.get ("imde").getStruct ("quit");
105 quit.name (trle.name, trle.desc);
106
107 currentL10n = miscOpts.L10n();
87 } 108 }
88 109
89 private: 110 private:
90 /** Takes the string "head.tail" where tail may contain '.' but head does not, returns "head", 111 /** Takes the string "head.tail" where tail may contain '.' but head does not, returns "head",
91 * with str set to "tail". */ 112 * with str set to "tail". */
99 else 120 else
100 str = str[i+1..$]; 121 str = str[i+1..$];
101 return ret; 122 return ret;
102 } 123 }
103 124
104 void loadTransl (Options p, char[] n) { 125 char[] currentL10n; // Strings will be reloaded if this is not miscOpts.L10n().
105 debug logger.trace ("Loading translation strings for Options."~n);
106 Translation trans = Translation.get (n);
107 Translation.Entry transled = trans.getStruct (n);
108 p.contentList = new ContentList (n, p.content);
109 p.contentList.name (transled.name, transled.desc);
110 foreach (s, v; p.content) {
111 transled = trans.getStruct (s);
112 v.name (transled.name, transled.desc);
113 }
114 }
115
116 bool imdeTransl = false; // Has section imde been translated?