1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| <?php
class Lang {
/**
* Get accepeted languages using browser capabilities
* @return array
*/
public static function getAcceptedLanguages() {
$httplanguages = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
$languages = array();
if (empty($httplanguages)) {
return $languages;
}
foreach (explode(',', $httplanguages) as $accept) {
$result = preg_match('/^([a-z]{1,8}(?:[-_][a-z]{1,8})*)(?:;\s*q=(0(?:\.[0-9]{1,3})?|1(?:\.0{1,3})?))?$/i', $accept, $match);
if (!$result) {
continue;
}
if (isset($match[2])) {
$quality = (float)$match[2];
}
else {
$quality = 1.0;
}
$countries = explode('-', $match[1]);
$region = array_shift($countries);
$country_sub = explode('_', $region);
$region = array_shift($country_sub);
foreach($countries as $country)
$languages[$region . '_' . strtoupper($country)] = $quality;
foreach($country_sub as $country)
$languages[$region . '_' . strtoupper($country)] = $quality;
$languages[$region] = $quality;
}
return $languages;
}
// autres trucs...
} |