# HG changeset patch # User Ingo Weinzierl # Date 1301984717 0 # Node ID c587903f02a47f5c99a8c5caf8153ea63b9ca196 # Parent 9c8f450655c79b92d9eee8c56da9be1606d8f8fb Implemented the button to toggle the current locale. flys-client/trunk@1644 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r 9c8f450655c7 -r c587903f02a4 flys-client/ChangeLog --- 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 + + * 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 * src/main/java/de/intevation/flys/client/client/FLYSMessages_en.properties: diff -r 9c8f450655c7 -r c587903f02a4 flys-client/src/main/java/de/intevation/flys/client/client/ui/MainMenu.java --- 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 currentLocale + * and the newLocale. 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 :