LearnGWT.com

RPC
Automatic GWT Internationalization detection
By mdamour1976 | Published on Friday January 04, 2008 | Categories: Internationalization

In any documentation I have seen to date, the language a GWT application will use is either based on the locale "query string" or "meta" tag in the HTML document.  While this gives us the ability to switch locales by changing either of these, it is not automatically picked up without the user designating their preference.

I personally don't really like having the language in the query string, many times my URL's are heavily modified by Apache mod_rewrite and I'd rather not add extra things in there.  So for me, the best bet is to use the "meta" tag.  This is very easy to do, just add this into your HTML:

<HTML>
    <HEAD>
           <META name="gwt:property" content="locale=en">
    </HEAD>
...

Now, the GWT application will use English by default, if the meta tag specified "locale=fr" then we'd be seeing French.  The way to make this automatic and dynamic is to use something like JSP or PHP to dish out your pages and build the meta tag.  If you currently have static HTML pages you could quickly create a JSP or PHP page out of it and only modify the meta section. 

All modern web-browsers send to the server a list of preferred languages that the client would like to see content.  I've seen this used for quite a few years.  When I did not see this sort of thing being done with GWT, I came up with this idea.

In the JSP, figure out the users preferred language, this is incredibly simple:

<meta name="gwt:property" content="locale=<%=request.getLocale()%>">

I can switch my browser settings to English or French and the application automatically uses the correct language!  If the language is not supported, then I get the default (English).

You can get more sophisticated by trying to use the "Accept-Language" header field and pick the locale best suited for the user.  For example they might prefer French, then Spanish and then English, in that order.  If we do not have French, then we'd want to "fall back" to Spanish according to their preference if we can, rather than go straight to English if we don't support their top choice.