Webmasters Stack Exchange is a question and answer site for pro webmasters. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a bilingual dotCMS site. Switching between languages with the pulldown at the top of the page works great, but the default language is always English (or whatever else I configure).

I'd much rather have the user be presented with the language specified by his browser (which of course he can then later change with the pulldown). It seems that dotCMS does not do this automatically.

Can I enable this by some setting or plugin ?

share|improve this question
up vote 2 down vote accepted

You can get the browser locale by using the request object:


That'll return a string that'll contain something like en-US, which you can then use to get the internal language object:


So, to get the base language ID:

    #set($userLangId = $text.getLanguage('en','US').getId())

To set it for the visit:


And remember, you can't set the language for the session if any HTML has been output to the user. You'll want to do this first thing in the template. You can easily make the code above as dynamic as you need to pass the country and language code entirely from the header, just make sure you have a failsafe in case it's a locale you don't support with a dictionary entry.

share|improve this answer
So I have to code this myself? Can I also do this in a servlet filter (I guess I'll have to do without $text then, but I know my language ids). – Thilo Mar 2 '13 at 6:23
Right, there's no magic bullet in the CMS for automatic language handling, especially if you want it to rely on the user's browser setting. But to answer your question, you certainly can do it in a servlet too, and the base principle wouldn't actually be any different. Get the user Accept-Language header, and set the language session value. If you only have a couple languages to deal with, you can basically skip the language lookup, and just hardcode an if statement to set the language to one or the other. – TheQuicksilver Mar 2 '13 at 18:27

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.