How to access “locale” value (of an Internationalization App) in server side in GWT?

StackOverflow https://stackoverflow.com//questions/25009115

  •  20-12-2019
  •  | 
  •  

Question

Ok, I am developing an app that supports Internationalization. I am using Static String Internationalization method for my app.

Let say my app can have English & German version, so -1st, I created MyMessages.java which has all message texts written in English. Then I created an MyMessages_ge which is almost the same as MyMessages.java except that the text written in German

-2nd, in MyProject.html i have <meta http-equiv="content-type" content="text/html; charset=UTF-8"> to make sure it will support all languages

-3rd, in MyProject.gwt.xml I have <extend-property name="locale" values="ge"/>

Now I run my app in eclipse http://localhost:8888/MyProject.html?gwt.codesvr=127.0.0.1:9997&locale=ge & it shows all the German text as I expected.

However, I have a need to know that my app is German At Server Side. That means whenever my GWT Gui in German version then my server will know that languageCode=1 if it is in English so server will use the default language languageCode=0

Here is what I did. At HeaderPresenter.java

@Override
public void prepareFromRequest(PlaceRequest request){
    super.prepareFromRequest(request);
    String locale=request.getParameter("locale", "");
    setLanguage(locale); // servercall back here
}

so when my app run first time it should let server to know which language the Gwt app is using

@Override
public SetLanguageResult execute(SetLanguage action, ExecutionContext context)
        throws ActionException {
    String locale=action.getLocale();
    if("ge".equals(locale)){
        Data.languageType=1;
    }
    return null;
}

However, it doesn't seem work When I go to orderPage the url will be http://localhost:8888/MyProject.html?gwt.codesvr=127.0.0.1:9997&locale=ge#!orderPage it not in the form http://localhost:8888/MyProject.html?gwt.codesvr=127.0.0.1:9997#!orderPage;locale=ge that is why server couldn't capture the locale value.

I think I misunderstood something here. I'm quite confusing.

So How to access "locale" value (of an Internationalization App) in server side in GWT?

Was it helpful?

Solution

There is a utility class to deal with locale information in the GWT SDK. No need to parse URL parameters yourself. Have a look at LocaleInfo.getCurrentLocale() then use the getLocaleName() method to get the actual code as a String.

I have not quite understand how you store the locale information on the server side? Mind that servlets are shared between users, you have to attach the local to the session or pass it with every call.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top