-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathi18n.test.ts
68 lines (57 loc) · 2.65 KB
/
i18n.test.ts
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
import {expect} from 'chai'
import {ensureSupportedLang, translate} from './i18n'
import langs from './langs'
function createEntry(lang: string, key: string, text: string) {
let keyWalker = langs[lang]
let splitKey = key.split('.')
let lastKey = splitKey.pop()
splitKey.forEach(k => {
keyWalker[k] = keyWalker[k] || {}
keyWalker = keyWalker[k]
})
keyWalker[lastKey] = text
}
it("test entry creation utility", () => {
createEntry('en', 'enkeytesting.testing.text', 'works!')
expect(langs['en']['enkeytesting']['testing']['text']).to.equal('works!')
})
it('translate', () => {
expect(translate('en', 'login.submit')).to.equal('Login')
expect(translate('en', 'login.otp.message', {values: {otp: '000111'}})).to.equal('Your one-time password is 000111')
})
it('translate with unsupported lang', () => {
expect(translate('??', 'login.submit')).to.equal('Login')
})
it('translation should fall back to en', () => {
createEntry('en', 'key.that.exists.only.in.en', 'Tere {name}!')
expect(translate('et', 'key.that.exists.only.in.en', {values: {name: 'Jaan'}})).to.equal('Tere Jaan!')
})
it('if translation fails it should return translation key', () => {
const nonExistingKey = 'some.key.that.does.not.exist'
expect(translate('et', nonExistingKey)).to.equal(nonExistingKey)
})
it('ensureSupportedLang', () => {
expect(ensureSupportedLang('en')).to.equal('en')
expect(ensureSupportedLang('??')).to.equal('en')
})
it('translate strings with plurals', () => {
const key = 'bup.bap.plural.test'
createEntry('en', key,
'Testing {count|zero:nothing|one:a single translation|other:# translations!}')
createEntry('en', key + '2',
'Testing {count|zero:nothing|one:a single translation|other:# translations but don\'t \#change this!}')
expect(translate('en', key, {values: {count: 0}})).to.equal("Testing nothing")
expect(translate('en', key, {values: {count: 1}})).to.equal("Testing a single translation")
expect(translate('en', key, {values: {count: 5}})).to.equal("Testing 5 translations!")
expect(translate('en', key + '2', {values: {count: 12}})).to.equal("Testing 12 translations but don't #change this!")
})
it('translate template with plurals and regular substitution', () => {
const key = 'messages.since'
createEntry('en', key, 'Tere {username}! You have {n|one:# message|other:# messages} since {date}')
expect(translate('en', key, {values: {n: 5, date: '2020/01/27', username: 'Piret'}})).to.equal('Tere Piret! You have 5 messages since 2020/01/27')
})
it('empty token', () => {
const key = 'messages.since'
createEntry('en', key, '{n|zero:|one:one|other:# messages}')
expect(translate('en', key, {values: {n: 0}})).to.equal('')
})