-
-
Notifications
You must be signed in to change notification settings - Fork 861
/
basic.test.js
761 lines (657 loc) · 22.1 KB
/
basic.test.js
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
import messages from './fixture/index'
import dateTimeFormats from './fixture/datetime'
import numberFormats from './fixture/number'
describe('basic', () => {
let i18n
beforeEach(() => {
i18n = new VueI18n({
locale: 'en',
fallbackLocale: 'en',
messages
})
})
describe('i18n#t', () => {
describe('en locale', () => {
it('should translate an english', () => {
assert.strictEqual(i18n.t('message.hello'), messages.en.message.hello)
})
})
describe('empty string', () => {
it('should support empty string', () => {
assert.strictEqual(i18n.t('message.empty'), messages.en.message.empty)
})
})
describe('linked translation', () => {
it('should translate simple link', () => {
assert.strictEqual(i18n.t('message.link'), messages.en.message.hello)
})
it('should translate link at the end of locale', () => {
assert.strictEqual(i18n.t('message.linkEnd'), 'This is a linked translation to the world')
})
it('should translate link within a locale', () => {
assert.strictEqual(i18n.t('message.linkWithin'), 'Isn\'t the world we live in great?')
})
it('should translate multiple links within a locale', () => {
assert.strictEqual(i18n.t('message.linkMultiple'), 'Hello hoge!, isn\'t the world great?')
})
it('should translate link with braces ', () => {
assert.strictEqual(i18n.t('message.linkBrackets'), 'Hello hoge. Isn\'t the world great?')
})
it('should translate link with lower-case formatting', () => {
assert.strictEqual(i18n.t('message.linkCaseLower'), 'Please provide home address')
})
it('should translate link with upper-case formatting', () => {
assert.strictEqual(i18n.t('message.linkCaseUpper'), 'HOME ADDRESS')
})
it('should translate link without formatting if modifier is not known.', () => {
assert.strictEqual(i18n.t('message.linkCaseUnknown'), 'Home address')
})
})
describe('ja locale', () => {
it('should translate a japanese', () => {
assert.strictEqual(i18n.t('message.hello', 'ja'), messages.ja.message.hello)
})
})
describe('key argument', () => {
describe('not specify', () => {
it('should return empty string', () => {
assert.strictEqual(i18n.t(), '')
})
})
describe('empty string', () => {
it('should return empty string', () => {
assert.strictEqual(i18n.t(''), '')
})
})
describe('not regist key', () => {
it('should return key string', () => {
assert.strictEqual(i18n.t('foo.bar'), 'foo.bar')
})
})
describe('sentence fragment', () => {
it('should translate fragment', () => {
assert.strictEqual(i18n.t('hello world'), 'Hello World')
})
it('should return replaced string if available', () => {
assert.strictEqual(
i18n.t('Hello {0}', ['kazupon']),
'Hello kazupon'
)
})
it('should return key if unavailable', () => {
assert.strictEqual(i18n.t('Hello'), 'Hello')
})
})
describe('object keypath', () => {
it('should be translated', () => {
assert.strictEqual(i18n.t('message.format'), messages.en.message.format)
})
})
describe('array keypath', () => {
describe('basic', () => {
it('should be translated', () => {
assert.strictEqual(i18n.t('errors[0]'), messages.en.errors[0])
})
})
describe('object attribute', () => {
it('should be translated', () => {
assert.strictEqual(i18n.t('errors[1].internal1'), messages.en.errors[1].internal1)
})
})
describe('object', () => {
it('should be translated', () => {
assert.strictEqual(i18n.t('errors[1]'), messages.en.errors[1])
})
})
describe('array', () => {
it('should be translated', () => {
assert.strictEqual(i18n.t('errors[2][0]'), messages.en.errors[2][0])
})
})
})
})
describe('format arguments', () => {
describe('named', () => {
it('should return replaced string', () => {
assert.strictEqual(
i18n.t('message.format.named', { name: 'kazupon' }),
'Hello kazupon, how are you?'
)
})
})
describe('list', () => {
it('should return replaced string', () => {
assert.strictEqual(
i18n.t('message.format.list', ['kazupon']),
'Hello kazupon, how are you?'
)
})
})
})
describe('locale argument', () => {
it('should return empty string', () => {
assert.strictEqual(i18n.t('message.hello', 'ja'), messages.ja.message.hello)
})
})
describe('format & locale arguments', () => {
it('should return replaced string', () => {
assert.strictEqual(
i18n.t('message.format.list', 'ja', ['kazupon']),
'こんにちは kazupon, ごきげんいかが?'
)
})
})
describe('fallback', () => {
it('should return fallback string', () => {
assert.strictEqual(
i18n.t('message.fallback', 'ja'),
messages.en.message.fallback
)
})
})
})
describe('i18n#tc', () => {
describe('default choice', () => {
it('should be choice singluar', () => {
assert.strictEqual(i18n.tc('plurals.apple'), 'one apple')
})
})
describe('split plural with zero choice', () => {
it('should allow a zero choice, a one choice and a plural choice', () => {
const count = 10
assert.strictEqual(i18n.tc('plurals.apple', 0), 'no apples')
assert.strictEqual(i18n.tc('plurals.apple', 1), 'one apple')
assert.strictEqual(i18n.tc('plurals.apple', count, { count }), '10 apples')
})
})
describe('implicit choice exposing', () => {
describe('en locale', () => {
it('should expose "count" implicitly to locale message', () => {
assert.strictEqual(i18n.tc('plurals.apple', 10), '10 apples')
})
it('should not expose if given explicitly', () => {
const explicitArgs = { 'count': 'Many' }
assert.strictEqual(i18n.tc('plurals.apple', 10, explicitArgs), 'Many apples')
})
})
describe('ja locale', () => {
it('should expose "count" and "n" implicitly to locale message', () => {
assert.strictEqual(i18n.tc('plurals.implicitPluralCount', 10, 'ja'), 'count:10, n:10')
})
it('should not expose if given explicitly', () => {
const explicitArgs = { 'count': 'たくさん', 'n': '大量' }
assert.strictEqual(i18n.tc('plurals.implicitPluralCount', 10, 'ja', explicitArgs), 'count:たくさん, n:大量')
})
})
})
describe('en locale', () => {
it('should translate an english', () => {
assert.strictEqual(i18n.tc('plurals.car', 1), 'car')
})
})
describe('multi plural check', () => {
it('should fetch pluralized string', () => {
assert.strictEqual(i18n.tc('plurals.car', 2), 'cars')
})
})
describe('ja locale', () => {
it('should translate a japanese', () => {
assert.strictEqual(i18n.tc('plurals.car', 1, 'ja'), 'ザ・ワールド')
})
})
describe('key argument', () => {
describe('not specify', () => {
it('should return empty string', () => {
assert.strictEqual(i18n.tc(), '')
})
})
describe('empty string', () => {
it('should return empty string', () => {
assert.strictEqual(i18n.tc(''), '')
})
})
describe('not regist key', () => {
it('should return key string', () => {
assert.strictEqual(i18n.tc('foo.bar'), 'foo.bar')
})
})
describe('sentence fragment', () => {
it('should translate fragment', () => {
assert.strictEqual(i18n.tc('hello world'), 'Hello World')
})
it('should return replaced string if available', () => {
assert.strictEqual(
i18n.tc('Hello {0}', 1, ['kazupon']),
'Hello kazupon'
)
})
it('should return key if unavailable', () => {
assert.strictEqual(i18n.tc('Hello'), 'Hello')
})
})
})
describe('format arguments', () => {
describe('named', () => {
it('should return replaced string', () => {
assert.strictEqual(
i18n.tc('plurals.format.named', 1, { name: 'kazupon' }),
'Hello kazupon, how are you?'
)
})
})
describe('list', () => {
it('should return replaced string', () => {
assert.strictEqual(
i18n.tc('plurals.format.list', 1, ['kazupon']),
'Hello kazupon, how are you?'
)
})
})
})
describe('locale argument', () => {
it('should return empty string', () => {
assert.strictEqual(i18n.tc('plurals.car', 1, 'ja'), 'ザ・ワールド')
})
})
describe('format & locale arguments', () => {
it('should return replaced string', () => {
assert.strictEqual(
i18n.tc('plurals.format.list', 1, 'ja', ['kazupon']),
'こんにちは kazupon, ごきげんいかが?'
)
})
})
describe('fallback', () => {
it('should return fallback string', () => {
assert.strictEqual(
i18n.tc('plurals.fallback', 1, 'ja'),
'これはフォールバック'
)
})
})
})
describe('i18n#te', () => {
describe('existing key', () => {
it('should return true', () => {
assert(i18n.te('message.hello') === true)
})
it('should return true with locale', () => {
assert(i18n.te('message.hello', 'ja') === true)
})
})
describe('not existing key', () => {
it('should return false', () => {
assert(i18n.te('message.hallo') === false)
})
it('should return false with locale', () => {
assert(i18n.te('message.hello', 'xx') === false)
})
})
})
describe('$t', () => {
describe('en locale', () => {
it('should translate an english', () => {
const vm = new Vue({ i18n })
assert.strictEqual(vm.$t('message.hello'), messages.en.message.hello)
})
})
describe('ja locale', () => {
it('should translate a japanese', () => {
const vm = new Vue({ i18n })
assert.strictEqual(vm.$t('message.hello', 'ja'), messages.ja.message.hello)
})
})
describe('key argument', () => {
describe('not specify', () => {
it('should return empty string', () => {
const vm = new Vue({ i18n })
assert.strictEqual(vm.$t(), '')
})
})
describe('empty string', () => {
it('should return empty string', () => {
const vm = new Vue({ i18n })
assert.strictEqual(vm.$t(''), '')
})
})
describe('not regist key', () => {
it('should return key string', () => {
const vm = new Vue({ i18n })
assert.strictEqual(vm.$t('foo.bar'), 'foo.bar')
})
})
describe('sentence fragment', () => {
it('should translate fragment', () => {
const vm = new Vue({ i18n })
assert.strictEqual(vm.$t('hello world'), 'Hello World')
})
it('should return replaced string if available', () => {
const vm = new Vue({ i18n })
assert.strictEqual(
vm.$t('Hello {0}', ['kazupon']),
'Hello kazupon'
)
})
it('should return key if unavailable', () => {
const vm = new Vue({ i18n })
assert.strictEqual(vm.$t('Hello'), 'Hello')
})
})
})
describe('format arguments', () => {
describe('named', () => {
it('should return replaced string', () => {
const vm = new Vue({ i18n })
assert.strictEqual(
vm.$t('message.format.named', { name: 'kazupon' }),
'Hello kazupon, how are you?'
)
})
})
describe('list', () => {
it('should return replaced string', () => {
const vm = new Vue({ i18n })
assert.strictEqual(
vm.$t('message.format.list', ['kazupon']),
'Hello kazupon, how are you?'
)
})
})
})
describe('locale argument', () => {
it('should return empty string', () => {
const vm = new Vue({ i18n })
assert.strictEqual(vm.$t('message.hello', 'ja'), messages.ja.message.hello)
})
})
describe('format & locale arguments', () => {
it('should return replaced string', () => {
const vm = new Vue({ i18n })
assert.strictEqual(
vm.$t('message.format.list', 'ja', ['kazupon']),
'こんにちは kazupon, ごきげんいかが?'
)
})
})
describe('fallback', () => {
it('should return fallback string', () => {
const vm = new Vue({ i18n })
assert.strictEqual(
vm.$t('message.fallback', 'ja'),
messages.en.message.fallback
)
})
})
})
describe('$tc', () => {
describe('en locale', () => {
it('should translate plural english', () => {
const vm = new Vue({ i18n })
assert.strictEqual(vm.$tc('plurals.car', 1), 'car')
})
})
describe('multi plural check', () => {
it('should fetch pluralized string', () => {
const vm = new Vue({ i18n })
assert.strictEqual(vm.$tc('plurals.car', 2), 'cars')
})
})
describe('key argument', () => {
describe('not specify', () => {
it('should return empty string', () => {
const vm = new Vue({ i18n })
assert.strictEqual(vm.$tc(), '')
})
})
describe('empty string', () => {
it('should return empty string', () => {
const vm = new Vue({ i18n })
assert.strictEqual(vm.$tc(''), '')
})
})
describe('not regist key', () => {
it('should return key string', () => {
const vm = new Vue({ i18n })
assert.strictEqual(vm.$tc('foo.bar'), 'foo.bar')
})
})
describe('sentence fragment', () => {
it('should translate fragment', () => {
const vm = new Vue({ i18n })
assert.strictEqual(vm.$tc('hello world'), 'Hello World')
})
it('should return replaced string if available', () => {
const vm = new Vue({ i18n })
assert.strictEqual(
vm.$tc('Hello {0}', 1, ['kazupon']),
'Hello kazupon'
)
})
it('should return key if unavailable', () => {
const vm = new Vue({ i18n })
assert.strictEqual(vm.$tc('Hello'), 'Hello')
})
})
})
describe('format arguments', () => {
describe('named', () => {
it('should return replaced string', () => {
const vm = new Vue({ i18n })
assert.strictEqual(
vm.$tc('plurals.format.named', 1, { name: 'kazupon' }),
'Hello kazupon, how are you?'
)
})
})
describe('list', () => {
it('should return replaced string', () => {
const vm = new Vue({ i18n })
assert.strictEqual(
vm.$tc('plurals.format.list', 1, ['kazupon']),
'Hello kazupon, how are you?'
)
})
})
})
describe('locale argument', () => {
it('should return empty string', () => {
const vm = new Vue({ i18n })
assert.strictEqual(vm.$tc('plurals.car', 1, 'ja'), 'ザ・ワールド')
})
})
describe('format & locale arguments', () => {
it('should return replaced string', () => {
const vm = new Vue({ i18n })
assert.strictEqual(
vm.$tc('plurals.format.list', 1, 'ja', ['kazupon']),
'こんにちは kazupon, ごきげんいかが?'
)
})
})
describe('fallback', () => {
it('should return fallback string', () => {
const vm = new Vue({ i18n })
assert.strictEqual(
vm.$tc('plurals.fallback', 2, 'ja'),
'ザ・ワールド'
)
})
})
})
describe('$te', () => {
describe('existing key', () => {
it('should return true', () => {
const vm = new Vue({ i18n })
assert(vm.$te('message.hello') === true)
})
it('should return true with locale', () => {
const vm = new Vue({ i18n })
assert(vm.$te('message.hello', 'ja') === true)
})
})
describe('not existing key', () => {
it('should return false', () => {
const vm = new Vue({ i18n })
assert(vm.$te('message.hallo') === false)
})
it('should return false with locale', () => {
const vm = new Vue({ i18n })
assert(vm.$te('message.hello', 'xx') === false)
})
})
})
describe('i18n#locale', () => {
let el
beforeEach(() => {
el = document.createElement('div')
document.body.appendChild(el)
})
it('should be reactivity translate', done => {
const vm = new Vue({
el, i18n,
render (h) {
return h('p', {}, [this.$t('message.hello')])
}
})
nextTick(() => {
assert.strictEqual(vm.$el.textContent, messages.en.message.hello)
i18n.locale = 'ja' // set japanese
}).then(() => {
assert.strictEqual(vm.$el.textContent, messages.ja.message.hello)
}).then(done)
})
})
describe('i18n#fallbackLocale', () => {
let el
beforeEach(() => {
el = document.createElement('div')
document.body.appendChild(el)
})
it('should be reactivity translate', done => {
const vm = new Vue({
el, i18n,
render (h) {
return h('p', {}, [this.$t('message.fallback1')])
}
})
nextTick(() => {
assert.strictEqual(vm.$el.textContent, 'message.fallback1')
i18n.fallbackLocale = 'ja' // set fallback locale
}).then(() => {
assert.strictEqual(vm.$el.textContent, messages.ja.message.fallback1)
}).then(done)
})
})
describe('i18n#availableLocales', () => {
it('should return locales defined in messages in lexical order', () => {
assert.deepStrictEqual(i18n.availableLocales, ['en', 'ja'])
})
})
let desc = VueI18n.availabilities.dateTimeFormat ? describe : describe.skip
desc('i18n#d', () => {
let dt
beforeEach(() => {
i18n = new VueI18n({
locale: 'en-US',
fallbackLocale: 'ja-JP',
dateTimeFormats
})
dt = new Date(Date.UTC(2012, 11, 20, 3, 0, 0))
})
describe('arguments nothing', () => {
it('should be formatted', () => {
assert.strictEqual(i18n.d(dt), '12/20/2012')
})
})
describe('number value', () => {
it('should be formatted', () => {
assert.strictEqual(i18n.d(dt.getTime()), '12/20/2012')
})
})
describe('key argument', () => {
// NOTE: avoid webkit(phatomjs/safari) & Intl polyfill wired localization...
isChrome && it('should be formatted', () => {
assert.strictEqual(i18n.d(dt, 'short'), '12/19/2012, 10:00 PM')
})
})
describe('locale argument', () => {
describe('with second argument', () => {
// NOTE: avoid webkit(phatomjs/safari) & Intl polyfill wired localization...
isChrome && it('should be formatted', () => {
assert.strictEqual(i18n.d(dt, 'short', 'ja-JP'), '2012/12/20 12:00')
})
})
describe('with object argument', () => {
// NOTE: avoid webkit(phatomjs/safari) & Intl polyfill wired localization...
isChrome && it('should be formatted', () => {
assert.strictEqual(i18n.d(dt, { key: 'short', locale: 'ja-JP' }), '2012/12/20 12:00')
})
})
})
describe('fallback', () => {
// NOTE: avoid webkit(phatomjs/safari) & Intl polyfill wired localization...
isChrome && it('should be formatted', () => {
assert.strictEqual(i18n.d(dt, 'long'), '2012/12/20 12:00:00')
})
})
})
desc = VueI18n.availabilities.numberFormat ? describe : describe.skip
desc('i18n#n', () => {
let money
beforeEach(() => {
i18n = new VueI18n({
locale: 'en-US',
fallbackLocale: 'ja-JP',
numberFormats
})
money = 10100
})
describe('arguments nothing', () => {
it('should be formatted', () => {
assert.strictEqual(i18n.n(money), '10,100')
})
})
describe('key argument', () => {
it('should be formatted', () => {
assert.strictEqual(i18n.n(money, 'currency'), '$10,100.00')
})
})
describe('locale argument', () => {
describe('with second argument', () => {
it('should be formatted', () => {
assert.strictEqual(i18n.n(money, 'currency', 'ja-JP'), '¥10,100')
})
})
describe('with object argument', () => {
it('should be formatted', () => {
assert.strictEqual(i18n.n(money, { key: 'currency', locale: 'ja-JP' }), '¥10,100')
})
})
})
describe('explicit options argument', () => {
describe('without key', () => {
it('should be formatted', () => {
assert.strictEqual(i18n.n(money, { style: 'currency', currency: 'JPY' }), '¥10,100')
})
it('should respect other number options', () => {
const options = { style: 'currency', currency: 'EUR', currencyDisplay: 'symbol' }
assert.strictEqual(i18n.n(money, options), '€10,100.00')
})
})
describe('with key', () => {
it('should be formatted', () => {
assert.strictEqual(i18n.n(money, { key: 'currency', currency: 'JPY' }), '¥10,100')
})
it('should respect other number options', () => {
const options = { key: 'currency', currency: 'EUR', currencyDisplay: 'symbol' }
assert.strictEqual(i18n.n(money, options), '€10,100.00')
})
})
})
describe('fallback', () => {
it('should be formatted', () => {
assert.strictEqual(i18n.n(0.9, 'percent'), '90%')
})
})
})
})