Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-20T08:52:53.953Z
Answers.Text: 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?
Answers.UserDisplayName: Tobias S.
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-20T14:33:03.373Z
Answers.Text: 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.
Answers.UserDisplayName: a-am
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-18T14:37:39.107Z
Answers.LastActivityDate: 2018-04-18T14:37:39.107Z
Answers.UserDisplayName: a-am
Body: <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>
  
Tags: <localization><language><switch>
Title: Craft 2: Serve language / locale i18n according to user agent
ThreadText: # 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.  
  
  ---
  
  