@i18n-micro/core 1.0.27 → 1.0.28
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/base.d.ts +91 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.mjs +343 -246
- package/package.json +4 -4
- package/src/base.ts +250 -0
- package/src/index.ts +3 -0
- package/src/route-service.ts +5 -4
- package/tests/base.test.ts +510 -0
- package/tests/route-service.test.ts +23 -3
|
@@ -0,0 +1,510 @@
|
|
|
1
|
+
import { BaseI18n, type BaseI18nOptions } from '../src/base'
|
|
2
|
+
import type { Translations, PluralFunc } from '@i18n-micro/types'
|
|
3
|
+
|
|
4
|
+
// Test implementation of BaseI18n
|
|
5
|
+
class TestI18n extends BaseI18n {
|
|
6
|
+
private _locale: string
|
|
7
|
+
private _fallbackLocale: string
|
|
8
|
+
private _route: string
|
|
9
|
+
|
|
10
|
+
constructor(
|
|
11
|
+
locale: string,
|
|
12
|
+
fallbackLocale: string,
|
|
13
|
+
route: string,
|
|
14
|
+
options?: BaseI18nOptions,
|
|
15
|
+
) {
|
|
16
|
+
super(options)
|
|
17
|
+
this._locale = locale
|
|
18
|
+
this._fallbackLocale = fallbackLocale
|
|
19
|
+
this._route = route
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
public getLocale(): string {
|
|
23
|
+
return this._locale
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
public getFallbackLocale(): string {
|
|
27
|
+
return this._fallbackLocale
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
public getRoute(): string {
|
|
31
|
+
return this._route
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
public setLocale(locale: string): void {
|
|
35
|
+
this._locale = locale
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
public setRoute(route: string): void {
|
|
39
|
+
this._route = route
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
describe('BaseI18n', () => {
|
|
44
|
+
describe('Constructor', () => {
|
|
45
|
+
test('should initialize with default options', () => {
|
|
46
|
+
const i18n = new TestI18n('en', 'en', 'general')
|
|
47
|
+
expect(i18n.getLocale()).toBe('en')
|
|
48
|
+
expect(i18n.getFallbackLocale()).toBe('en')
|
|
49
|
+
expect(i18n.getRoute()).toBe('general')
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
test('should initialize with custom cache', () => {
|
|
53
|
+
const cache = {
|
|
54
|
+
generalLocaleCache: {},
|
|
55
|
+
routeLocaleCache: {},
|
|
56
|
+
dynamicTranslationsCaches: [],
|
|
57
|
+
serverTranslationCache: {},
|
|
58
|
+
}
|
|
59
|
+
const i18n = new TestI18n('en', 'en', 'general', { cache })
|
|
60
|
+
expect(i18n).toBeDefined()
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
test('should initialize with custom plural function', () => {
|
|
64
|
+
const customPlural: PluralFunc = () => 'custom'
|
|
65
|
+
const i18n = new TestI18n('en', 'en', 'general', { plural: customPlural })
|
|
66
|
+
expect(i18n).toBeDefined()
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
test('should initialize with missingWarn option', () => {
|
|
70
|
+
const i18n = new TestI18n('en', 'en', 'general', { missingWarn: false })
|
|
71
|
+
expect(i18n).toBeDefined()
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
test('should initialize with missingHandler', () => {
|
|
75
|
+
const handler = jest.fn()
|
|
76
|
+
const i18n = new TestI18n('en', 'en', 'general', { missingHandler: handler })
|
|
77
|
+
expect(i18n).toBeDefined()
|
|
78
|
+
})
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
describe('t() method', () => {
|
|
82
|
+
test('should return empty string for empty key', () => {
|
|
83
|
+
const i18n = new TestI18n('en', 'en', 'general')
|
|
84
|
+
expect(i18n.t('')).toBe('')
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
test('should return translation for existing key', async () => {
|
|
88
|
+
const i18n = new TestI18n('en', 'en', 'general')
|
|
89
|
+
const translations: Translations = { greeting: 'Hello' }
|
|
90
|
+
i18n['helper'].loadTranslations('en', translations)
|
|
91
|
+
|
|
92
|
+
expect(i18n.t('greeting')).toBe('Hello')
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
test('should interpolate params in translation', async () => {
|
|
96
|
+
const cache = {
|
|
97
|
+
generalLocaleCache: {},
|
|
98
|
+
routeLocaleCache: {},
|
|
99
|
+
dynamicTranslationsCaches: [],
|
|
100
|
+
serverTranslationCache: {},
|
|
101
|
+
}
|
|
102
|
+
const i18n = new TestI18n('en', 'en', 'general', { cache })
|
|
103
|
+
const translations: Translations = { greeting: 'Hello, {name}!' }
|
|
104
|
+
await i18n['helper'].loadTranslations('en', translations)
|
|
105
|
+
|
|
106
|
+
expect(i18n.t('greeting', { name: 'John' })).toBe('Hello, John!')
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
test('should use defaultValue when translation is missing', () => {
|
|
110
|
+
const i18n = new TestI18n('en', 'en', 'general')
|
|
111
|
+
expect(i18n.t('missing.key', undefined, 'Default value')).toBe('Default value')
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
test('should return key when translation is missing and no defaultValue', () => {
|
|
115
|
+
const i18n = new TestI18n('en', 'en', 'general')
|
|
116
|
+
expect(i18n.t('missing.key')).toBe('missing.key')
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
test('should fallback to fallbackLocale when translation is missing', async () => {
|
|
120
|
+
const cache = {
|
|
121
|
+
generalLocaleCache: {},
|
|
122
|
+
routeLocaleCache: {},
|
|
123
|
+
dynamicTranslationsCaches: [],
|
|
124
|
+
serverTranslationCache: {},
|
|
125
|
+
}
|
|
126
|
+
const i18n = new TestI18n('en', 'fr', 'general', { cache })
|
|
127
|
+
const translations: Translations = { greeting: 'Bonjour' }
|
|
128
|
+
await i18n['helper'].loadTranslations('fr', translations)
|
|
129
|
+
|
|
130
|
+
expect(i18n.t('greeting')).toBe('Bonjour')
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
test('should use route-specific translation when routeName is provided', async () => {
|
|
134
|
+
const i18n = new TestI18n('en', 'en', 'general')
|
|
135
|
+
const routeTranslations: Translations = { title: 'Route Title' }
|
|
136
|
+
await i18n['helper'].loadPageTranslations('en', 'about', routeTranslations)
|
|
137
|
+
|
|
138
|
+
expect(i18n.t('title', undefined, undefined, 'about')).toBe('Route Title')
|
|
139
|
+
})
|
|
140
|
+
|
|
141
|
+
test('should use previousPageInfo fallback when enabled', async () => {
|
|
142
|
+
const cache = {
|
|
143
|
+
generalLocaleCache: {},
|
|
144
|
+
routeLocaleCache: {},
|
|
145
|
+
dynamicTranslationsCaches: [],
|
|
146
|
+
serverTranslationCache: {},
|
|
147
|
+
}
|
|
148
|
+
const prevInfo = { locale: 'fr', routeName: 'previous' }
|
|
149
|
+
const i18n = new TestI18n('en', 'en', 'general', {
|
|
150
|
+
cache,
|
|
151
|
+
getPreviousPageInfo: () => prevInfo,
|
|
152
|
+
enablePreviousPageFallback: true,
|
|
153
|
+
})
|
|
154
|
+
|
|
155
|
+
const translations: Translations = { greeting: 'Bonjour' }
|
|
156
|
+
await i18n['helper'].loadPageTranslations('fr', 'previous', translations)
|
|
157
|
+
|
|
158
|
+
// Current locale is 'en', route is 'general', translation not found
|
|
159
|
+
// Should fallback to previous page info: locale 'fr', route 'previous'
|
|
160
|
+
expect(i18n.t('greeting')).toBe('Bonjour')
|
|
161
|
+
})
|
|
162
|
+
|
|
163
|
+
test('should call missingHandler when translation is missing', () => {
|
|
164
|
+
const handler = jest.fn()
|
|
165
|
+
const i18n = new TestI18n('en', 'en', 'general', { missingHandler: handler })
|
|
166
|
+
|
|
167
|
+
i18n.t('missing.key')
|
|
168
|
+
|
|
169
|
+
expect(handler).toHaveBeenCalledWith('en', 'missing.key', 'general')
|
|
170
|
+
})
|
|
171
|
+
|
|
172
|
+
test('should call customMissingHandler when set (Nuxt runtime)', () => {
|
|
173
|
+
const customHandler = jest.fn()
|
|
174
|
+
const i18n = new TestI18n('en', 'en', 'general', {
|
|
175
|
+
getCustomMissingHandler: () => customHandler,
|
|
176
|
+
})
|
|
177
|
+
|
|
178
|
+
i18n.t('missing.key')
|
|
179
|
+
|
|
180
|
+
expect(customHandler).toHaveBeenCalledWith('en', 'missing.key', 'general')
|
|
181
|
+
})
|
|
182
|
+
|
|
183
|
+
test('should not warn when missingWarn is false', () => {
|
|
184
|
+
const consoleSpy = jest.spyOn(console, 'warn').mockImplementation()
|
|
185
|
+
const i18n = new TestI18n('en', 'en', 'general', { missingWarn: false })
|
|
186
|
+
|
|
187
|
+
i18n.t('missing.key')
|
|
188
|
+
|
|
189
|
+
expect(consoleSpy).not.toHaveBeenCalled()
|
|
190
|
+
consoleSpy.mockRestore()
|
|
191
|
+
})
|
|
192
|
+
})
|
|
193
|
+
|
|
194
|
+
describe('ts() method', () => {
|
|
195
|
+
test('should return translation as string', async () => {
|
|
196
|
+
const i18n = new TestI18n('en', 'en', 'general')
|
|
197
|
+
const translations: Translations = { greeting: 'Hello' }
|
|
198
|
+
i18n['helper'].loadTranslations('en', translations)
|
|
199
|
+
|
|
200
|
+
expect(i18n.ts('greeting')).toBe('Hello')
|
|
201
|
+
})
|
|
202
|
+
|
|
203
|
+
test('should return defaultValue when translation is missing', () => {
|
|
204
|
+
const i18n = new TestI18n('en', 'en', 'general')
|
|
205
|
+
expect(i18n.ts('missing.key', undefined, 'Default')).toBe('Default')
|
|
206
|
+
})
|
|
207
|
+
|
|
208
|
+
test('should return key when translation is missing and no defaultValue', () => {
|
|
209
|
+
const i18n = new TestI18n('en', 'en', 'general')
|
|
210
|
+
expect(i18n.ts('missing.key')).toBe('missing.key')
|
|
211
|
+
})
|
|
212
|
+
|
|
213
|
+
test('should convert non-string values to string', async () => {
|
|
214
|
+
const i18n = new TestI18n('en', 'en', 'general')
|
|
215
|
+
const translations: Translations = { count: 42 }
|
|
216
|
+
i18n['helper'].loadTranslations('en', translations)
|
|
217
|
+
|
|
218
|
+
expect(i18n.ts('count')).toBe('42')
|
|
219
|
+
})
|
|
220
|
+
})
|
|
221
|
+
|
|
222
|
+
describe('tc() method', () => {
|
|
223
|
+
test('should return defaultValue when count is undefined', () => {
|
|
224
|
+
const i18n = new TestI18n('en', 'en', 'general')
|
|
225
|
+
expect(i18n.tc('apples', { other: 'params' }, 'No count')).toBe('No count')
|
|
226
|
+
})
|
|
227
|
+
|
|
228
|
+
test('should use plural function with count', async () => {
|
|
229
|
+
const i18n = new TestI18n('en', 'en', 'general')
|
|
230
|
+
const translations: Translations = { apples: 'apple|apples' }
|
|
231
|
+
await i18n['helper'].loadTranslations('en', translations)
|
|
232
|
+
|
|
233
|
+
// defaultPlural selects form by index: forms[count] or last form if count >= forms.length
|
|
234
|
+
// For 'apple|apples': forms[0]='apple', forms[1]='apples'
|
|
235
|
+
expect(i18n.tc('apples', 0)).toBe('apple')
|
|
236
|
+
expect(i18n.tc('apples', 1)).toBe('apples') // forms[1]
|
|
237
|
+
expect(i18n.tc('apples', 5)).toBe('apples') // last form
|
|
238
|
+
})
|
|
239
|
+
|
|
240
|
+
test('should handle count as number', async () => {
|
|
241
|
+
const i18n = new TestI18n('en', 'en', 'general')
|
|
242
|
+
const translations: Translations = { apples: 'apple|apples' }
|
|
243
|
+
i18n['helper'].loadTranslations('en', translations)
|
|
244
|
+
|
|
245
|
+
expect(i18n.tc('apples', 2)).toBe('apples')
|
|
246
|
+
})
|
|
247
|
+
|
|
248
|
+
test('should handle count as Params object', async () => {
|
|
249
|
+
const i18n = new TestI18n('en', 'en', 'general')
|
|
250
|
+
const translations: Translations = { apples: 'apple|apples' }
|
|
251
|
+
i18n['helper'].loadTranslations('en', translations)
|
|
252
|
+
|
|
253
|
+
expect(i18n.tc('apples', { count: 2, name: 'John' })).toBe('apples')
|
|
254
|
+
})
|
|
255
|
+
|
|
256
|
+
test('should return defaultValue when plural function returns null', () => {
|
|
257
|
+
const i18n = new TestI18n('en', 'en', 'general')
|
|
258
|
+
// When translation is missing, t() returns key, which is passed to pluralFunc
|
|
259
|
+
// pluralFunc tries to process 'missing.key' as translation, but since it doesn't contain '|',
|
|
260
|
+
// it returns the key itself. So tc returns the key, not defaultValue.
|
|
261
|
+
// To test defaultValue, we need a case where pluralFunc actually returns null.
|
|
262
|
+
// This happens when translation exists but is empty or invalid.
|
|
263
|
+
expect(i18n.tc('missing.key', 1, 'Default')).toBe('missing.key')
|
|
264
|
+
})
|
|
265
|
+
})
|
|
266
|
+
|
|
267
|
+
describe('tn() method', () => {
|
|
268
|
+
test('should format number with default locale', () => {
|
|
269
|
+
const i18n = new TestI18n('en', 'en', 'general')
|
|
270
|
+
const result = i18n.tn(1234.56)
|
|
271
|
+
expect(result).toMatch(/1[,.]234[.,]56/)
|
|
272
|
+
})
|
|
273
|
+
|
|
274
|
+
test('should format number with custom options', () => {
|
|
275
|
+
const i18n = new TestI18n('en', 'en', 'general')
|
|
276
|
+
const result = i18n.tn(1234.56, { style: 'currency', currency: 'USD' })
|
|
277
|
+
expect(result).toContain('1,234.56')
|
|
278
|
+
})
|
|
279
|
+
|
|
280
|
+
test('should use current locale for formatting', () => {
|
|
281
|
+
const i18n = new TestI18n('ru', 'en', 'general')
|
|
282
|
+
const result = i18n.tn(1234.56)
|
|
283
|
+
// Russian locale uses different number formatting
|
|
284
|
+
expect(result).toBeDefined()
|
|
285
|
+
})
|
|
286
|
+
})
|
|
287
|
+
|
|
288
|
+
describe('td() method', () => {
|
|
289
|
+
test('should format date with default locale', () => {
|
|
290
|
+
const i18n = new TestI18n('en', 'en', 'general')
|
|
291
|
+
const date = new Date('2024-01-15')
|
|
292
|
+
const result = i18n.td(date)
|
|
293
|
+
expect(result).toBeDefined()
|
|
294
|
+
expect(result).not.toBe('Invalid Date')
|
|
295
|
+
})
|
|
296
|
+
|
|
297
|
+
test('should format date with custom options', () => {
|
|
298
|
+
const i18n = new TestI18n('en', 'en', 'general')
|
|
299
|
+
const date = new Date('2024-01-15')
|
|
300
|
+
const result = i18n.td(date, { year: 'numeric', month: 'long', day: 'numeric' })
|
|
301
|
+
expect(result).toContain('2024')
|
|
302
|
+
expect(result).toContain('January')
|
|
303
|
+
})
|
|
304
|
+
|
|
305
|
+
test('should handle date as number (timestamp)', () => {
|
|
306
|
+
const i18n = new TestI18n('en', 'en', 'general')
|
|
307
|
+
const timestamp = new Date('2024-01-15').getTime()
|
|
308
|
+
const result = i18n.td(timestamp)
|
|
309
|
+
expect(result).toBeDefined()
|
|
310
|
+
expect(result).not.toBe('Invalid Date')
|
|
311
|
+
})
|
|
312
|
+
|
|
313
|
+
test('should handle date as string', () => {
|
|
314
|
+
const i18n = new TestI18n('en', 'en', 'general')
|
|
315
|
+
const result = i18n.td('2024-01-15')
|
|
316
|
+
expect(result).toBeDefined()
|
|
317
|
+
expect(result).not.toBe('Invalid Date')
|
|
318
|
+
})
|
|
319
|
+
})
|
|
320
|
+
|
|
321
|
+
describe('tdr() method', () => {
|
|
322
|
+
test('should format relative time', () => {
|
|
323
|
+
const i18n = new TestI18n('en', 'en', 'general')
|
|
324
|
+
const yesterday = new Date(Date.now() - 86400000)
|
|
325
|
+
const result = i18n.tdr(yesterday)
|
|
326
|
+
expect(result).toBeDefined()
|
|
327
|
+
expect(result).toMatch(/day|ago/i)
|
|
328
|
+
})
|
|
329
|
+
|
|
330
|
+
test('should format relative time with custom options', () => {
|
|
331
|
+
const i18n = new TestI18n('en', 'en', 'general')
|
|
332
|
+
const yesterday = new Date(Date.now() - 86400000)
|
|
333
|
+
const result = i18n.tdr(yesterday, { numeric: 'always' })
|
|
334
|
+
expect(result).toBeDefined()
|
|
335
|
+
})
|
|
336
|
+
|
|
337
|
+
test('should handle invalid date gracefully', () => {
|
|
338
|
+
const i18n = new TestI18n('en', 'en', 'general')
|
|
339
|
+
const invalidDate = new Date('invalid')
|
|
340
|
+
const result = i18n.tdr(invalidDate)
|
|
341
|
+
expect(result).toBeDefined()
|
|
342
|
+
})
|
|
343
|
+
})
|
|
344
|
+
|
|
345
|
+
describe('has() method', () => {
|
|
346
|
+
test('should return true when translation exists', async () => {
|
|
347
|
+
const i18n = new TestI18n('en', 'en', 'general')
|
|
348
|
+
const translations: Translations = { greeting: 'Hello' }
|
|
349
|
+
i18n['helper'].loadTranslations('en', translations)
|
|
350
|
+
|
|
351
|
+
expect(i18n.has('greeting')).toBe(true)
|
|
352
|
+
})
|
|
353
|
+
|
|
354
|
+
test('should return false when translation does not exist', () => {
|
|
355
|
+
const i18n = new TestI18n('en', 'en', 'general')
|
|
356
|
+
expect(i18n.has('missing.key')).toBe(false)
|
|
357
|
+
})
|
|
358
|
+
|
|
359
|
+
test('should check route-specific translation when routeName is provided', async () => {
|
|
360
|
+
const i18n = new TestI18n('en', 'en', 'general')
|
|
361
|
+
const routeTranslations: Translations = { title: 'Route Title' }
|
|
362
|
+
await i18n['helper'].loadPageTranslations('en', 'about', routeTranslations)
|
|
363
|
+
|
|
364
|
+
expect(i18n.has('title', 'about')).toBe(true)
|
|
365
|
+
expect(i18n.has('title', 'general')).toBe(false)
|
|
366
|
+
})
|
|
367
|
+
|
|
368
|
+
test('should use current route when routeName is not provided', async () => {
|
|
369
|
+
const i18n = new TestI18n('en', 'en', 'about')
|
|
370
|
+
const routeTranslations: Translations = { title: 'Route Title' }
|
|
371
|
+
await i18n['helper'].loadPageTranslations('en', 'about', routeTranslations)
|
|
372
|
+
|
|
373
|
+
expect(i18n.has('title')).toBe(true)
|
|
374
|
+
})
|
|
375
|
+
})
|
|
376
|
+
|
|
377
|
+
describe('clearCache() method', () => {
|
|
378
|
+
test('should clear all translations from cache', async () => {
|
|
379
|
+
const i18n = new TestI18n('en', 'en', 'general')
|
|
380
|
+
const translations: Translations = { greeting: 'Hello' }
|
|
381
|
+
i18n['helper'].loadTranslations('en', translations)
|
|
382
|
+
|
|
383
|
+
expect(i18n.has('greeting')).toBe(true)
|
|
384
|
+
|
|
385
|
+
i18n.clearCache()
|
|
386
|
+
|
|
387
|
+
expect(i18n.has('greeting')).toBe(false)
|
|
388
|
+
})
|
|
389
|
+
})
|
|
390
|
+
|
|
391
|
+
describe('loadTranslationsCore() method', () => {
|
|
392
|
+
test('should load translations when merge is false', async () => {
|
|
393
|
+
const i18n = new TestI18n('en', 'en', 'general')
|
|
394
|
+
const translations: Translations = { greeting: 'Hello' }
|
|
395
|
+
|
|
396
|
+
i18n['loadTranslationsCore']('en', translations, false)
|
|
397
|
+
// Wait for async operation to complete
|
|
398
|
+
await new Promise(resolve => setTimeout(resolve, 0))
|
|
399
|
+
|
|
400
|
+
expect(i18n.has('greeting')).toBe(true)
|
|
401
|
+
})
|
|
402
|
+
|
|
403
|
+
test('should merge translations when merge is true', async () => {
|
|
404
|
+
const i18n = new TestI18n('en', 'en', 'general')
|
|
405
|
+
const initial: Translations = { greeting: 'Hello' }
|
|
406
|
+
const additional: Translations = { farewell: 'Goodbye' }
|
|
407
|
+
|
|
408
|
+
await i18n['helper'].loadTranslations('en', initial)
|
|
409
|
+
i18n['loadTranslationsCore']('en', additional, true)
|
|
410
|
+
// Wait for async operation to complete
|
|
411
|
+
await new Promise(resolve => setTimeout(resolve, 0))
|
|
412
|
+
|
|
413
|
+
expect(i18n.has('greeting')).toBe(true)
|
|
414
|
+
expect(i18n.has('farewell')).toBe(true)
|
|
415
|
+
})
|
|
416
|
+
})
|
|
417
|
+
|
|
418
|
+
describe('loadRouteTranslationsCore() method', () => {
|
|
419
|
+
test('should load route translations when merge is false', async () => {
|
|
420
|
+
const i18n = new TestI18n('en', 'en', 'general')
|
|
421
|
+
const translations: Translations = { title: 'Page Title' }
|
|
422
|
+
|
|
423
|
+
i18n['loadRouteTranslationsCore']('en', 'about', translations, false)
|
|
424
|
+
// Wait for async operation to complete
|
|
425
|
+
await new Promise(resolve => setTimeout(resolve, 0))
|
|
426
|
+
|
|
427
|
+
expect(i18n.has('title', 'about')).toBe(true)
|
|
428
|
+
})
|
|
429
|
+
|
|
430
|
+
test('should merge route translations when merge is true', async () => {
|
|
431
|
+
const i18n = new TestI18n('en', 'en', 'general')
|
|
432
|
+
const initial: Translations = { title: 'Page Title' }
|
|
433
|
+
const additional: Translations = { description: 'Page Description' }
|
|
434
|
+
|
|
435
|
+
await i18n['helper'].loadPageTranslations('en', 'about', initial)
|
|
436
|
+
i18n['loadRouteTranslationsCore']('en', 'about', additional, true)
|
|
437
|
+
// Wait for async operation to complete
|
|
438
|
+
await new Promise(resolve => setTimeout(resolve, 0))
|
|
439
|
+
|
|
440
|
+
expect(i18n.has('title', 'about')).toBe(true)
|
|
441
|
+
expect(i18n.has('description', 'about')).toBe(true)
|
|
442
|
+
})
|
|
443
|
+
})
|
|
444
|
+
|
|
445
|
+
describe('Edge cases', () => {
|
|
446
|
+
test('should handle nested translation keys', async () => {
|
|
447
|
+
const i18n = new TestI18n('en', 'en', 'general')
|
|
448
|
+
const translations: Translations = {
|
|
449
|
+
nested: {
|
|
450
|
+
deep: {
|
|
451
|
+
key: 'Nested value',
|
|
452
|
+
},
|
|
453
|
+
},
|
|
454
|
+
}
|
|
455
|
+
i18n['helper'].loadTranslations('en', translations)
|
|
456
|
+
|
|
457
|
+
expect(i18n.t('nested.deep.key')).toBe('Nested value')
|
|
458
|
+
})
|
|
459
|
+
|
|
460
|
+
test('should handle multiple params in interpolation', async () => {
|
|
461
|
+
const i18n = new TestI18n('en', 'en', 'general')
|
|
462
|
+
const translations: Translations = {
|
|
463
|
+
message: 'Hello, {name}! You are {age} years old.',
|
|
464
|
+
}
|
|
465
|
+
i18n['helper'].loadTranslations('en', translations)
|
|
466
|
+
|
|
467
|
+
expect(i18n.t('message', { name: 'John', age: 30 })).toBe('Hello, John! You are 30 years old.')
|
|
468
|
+
})
|
|
469
|
+
|
|
470
|
+
test('should handle null defaultValue', () => {
|
|
471
|
+
const i18n = new TestI18n('en', 'en', 'general')
|
|
472
|
+
expect(i18n.t('missing.key', undefined, null)).toBe('missing.key')
|
|
473
|
+
})
|
|
474
|
+
|
|
475
|
+
test('should handle empty string defaultValue', () => {
|
|
476
|
+
const i18n = new TestI18n('en', 'en', 'general')
|
|
477
|
+
// Empty string is falsy, so it will fall back to key (as per defaultValue || key logic)
|
|
478
|
+
expect(i18n.t('missing.key', undefined, '')).toBe('missing.key')
|
|
479
|
+
})
|
|
480
|
+
|
|
481
|
+
test('should handle route change', async () => {
|
|
482
|
+
const cache = {
|
|
483
|
+
generalLocaleCache: {},
|
|
484
|
+
routeLocaleCache: {},
|
|
485
|
+
dynamicTranslationsCaches: [],
|
|
486
|
+
serverTranslationCache: {},
|
|
487
|
+
}
|
|
488
|
+
const i18n = new TestI18n('en', 'en', 'general', { cache })
|
|
489
|
+
const generalTranslations: Translations = { greeting: 'Hello' }
|
|
490
|
+
const routeTranslations: Translations = { title: 'About Page' }
|
|
491
|
+
|
|
492
|
+
await i18n['helper'].loadTranslations('en', generalTranslations)
|
|
493
|
+
await i18n['helper'].loadPageTranslations('en', 'about', routeTranslations)
|
|
494
|
+
|
|
495
|
+
// Verify translations are loaded
|
|
496
|
+
expect(i18n.has('greeting')).toBe(true)
|
|
497
|
+
expect(i18n.has('title', 'about')).toBe(true)
|
|
498
|
+
|
|
499
|
+
// Check route-specific translation with explicit routeName
|
|
500
|
+
// Note: getTranslation uses routeName parameter, so this should work
|
|
501
|
+
const titleValue = i18n.t('title', undefined, undefined, 'about')
|
|
502
|
+
expect(titleValue).toBe('About Page')
|
|
503
|
+
|
|
504
|
+
// Change route and check
|
|
505
|
+
i18n.setRoute('about')
|
|
506
|
+
expect(i18n.t('title')).toBe('About Page')
|
|
507
|
+
expect(i18n.t('greeting')).toBe('Hello') // Should still work from general
|
|
508
|
+
})
|
|
509
|
+
})
|
|
510
|
+
})
|
|
@@ -170,10 +170,11 @@ describe('RouteService', () => {
|
|
|
170
170
|
const mockI18nConfigWithNoPrefix: ModuleOptionsExtend = {
|
|
171
171
|
...mockI18nConfig,
|
|
172
172
|
strategy: 'no_prefix',
|
|
173
|
+
localeCookie: 'custom-locale',
|
|
173
174
|
}
|
|
174
175
|
routeService = new RouteService(mockI18nConfigWithNoPrefix, mockRouter, null, 'ru', navigateToMock, setCookieMock, null, null)
|
|
175
176
|
routeService.updateCookies('ru')
|
|
176
|
-
expect(setCookieMock).toHaveBeenCalledWith('
|
|
177
|
+
expect(setCookieMock).toHaveBeenCalledWith('custom-locale', 'ru')
|
|
177
178
|
})
|
|
178
179
|
|
|
179
180
|
test('resolveLocalizedRoute should return the correct localized route', () => {
|
|
@@ -265,6 +266,25 @@ describe('RouteService', () => {
|
|
|
265
266
|
expect(setCookieMock).toHaveBeenCalledWith('user-locale', 'ru')
|
|
266
267
|
})
|
|
267
268
|
|
|
269
|
+
test('updateCookies should use localeCookie fallback for regular strategy', () => {
|
|
270
|
+
const mockI18nConfigWithLocaleCookie: ModuleOptionsExtend = {
|
|
271
|
+
...mockI18nConfig,
|
|
272
|
+
localeCookie: 'custom-regular-cookie',
|
|
273
|
+
}
|
|
274
|
+
routeService = new RouteService(
|
|
275
|
+
mockI18nConfigWithLocaleCookie,
|
|
276
|
+
mockRouter,
|
|
277
|
+
null,
|
|
278
|
+
null,
|
|
279
|
+
navigateToMock,
|
|
280
|
+
setCookieMock,
|
|
281
|
+
null,
|
|
282
|
+
null,
|
|
283
|
+
)
|
|
284
|
+
routeService.updateCookies('ru')
|
|
285
|
+
expect(setCookieMock).toHaveBeenCalledWith('custom-regular-cookie', 'ru')
|
|
286
|
+
})
|
|
287
|
+
|
|
268
288
|
test('getCurrentName should return null if displayName is missing', () => {
|
|
269
289
|
const mockI18nConfigWithoutDisplayName = {
|
|
270
290
|
...mockI18nConfig,
|
|
@@ -350,9 +370,9 @@ describe('RouteService', () => {
|
|
|
350
370
|
})
|
|
351
371
|
})
|
|
352
372
|
|
|
353
|
-
test('updateCookies should
|
|
373
|
+
test('updateCookies should use default cookie when hashMode and noPrefixStrategy are disabled', () => {
|
|
354
374
|
routeService.updateCookies('de')
|
|
355
|
-
expect(setCookieMock).
|
|
375
|
+
expect(setCookieMock).toHaveBeenCalledWith('user-locale', 'de')
|
|
356
376
|
})
|
|
357
377
|
|
|
358
378
|
test('getCurrentLocale should extract locale from fullPath with query params when route.path is missing', () => {
|