1

As the official documentation does not give any hints on this:

I am wondering how to serve the correct localization of my website according to the user’s language preference and/or browser/OS language with Craft.

From other platforms/plugins I know that this is done through user agent detection, e.g. if the browser/OS is English you’ll get the EN, if it’s Spanish you’ll get the ES version of the content, etc.

The project I am working on has DE as the primary locale and the language switcher shall only appear subtly within the footer. Non-German visitors should automatically get the EN version without having to interact with any interface element.

How can I implement this?

1 Answer 1

2

You could accomplish this before your PHP is executed as redirects listening for the accept_language header (both are untested from my part):

APACHE

RewriteEngine on
RewriteCond %{HTTP:Accept-Language} (de) [NC]
RewriteRule .* http://example.com/de/ [R,L]

NGINX

map $http_accept_language $lang {
  default en;
  ~de de;
}

location ~* "/" {
  return 302 https://example.com/$lang;
}
2
  • Would the user still be able to switch to EN with this configuration? Or would it be impossible because the .htaccess always referrs him to DE?
    – Tobias S.
    Commented Apr 20, 2018 at 8:52
  • We do something similar with detecting a cookie we have set for a locale. So you could possibly default to the http header's language however if they have selected a different locale from the switcher that could set a cookie for their preference and you could use that to force the redirect in the future. The redirects may be different from above because you would be prioritizing the redirect logic.
    – a-am
    Commented Apr 20, 2018 at 14:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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