-
Notifications
You must be signed in to change notification settings - Fork 4
/
translation.test.ts
271 lines (233 loc) · 8.43 KB
/
translation.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
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
import type { NextcloudWindowWithRegistry } from '../lib/registry'
import {
getPlural,
register,
translate,
translatePlural,
unregister,
} from '../lib/translation'
declare const window: NextcloudWindowWithRegistry
const setLocale = (locale: string) => document.documentElement.setAttribute('data-locale', locale)
const setLanguage = (lang: string) => document.documentElement.setAttribute('lang', lang)
describe('translate', () => {
const mockWindowDE = () => {
window._oc_l10n_registry_translations = {
core: {
'Hello world!': 'Hallo Welt!',
'Hello {name}': 'Hallo {name}',
'_download %n file_::_download %n files_': [
'Lade %n Datei herunter',
'Lade %n Dateien herunter',
],
'_download %n file from {url}_::_download %n files from {url}_': [
'Lade %n Datei von {url} herunter',
'Lade %n Dateien von {url} herunter',
],
},
}
window._oc_l10n_registry_plural_functions = {
core: (t) => t === 1 ? 0 : 1,
}
setLocale('de')
}
beforeAll(mockWindowDE)
it('without placeholder HTML escaping', () => {
const text = 'Hello {name}'
const translation = translate('core', text, { name: '<del>Name</del>' }, undefined, { escape: false })
expect(translation).toBe('Hallo <del>Name</del>')
})
it('without placeholder HTML escaping on links', () => {
const text = 'Hello {start}Nextcloud{end}'
const translation = translate('core', text, { start: '<a href="https://nextcloud.com">', end: '</a>' }, undefined, { escape: false })
expect(translation).toBe('Hello <a href="https://nextcloud.com">Nextcloud</a>')
})
it('with placeholder HTML escaping', () => {
const text = 'Hello {name}'
const translation = translate('core', text, { name: '<del>Name</del>' })
expect(translation).toBe('Hallo <del>Name</del>')
})
it('without placeholder XSS sanitizing', () => {
const text = 'Hello {name}'
const translation = translate('core', text, { name: '<img src=x onerror=alert(1)//>' }, undefined, { sanitize: false, escape: false })
expect(translation).toBe('Hallo <img src=x onerror=alert(1)//>')
})
it('with placeholder XSS sanitizing', () => {
const text = 'Hello {name}'
const translation = translate('core', text, { name: '<img src=x onerror=alert(1)//>' }, undefined, { escape: false })
expect(translation).toBe('Hallo <img src="x">')
})
it('singular', () => {
const text = 'Hello world!'
const translation = translate('core', text)
expect(translation).toBe('Hallo Welt!')
})
it('singular with variable', () => {
const text = 'Hello {name}'
const translation = translate('core', text, { name: 'J. Doe' })
expect(translation).toBe('Hallo J. Doe')
})
it('singular with missing variable', () => {
const text = 'Hello {name}'
const translation = translate('core', text, {})
expect(translation).toBe('Hallo {name}')
})
it('singular with invalid variable', () => {
const text = 'Hello {name}'
// We need to disable ts here as users might use it from JS were no type checking is available
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const translation = translate('core', text, { name: true }) // invalid as only `string` or `number` are allowed
expect(translation).toBe('Hallo {name}')
})
it('singular with missing translation', () => {
const text = 'Good bye!'
const translation = translate('core', text)
expect(translation).toBe('Good bye!')
})
it('singular with missing application', () => {
const text = 'Good bye!'
const translation = translate('unavailable', text)
expect(translation).toBe('Good bye!')
})
it('singular with plural translation', () => {
const text = '_download %n file_::_download %n files_'
const translation = translate('core', text)
expect(translation).toBe('Lade %n Datei herunter')
})
it('plural', () => {
const text = ['download %n file', 'download %n files'] as const
expect(translatePlural('core', ...text, 1)).toBe('Lade 1 Datei herunter')
expect(translatePlural('core', ...text, 2)).toBe('Lade 2 Dateien herunter')
})
it('plural with missing translation', () => {
const text = ['%n translation does not exist', '%n translations do not exist'] as const
expect(translatePlural('core', ...text, 1)).toBe('1 translation does not exist')
expect(translatePlural('core', ...text, 2)).toBe('2 translations do not exist')
})
it('plural with variable', () => {
const text = ['download %n file from {url}', 'download %n files from {url}'] as const
expect(translatePlural('core', ...text, 1, { url: 'nextcloud.com' })).toBe('Lade 1 Datei von nextcloud.com herunter')
expect(translatePlural('core', ...text, 2, { url: 'nextcloud.com' })).toBe('Lade 2 Dateien von nextcloud.com herunter')
})
it('plural with missing variable', () => {
const text = ['download %n file from {url}', 'download %n files from {url}'] as const
expect(translatePlural('core', ...text, 1)).toBe('Lade 1 Datei von {url} herunter')
expect(translatePlural('core', ...text, 2)).toBe('Lade 2 Dateien von {url} herunter')
})
})
describe('register', () => {
beforeEach(() => {
setLocale('de_DE')
window._oc_l10n_registry_translations = undefined
window._oc_l10n_registry_plural_functions = undefined
})
it('with blank registry', () => {
register('app', {
Application: 'Anwendung',
'_%n guest_::_%n guests_': ['%n Gast', '%n Gäste'],
})
expect(translate('app', 'Application')).toBe('Anwendung')
expect(translatePlural('app', '%n guest', '%n guests', 1)).toBe('1 Gast')
expect(translatePlural('app', '%n guest', '%n guests', 2)).toBe('2 Gäste')
})
it('extend registered translations', () => {
window._oc_l10n_registry_translations = {
app: {
Application: 'Anwendung',
},
}
window._oc_l10n_registry_plural_functions = {
app: (t) => t === 1 ? 0 : 1,
}
register('app', {
Translation: 'Übersetzung',
})
expect(translate('app', 'Application')).toBe('Anwendung')
expect(translate('app', 'Translation')).toBe('Übersetzung')
})
it('extend with another new app', () => {
window._oc_l10n_registry_translations = {
core: {
'Hello world!': 'Hallo Welt!',
},
}
window._oc_l10n_registry_plural_functions = {
core: (t) => t === 1 ? 0 : 1,
}
register('app', {
Application: 'Anwendung',
})
expect(translate('core', 'Hello world!')).toBe('Hallo Welt!')
expect(translate('app', 'Application')).toBe('Anwendung')
})
it('unregister', () => {
window._oc_l10n_registry_translations = {}
window._oc_l10n_registry_plural_functions = {}
register('app', {
Application: 'Anwendung',
})
unregister('app')
expect(translate('app', 'Application')).toBe('Application')
})
})
describe('getPlural', () => {
it('handles single form language like Azerbaijani', () => {
setLanguage('az')
expect(getPlural(0)).toBe(0)
expect(getPlural(1)).toBe(0)
expect(getPlural(2)).toBe(0)
})
it('handles single plural language like Amharic', () => {
setLanguage('am')
expect(getPlural(0)).toBe(0)
expect(getPlural(1)).toBe(0)
expect(getPlural(2)).toBe(1)
})
it('handles single plural language with plural zero like Afrikaans', () => {
setLanguage('af')
expect(getPlural(0)).toBe(1)
expect(getPlural(1)).toBe(0)
expect(getPlural(2)).toBe(1)
})
it('can handle Czech and Slovak', () => {
// Three forms, special cases for 1 and 2, 3, 4
setLanguage('cs')
expect(getPlural(0)).toBe(2)
expect(getPlural(1)).toBe(0)
expect(getPlural(2)).toBe(1)
expect(getPlural(3)).toBe(1)
expect(getPlural(4)).toBe(1)
expect(getPlural(5)).toBe(2)
})
it('can handle Gaeilge (Irish)', () => {
// Three forms, special cases for one and two
setLanguage('ga')
expect(getPlural(0)).toBe(2)
expect(getPlural(1)).toBe(0)
expect(getPlural(2)).toBe(1)
expect(getPlural(3)).toBe(2)
})
// special because brazillian Portuguese use sigular for 0 and 1 wheras Portuguese use plural for 0
it('can handle brazillian Portuguese', () => {
// check Portuguese
setLanguage('pt')
expect(getPlural(0)).toBe(1)
expect(getPlural(1)).toBe(0)
expect(getPlural(2)).toBe(1)
// check brazillian Portuguese
setLanguage('pt-BR')
expect(getPlural(0)).toBe(0)
expect(getPlural(1)).toBe(0)
expect(getPlural(2)).toBe(1) // ensure no fallback to `default`
})
it('can handle language designators with regions', () => {
setLanguage('be-BY')
// default for unknown is 0 for all input so we can assume it works if we get 1
expect(getPlural(0)).toBe(2)
})
it('has a fallback', () => {
setLanguage('xxx')
expect(getPlural(0)).toBe<number>(0)
expect(getPlural(1)).toBe<number>(0)
})
})