how to show the locale that the browser is showing in gwt I18N?

748 Views Asked by At

i'm doing i18n to my application i did something that is shown in showcase example i'm using the listbox to show different locales, the problem is when i'm swapping from one locale to another locale,locale is changing but the list box is not showing the locale in which i am, it is showing first element of the list every time as it is reloading

here is code:

@Override
    public void changeSwap() {

    final String queryParam = LocaleInfo.getLocaleQueryParam(); 
    String locale=getView().getLocale().getValue(getView().getLocale().getSelectedIndex());
        Log.info("revealed locale is"+locale);
     if (queryParam != null) {
         UrlBuilder builder = Location.createUrlBuilder().setParameter(
             queryParam, locale);
         Window.Location.replace(builder.buildString());
       } else {
         // If we are using only cookies, just reload
         Window.Location.reload();
       }
    }

i didn't find any method in listbox api to do it

thanks

1

There are 1 best solutions below

7
On BEST ANSWER

You're using a part of the code from the Showcase (ShowcaseShell.initializeLocaleBox()), but the part that is responsible for selecting the correct value in the ListBox is this:

String currentLocale = LocaleInfo.getCurrentLocale().getLocaleName();
if (currentLocale.equals("default")) {
  currentLocale = "en";
}
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);
    }
  }
}

Make sure to add it to your code before adding the ChangeHandler to the localeBox.


Here's the full setup in the Showcase 2.4.0 example:

I would recommend to create a simple new GWT project (at first without gwtp or anything special) and just add these bits of code. Then you can try to migrate the setup to your full project.