31548b6bd27ecf1ab7499be65b5ecdfc546f7186
[lhc/web/wiklou.git] / tests / qunit / suites / resources / mediawiki / mediawiki.language.test.js
1 ( function ( mw, $ ) {
2 'use strict';
3
4 var grammarTests, bcp47Tests;
5
6 QUnit.module( 'mediawiki.language', QUnit.newMwEnvironment( {
7 setup: function () {
8 this.liveLangData = mw.language.data;
9 mw.language.data = {};
10 },
11 teardown: function () {
12 mw.language.data = this.liveLangData;
13 },
14 messages: {
15 // mw.language.listToText test
16 and: ' and',
17 'comma-separator': ', ',
18 'word-separator': ' '
19 }
20 } ) );
21
22 QUnit.test( 'mw.language getData and setData', function ( assert ) {
23 mw.language.setData( 'en', 'testkey', 'testvalue' );
24 assert.equal( mw.language.getData( 'en', 'testkey' ), 'testvalue', 'Getter setter test for mw.language' );
25 assert.equal( mw.language.getData( 'en', 'invalidkey' ), undefined, 'Getter setter test for mw.language with invalid key' );
26 mw.language.setData( 'en-us', 'testkey', 'testvalue' );
27 assert.equal( mw.language.getData( 'en-US', 'testkey' ), 'testvalue', 'Case insensitive test for mw.language' );
28 } );
29
30 QUnit.test( 'mw.language.commafy test', function ( assert ) {
31 mw.language.setData( 'en', 'digitGroupingPattern', null );
32 mw.language.setData( 'en', 'digitTransformTable', null );
33 mw.language.setData( 'en', 'separatorTransformTable', null );
34
35 mw.config.set( 'wgUserLanguage', 'en' );
36 // Number grouping patterns are as per http://cldr.unicode.org/translation/number-patterns
37 assert.equal( mw.language.commafy( 1234.567, '###0.#####' ), '1234.567', 'Pattern with no digit grouping separator defined' );
38 assert.equal( mw.language.commafy( 123456789.567, '###0.#####' ), '123456789.567', 'Pattern with no digit grouping separator defined, bigger decimal part' );
39 assert.equal( mw.language.commafy( 0.567, '###0.#####' ), '0.567', 'Decimal part 0' );
40 assert.equal( mw.language.commafy( '.567', '###0.#####' ), '0.567', 'Decimal part missing. replace with zero' );
41 assert.equal( mw.language.commafy( 1234, '##,#0.#####' ), '12,34', 'Pattern with no fractional part' );
42 assert.equal( mw.language.commafy( -1234.567, '###0.#####' ), '-1234.567', 'Negative number' );
43 assert.equal( mw.language.commafy( -1234.567, '#,###.00' ), '-1,234.56', 'Fractional part bigger than pattern.' );
44 assert.equal( mw.language.commafy( 123456789.567, '###,##0.00' ), '123,456,789.56', 'Decimal part as group of 3' );
45 assert.equal( mw.language.commafy( 123456789.567, '###,###,#0.00' ), '1,234,567,89.56', 'Decimal part as group of 3 and last one 2' );
46 } );
47
48 QUnit.test( 'mw.language.convertNumber', function ( assert ) {
49 mw.language.setData( 'en', 'digitGroupingPattern', null );
50 mw.language.setData( 'en', 'digitTransformTable', null );
51 mw.language.setData( 'en', 'separatorTransformTable', { ',': '.', '.': ',' } );
52 mw.language.setData( 'en', 'minimumGroupingDigits', null );
53 mw.config.set( 'wgUserLanguage', 'en' );
54 mw.config.set( 'wgTranslateNumerals', true );
55
56 assert.equal( mw.language.convertNumber( 180 ), '180', 'formatting 3-digit' );
57 assert.equal( mw.language.convertNumber( 1800 ), '1.800', 'formatting 4-digit' );
58 assert.equal( mw.language.convertNumber( 18000 ), '18.000', 'formatting 5-digit' );
59
60 assert.equal( mw.language.convertNumber( '1.800', true ), '1800', 'unformatting' );
61
62 mw.language.setData( 'en', 'minimumGroupingDigits', 2 );
63 assert.equal( mw.language.convertNumber( 180 ), '180', 'formatting 3-digit with minimumGroupingDigits=2' );
64 assert.equal( mw.language.convertNumber( 1800 ), '1800', 'formatting 4-digit with minimumGroupingDigits=2' );
65 assert.equal( mw.language.convertNumber( 18000 ), '18.000', 'formatting 5-digit with minimumGroupingDigits=2' );
66 } );
67
68 QUnit.test( 'mw.language.convertNumber - digitTransformTable', function ( assert ) {
69 mw.config.set( 'wgUserLanguage', 'hi' );
70 mw.config.set( 'wgTranslateNumerals', true );
71 mw.language.setData( 'hi', 'digitGroupingPattern', null );
72 mw.language.setData( 'hi', 'separatorTransformTable', { ',': '.', '.': ',' } );
73 mw.language.setData( 'hi', 'minimumGroupingDigits', null );
74
75 // Example from Hindi (MessagesHi.php)
76 mw.language.setData( 'hi', 'digitTransformTable', {
77 0: '०',
78 1: '१',
79 2: '२'
80 } );
81
82 assert.equal( mw.language.convertNumber( 1200 ), '१.२००', 'format' );
83 assert.equal( mw.language.convertNumber( '१.२००', true ), '1200', 'unformat from digit transform' );
84 assert.equal( mw.language.convertNumber( '1.200', true ), '1200', 'unformat plain' );
85
86 mw.config.set( 'wgTranslateNumerals', false );
87
88 assert.equal( mw.language.convertNumber( 1200 ), '1.200', 'format (digit transform disabled)' );
89 assert.equal( mw.language.convertNumber( '१.२००', true ), '1200', 'unformat from digit transform (when disabled)' );
90 assert.equal( mw.language.convertNumber( '1.200', true ), '1200', 'unformat plain (digit transform disabled)' );
91 } );
92
93 function grammarTest( langCode, test ) {
94 // The test works only if the content language is opt.language
95 // because it requires [lang].js to be loaded.
96 QUnit.test( 'Grammar test for lang=' + langCode, function ( assert ) {
97 var i;
98 for ( i = 0; i < test.length; i++ ) {
99 assert.equal(
100 mw.language.convertGrammar( test[ i ].word, test[ i ].grammarForm ),
101 test[ i ].expected,
102 test[ i ].description
103 );
104 }
105 } );
106 }
107
108 // These tests run only for the current UI language.
109 grammarTests = {
110 bs: [
111 {
112 word: 'word',
113 grammarForm: 'instrumental',
114 expected: 's word',
115 description: 'Grammar test for instrumental case'
116 },
117 {
118 word: 'word',
119 grammarForm: 'lokativ',
120 expected: 'o word',
121 description: 'Grammar test for lokativ case'
122 }
123 ],
124
125 he: [
126 {
127 word: 'ויקיפדיה',
128 grammarForm: 'prefixed',
129 expected: 'וויקיפדיה',
130 description: 'Duplicate the "Waw" if prefixed'
131 },
132 {
133 word: 'וולפגנג',
134 grammarForm: 'prefixed',
135 expected: 'וולפגנג',
136 description: 'Duplicate the "Waw" if prefixed, but not if it is already duplicated.'
137 },
138 {
139 word: 'הקובץ',
140 grammarForm: 'prefixed',
141 expected: 'קובץ',
142 description: 'Remove the "He" if prefixed'
143 },
144 {
145 word: 'Wikipedia',
146 grammarForm: 'תחילית',
147 expected: '־Wikipedia',
148 description: 'Add a hyphen (maqaf) before non-Hebrew letters'
149 },
150 {
151 word: '1995',
152 grammarForm: 'תחילית',
153 expected: '־1995',
154 description: 'Add a hyphen (maqaf) before numbers'
155 }
156 ],
157
158 hsb: [
159 {
160 word: 'word',
161 grammarForm: 'instrumental',
162 expected: 'z word',
163 description: 'Grammar test for instrumental case'
164 },
165 {
166 word: 'word',
167 grammarForm: 'lokatiw',
168 expected: 'wo word',
169 description: 'Grammar test for lokatiw case'
170 }
171 ],
172
173 dsb: [
174 {
175 word: 'word',
176 grammarForm: 'instrumental',
177 expected: 'z word',
178 description: 'Grammar test for instrumental case'
179 },
180 {
181 word: 'word',
182 grammarForm: 'lokatiw',
183 expected: 'wo word',
184 description: 'Grammar test for lokatiw case'
185 }
186 ],
187
188 hy: [
189 {
190 word: 'Մաունա',
191 grammarForm: 'genitive',
192 expected: 'Մաունայի',
193 description: 'Grammar test for genitive case'
194 },
195 {
196 word: 'հետո',
197 grammarForm: 'genitive',
198 expected: 'հետոյի',
199 description: 'Grammar test for genitive case'
200 },
201 {
202 word: 'գիրք',
203 grammarForm: 'genitive',
204 expected: 'գրքի',
205 description: 'Grammar test for genitive case'
206 },
207 {
208 word: 'ժամանակի',
209 grammarForm: 'genitive',
210 expected: 'ժամանակիի',
211 description: 'Grammar test for genitive case'
212 }
213 ],
214
215 fi: [
216 {
217 word: 'talo',
218 grammarForm: 'genitive',
219 expected: 'talon',
220 description: 'Grammar test for genitive case'
221 },
222 {
223 word: 'linux',
224 grammarForm: 'genitive',
225 expected: 'linuxin',
226 description: 'Grammar test for genitive case'
227 },
228 {
229 word: 'talo',
230 grammarForm: 'elative',
231 expected: 'talosta',
232 description: 'Grammar test for elative case'
233 },
234 {
235 word: 'pastöroitu',
236 grammarForm: 'partitive',
237 expected: 'pastöroitua',
238 description: 'Grammar test for partitive case'
239 },
240 {
241 word: 'talo',
242 grammarForm: 'partitive',
243 expected: 'taloa',
244 description: 'Grammar test for partitive case'
245 },
246 {
247 word: 'talo',
248 grammarForm: 'illative',
249 expected: 'taloon',
250 description: 'Grammar test for illative case'
251 },
252 {
253 word: 'linux',
254 grammarForm: 'inessive',
255 expected: 'linuxissa',
256 description: 'Grammar test for inessive case'
257 }
258 ],
259
260 ru: [
261 {
262 word: 'тесть',
263 grammarForm: 'genitive',
264 expected: 'тестя',
265 description: 'Grammar test for genitive case, тесть -> тестя'
266 },
267 {
268 word: 'привилегия',
269 grammarForm: 'genitive',
270 expected: 'привилегии',
271 description: 'Grammar test for genitive case, привилегия -> привилегии'
272 },
273 {
274 word: 'установка',
275 grammarForm: 'genitive',
276 expected: 'установки',
277 description: 'Grammar test for genitive case, установка -> установки'
278 },
279 {
280 word: 'похоти',
281 grammarForm: 'genitive',
282 expected: 'похотей',
283 description: 'Grammar test for genitive case, похоти -> похотей'
284 },
285 {
286 word: 'доводы',
287 grammarForm: 'genitive',
288 expected: 'доводов',
289 description: 'Grammar test for genitive case, доводы -> доводов'
290 },
291 {
292 word: 'песчаник',
293 grammarForm: 'genitive',
294 expected: 'песчаника',
295 description: 'Grammar test for genitive case, песчаник -> песчаника'
296 },
297 {
298 word: 'данные',
299 grammarForm: 'genitive',
300 expected: 'данных',
301 description: 'Grammar test for genitive case, данные -> данных'
302 },
303 {
304 word: 'тесть',
305 grammarForm: 'prepositional',
306 expected: 'тесте',
307 description: 'Grammar test for prepositional case, тесть -> тесте'
308 },
309 {
310 word: 'привилегия',
311 grammarForm: 'prepositional',
312 expected: 'привилегии',
313 description: 'Grammar test for prepositional case, привилегия -> привилегии'
314 },
315 {
316 word: 'установка',
317 grammarForm: 'prepositional',
318 expected: 'установке',
319 description: 'Grammar test for prepositional case, установка -> установке'
320 },
321 {
322 word: 'похоти',
323 grammarForm: 'prepositional',
324 expected: 'похотях',
325 description: 'Grammar test for prepositional case, похоти -> похотях'
326 },
327 {
328 word: 'доводы',
329 grammarForm: 'prepositional',
330 expected: 'доводах',
331 description: 'Grammar test for prepositional case, доводы -> доводах'
332 },
333 {
334 word: 'Викисклад',
335 grammarForm: 'prepositional',
336 expected: 'Викискладе',
337 description: 'Grammar test for prepositional case, Викисклад -> Викискладе'
338 },
339 {
340 word: 'Викисклад',
341 grammarForm: 'genitive',
342 expected: 'Викисклада',
343 description: 'Grammar test for genitive case, Викисклад -> Викисклада'
344 },
345 {
346 word: 'песчаник',
347 grammarForm: 'prepositional',
348 expected: 'песчанике',
349 description: 'Grammar test for prepositional case, песчаник -> песчанике'
350 },
351 {
352 word: 'данные',
353 grammarForm: 'prepositional',
354 expected: 'данных',
355 description: 'Grammar test for prepositional case, данные -> данных'
356 },
357 {
358 word: 'русский',
359 grammarForm: 'languagegen',
360 expected: 'русского',
361 description: 'Grammar test for languagegen case, русский -> русского'
362 },
363 {
364 word: 'немецкий',
365 grammarForm: 'languagegen',
366 expected: 'немецкого',
367 description: 'Grammar test for languagegen case, немецкий -> немецкого'
368 },
369 {
370 word: 'иврит',
371 grammarForm: 'languagegen',
372 expected: 'иврита',
373 description: 'Grammar test for languagegen case, иврит -> иврита'
374 },
375 {
376 word: 'эсперанто',
377 grammarForm: 'languagegen',
378 expected: 'эсперанто',
379 description: 'Grammar test for languagegen case, эсперанто -> эсперанто'
380 },
381 {
382 word: 'русский',
383 grammarForm: 'languageprep',
384 expected: 'русском',
385 description: 'Grammar test for languageprep case, русский -> русском'
386 },
387 {
388 word: 'немецкий',
389 grammarForm: 'languageprep',
390 expected: 'немецком',
391 description: 'Grammar test for languageprep case, немецкий -> немецком'
392 },
393 {
394 word: 'идиш',
395 grammarForm: 'languageprep',
396 expected: 'идише',
397 description: 'Grammar test for languageprep case, идиш -> идише'
398 },
399 {
400 word: 'эсперанто',
401 grammarForm: 'languageprep',
402 expected: 'эсперанто',
403 description: 'Grammar test for languageprep case, эсперанто -> эсперанто'
404 },
405 {
406 word: 'русский',
407 grammarForm: 'languageadverb',
408 expected: 'по-русски',
409 description: 'Grammar test for languageadverb case, русский -> по-русски'
410 },
411 {
412 word: 'немецкий',
413 grammarForm: 'languageadverb',
414 expected: 'по-немецки',
415 description: 'Grammar test for languageadverb case, немецкий -> по-немецки'
416 },
417 {
418 word: 'иврит',
419 grammarForm: 'languageadverb',
420 expected: 'на иврите',
421 description: 'Grammar test for languageadverb case, иврит -> на иврите'
422 },
423 {
424 word: 'эсперанто',
425 grammarForm: 'languageadverb',
426 expected: 'на эсперанто',
427 description: 'Grammar test for languageadverb case, эсперанто -> на эсперанто'
428 },
429 {
430 word: 'гуарани',
431 grammarForm: 'languageadverb',
432 expected: 'на языке гуарани',
433 description: 'Grammar test for languageadverb case, гуарани -> на языке гуарани'
434 }
435 ],
436
437 hu: [
438 {
439 word: 'Wikipédiá',
440 grammarForm: 'rol',
441 expected: 'Wikipédiáról',
442 description: 'Grammar test for rol case'
443 },
444 {
445 word: 'Wikipédiá',
446 grammarForm: 'ba',
447 expected: 'Wikipédiába',
448 description: 'Grammar test for ba case'
449 },
450 {
451 word: 'Wikipédiá',
452 grammarForm: 'k',
453 expected: 'Wikipédiák',
454 description: 'Grammar test for k case'
455 }
456 ],
457
458 ga: [
459 {
460 word: 'an Domhnach',
461 grammarForm: 'ainmlae',
462 expected: 'Dé Domhnaigh',
463 description: 'Grammar test for ainmlae case'
464 },
465 {
466 word: 'an Luan',
467 grammarForm: 'ainmlae',
468 expected: 'Dé Luain',
469 description: 'Grammar test for ainmlae case'
470 },
471 {
472 word: 'an Satharn',
473 grammarForm: 'ainmlae',
474 expected: 'Dé Sathairn',
475 description: 'Grammar test for ainmlae case'
476 }
477 ],
478
479 uk: [
480 {
481 word: 'Вікіпедія',
482 grammarForm: 'genitive',
483 expected: 'Вікіпедії',
484 description: 'Grammar test for genitive case'
485 },
486 {
487 word: 'Віківиди',
488 grammarForm: 'genitive',
489 expected: 'Віківидів',
490 description: 'Grammar test for genitive case'
491 },
492 {
493 word: 'Вікіцитати',
494 grammarForm: 'genitive',
495 expected: 'Вікіцитат',
496 description: 'Grammar test for genitive case'
497 },
498 {
499 word: 'Вікіпідручник',
500 grammarForm: 'genitive',
501 expected: 'Вікіпідручника',
502 description: 'Grammar test for genitive case'
503 },
504 {
505 word: 'Вікіпедія',
506 grammarForm: 'accusative',
507 expected: 'Вікіпедію',
508 description: 'Grammar test for accusative case'
509 }
510 ],
511
512 sl: [
513 {
514 word: 'word',
515 grammarForm: 'orodnik',
516 expected: 'z word',
517 description: 'Grammar test for orodnik case'
518 },
519 {
520 word: 'word',
521 grammarForm: 'mestnik',
522 expected: 'o word',
523 description: 'Grammar test for mestnik case'
524 }
525 ],
526
527 os: [
528 {
529 word: 'бæстæ',
530 grammarForm: 'genitive',
531 expected: 'бæсты',
532 description: 'Grammar test for genitive case'
533 },
534 {
535 word: 'бæстæ',
536 grammarForm: 'allative',
537 expected: 'бæстæм',
538 description: 'Grammar test for allative case'
539 },
540 {
541 word: 'Тигр',
542 grammarForm: 'dative',
543 expected: 'Тигрæн',
544 description: 'Grammar test for dative case'
545 },
546 {
547 word: 'цъити',
548 grammarForm: 'dative',
549 expected: 'цъитийæн',
550 description: 'Grammar test for dative case'
551 },
552 {
553 word: 'лæппу',
554 grammarForm: 'genitive',
555 expected: 'лæппуйы',
556 description: 'Grammar test for genitive case'
557 },
558 {
559 word: '2011',
560 grammarForm: 'equative',
561 expected: '2011-ау',
562 description: 'Grammar test for equative case'
563 }
564 ],
565
566 la: [
567 {
568 word: 'Translatio',
569 grammarForm: 'genitive',
570 expected: 'Translationis',
571 description: 'Grammar test for genitive case'
572 },
573 {
574 word: 'Translatio',
575 grammarForm: 'accusative',
576 expected: 'Translationem',
577 description: 'Grammar test for accusative case'
578 },
579 {
580 word: 'Translatio',
581 grammarForm: 'ablative',
582 expected: 'Translatione',
583 description: 'Grammar test for ablative case'
584 }
585 ]
586 };
587
588 $.each( grammarTests, function ( langCode, test ) {
589 if ( langCode === mw.config.get( 'wgUserLanguage' ) ) {
590 grammarTest( langCode, test );
591 }
592 } );
593
594 QUnit.test( 'List to text test', function ( assert ) {
595 assert.equal( mw.language.listToText( [] ), '', 'Blank list' );
596 assert.equal( mw.language.listToText( [ 'a' ] ), 'a', 'Single item' );
597 assert.equal( mw.language.listToText( [ 'a', 'b' ] ), 'a and b', 'Two items' );
598 assert.equal( mw.language.listToText( [ 'a', 'b', 'c' ] ), 'a, b and c', 'More than two items' );
599 } );
600
601 bcp47Tests = [
602 // Extracted from BCP 47 (list not exhaustive)
603 // # 2.1.1
604 [ 'en-ca-x-ca', 'en-CA-x-ca' ],
605 [ 'sgn-be-fr', 'sgn-BE-FR' ],
606 [ 'az-latn-x-latn', 'az-Latn-x-latn' ],
607 // # 2.2
608 [ 'sr-Latn-RS', 'sr-Latn-RS' ],
609 [ 'az-arab-ir', 'az-Arab-IR' ],
610
611 // # 2.2.5
612 [ 'sl-nedis', 'sl-nedis' ],
613 [ 'de-ch-1996', 'de-CH-1996' ],
614
615 // # 2.2.6
616 [
617 'en-latn-gb-boont-r-extended-sequence-x-private',
618 'en-Latn-GB-boont-r-extended-sequence-x-private'
619 ],
620
621 // Examples from BCP 47 Appendix A
622 // # Simple language subtag:
623 [ 'DE', 'de' ],
624 [ 'fR', 'fr' ],
625 [ 'ja', 'ja' ],
626
627 // # Language subtag plus script subtag:
628 [ 'zh-hans', 'zh-Hans' ],
629 [ 'sr-cyrl', 'sr-Cyrl' ],
630 [ 'sr-latn', 'sr-Latn' ],
631
632 // # Extended language subtags and their primary language subtag
633 // # counterparts:
634 [ 'zh-cmn-hans-cn', 'zh-cmn-Hans-CN' ],
635 [ 'cmn-hans-cn', 'cmn-Hans-CN' ],
636 [ 'zh-yue-hk', 'zh-yue-HK' ],
637 [ 'yue-hk', 'yue-HK' ],
638
639 // # Language-Script-Region:
640 [ 'zh-hans-cn', 'zh-Hans-CN' ],
641 [ 'sr-latn-RS', 'sr-Latn-RS' ],
642
643 // # Language-Variant:
644 [ 'sl-rozaj', 'sl-rozaj' ],
645 [ 'sl-rozaj-biske', 'sl-rozaj-biske' ],
646 [ 'sl-nedis', 'sl-nedis' ],
647
648 // # Language-Region-Variant:
649 [ 'de-ch-1901', 'de-CH-1901' ],
650 [ 'sl-it-nedis', 'sl-IT-nedis' ],
651
652 // # Language-Script-Region-Variant:
653 [ 'hy-latn-it-arevela', 'hy-Latn-IT-arevela' ],
654
655 // # Language-Region:
656 [ 'de-de', 'de-DE' ],
657 [ 'en-us', 'en-US' ],
658 [ 'es-419', 'es-419' ],
659
660 // # Private use subtags:
661 [ 'de-ch-x-phonebk', 'de-CH-x-phonebk' ],
662 [ 'az-arab-x-aze-derbend', 'az-Arab-x-aze-derbend' ],
663 /**
664 * Previous test does not reflect the BCP 47 which states:
665 * az-Arab-x-AZE-derbend
666 * AZE being private, it should be lower case, hence the test above
667 * should probably be:
668 * [ 'az-arab-x-aze-derbend', 'az-Arab-x-AZE-derbend' ],
669 */
670
671 // # Private use registry values:
672 [ 'x-whatever', 'x-whatever' ],
673 [ 'qaa-qaaa-qm-x-southern', 'qaa-Qaaa-QM-x-southern' ],
674 [ 'de-qaaa', 'de-Qaaa' ],
675 [ 'sr-latn-qm', 'sr-Latn-QM' ],
676 [ 'sr-qaaa-rs', 'sr-Qaaa-RS' ],
677
678 // # Tags that use extensions
679 [ 'en-us-u-islamcal', 'en-US-u-islamcal' ],
680 [ 'zh-cn-a-myext-x-private', 'zh-CN-a-myext-x-private' ],
681 [ 'en-a-myext-b-another', 'en-a-myext-b-another' ]
682
683 // # Invalid:
684 // de-419-DE
685 // a-DE
686 // ar-a-aaa-b-bbb-a-ccc
687 ];
688
689 QUnit.test( 'mw.language.bcp47', function ( assert ) {
690 bcp47Tests.forEach( function ( data ) {
691 var input = data[ 0 ],
692 expected = data[ 1 ];
693 assert.equal( mw.language.bcp47( input ), expected );
694 } );
695 } );
696 }( mediaWiki, jQuery ) );