@@ -11,89 +11,89 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class Locale |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * Callback for PHP sort functions - allows lists of locales to be sorted. |
|
| 16 | - * Diacritics are removed and text is capitalized to allow fast/simple sorting. |
|
| 17 | - * |
|
| 18 | - * @param LocaleInterface $x |
|
| 19 | - * @param LocaleInterface $y |
|
| 20 | - * |
|
| 21 | - * @return int |
|
| 22 | - */ |
|
| 23 | - public static function compare(LocaleInterface $x, LocaleInterface $y) |
|
| 24 | - { |
|
| 25 | - return strcmp($x->endonymSortable(), $y->endonymSortable()); |
|
| 26 | - } |
|
| 14 | + /** |
|
| 15 | + * Callback for PHP sort functions - allows lists of locales to be sorted. |
|
| 16 | + * Diacritics are removed and text is capitalized to allow fast/simple sorting. |
|
| 17 | + * |
|
| 18 | + * @param LocaleInterface $x |
|
| 19 | + * @param LocaleInterface $y |
|
| 20 | + * |
|
| 21 | + * @return int |
|
| 22 | + */ |
|
| 23 | + public static function compare(LocaleInterface $x, LocaleInterface $y) |
|
| 24 | + { |
|
| 25 | + return strcmp($x->endonymSortable(), $y->endonymSortable()); |
|
| 26 | + } |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * Create a locale from a language tag (or locale code). |
|
| 30 | - * |
|
| 31 | - * @param string $code |
|
| 32 | - * |
|
| 33 | - * @return LocaleInterface |
|
| 34 | - * @throws \DomainException |
|
| 35 | - */ |
|
| 36 | - public static function create($code) |
|
| 37 | - { |
|
| 38 | - $class = __NAMESPACE__ . '\Locale\Locale' . implode(array_map(function ($x) { |
|
| 39 | - return ucfirst(strtolower($x)); |
|
| 40 | - }, preg_split('/[^a-zA-Z0-9]+/', $code))); |
|
| 28 | + /** |
|
| 29 | + * Create a locale from a language tag (or locale code). |
|
| 30 | + * |
|
| 31 | + * @param string $code |
|
| 32 | + * |
|
| 33 | + * @return LocaleInterface |
|
| 34 | + * @throws \DomainException |
|
| 35 | + */ |
|
| 36 | + public static function create($code) |
|
| 37 | + { |
|
| 38 | + $class = __NAMESPACE__ . '\Locale\Locale' . implode(array_map(function ($x) { |
|
| 39 | + return ucfirst(strtolower($x)); |
|
| 40 | + }, preg_split('/[^a-zA-Z0-9]+/', $code))); |
|
| 41 | 41 | |
| 42 | - if (class_exists($class)) { |
|
| 43 | - return new $class(); |
|
| 44 | - } else { |
|
| 45 | - throw new \DomainException($code); |
|
| 46 | - } |
|
| 47 | - } |
|
| 42 | + if (class_exists($class)) { |
|
| 43 | + return new $class(); |
|
| 44 | + } else { |
|
| 45 | + throw new \DomainException($code); |
|
| 46 | + } |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * Create a locale from a language tag (or locale code). |
|
| 51 | - * |
|
| 52 | - * @param string[] $server The $_SERVER array |
|
| 53 | - * @param LocaleInterface[] $available All locales supported by the application |
|
| 54 | - * @param LocaleInterface $default Locale to show in no matching locales |
|
| 55 | - * |
|
| 56 | - * @return LocaleInterface |
|
| 57 | - */ |
|
| 58 | - public static function httpAcceptLanguage(array $server, array $available, LocaleInterface $default) |
|
| 59 | - { |
|
| 60 | - if (!empty($server['HTTP_ACCEPT_LANGUAGE'])) { |
|
| 61 | - $http_accept_language = strtolower(str_replace(' ', '', $server['HTTP_ACCEPT_LANGUAGE'])); |
|
| 62 | - preg_match_all('/(?:([a-z][a-z0-9_-]+)(?:;q=([0-9.]+))?)/', $http_accept_language, $match); |
|
| 63 | - $preferences = array_map(function ($x) { |
|
| 64 | - return $x === '' ? 1.0 : (float) $x; |
|
| 65 | - }, array_combine($match[1], $match[2])); |
|
| 49 | + /** |
|
| 50 | + * Create a locale from a language tag (or locale code). |
|
| 51 | + * |
|
| 52 | + * @param string[] $server The $_SERVER array |
|
| 53 | + * @param LocaleInterface[] $available All locales supported by the application |
|
| 54 | + * @param LocaleInterface $default Locale to show in no matching locales |
|
| 55 | + * |
|
| 56 | + * @return LocaleInterface |
|
| 57 | + */ |
|
| 58 | + public static function httpAcceptLanguage(array $server, array $available, LocaleInterface $default) |
|
| 59 | + { |
|
| 60 | + if (!empty($server['HTTP_ACCEPT_LANGUAGE'])) { |
|
| 61 | + $http_accept_language = strtolower(str_replace(' ', '', $server['HTTP_ACCEPT_LANGUAGE'])); |
|
| 62 | + preg_match_all('/(?:([a-z][a-z0-9_-]+)(?:;q=([0-9.]+))?)/', $http_accept_language, $match); |
|
| 63 | + $preferences = array_map(function ($x) { |
|
| 64 | + return $x === '' ? 1.0 : (float) $x; |
|
| 65 | + }, array_combine($match[1], $match[2])); |
|
| 66 | 66 | |
| 67 | - // Need a stable sort, as the original order is significant |
|
| 68 | - $preferences = array_map(function ($x) { |
|
| 69 | - static $n = 0; |
|
| 67 | + // Need a stable sort, as the original order is significant |
|
| 68 | + $preferences = array_map(function ($x) { |
|
| 69 | + static $n = 0; |
|
| 70 | 70 | |
| 71 | - return array($x, --$n); |
|
| 72 | - }, $preferences); |
|
| 73 | - arsort($preferences); |
|
| 74 | - $preferences = array_map(function ($x) { |
|
| 75 | - return $x[0]; |
|
| 76 | - }, $preferences); |
|
| 71 | + return array($x, --$n); |
|
| 72 | + }, $preferences); |
|
| 73 | + arsort($preferences); |
|
| 74 | + $preferences = array_map(function ($x) { |
|
| 75 | + return $x[0]; |
|
| 76 | + }, $preferences); |
|
| 77 | 77 | |
| 78 | - // If "de-DE" requested, but not "de", then add it at a lower priority |
|
| 79 | - foreach ($preferences as $code => $priority) { |
|
| 80 | - if (preg_match('/^([a-z]+)[^a-z]/', $code, $match) && !isset($preferences[$match[1]])) { |
|
| 81 | - $preferences[$match[1]] = $priority * 0.5; |
|
| 82 | - } |
|
| 83 | - } |
|
| 78 | + // If "de-DE" requested, but not "de", then add it at a lower priority |
|
| 79 | + foreach ($preferences as $code => $priority) { |
|
| 80 | + if (preg_match('/^([a-z]+)[^a-z]/', $code, $match) && !isset($preferences[$match[1]])) { |
|
| 81 | + $preferences[$match[1]] = $priority * 0.5; |
|
| 82 | + } |
|
| 83 | + } |
|
| 84 | 84 | |
| 85 | - foreach (array_keys($preferences) as $code) { |
|
| 86 | - try { |
|
| 87 | - $locale = Locale::create($code); |
|
| 88 | - if (in_array($locale, $available)) { |
|
| 89 | - return $locale; |
|
| 90 | - } |
|
| 91 | - } catch (\DomainException $ex) { |
|
| 92 | - // An unknown locale? Ignore it. |
|
| 93 | - } |
|
| 94 | - } |
|
| 95 | - } |
|
| 85 | + foreach (array_keys($preferences) as $code) { |
|
| 86 | + try { |
|
| 87 | + $locale = Locale::create($code); |
|
| 88 | + if (in_array($locale, $available)) { |
|
| 89 | + return $locale; |
|
| 90 | + } |
|
| 91 | + } catch (\DomainException $ex) { |
|
| 92 | + // An unknown locale? Ignore it. |
|
| 93 | + } |
|
| 94 | + } |
|
| 95 | + } |
|
| 96 | 96 | |
| 97 | - return $default; |
|
| 98 | - } |
|
| 97 | + return $default; |
|
| 98 | + } |
|
| 99 | 99 | } |