-
-
Save nicolasverlhiac/4a6ae8bbda376036e5e2 to your computer and use it in GitHub Desktop.
Compare supported languages with HTTP_ACCEPT_LANGUAGE in a users' browser.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Languages we support | |
$available_languages = array("zh-cn", "ca", "es", "fr", "af","nl", "sp", "en"); | |
$default_language = "en"; // a default language to fall back to in case there's no match | |
function prefered_language($available_languages, $http_accept_language) { | |
global $default_language; | |
$available_languages = array_flip($available_languages); | |
$langs = array(); | |
preg_match_all('~([\w-]+)(?:[^,\d]+([\d.]+))?~', strtolower($http_accept_language), $matches, PREG_SET_ORDER); | |
foreach($matches as $match) { | |
list($a, $b) = explode('-', $match[1]) + array('', ''); | |
$value = isset($match[2]) ? (float) $match[2] : 1.0; | |
if(isset($available_languages[$match[1]])) { | |
$langs[$match[1]] = $value; | |
continue; | |
} | |
if(isset($available_languages[$a])) { | |
$langs[$a] = $value - 0.1; | |
} | |
} | |
if($langs) { | |
arsort($langs); | |
return key($langs); // We don't need the whole array of choices since we have a match | |
} else { | |
return $default_language; | |
} | |
} | |
$lang = prefered_language($available_languages, strtolower($_SERVER["HTTP_ACCEPT_LANGUAGE"])); | |
print "<h3>Site available in:</h3><pre>"; | |
print_r($available_languages); | |
print "</pre>\n<h3>Browser supported languages:</h3><pre>"; | |
print_r(explode(',', strtolower($_SERVER["HTTP_ACCEPT_LANGUAGE"]))); | |
print "</pre>\n<h3>site will display in: <em>".$lang."</em></h3>"; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment