Over a million developers have joined DZone.
Platinum Partner

URL-based Locale

· Java Zone

The Java Zone is brought to you in partnership with ZeroTurnaround. Discover how you can skip the build and redeploy process by using JRebel by ZeroTurnaround.

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 {

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) {

public Locale getLocale() {
return locale;

}, response);

public void destroy() {
// empty

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

In order to test this, you should add this entry to your hosts file: 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

The Java Zone is brought to you in partnership with ZeroTurnaround. Discover how you can skip the build and redeploy process by using JRebel by ZeroTurnaround.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}