Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

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

1 Answer 1

up vote 2 down vote accepted

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

$request.getHeader('Accept-Language')

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

$text.getLanguage('en','US')

So, to get the base language ID:

#if($request.getHeader('Accept-Language').contains('en-US'))
    #set($userLangId = $text.getLanguage('en','US').getId())
#end

To set it for the visit:

$request.getSession().setAttribute('com.dotmarketing.htmlpage.language',$userLangId)

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
1  
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

 
discard

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.