: How do I make dotCMS pick up the preferred language from the browser's settings? I have a bilingual dotCMS site. Switching between languages with the pulldown at the top of the page works great,
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 ?
More posts by @Merenda212
1 Comments
Sorted by latest first Latest Oldest Best
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.
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2024 All Rights reserved.