aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/Utils.php
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2017-03-07 19:32:36 +0100
committerGitHub <noreply@github.com>2017-03-07 19:32:36 +0100
commitdb0caba3c40c8d073779e77b2bada8cdba6d0518 (patch)
tree7d3b4090960953edcf7cec663d45e879dfb99f2b /application/Utils.php
parent9971f7c82cc47446b457464cec5b4fefcae470e1 (diff)
parent03b9cb600a85fed79b8afa72ccdad2725da5f3da (diff)
downloadShaarli-db0caba3c40c8d073779e77b2bada8cdba6d0518.tar.gz
Shaarli-db0caba3c40c8d073779e77b2bada8cdba6d0518.tar.zst
Shaarli-db0caba3c40c8d073779e77b2bada8cdba6d0518.zip
Merge pull request #790 from ArthurHoaro/feature/intl-dates
Fix autoLocale error and cover it with unit tests
Diffstat (limited to 'application/Utils.php')
-rw-r--r--application/Utils.php34
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)
216function autoLocale($headerLocale) 216function 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/**