Ignacio has posted 8 posts at DZone. View Full User Profile

URL-based Locale

08.06.2010
| 5442 views |
  • submit to reddit

We should assume that users will not always be browsing from a comfortable location (pronounce: home / work). Even if they do, every now and then I have guests at home that do not speak Spanish at all. Try opening the Google home page in a cybercafé in Germany or Finland to see what I mean. It may be convenient, but the browser language is often not good enough for the real world.



If your application supports multiple languages you should give the user an option to change the locale without messing with the browser settings. Specifically, don't force the user to find the settings in a foreign language and foreign browser, and don't assume that he/she is allowed to change them.
There are a couple of possible implementations for this:

  • Store the locale in the user session: this solution does not persist the locale during browser restarts and implies an existing session for each anonymous user, which is not always an option.
  • Store the locale in the user settings (database): this is possible only if you do not have anonymous users.
  • Store the locale in a browser cookie.
  • Store the locale in the URL
Of these options only the last three have reasonable quality, only the last two work for public websites, and only the last one works for web crawlers. Google recommends to use locale-aware DNS names, which is lingo speak to put your locale somewhere in your DNS name. If you are reading this sitting comfortably on your big heap of money, you may start registering "myhost.es" and "myhost.de", but the rest of us will go with "es.myhost.com" and "de.myhost.com".

Implementing this in your own application is easy: Loom has PrefixLocaleResolver, but with other frameworks you may just use your own web Filter to resolve the user locale as well. Naïve version follows:
public class LocaleResolverFilter implements Filter {

@Override
public void doFilter(ServletRequest req, ServletResponse response, FilterChain chain)
throws IOException, ServletException {

HttpServletRequest request = (HttpServletRequest) req;
String serverName = request.getServerName();
String lang = StringUtils.substringBefore(serverName, ".");
if (lang.length() != 2) {
lang = "en";
}

final Locale locale = new Locale(lang);
chain.doFilter(new HttpServletRequestWrapper(request) {

@Override
public Locale getLocale() {
return locale;
}

}, response);
}

@Override
public void destroy() {
// empty
}

@Override
public void init(FilterConfig filterConfig) throws ServletException {
// empty
}
}

In order to test this, you should add this entry to your hosts file:

127.0.0.1 localhost es.localhost en.localhost www.localhost

Me, I find it funny to think about my "English localhost" :) If you have any interesting ways of detecting your user language, I would love to hear it!

From http://icoloma.blogspot.com/2010/08/url-based-locale.html

Published at DZone with permission of its author, Ignacio Coloma.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Jilles Van Gurp replied on Fri, 2010/08/06 - 1:09am

Having lived abroad in Sweden, Finland, and recently Germany, this is one of my pet peeves. Particularly when it comes to IP based localization.

 At work we are behind a proxy that is located in Amsterdam so funnily enough my colleagues always complain about Google being localized to my native language (Dutch).

It seems web application developers are really proud of themselves when they figure out the user's locale all by themselves and are able to present the UI in Finnish instead of English.

Except ... about 10% of the population in Finland is Swedish speaking and at any time there are tens of thousands of foreigners in the country who speak neither Finnish nor Swedish. Add to that that Finnish is one of the most alien languages around on this planet (small group of native speakers, doesn't really resemble anything else you might have mastered) and you have a usability problem waiting to happen. 

Blogger does this. Luckily in German these days but the site was near unusable when I was living in Finland. It doesn't matter how you get to the site. It doesn't matter that you are logged on to gmail and have your locale set to US_en. It will default to whatever is the dominant language where your ip address is. The preference to change the locale is well hidden two or three clicks from the homepage. Near impossible to find if you don't speak the language the site happens to be localized in.

Figuring out the default locale is fine but give your users an obvious way out and beware that you are going to get it completely wrong quite often. Like 1 in 10 or even more often.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.