/
LocaleTest.php
381 lines (317 loc) · 10.7 KB
/
LocaleTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
<?php
/**
* Lithium: the most rad php framework
*
* @copyright Copyright 2012, Union of RAD (http://union-of-rad.org)
* @license http://opensource.org/licenses/bsd-license.php The BSD License
*/
namespace lithium\tests\cases\g11n;
use lithium\g11n\Locale;
use lithium\action\Request as ActionRequest;
use lithium\console\Request as ConsoleRequest;
class LocaleTest extends \lithium\test\Unit {
/**
* Tests composing of a locale from tags.
*/
public function testCompose() {
$result = Locale::compose(array('language' => 'en'));
$this->assertEqual('en', $result);
$result = Locale::compose(array('language' => 'en', 'territory' => 'US'));
$this->assertEqual('en_US', $result);
$result = Locale::compose(array('language' => 'EN', 'territory' => 'US'));
$this->assertEqual('EN_US', $result);
$result = Locale::compose(array(
'language' => 'zh',
'script' => 'Hans',
'territory' => 'HK',
'variant' => 'REVISED'
));
$this->assertEqual('zh_Hans_HK_REVISED', $result);
$result = Locale::compose(array(
'territory' => 'HK',
'language' => 'zh',
'script' => 'Hans'
));
$this->assertEqual('zh_Hans_HK', $result);
$this->assertNull(Locale::compose(array()));
}
/**
* Tests parsing of locales formatted strictly according to
* the definition of the unicode locale identifier.
*/
public function testDecomposeStrict() {
$expected = array('language' => 'en');
$this->assertEqual($expected, Locale::decompose('en'));
$expected = array('language' => 'en', 'territory' => 'US');
$this->assertEqual($expected, Locale::decompose('en_US'));
$expected = array(
'language' => 'en',
'territory' => 'US',
'variant' => 'POSIX'
);
$this->assertEqual($expected, Locale::decompose('en_US_POSIX'));
$expected = array('language' => 'kpe', 'territory' => 'GN');
$this->assertEqual($expected, Locale::decompose('kpe_GN'));
$expected = array('language' => 'zh', 'script' => 'Hans');
$this->assertEqual($expected, Locale::decompose('zh_Hans'));
$expected = array(
'language' => 'zh',
'script' => 'Hans',
'territory' => 'HK'
);
$this->assertEqual($expected, Locale::decompose('zh_Hans_HK'));
$expected = array(
'language' => 'zh',
'script' => 'Hans',
'territory' => 'HK',
'variant' => 'REVISED'
);
$this->assertEqual($expected, Locale::decompose('zh_Hans_HK_REVISED'));
}
/**
* Tests parsing of locales formatted loosely according to
* the definition of the unicode locale identifier.
*/
public function testDecomposeLoose() {
$expected = array('language' => 'en', 'territory' => 'US');
$this->assertEqual($expected, Locale::decompose('en-US'));
$expected = array(
'language' => 'en',
'territory' => 'US',
'variant' => 'posiX'
);
$this->assertEqual($expected, Locale::decompose('en_US-posiX'));
$expected = array('language' => 'kpe', 'territory' => 'gn');
$this->assertEqual($expected, Locale::decompose('kpe_gn'));
$expected = array(
'language' => 'ZH',
'script' => 'HANS',
'territory' => 'HK',
'variant' => 'REVISED'
);
$this->assertEqual($expected, Locale::decompose('ZH-HANS-HK_REVISED'));
}
/**
* Tests failing of parsing invalid locales.
*/
public function testDecomposeFail() {
$this->expectException();
try {
Locale::decompose('deee_DE');
$this->assert(false);
} catch (Exception $e) {
$this->assert(true);
}
$this->expectException();
try {
Locale::decompose('ZH-HANS-HK_REVISED_INVALID');
$this->assert(false);
} catch (Exception $e) {
$this->assert(true);
}
}
/**
* Tests parsing of locales using shortcut methods.
*/
public function testDecomposeUsingShortcutMethods() {
$this->assertEqual('zh', Locale::language('zh_Hans_HK_REVISED'));
$this->assertEqual('Hans', Locale::script('zh_Hans_HK_REVISED'));
$this->assertEqual('HK', Locale::territory('zh_Hans_HK_REVISED'));
$this->assertEqual('REVISED', Locale::variant('zh_Hans_HK_REVISED'));
$this->assertNull(Locale::script('zh_HK'));
$this->assertNull(Locale::territory('zh'));
$this->assertNull(Locale::variant('zh'));
$this->expectException();
try {
Locale::notAValidTag('zh_Hans_HK_REVISED');
$this->assert(false);
} catch (Exception $e) {
$this->assert(true);
}
}
/**
* Tests if the ouput of `compose()` can be used as the input for `decompose()`
* and vice versa.
*/
public function testComposeDecomposeCompose() {
$data = array('language' => 'en');
$result = Locale::compose(Locale::decompose(Locale::compose($data)));
$this->assertEqual('en', $result);
$data = array('language' => 'en', 'territory' => 'US');
$result = Locale::compose(Locale::decompose(Locale::compose($data)));
$this->assertEqual('en_US', $result);
$data = array(
'language' => 'zh',
'script' => 'Hans',
'territory' => 'HK',
'variant' => 'REVISED'
);
$result = Locale::compose(Locale::decompose(Locale::compose($data)));
$this->assertEqual('zh_Hans_HK_REVISED', $result);
}
/**
* Tests cascading of locales.
*/
public function testCascade() {
$this->assertEqual(array('root'), Locale::cascade('root'));
$this->assertEqual(array('en', 'root'), Locale::cascade('en'));
$this->assertEqual(array('en_US', 'en', 'root'), Locale::cascade('en_US'));
$expected = array('zh_HK_REVISED', 'zh_HK', 'zh', 'root');
$this->assertEqual($expected, Locale::cascade('zh_HK_REVISED'));
$expected = array('zh_Hans_HK', 'zh_Hans', 'zh', 'root');
$this->assertEqual($expected, Locale::cascade('zh_Hans_HK'));
$expected = array('zh_Hans_HK_REVISED', 'zh_Hans_HK', 'zh_Hans', 'zh', 'root');
$this->assertEqual($expected, Locale::cascade('zh_Hans_HK_REVISED'));
}
/**
* Tests formatting of locale.
*/
public function testCanonicalize() {
$this->assertEqual('en_US', Locale::canonicalize('en-US'));
$this->assertEqual('en_US_POSIX', Locale::canonicalize('en_US-posiX'));
$this->assertEqual('kpe_GN', Locale::canonicalize('kpe_gn'));
$this->assertEqual('zh_Hans_HK_REVISED', Locale::canonicalize('ZH-HANS-HK_REVISED'));
}
public function testLookup() {
$result = Locale::lookup(
array('zh_Hans_REVISED', 'zh_Hans_HK', 'zh', 'zh_Hans'), 'zh_Hans_HK_REVISED'
);
$this->assertEqual('zh_Hans_HK', $result);
$result = Locale::lookup(
array('zh', 'zh_Hans_REVISED', 'zh_Hans_HK', 'zh_Hans'),
'zh_Hans_HK_REVISED'
);
$this->assertEqual('zh_Hans_HK', $result);
$result = Locale::lookup(
array('en', 'en_UK', 'en_US', 'es', 'es_AR'),
'en'
);
$this->assertEqual('en', $result);
$result = Locale::lookup(
array('en', 'en_UK', 'en_US', 'es', 'es_AR'),
'en_UK'
);
$this->assertEqual('en_UK', $result);
$result = Locale::lookup(
array('en', 'en_UK', 'en_US', 'es', 'es_AR'),
'es_ES'
);
$this->assertEqual('es', $result);
$result = Locale::lookup(
array('en', 'en_UK', 'en_US', 'es_AR'),
'es_ES'
);
$this->assertEqual('es_AR', $result);
}
public function testPreferredFromActionRequest() {
$request = new ActionRequest(array(
'env' => array('HTTP_ACCEPT_LANGUAGE' => 'da, en-gb;q=0.8, en;q=0.7')
));
$result = Locale::preferred($request);
$this->assertEqual('da', $result);
$request = new ActionRequest(array('env' => array(
'HTTP_ACCEPT_LANGUAGE' => 'en-gb;q=0.8, da, en;q=0.7'
)));
$result = Locale::preferred($request);
$this->assertEqual('da', $result);
$request = new ActionRequest(array('env' => array(
'HTTP_ACCEPT_LANGUAGE' => 'en-gb;q=0.8, en;q=0.7'
)));
$result = Locale::preferred($request);
$this->assertEqual('en_GB', $result);
$request = new ActionRequest(array(
'env' => array('HTTP_ACCEPT_LANGUAGE' => 'da')
));
$result = Locale::preferred($request);
$this->assertEqual('da', $result);
}
public function testPreferredFromConsoleRequestLcAll() {
$request = new ConsoleRequest(array('env' => array('LC_ALL' => 'es_ES@euro')));
$result = Locale::preferred($request);
$this->assertEqual('es_ES', $result);
$request = new ConsoleRequest(array('env' => array('LC_ALL' => 'en_US.UTF-8')));
$result = Locale::preferred($request);
$this->assertEqual('en_US', $result);
$request = new ConsoleRequest(array('env' => array('LC_ALL' => 'en_US')));
$result = Locale::preferred($request);
$this->assertEqual('en_US', $result);
}
public function testPreferredFromConsoleRequestLanguage() {
$request = new ConsoleRequest(array('env' => array('LANGUAGE' => 'sv_SE:nn_NO:de_DE')));
$result = Locale::preferred($request);
$this->assertEqual('sv_SE', $result);
}
public function testPreferredFromConsoleRequestLang() {
$request = new ConsoleRequest(array('env' => array(
'LANG' => 'es_ES@euro', 'LANGUAGE' => null, 'LC_ALL' => null
)));
$result = Locale::preferred($request);
$this->assertEqual('es_ES', $result);
}
public function testPreferredFromConsoleRequestPrecedence() {
$request = new ConsoleRequest(array('env' => array(
'LANGUAGE' => 'da_DK:ja_JP',
'LC_ALL' => 'fr_CA',
'LANG' => 'de_DE'
)));
$result = Locale::preferred($request);
$this->assertEqual('da_DK', $result);
$request = new ConsoleRequest(array(
'env' => array('LC_ALL' => 'fr_CA', 'LANG' => 'de_DE')
));
$result = Locale::preferred($request);
$this->assertEqual('fr_CA', $result);
}
public function testPreferredFromConsoleRequestEmptyLocales() {
$request = new ConsoleRequest(array(
'env' => array('LC_ALL' => 'C', 'LANG' => null, 'LANGUAGE' => null)
));
$this->assertNull(Locale::preferred($request));
$request = new ConsoleRequest(array(
'env' => array('LC_ALL' => 'POSIX', 'LANG' => null, 'LANGUAGE' => null)
));
$this->assertNull(Locale::preferred($request));
$request = new ConsoleRequest(array(
'env' => array('LC_ALL' => '', 'LANG' => null, 'LANGUAGE' => null)
));
$this->assertNull(Locale::preferred($request));
}
public function testPreferredAvailableNegotiation() {
$result = Locale::preferred(
array('nl_NL', 'nl_BE', 'nl', 'en_US', 'en'),
array('en', 'en_US', 'nl_BE')
);
$this->assertEqual('nl_BE', $result);
$result = Locale::preferred(
array('da', 'en_GB', 'en'),
array('da', 'en_GB', 'en')
);
$this->assertEqual('da', $result);
$result = Locale::preferred(
array('da', 'en_GB', 'en'),
array('en', 'en_GB', 'da')
);
$this->assertEqual('da', $result);
$result = Locale::preferred(
array('da', 'en_GB', 'en'),
array('en_GB', 'en')
);
$this->assertEqual('en_GB', $result);
$result = Locale::preferred(
array('da_DK', 'en_GB', 'en'),
array('da', 'en_GB', 'en')
);
$this->assertEqual('da', $result);
$result = Locale::preferred(
array('zh_Hans_REVISED', 'zh_Hans_HK', 'zh', 'en'),
array('zh_Hans_HK_REVISED', 'zh_Hans_HK', 'zh', 'en')
);
$this->assertEqual('zh', $result);
$result = Locale::preferred(
array('es_ES', 'en'),
array('da', 'en_GB', 'en', 'es_AR')
);
$this->assertEqual('es_AR', $result);
}
}
?>