1

I'm developping a project with SmartGWT and I want to make it international,but how to know the browser's language in my EntryPoint (onModuleLoad) ? I'm using Spring in the Server Side, and in my onModuleLoad I'm sending an RPC call to my Service which is gathering data from properties Files and respond with a Map contains all my keys value for internationalization, so while creating IU Widgets I'm using my Map like this lables.get("myLabel"). now that everytings is working fine, I want to detect the browser's language and use it for querying the right properties. Sorry if my english is so bad

Slifer
  • 175
  • 1
  • 1
  • 10

3 Answers3

1
com.google.gwt.i18n.client.LocaleInfo#getCurrentLocale()
Arnaud Denoyelle
  • 29,980
  • 16
  • 92
  • 148
  • I've already tried this but thats return always "default" as string – Slifer Aug 23 '13 at 16:01
  • @Slifer Have you tried `com.google.gwt.i18n.client.LocaleInfo.getCurrentLocale().getLocaleName()`? – Sithsu Aug 23 '13 at 21:54
  • yes I've already tried it, it returns always "default", even if I put the tag and put properties in the *.gwt.xml – Slifer Aug 24 '13 at 12:33
1

then I ended with a dirty solution, I created a javascript function as Lt_Shade told me,which put the navigator.language, then I retreive it in my EntryPoint thanks to the Document object, the probleme was when I run with IE or FireFox, the EntryPoint run before my HTML Page, so before my Javascript function and I dont find my navigator.language, so what I did is to create a JSNI function which call my javascript object within my EntryPoint (Calling Javascript function from java:gwt code) so I emphasize and insist the call then I'l sure that my navigator.language is loaded and I can retreive it.

in my HTML page

....
<script language="javascript">
    function loadLocaleLanguage(){
        document.getElementById("localeLanguage").setAttribute("value",(navigator.language).substring(0,2); // I don't need Country code
    }
</script>
...
<input type="hidden" id="localeLanguage" value="" />
....

in my EntryPoint

public static native void getLocaleLanguage() /*-{
      $wnd.loadLocaleLanguage();
}-*/;

public void onModuleLoad(){
    // I call my javascript function to ensure that the input have my localeLanguage
    getLocaleLanguage();
    // now I'm retreiving it using DOM thanks to Document object
    String localeLanguage = Document.get().getElementById("localeLanguage").getAttribute("value");
    ....
}

I'm sure that's not the best solution but at least it works if someone can help me find how to acces the httpRequest within the EntryPoint (it containts the accept-language) or to directly acces the navigator.language in the EntryPoint, it'll be better. thanks guys

Slifer
  • 175
  • 1
  • 1
  • 10
  • hi guys, I always about internationalizing my applicaiton, now I want to make all my widgets international, I asked a question here, http://stackoverflow.com/questions/18586500/ please I need you help – Slifer Sep 03 '13 at 07:31
  • Have you ever found a "clean" solution? I am also wondering how I can get the browser language here .. – Stefan Falk Feb 27 '16 at 09:01
0

I am not sure if this helps but there is a question like this which includes some java code for getting the language in a Java servlet.

Automatically selecting country and language for user in Java Servlet

You could also try add a JavaScript function into your EntryPoint something like

var userLang = (navigator.language) ? navigator.language : navigator.userLanguage; 
alert ("The language is: " + userLang);
Community
  • 1
  • 1
Lt_Shade
  • 590
  • 1
  • 7
  • 18
  • it'll be nice if I can do the the same but in my GWT EntryPoint, I don't have (or I don't know how to) access to the Request. – Slifer Aug 23 '13 at 11:31
  • You could try add a JavaScript function into your EntryPoint something like `var userLang = (navigator.language) ? navigator.language : navigator.userLanguage; alert ("The language is: " + userLang);` – Lt_Shade Aug 23 '13 at 12:24
  • yeah, it's not proper but its works, I added a hidden input with the value of navigator.language, then I'm retreiving this value in my EntryPoint with Document.getElementById("localeLangauge").getAttribute("value"). thanks for the hint – Slifer Aug 23 '13 at 12:40
  • euuh, that's not working very well, I have a probleme with FireFox and IE10, because the EntryPoint is loaded before the HTML page. in the I make it to put navigator.language in the value of my hidden input, the probleme is that with firefox and IE, the entryPoint is loading before the HTML page, then onload hasnt been excuted yet, and the value returned is an empty String... what is strange is that with Chrome seems to work, and my EntryPoint has the right value ... – Slifer Aug 23 '13 at 13:53