lundi 5 janvier 2009

GWT L10N : Accept-language auto-detection completed ! (Server side locale selection)

When you develop a web application, you may want let the user choose his language to show all the message with the user locale.

I searched on google and found different solution, here is the one we choose for GWT 1.5.3 (in 1.6 we will change our mind).

If you want to set the locale, you can add this line to yout HTML page:
<meta name="gwt:property" content="locale=en"/>
So if you want to find the user locale, juste change yout HTML file to JSP and replace the previous line with this block:
<meta name="gwt:property" content="locale=<%List<String> list = new ArrayList<String>();
LocaleMatcher localeMatcher = new LocaleMatcher(list);
String locale = localeMatcher.findBestMatch(request.getHeader("Accept-language"));
We just use a GWT 1.6 incubation class LocaleMatcher and we authorized two locale "fr" and "en" only. This code will use the favorite locale of the user that has been set in the browser configuration.

We don't need to implement a special UI to manage the user locale, we use the browser feature.

Of course you must enable the locale in the module XML file too !!!
<extend-property name="locale" values="en"/>
Here we choose to activate the possible locale by code with an Arraylist of the locale we accept to manage, but you can also use another GWT incubation class that scroll you module XML file to find the locale used by your module. That will probably a native feature of GWT 1.6 so wait and see in a few weeks (or months).