diff options
-rw-r--r-- | application/Utils.php | 34 | ||||
-rw-r--r-- | tests/languages/de/UtilsDeTest.php | 76 | ||||
-rw-r--r-- | tests/languages/en/UtilsEnTest.php | 76 | ||||
-rw-r--r-- | tests/languages/fr/UtilsFrTest.php | 76 |
4 files changed, 249 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 | /** |
diff --git a/tests/languages/de/UtilsDeTest.php b/tests/languages/de/UtilsDeTest.php index 8a740389..545fa572 100644 --- a/tests/languages/de/UtilsDeTest.php +++ b/tests/languages/de/UtilsDeTest.php | |||
@@ -22,4 +22,80 @@ class UtilsDeTest extends UtilsTest | |||
22 | $date = DateTime::createFromFormat('Ymd_His', '20170101_101112'); | 22 | $date = DateTime::createFromFormat('Ymd_His', '20170101_101112'); |
23 | $this->assertEquals('So 01 Jan 2017 10:11:12 EAT', format_date($date, false)); | 23 | $this->assertEquals('So 01 Jan 2017 10:11:12 EAT', format_date($date, false)); |
24 | } | 24 | } |
25 | |||
26 | /** | ||
27 | * Test autoLocale with a simple value | ||
28 | */ | ||
29 | public function testAutoLocaleValid() | ||
30 | { | ||
31 | $current = setlocale(LC_ALL, 0); | ||
32 | $header = 'en-us'; | ||
33 | autoLocale($header); | ||
34 | $this->assertEquals('en_US.utf8', setlocale(LC_ALL, 0)); | ||
35 | |||
36 | setlocale(LC_ALL, $current); | ||
37 | } | ||
38 | |||
39 | /** | ||
40 | * Test autoLocale with an alternative locale value | ||
41 | */ | ||
42 | public function testAutoLocaleValidAlternative() | ||
43 | { | ||
44 | $current = setlocale(LC_ALL, 0); | ||
45 | $header = 'en_us.UTF8'; | ||
46 | autoLocale($header); | ||
47 | $this->assertEquals('en_US.utf8', setlocale(LC_ALL, 0)); | ||
48 | |||
49 | setlocale(LC_ALL, $current); | ||
50 | } | ||
51 | |||
52 | /** | ||
53 | * Test autoLocale with multiples value, the first one is valid | ||
54 | */ | ||
55 | public function testAutoLocaleMultipleFirstValid() | ||
56 | { | ||
57 | $current = setlocale(LC_ALL, 0); | ||
58 | $header = 'en-us,de-de'; | ||
59 | autoLocale($header); | ||
60 | $this->assertEquals('en_US.utf8', setlocale(LC_ALL, 0)); | ||
61 | |||
62 | setlocale(LC_ALL, $current); | ||
63 | } | ||
64 | |||
65 | /** | ||
66 | * Test autoLocale with multiples value, the second one is valid | ||
67 | */ | ||
68 | public function testAutoLocaleMultipleSecondValid() | ||
69 | { | ||
70 | $current = setlocale(LC_ALL, 0); | ||
71 | $header = 'pt_BR,fr-fr'; | ||
72 | autoLocale($header); | ||
73 | $this->assertEquals('fr_FR.utf8', setlocale(LC_ALL, 0)); | ||
74 | |||
75 | setlocale(LC_ALL, $current); | ||
76 | } | ||
77 | |||
78 | /** | ||
79 | * Test autoLocale without value: defaults to en_US. | ||
80 | */ | ||
81 | public function testAutoLocaleBlank() | ||
82 | { | ||
83 | $current = setlocale(LC_ALL, 0); | ||
84 | autoLocale(''); | ||
85 | $this->assertEquals('en_US.utf8', setlocale(LC_ALL, 0)); | ||
86 | |||
87 | setlocale(LC_ALL, $current); | ||
88 | } | ||
89 | |||
90 | /** | ||
91 | * Test autoLocale with an invalid value: defaults to en_US. | ||
92 | */ | ||
93 | public function testAutoLocaleInvalid() | ||
94 | { | ||
95 | $current = setlocale(LC_ALL, 0); | ||
96 | autoLocale('pt_BR'); | ||
97 | $this->assertEquals('en_US.utf8', setlocale(LC_ALL, 0)); | ||
98 | |||
99 | setlocale(LC_ALL, $current); | ||
100 | } | ||
25 | } | 101 | } |
diff --git a/tests/languages/en/UtilsEnTest.php b/tests/languages/en/UtilsEnTest.php index 60bcb653..7c829ac7 100644 --- a/tests/languages/en/UtilsEnTest.php +++ b/tests/languages/en/UtilsEnTest.php | |||
@@ -22,4 +22,80 @@ class UtilsEnTest extends UtilsTest | |||
22 | $date = DateTime::createFromFormat('Ymd_His', '20170101_101112'); | 22 | $date = DateTime::createFromFormat('Ymd_His', '20170101_101112'); |
23 | $this->assertEquals('Sun 01 Jan 2017 10:11:12 AM EAT', format_date($date, false)); | 23 | $this->assertEquals('Sun 01 Jan 2017 10:11:12 AM EAT', format_date($date, false)); |
24 | } | 24 | } |
25 | |||
26 | /** | ||
27 | * Test autoLocale with a simple value | ||
28 | */ | ||
29 | public function testAutoLocaleValid() | ||
30 | { | ||
31 | $current = setlocale(LC_ALL, 0); | ||
32 | $header = 'de-de'; | ||
33 | autoLocale($header); | ||
34 | $this->assertEquals('de_DE.utf8', setlocale(LC_ALL, 0)); | ||
35 | |||
36 | setlocale(LC_ALL, $current); | ||
37 | } | ||
38 | |||
39 | /** | ||
40 | * Test autoLocale with an alternative locale value | ||
41 | */ | ||
42 | public function testAutoLocaleValidAlternative() | ||
43 | { | ||
44 | $current = setlocale(LC_ALL, 0); | ||
45 | $header = 'de_de.UTF8'; | ||
46 | autoLocale($header); | ||
47 | $this->assertEquals('de_DE.utf8', setlocale(LC_ALL, 0)); | ||
48 | |||
49 | setlocale(LC_ALL, $current); | ||
50 | } | ||
51 | |||
52 | /** | ||
53 | * Test autoLocale with multiples value, the first one is valid | ||
54 | */ | ||
55 | public function testAutoLocaleMultipleFirstValid() | ||
56 | { | ||
57 | $current = setlocale(LC_ALL, 0); | ||
58 | $header = 'de-de;en-us'; | ||
59 | autoLocale($header); | ||
60 | $this->assertEquals('de_DE.utf8', setlocale(LC_ALL, 0)); | ||
61 | |||
62 | setlocale(LC_ALL, $current); | ||
63 | } | ||
64 | |||
65 | /** | ||
66 | * Test autoLocale with multiples value, the second one is valid | ||
67 | */ | ||
68 | public function testAutoLocaleMultipleSecondValid() | ||
69 | { | ||
70 | $current = setlocale(LC_ALL, 0); | ||
71 | $header = 'pt_BR,fr-fr'; | ||
72 | autoLocale($header); | ||
73 | $this->assertEquals('fr_FR.utf8', setlocale(LC_ALL, 0)); | ||
74 | |||
75 | setlocale(LC_ALL, $current); | ||
76 | } | ||
77 | |||
78 | /** | ||
79 | * Test autoLocale without value: defaults to en_US. | ||
80 | */ | ||
81 | public function testAutoLocaleBlank() | ||
82 | { | ||
83 | $current = setlocale(LC_ALL, 0); | ||
84 | autoLocale(''); | ||
85 | $this->assertEquals('en_US.utf8', setlocale(LC_ALL, 0)); | ||
86 | |||
87 | setlocale(LC_ALL, $current); | ||
88 | } | ||
89 | |||
90 | /** | ||
91 | * Test autoLocale with an invalid value: defaults to en_US. | ||
92 | */ | ||
93 | public function testAutoLocaleInvalid() | ||
94 | { | ||
95 | $current = setlocale(LC_ALL, 0); | ||
96 | autoLocale('pt_BR'); | ||
97 | $this->assertEquals('en_US.utf8', setlocale(LC_ALL, 0)); | ||
98 | |||
99 | setlocale(LC_ALL, $current); | ||
100 | } | ||
25 | } | 101 | } |
diff --git a/tests/languages/fr/UtilsFrTest.php b/tests/languages/fr/UtilsFrTest.php index 890308d3..45996ee2 100644 --- a/tests/languages/fr/UtilsFrTest.php +++ b/tests/languages/fr/UtilsFrTest.php | |||
@@ -22,4 +22,80 @@ class UtilsFrTest extends UtilsTest | |||
22 | $date = DateTime::createFromFormat('Ymd_His', '20170101_101112'); | 22 | $date = DateTime::createFromFormat('Ymd_His', '20170101_101112'); |
23 | $this->assertEquals('dim. 01 janv. 2017 10:11:12 EAT', format_date($date, false)); | 23 | $this->assertEquals('dim. 01 janv. 2017 10:11:12 EAT', format_date($date, false)); |
24 | } | 24 | } |
25 | |||
26 | /** | ||
27 | * Test autoLocale with a simple value | ||
28 | */ | ||
29 | public function testAutoLocaleValid() | ||
30 | { | ||
31 | $current = setlocale(LC_ALL, 0); | ||
32 | $header = 'de-de'; | ||
33 | autoLocale($header); | ||
34 | $this->assertEquals('de_DE.utf8', setlocale(LC_ALL, 0)); | ||
35 | |||
36 | setlocale(LC_ALL, $current); | ||
37 | } | ||
38 | |||
39 | /** | ||
40 | * Test autoLocale with an alternative locale value | ||
41 | */ | ||
42 | public function testAutoLocaleValidAlternative() | ||
43 | { | ||
44 | $current = setlocale(LC_ALL, 0); | ||
45 | $header = 'de_de.UTF8'; | ||
46 | autoLocale($header); | ||
47 | $this->assertEquals('de_DE.utf8', setlocale(LC_ALL, 0)); | ||
48 | |||
49 | setlocale(LC_ALL, $current); | ||
50 | } | ||
51 | |||
52 | /** | ||
53 | * Test autoLocale with multiples value, the first one is valid | ||
54 | */ | ||
55 | public function testAutoLocaleMultipleFirstValid() | ||
56 | { | ||
57 | $current = setlocale(LC_ALL, 0); | ||
58 | $header = 'de-de;en-us'; | ||
59 | autoLocale($header); | ||
60 | $this->assertEquals('de_DE.utf8', setlocale(LC_ALL, 0)); | ||
61 | |||
62 | setlocale(LC_ALL, $current); | ||
63 | } | ||
64 | |||
65 | /** | ||
66 | * Test autoLocale with multiples value, the second one is valid | ||
67 | */ | ||
68 | public function testAutoLocaleMultipleSecondValid() | ||
69 | { | ||
70 | $current = setlocale(LC_ALL, 0); | ||
71 | $header = 'pt_BR,de-de'; | ||
72 | autoLocale($header); | ||
73 | $this->assertEquals('de_DE.utf8', setlocale(LC_ALL, 0)); | ||
74 | |||
75 | setlocale(LC_ALL, $current); | ||
76 | } | ||
77 | |||
78 | /** | ||
79 | * Test autoLocale without value: defaults to en_US. | ||
80 | */ | ||
81 | public function testAutoLocaleBlank() | ||
82 | { | ||
83 | $current = setlocale(LC_ALL, 0); | ||
84 | autoLocale(''); | ||
85 | $this->assertEquals('en_US.utf8', setlocale(LC_ALL, 0)); | ||
86 | |||
87 | setlocale(LC_ALL, $current); | ||
88 | } | ||
89 | |||
90 | /** | ||
91 | * Test autoLocale with an invalid value: defaults to en_US. | ||
92 | */ | ||
93 | public function testAutoLocaleInvalid() | ||
94 | { | ||
95 | $current = setlocale(LC_ALL, 0); | ||
96 | autoLocale('pt_BR'); | ||
97 | $this->assertEquals('en_US.utf8', setlocale(LC_ALL, 0)); | ||
98 | |||
99 | setlocale(LC_ALL, $current); | ||
100 | } | ||
25 | } | 101 | } |