Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
function prefered_language($available_languages, $http_accept_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);
}
}
$available_languages = array("cs-cz", "sk-sk", "en-EN", "en", "cs", "sk");
$lang = prefered_language($available_languages, $_SERVER["HTTP_ACCEPT_LANGUAGE"]);
switch($lang) {
case "sk-sk":
case "sk":
header("Location: /sk/", true, 302);
break;
case "cs-cz":
case "cs":
header("Location: /cs/", true, 302);
break;
default:
header("Location: /en/", true, 302);
break;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment