Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I do this in PHP. Accept-Language is a complex thing. A browser can suggest more than one language that it would like to accept (each with a quality factor that shows which it prefers). For my site I have a default language to display (which is shown when none of the Accept-Languages is in my translation list). Otherwise if there is no language set (setLang) I choose it based on the most acceptable for the browser by parsing the Accept-Language. The function I use is below (it contains my session manager for setting cookies - but you could reimplement that with direct calls to $_SESSION[etc] = $foo;).</p> <p><strong>Edit</strong>: Unfortunately my website only has translations for the primary languages (EN, ES, FR) rather than (en_US, en_GB, es_MX, es_ES) so I choose the highest quality factor specified in these for the primary language.</p> <pre><code> public function setLanguage($setLang='') { if (!empty($setLang)) { $this-&gt;setup['Session']-&gt;set($this-&gt;setup['Lang_Key'], $setLang); } else { if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { // Parse the Accept-Language according to: // http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4 preg_match_all( '/([a-z]{1,8})' . // First part of language e.g en '(-[a-z]{1,8})*\s*' . // other parts of language e.g -us // Optional quality factor '(;\s*q\s*=\s*((1(\.0{0,3}))|(0(\.[0-9]{0,3}))))?/i', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $langParse); $langs = $langParse[1]; $quals = $langParse[4]; $numLanguages = count($langs); $langArr = array(); for ($num = 0; $num &lt; $numLanguages; $num++) { $newLang = strtoupper($langs[$num]); $newQual = isset($quals[$num]) ? (empty($quals[$num]) ? 1.0 : floatval($quals[$num])) : 0.0; // Choose whether to upgrade or set the quality factor for the // primary language. $langArr[$newLang] = (isset($langArr[$newLang])) ? max($langArr[$newLang], $newQual) : $newQual; } // sort list based on value arsort($langArr, SORT_NUMERIC); $acceptedLanguages = array_keys($langArr); $preferredLanguage = reset($acceptedLanguages); $this-&gt;setup['Session']-&gt;set( $this-&gt;setup['Lang_Key'], $preferredLanguage); } else { $this-&gt;setup['Session']-&gt;set( $this-&gt;setup['Lang_Key'], $this-&gt;setup['Default_Language']); } } return $this-&gt;setup['Session']-&gt;get($this-&gt;setup['Lang_Key']); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload