diff options
author | ArthurHoaro <arthur@hoa.ro> | 2017-03-07 19:27:17 +0100 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2017-03-07 19:27:17 +0100 |
commit | 03b9cb600a85fed79b8afa72ccdad2725da5f3da (patch) | |
tree | 7d3b4090960953edcf7cec663d45e879dfb99f2b /application | |
parent | 36c8fb1ef869c29e783f0dd5ebef2fb5566e2611 (diff) | |
download | Shaarli-03b9cb600a85fed79b8afa72ccdad2725da5f3da.tar.gz Shaarli-03b9cb600a85fed79b8afa72ccdad2725da5f3da.tar.zst Shaarli-03b9cb600a85fed79b8afa72ccdad2725da5f3da.zip |
Fix autoLocale error and cover it with unit tests
Diffstat (limited to 'application')
-rw-r--r-- | application/Utils.php | 34 |
1 files changed, 21 insertions, 13 deletions
diff --git a/application/Utils.php b/application/Utils.php index a936b09f..5c077450 100644 --- a/application/Utils.php +++ b/application/Utils.php | |||
@@ -216,22 +216,30 @@ function is_session_id_valid($sessionId) | |||
216 | function autoLocale($headerLocale) | 216 | function autoLocale($headerLocale) |
217 | { | 217 | { |
218 | // Default if browser does not send HTTP_ACCEPT_LANGUAGE | 218 | // Default if browser does not send HTTP_ACCEPT_LANGUAGE |
219 | $attempts = array('en_US', 'en_US.utf8', 'en_US.UTF-8'); | 219 | $locales = array('en_US', 'en_US.utf8', 'en_US.UTF-8'); |
220 | if (isset($headerLocale)) { | 220 | if (! empty($headerLocale)) { |
221 | // (It's a bit crude, but it works very well. Preferred language is always presented first.) | 221 | if (preg_match_all('/([a-z]{2,3})[-_]?([a-z]{2})?,?/i', $headerLocale, $matches, PREG_SET_ORDER)) { |
222 | if (preg_match('/([a-z]{2,3})[-_]?([a-z]{2})?/i', $headerLocale, $matches)) { | 222 | $attempts = []; |
223 | $first = [strtolower($matches[1]), strtoupper($matches[1])]; | 223 | foreach ($matches as $match) { |
224 | $separators = ['_', '-']; | 224 | $first = [strtolower($match[1]), strtoupper($match[1])]; |
225 | $encodings = ['utf8', 'UTF-8']; | 225 | $separators = ['_', '-']; |
226 | if (!empty($matches[2])) { | 226 | $encodings = ['utf8', 'UTF-8']; |
227 | $second = [strtoupper($matches[2]), strtolower($matches[2])]; | 227 | if (!empty($match[2])) { |
228 | $attempts = cartesian_product_generator([$first, $separators, $second, ['.'], $encodings]); | 228 | $second = [strtoupper($match[2]), strtolower($match[2])]; |
229 | } else { | 229 | $items = [$first, $separators, $second, ['.'], $encodings]; |
230 | $attempts = cartesian_product_generator([$first, $separators, $first, ['.'], $encodings]); | 230 | } else { |
231 | $items = [$first, $separators, $first, ['.'], $encodings]; | ||
232 | } | ||
233 | $attempts = array_merge($attempts, iterator_to_array(cartesian_product_generator($items))); | ||
234 | } | ||
235 | |||
236 | if (! empty($attempts)) { | ||
237 | $locales = array_merge(array_map('implode', $attempts), $locales); | ||
231 | } | 238 | } |
232 | } | 239 | } |
233 | } | 240 | } |
234 | setlocale(LC_ALL, implode('implode', iterator_to_array($attempts))); | 241 | |
242 | setlocale(LC_ALL, $locales); | ||
235 | } | 243 | } |
236 | 244 | ||
237 | /** | 245 | /** |