|
28 | 28 |
|
29 | 29 | */
|
30 | 30 |
|
| 31 | +use phpweb\LangChooser; |
| 32 | + |
| 33 | +require_once __DIR__ . '/../src/autoload.php'; |
| 34 | + |
31 | 35 | // Default STRIPPED_URI
|
32 | 36 | $_SERVER['STRIPPED_URI'] = htmlspecialchars($_SERVER['REQUEST_URI'], ENT_QUOTES, 'UTF-8');
|
33 | 37 |
|
34 | 38 | // The code is encapsulated in a function,
|
35 | 39 | // so the variable namespace is not polluted
|
36 |
| -list($LANG, $EXPL_LANG, $UA_LANGS) = language_choose_code(); |
| 40 | +list($LANG, $EXPL_LANG) = (new LangChooser($LANGUAGES, $INACTIVE_ONLINE_LANGUAGES, myphpnet_language(), default_language() ?: ''))->chooseCode( |
| 41 | + $_REQUEST['lang'] ?? null, |
| 42 | + $_SERVER['REQUEST_URI'], |
| 43 | + $_SERVER['HTTP_ACCEPT_LANGUAGE'] ?? null, |
| 44 | +); |
37 | 45 |
|
38 | 46 | // Compatibility
|
39 | 47 | if ($EXPL_LANG == '') { unset($EXPL_LANG); }
|
40 |
| - |
41 |
| -function language_choose_code() |
42 |
| -{ |
43 |
| - // Contains all the languages picked up by the |
44 |
| - // process in priority order (without repeating codes) |
45 |
| - $languages = []; |
46 |
| - |
47 |
| - // Default values for languages |
48 |
| - $explicitly_specified = ''; $selected = ''; |
49 |
| - |
50 |
| - // Specified for the request (GET/POST parameter) |
51 |
| - if (!empty($_REQUEST['lang']) && is_string($_REQUEST['lang'])) { |
52 |
| - $explicitly_specified = language_add(htmlspecialchars($_REQUEST['lang'], ENT_QUOTES, 'UTF-8'), $languages); |
53 |
| - } |
54 |
| - |
55 |
| - // Specified in a shortcut URL (eg. /en/echo or /pt_br/echo) |
56 |
| - if (preg_match("!^/(\\w{2}(_\\w{2})?)/!", htmlspecialchars($_SERVER['REQUEST_URI'],ENT_QUOTES, 'UTF-8'), $flang)) { |
57 |
| - |
58 |
| - // Put language into preference list |
59 |
| - $rlang = language_add($flang[1], $languages); |
60 |
| - |
61 |
| - // Set explicity specified language |
62 |
| - if (empty($explicitly_specified)) { |
63 |
| - $explicitly_specified = $rlang; |
64 |
| - } |
65 |
| - |
66 |
| - // Drop out langauge specification from URL, as this is already handled |
67 |
| - $_SERVER['STRIPPED_URI'] = preg_replace( |
68 |
| - "!^/$flang[1]/!", "/", htmlspecialchars($_SERVER['REQUEST_URI'], ENT_QUOTES, 'UTF-8'), |
69 |
| - ); |
70 |
| - |
71 |
| - } |
72 |
| - |
73 |
| - // Specified in a manual URL (eg. manual/en/ or manual/pt_br/) |
74 |
| - if (preg_match("!^/manual/(\\w{2}(_\\w{2})?)(/|$)!", htmlspecialchars($_SERVER['REQUEST_URI'], ENT_QUOTES, 'UTF-8'), $flang)) { |
75 |
| - |
76 |
| - $flang = language_add($flang[1], $languages); |
77 |
| - |
78 |
| - // Set explicity specified language |
79 |
| - if (empty($explicitly_specified)) { |
80 |
| - $explicitly_specified = $flang; |
81 |
| - } |
82 |
| - } |
83 |
| - |
84 |
| - // Honor the users own language setting (if available) |
85 |
| - if (myphpnet_language()) { |
86 |
| - language_add(myphpnet_language(), $languages); |
87 |
| - } |
88 |
| - |
89 |
| - // Specified by the user via the browser's Accept Language setting |
90 |
| - // Samples: "hu, en-us;q=0.66, en;q=0.33", "hu,en-us;q=0.5" |
91 |
| - $browser_langs = []; $parsed_langs = []; |
92 |
| - |
93 |
| - // Check if we have $_SERVER['HTTP_ACCEPT_LANGUAGE'] set and |
94 |
| - // it no longer breaks if you only have one language set :) |
95 |
| - if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { |
96 |
| - $browser_accept = explode(",", $_SERVER['HTTP_ACCEPT_LANGUAGE']); |
97 |
| - |
98 |
| - // Go through all language preference specs |
99 |
| - foreach ($browser_accept as $value) { |
100 |
| - // The language part is either a code or a code with a quality |
101 |
| - // We cannot do anything with a * code, so it is skipped |
102 |
| - // If the quality is missing, it is assumed to be 1 according to the RFC |
103 |
| - if (preg_match("!([a-z-]+)(;q=([0-9\\.]+))?!", trim($value), $found)) { |
104 |
| - $quality = (isset($found[3]) ? (float) $found[3] : 1.0); |
105 |
| - $browser_langs[] = [$found[1], $quality]; |
106 |
| - } |
107 |
| - unset($found); |
108 |
| - } |
109 |
| - } |
110 |
| - |
111 |
| - // Order the codes by quality |
112 |
| - usort($browser_langs, "language_accept_order"); |
113 |
| - |
114 |
| - // For all languages found in the accept-language |
115 |
| - foreach ($browser_langs as $langdata) { |
116 |
| - |
117 |
| - // Translation table for accept-language codes and phpdoc codes |
118 |
| - switch ($langdata[0]) { |
119 |
| - case "pt-br": |
120 |
| - $langdata[0] = 'pt_br'; |
121 |
| - break; |
122 |
| - case "zh-cn": |
123 |
| - $langdata[0] = 'zh'; |
124 |
| - break; |
125 |
| - case "zh-hk": |
126 |
| - $langdata[0] = 'hk'; |
127 |
| - break; |
128 |
| - case "zh-tw": |
129 |
| - $langdata[0] = 'tw'; |
130 |
| - break; |
131 |
| - } |
132 |
| - |
133 |
| - // We do not support flavors of languages (except the ones above) |
134 |
| - // This is not in conformance to the RFC, but it here for user |
135 |
| - // convinience reasons |
136 |
| - if (preg_match("!^(.+)-!", $langdata[0], $match)) { |
137 |
| - $langdata[0] = $match[1]; |
138 |
| - } |
139 |
| - |
140 |
| - // Add language to priority order |
141 |
| - $parsed_langs[] = language_add($langdata[0], $languages); |
142 |
| - } |
143 |
| - |
144 |
| - // Language preferred by this mirror site |
145 |
| - language_add(default_language(), $languages); |
146 |
| - |
147 |
| - // Last default language is English |
148 |
| - language_add("en", $languages); |
149 |
| - |
150 |
| - // Try to find out what language is available on this mirror. |
151 |
| - // As most of the language dependant operations involve manual |
152 |
| - // page display (lookup, search, shortcuts), we will check for |
153 |
| - // the index file of manuals. |
154 |
| -/* |
155 |
| - foreach ($languages as $language) { |
156 |
| - if (file_exists($_SERVER['DOCUMENT_ROOT'] . "/manual/$language/index.php")) { |
157 |
| - $selected = $language; |
158 |
| - break; |
159 |
| - } |
160 |
| - } |
161 |
| -*/ |
162 |
| - $selected = $languages[0]; |
163 |
| - |
164 |
| - // Return with all found data |
165 |
| - return [$selected, $explicitly_specified, $parsed_langs]; |
166 |
| -} |
167 |
| - |
168 |
| -// Add a language to the possible languages' list |
169 |
| -function language_add($langcode, &$langs) |
170 |
| -{ |
171 |
| - global $LANGUAGES, $INACTIVE_ONLINE_LANGUAGES; |
172 |
| - |
173 |
| - // Make language code lowercase, html encode special chars and remove slashes |
174 |
| - $langcode = strtolower(htmlspecialchars($langcode)); |
175 |
| - |
176 |
| - // The Brazilian Portuguese code needs special attention |
177 |
| - if ($langcode == 'pt_br') { $langcode = 'pt_BR'; } |
178 |
| - |
179 |
| - // Append language code in priority order if it is not |
180 |
| - // there already and supported by the PHP site. Try to |
181 |
| - // lower number of file_exists() calls to the minumum... |
182 |
| - if (!in_array($langcode, $langs, false) && isset($LANGUAGES[$langcode]) |
183 |
| - && !isset($INACTIVE_ONLINE_LANGUAGES[$langcode])) { |
184 |
| - $langs[] = $langcode; |
185 |
| - } |
186 |
| - |
187 |
| - // Return with language code |
188 |
| - return $langcode; |
189 |
| -} |
190 |
| - |
191 |
| -// Order the array of compiled |
192 |
| -// accept-language codes by quality value |
193 |
| -function language_accept_order($a, $b) |
194 |
| -{ |
195 |
| - if ($a[1] == $b[1]) { return 0; } |
196 |
| - return ($a[1] > $b[1]) ? -1 : 1; |
197 |
| -} |
0 commit comments