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

<p>APACHE</p>

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

<p>NGINX</p>

<pre><code>map $http_accept_language $lang {
  default en;
  ~de de;
}

location ~* "/" {
  return 302 https://example.com/$lang;
}
</code></pre>

CC BY-SA 3.0
2018-04-20T08:52:53.953Z
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.
CC BY-SA 3.0
2018-04-20T14:33:03.373Z
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
CC BY-SA 3.0
2018-04-18T14:37:39.107Z
2018-04-18T14:37:39.107Z
a-am
<p>As the official documentation does not give any hints on this:</p>

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

<p>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.</p>

<p>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.</p>

<p>How can I implement this?</p>

<localization><language><switch>
Craft 2: Serve language / locale i18n according to user agent
# Craft 2: Serve language / locale i18n according to user agent

(Asked by: username_0 on 2018-04-18)

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?

**Tags:** localization, language, switch


---

## 1 answers

### Answer by username_1 on 2018-04-18. Score: 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;
}

> **Comments:**
> **username_0**: 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?  
> **username_1**: 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.  

---

