Mobile app version of vmapp.org
Login or Join
Merenda212

: 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,

@Merenda212

Posted in: #Cms #Internationalization

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 ?

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Merenda212

1 Comments

Sorted by latest first Latest Oldest Best

 

@Kevin317

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.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme