1 | <?php |
||
8 | class LanguageNegotiator |
||
9 | { |
||
10 | /** |
||
11 | * @var string |
||
12 | */ |
||
13 | private $defaultLocale; |
||
14 | |||
15 | /** |
||
16 | * @var array |
||
17 | */ |
||
18 | private $supportedLanguages; |
||
19 | |||
20 | /** |
||
21 | * @var Request |
||
22 | */ |
||
23 | private $request; |
||
24 | |||
25 | /** |
||
26 | * @param string $defaultLocale |
||
27 | * @param array $supportedLanguages |
||
28 | * @param Request $request |
||
29 | */ |
||
30 | public function __construct($defaultLocale, $supportedLanguages, Request $request) |
||
36 | |||
37 | /** |
||
38 | * Negotiates language with the user's browser through the Accept-Language |
||
39 | * HTTP header or the user's host address. Language codes are generally in |
||
40 | * the form "ll" for a language spoken in only one country, or "ll-CC" for a |
||
41 | * language spoken in a particular country. For example, U.S. English is |
||
42 | * "en-US", while British English is "en-UK". Portuguese as spoken in |
||
43 | * Portugal is "pt-PT", while Brazilian Portuguese is "pt-BR". |
||
44 | * |
||
45 | * This function is based on negotiateLanguage from Pear HTTP2 |
||
46 | * http://pear.php.net/package/HTTP2/ |
||
47 | * |
||
48 | * Quality factors in the Accept-Language: header are supported, e.g.: |
||
49 | * Accept-Language: en-UK;q=0.7, en-US;q=0.6, no, dk;q=0.8 |
||
50 | * |
||
51 | * @return string The negotiated language result or app.locale. |
||
52 | */ |
||
53 | public function negotiateLanguage() |
||
54 | { |
||
55 | $matches = $this->getMatchesFromAcceptedLanguages(); |
||
56 | foreach ($matches as $key => $q) { |
||
57 | if (!empty($this->supportedLanguages[$key])) { |
||
58 | return $key; |
||
59 | } |
||
60 | |||
61 | // Search for acceptable locale by 'regional' => 'af_ZA' or 'lang' => 'af-ZA' match. |
||
62 | foreach ( $this->supportedLanguages as $key_supported => $locale ) { |
||
63 | if ( (isset($locale['regional']) && $locale['regional'] == $key) || (isset($locale['lang']) && $locale['lang'] == $key) ) { |
||
64 | return $key_supported; |
||
65 | } |
||
66 | } |
||
67 | } |
||
68 | // If any (i.e. "*") is acceptable, return the first supported format |
||
69 | if (isset($matches['*'])) { |
||
70 | reset($this->supportedLanguages); |
||
71 | |||
72 | return key($this->supportedLanguages); |
||
73 | } |
||
74 | |||
75 | if (class_exists('Locale') && !empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { |
||
76 | $http_accept_language = Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']); |
||
77 | |||
78 | if (!empty($this->supportedLanguages[$http_accept_language])) { |
||
79 | return $http_accept_language; |
||
80 | } |
||
81 | } |
||
82 | |||
83 | if ($this->request->server('REMOTE_HOST')) { |
||
84 | $remote_host = explode('.', $this->request->server('REMOTE_HOST')); |
||
85 | $lang = strtolower(end($remote_host)); |
||
86 | |||
87 | if (!empty($this->supportedLanguages[$lang])) { |
||
88 | return $lang; |
||
89 | } |
||
90 | } |
||
91 | |||
92 | return $this->defaultLocale; |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Return all the accepted languages from the browser. |
||
97 | * |
||
98 | * @return array Matches from the header field Accept-Languages |
||
99 | */ |
||
100 | private function getMatchesFromAcceptedLanguages() |
||
149 | } |
||
150 |