0
votes

I am using gwt 2.5.1. I have a small requirement in my project. We support localization in our project. If the user enters invalid locale (ie.., other than supported languages) we are displaying in english language by default. So, inorder to get the locale parameter from the url, I am using the method com.google.gwt.user.client.Window.Location.getParameterMap(). If the user enters '%' special character as a locale parameter, the getParameterMap() is throwing exception which is :

com.google.gwt.core.client.JavaScriptException: (URIError) @com.google.gwt.http.client.URL::decodeQueryStringImpl(Ljava/lang/String;)([string: '%']): malformed URI sequence
    at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:249)
    at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:136)
    at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:571)
    at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:279)
    at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91)
    at com.google.gwt.http.client.URL.decodeQueryStringImpl(URL.java)
    at com.google.gwt.http.client.URL.decodeQueryString(URL.java:117)
    at com.google.gwt.user.client.Window$Location.buildListParamMap(Window.java:310)
    at com.google.gwt.user.client.Window$Location.ensureListParameterMap(Window.java:327)
    at com.google.gwt.user.client.Window$Location.getParameterMap(Window.java:230)
    at srdm.cloud.client.sso.WebUISession.checkSession(WebUISession.java:283)
    at srdm.cloud.client.WebUI.onModuleLoad(WebUI.java:79)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.google.gwt.dev.shell.ModuleSpace.onLoad(ModuleSpace.java:406)
    at com.google.gwt.dev.shell.OophmSessionHandler.loadModule(OophmSessionHandler.java:200)
    at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:526)
    at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:364)
    at java.lang.Thread.run(Thread.java:744)

So, Now how can I get the locale parameter from the url, by ignoring the special characters?

1

1 Answers

0
votes

I would like to suggest to read and use GWT Internationalization and i18n Messages for your GWT project localization. Add below locale declarations in your module.xml file

<extend-property name="locale" values="en"/>
<extend-property name="locale" values="ja"/>
<!-- below is default -->
<set-property-fallback name="locale" value="ja"/>

(en for English and ja for Japan)

How to get avilable locales of clients ? How to get current local ? How to save client's prefer locale ? Below is snippet for initializing local listbox. Expand as you needed.

public static void initializeLocaleBox(final ListBox localeBox) {
    String currentLocale = LocaleInfo.getCurrentLocale().getLocaleName();
    if (currentLocale.equals("default")) {
        currentLocale = "ja";
    }
    String[] localeNames = LocaleInfo.getAvailableLocaleNames();
    for (String localeName : localeNames) {
        if (!localeName.equals("default")) {
            String nativeName = LocaleInfo.getLocaleNativeDisplayName(localeName);
            localeBox.addItem(nativeName, localeName);
            if (localeName.equals(currentLocale)) {
                localeBox.setSelectedIndex(localeBox.getItemCount() - 1);
            }
        }
    }
    localeBox.addChangeHandler(new ChangeHandler() {
        public void onChange(final ChangeEvent event) {
            String localeName = localeBox.getValue(localeBox.getSelectedIndex());

            Date now = new Date();
            long sevenDays = now.getTime();
            // seven days
            sevenDays = sevenDays + (1000 * 60 * 60 * 24 * 7);
            now.setTime(sevenDays);
            Cookies.setCookie("locale", localeName, now);
            UrlBuilder builder = Location.createUrlBuilder().setParameter("locale", localeName);
            Window.Location.replace(builder.buildString());
        }
    });
}

To check client's locale

public static boolean isLocaleJapan() {
    String currentLocale = LocaleInfo.getCurrentLocale().getLocaleName();
    return currentLocale != null && currentLocale.equals("ja") ? true : false;
}
public static boolean isLocaleEnglish() {
    String currentLocale = LocaleInfo.getCurrentLocale().getLocaleName();
    return currentLocale != null && currentLocale.equals("en") ? true : false;
}