changeset 210:c587903f02a4

Implemented the button to toggle the current locale. flys-client/trunk@1644 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 05 Apr 2011 06:25:17 +0000
parents 9c8f450655c7
children b92281182c6b
files flys-client/ChangeLog flys-client/src/main/java/de/intevation/flys/client/client/ui/MainMenu.java
diffstat 2 files changed, 59 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/flys-client/ChangeLog	Tue Apr 05 06:24:13 2011 +0000
+++ b/flys-client/ChangeLog	Tue Apr 05 06:25:17 2011 +0000
@@ -1,3 +1,8 @@
+2011-04-05  Ingo Weinzierl <ingo@intevation.de>
+
+	* src/main/java/de/intevation/flys/client/client/ui/MainMenu.java:
+	  Implemented the toggle button to switch the current locale.
+
 2011-04-05  Ingo Weinzierl <ingo@intevation.de>
 
 	* src/main/java/de/intevation/flys/client/client/FLYSMessages_en.properties:
--- a/flys-client/src/main/java/de/intevation/flys/client/client/ui/MainMenu.java	Tue Apr 05 06:24:13 2011 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/client/ui/MainMenu.java	Tue Apr 05 06:25:17 2011 +0000
@@ -1,6 +1,8 @@
 package de.intevation.flys.client.client.ui;
 
 import com.google.gwt.core.client.GWT;
+import com.google.gwt.i18n.client.LocaleInfo;
+import com.google.gwt.user.client.Window;
 
 import com.smartgwt.client.types.Alignment;
 import com.smartgwt.client.widgets.Label;
@@ -87,8 +89,13 @@
 
         language.addClickHandler(new ClickHandler() {
             public void onClick(ClickEvent event) {
-                GWT.log("Clicked 'language' button.");
-                GWT.log("IMPLEMENT the 'switch language' function.");
+                LocaleInfo info      = LocaleInfo.getCurrentLocale();
+                String currentLocale = info.getLocaleName();
+                String newLocale     = currentLocale.equals("de") ? "en" : "de";
+
+                // XXX  Maybe we should inform the user about fact, that we will
+                // loose the work of the current session.
+                switchLanguage(currentLocale, newLocale);
             }
         });
 
@@ -198,5 +205,50 @@
     protected void createNewProject() {
         flys.newProject();
     }
+
+
+    /**
+     * This method triggers the language switch between the <i>currentLocale</i>
+     * and the <i>newLocale</i>. The switch is done by replacing a "locale="
+     * parameter in the url of the application. We could use the GWT UrlBuilder
+     * class to create a new URL, but - in my eyes - this class is a bit
+     * inconsistens in its implementation.
+     *
+     * @param currentLocale The current locale string (e.g. "en").
+     * @param newLocale The new locale string (e.g. "de").
+     */
+    protected void switchLanguage(String currentLocale, String newLocale) {
+        String newLocation = Window.Location.getHref();
+
+        if (newLocation.indexOf("&locale=") >= 0) {
+            String replace = currentLocale.equals("de")
+                ? "&locale=de"
+                : "&locale=en";
+
+            String replaceWith = newLocale.equals("de")
+                ? "&locale=de"
+                : "&locale=en";
+
+            newLocation = newLocation.replace(replace, replaceWith);
+        }
+        else if (newLocation.indexOf("?locale=") >= 0) {
+            String replace = currentLocale.equals("de")
+                ? "?locale=de"
+                : "?locale=en";
+
+            String replaceWith = currentLocale.equals("de")
+                ? "?locale=de"
+                : "?locale=en";
+
+            newLocation = newLocation.replace(replace, replaceWith);
+        }
+        else {
+            newLocation += newLocation.indexOf("?") >= 0
+                ? "&locale=" + newLocale
+                : "?locale=" + newLocale;
+        }
+
+        Window.open(newLocation, "_self", "");
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org