diff 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
line wrap: on
line diff
--- a/mde/content/Items.d	Thu Dec 04 10:32:20 2008 +0000
+++ b/mde/content/Items.d	Fri Dec 05 11:29:39 2008 +0000
@@ -35,34 +35,21 @@
 
     /** Get a specific content item.
      *
+     * loadTranslation() $(B must) be called before this function.
+     *
      * E.g. get ("Options.MiscOptions.L10n") returns miscOpts.L10n,
      * Items.get ("Options.MiscOptions") returns a ContentList of all misc options. */
     AContent get (char[] item) {
+	assert (currentL10n is miscOpts.L10n(), "must call loadTranslation (code error)");
+	
 	char[] h = head (item);
 	if (h == "Options") {
-	    if (item is null) {
-		if (Options.allContentList is null) {
-		    AContent[] list;
-		    list.length = Options.optionsClasses.length;
-		    size_t i;
-		    foreach (n,opts; Options.optionsClasses) {
-			if (opts.contentList is null)
-			    loadTransl (opts, n);
-			list[i++] = opts.contentList;
-		    }
-		    Options.allContentList = new ContentList (h, list);
-		    Translation trl = Translation.get (h);
-		    Translation.Entry trle = trl.getStruct (h);
-		    Options.allContentList.name (trle.name, trle.desc);
-		}
+	    if (item is null)
 		return Options.allContentList;
-	    }
+	    
 	    h = head (item);
 	    auto p = h in Options.optionsClasses;
 	    if (p) {
-		if (p.contentList is null)
-		    loadTransl (*p, h);
-		
 		if (item == null)
 		    return p.contentList;
 		
@@ -71,19 +58,53 @@
 		    return *q;
 	    }
 	} else if (h == "imde") {
-	    if (!imdeTransl) {
-		Translation trl = Translation.get (h);
-		Translation.Entry trle = trl.getStruct ("quit");
-		quit.name (trle.name, trle.desc);
-		imdeTransl = true;
+	    h = head (item);
+	    if (h == "quit" && item is null)
+		return quit;
+	}
+	throw new ContentItemException (h);
+    }
+    
+    /** Creates some content on first run (required by get()).
+     *
+     * If the correct translation strings are not loaded, this loads them. */
+    void loadTranslation () {
+	if (currentL10n is miscOpts.L10n()) return;
+	
+	// Create Option classes' ContentLists if necessary:
+	if (Options.allContentList is null) {
+	    AContent[] list;
+	    list.length = Options.optionsClasses.length;
+	    size_t i;
+	    foreach (n,opts; Options.optionsClasses) {
+		opts.contentList = new ContentList (n, opts.content);
+		list[i++] = opts.contentList;
 	    }
-	    h = head (item);
-	    if (h == "quit" && item is null) {
-		quit.name ("Quit");	//FIXME
-		return quit;
+	    Options.allContentList = new ContentList ("Options", list);
+	}
+	
+	// Translate Options:
+	Translation.Entry trle;
+	with (Options.allContentList) {
+	    trle = Translation.get (symbol).getStruct (symbol);
+	    name (trle.name, trle.desc);
+	}
+	foreach (n,opts; Options.optionsClasses) {
+	    Translation trl;
+	    trl = Translation.get (n);
+	    trle = trl.getStruct (n);
+	    opts.contentList.name (trle.name, trle.desc);
+	    foreach (s, v; opts.content) {
+		trle = trl.getStruct (s);
+		v.name (trle.name, trle.desc);
 	    }
 	}
-	throw new ContentItemException (h);
+	
+	// Translate imde:
+	trle = Translation.get ("imde").getStruct ("quit");
+	quit.name (trle.name, trle.desc);
+	
+	currentL10n = miscOpts.L10n();
     }
     
 private:
@@ -101,16 +122,4 @@
 	return ret;
     }
     
-    void loadTransl (Options p, char[] n) {
-	debug logger.trace ("Loading translation strings for Options."~n);
-	Translation trans = Translation.get (n);
-	Translation.Entry transled = trans.getStruct (n);
-	p.contentList = new ContentList (n, p.content);
-	p.contentList.name (transled.name, transled.desc);
-	foreach (s, v; p.content) {
-	    transled = trans.getStruct (s);
-	    v.name (transled.name, transled.desc);
-	}
-    }
-    
-    bool imdeTransl = false;	// Has section imde been translated?
+    char[] currentL10n;	// Strings will be reloaded if this is not miscOpts.L10n().