@i18n-micro/core 1.0.28 → 1.1.0
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 +2 -2
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.mjs +200 -236
- package/dist/route-service.d.ts +6 -5
- package/dist/translation.d.ts +16 -17
- package/package.json +3 -3
- package/src/base.ts +4 -4
- package/src/helpers.ts +7 -6
- package/src/index.ts +2 -2
- package/src/route-service.ts +29 -34
- package/src/translation.ts +71 -181
- package/tests/base.test.ts +11 -36
- package/tests/route-service.test.ts +31 -81
|
@@ -13,7 +13,6 @@ describe('RouteService', () => {
|
|
|
13
13
|
let routeService: RouteService
|
|
14
14
|
let mockRouter: jest.Mocked<Router>
|
|
15
15
|
let mockI18nConfig: ModuleOptionsExtend
|
|
16
|
-
let setCookieMock: jest.Mock
|
|
17
16
|
let navigateToMock: jest.Mock
|
|
18
17
|
|
|
19
18
|
beforeEach(() => {
|
|
@@ -47,20 +46,15 @@ describe('RouteService', () => {
|
|
|
47
46
|
hashMode: false,
|
|
48
47
|
}
|
|
49
48
|
|
|
50
|
-
// Mock setCookie and navigateTo
|
|
51
|
-
setCookieMock = jest.fn()
|
|
52
49
|
navigateToMock = jest.fn()
|
|
53
50
|
|
|
54
|
-
// Initialize RouteService
|
|
51
|
+
// Initialize RouteService (no setCookie - plugin handles cookies)
|
|
55
52
|
routeService = new RouteService(
|
|
56
53
|
mockI18nConfig,
|
|
57
54
|
mockRouter,
|
|
58
55
|
null,
|
|
59
56
|
null,
|
|
60
57
|
navigateToMock,
|
|
61
|
-
setCookieMock,
|
|
62
|
-
null,
|
|
63
|
-
null,
|
|
64
58
|
)
|
|
65
59
|
})
|
|
66
60
|
|
|
@@ -71,14 +65,14 @@ describe('RouteService', () => {
|
|
|
71
65
|
|
|
72
66
|
test('getCurrentLocale should return the hash locale if hashMode is enabled', () => {
|
|
73
67
|
const mockI18nConfigWithHashMode = { ...mockI18nConfig, hashMode: true }
|
|
74
|
-
routeService = new RouteService(mockI18nConfigWithHashMode, mockRouter, 'de', null, navigateToMock
|
|
68
|
+
routeService = new RouteService(mockI18nConfigWithHashMode, mockRouter, 'de', null, navigateToMock)
|
|
75
69
|
const locale = routeService.getCurrentLocale()
|
|
76
70
|
expect(locale).toBe('de')
|
|
77
71
|
})
|
|
78
72
|
|
|
79
73
|
test('getCurrentLocale should return the noPrefix locale if noPrefix strategy is enabled', () => {
|
|
80
74
|
const mockI18nConfigWithNoPrefix: ModuleOptionsExtend = { ...mockI18nConfig, strategy: 'no_prefix' }
|
|
81
|
-
routeService = new RouteService(mockI18nConfigWithNoPrefix, mockRouter, null, 'ru', navigateToMock
|
|
75
|
+
routeService = new RouteService(mockI18nConfigWithNoPrefix, mockRouter, null, 'ru', navigateToMock)
|
|
82
76
|
const locale = routeService.getCurrentLocale()
|
|
83
77
|
expect(locale).toBe('ru')
|
|
84
78
|
})
|
|
@@ -92,7 +86,7 @@ describe('RouteService', () => {
|
|
|
92
86
|
{ code: 'ru', iso: 'ru-RU', displayName: 'Russian' },
|
|
93
87
|
],
|
|
94
88
|
}
|
|
95
|
-
routeService = new RouteService(mockI18nConfigWithDisplayName, mockRouter, null, null, navigateToMock
|
|
89
|
+
routeService = new RouteService(mockI18nConfigWithDisplayName, mockRouter, null, null, navigateToMock)
|
|
96
90
|
const route = { name: 'localized-about-en' } as RouteLocationNormalizedLoaded
|
|
97
91
|
const displayName = routeService.getCurrentName(route)
|
|
98
92
|
expect(displayName).toBe('English')
|
|
@@ -157,26 +151,6 @@ describe('RouteService', () => {
|
|
|
157
151
|
expect(fullPath).toBe('https://example.com/de/about')
|
|
158
152
|
})
|
|
159
153
|
|
|
160
|
-
test('updateCookies should update cookies for hashMode and noPrefixStrategy', () => {
|
|
161
|
-
const mockI18nConfigWithHashMode: ModuleOptionsExtend = {
|
|
162
|
-
...mockI18nConfig,
|
|
163
|
-
hashMode: true,
|
|
164
|
-
strategy: 'prefix_except_default',
|
|
165
|
-
}
|
|
166
|
-
routeService = new RouteService(mockI18nConfigWithHashMode, mockRouter, 'de', null, navigateToMock, setCookieMock, null, null)
|
|
167
|
-
routeService.updateCookies('de')
|
|
168
|
-
expect(setCookieMock).toHaveBeenCalledWith('hash-locale', 'de')
|
|
169
|
-
|
|
170
|
-
const mockI18nConfigWithNoPrefix: ModuleOptionsExtend = {
|
|
171
|
-
...mockI18nConfig,
|
|
172
|
-
strategy: 'no_prefix',
|
|
173
|
-
localeCookie: 'custom-locale',
|
|
174
|
-
}
|
|
175
|
-
routeService = new RouteService(mockI18nConfigWithNoPrefix, mockRouter, null, 'ru', navigateToMock, setCookieMock, null, null)
|
|
176
|
-
routeService.updateCookies('ru')
|
|
177
|
-
expect(setCookieMock).toHaveBeenCalledWith('custom-locale', 'ru')
|
|
178
|
-
})
|
|
179
|
-
|
|
180
154
|
test('resolveLocalizedRoute should return the correct localized route', () => {
|
|
181
155
|
const to = { path: '/about' } as RouteLocationAsPathGeneric
|
|
182
156
|
mockRouter.resolve.mockReturnValueOnce({
|
|
@@ -210,21 +184,38 @@ describe('RouteService', () => {
|
|
|
210
184
|
expect(locale).toBe('ru') // Should extract from URL
|
|
211
185
|
})
|
|
212
186
|
|
|
213
|
-
test('getCurrentLocale should
|
|
187
|
+
test('getCurrentLocale should return defaultLocale for prefix_except_default when URL has no locale prefix', () => {
|
|
214
188
|
routeService = new RouteService(
|
|
215
|
-
mockI18nConfig,
|
|
189
|
+
mockI18nConfig, // strategy: prefix_except_default
|
|
216
190
|
mockRouter,
|
|
217
191
|
null,
|
|
218
192
|
null,
|
|
219
193
|
navigateToMock,
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
194
|
+
() => 'de', // getter from plugin (cookie/state)
|
|
195
|
+
)
|
|
196
|
+
mockRouter.currentRoute.value.params = {} // Remove locale from params
|
|
197
|
+
mockRouter.currentRoute.value.path = '/' // No locale in path
|
|
198
|
+
const locale = routeService.getCurrentLocale()
|
|
199
|
+
// For prefix_except_default: URL without locale = defaultLocale (SEO requirement)
|
|
200
|
+
// Getter is NOT used because URL explicitly indicates defaultLocale
|
|
201
|
+
expect(locale).toBe('en')
|
|
202
|
+
})
|
|
203
|
+
|
|
204
|
+
test('getCurrentLocale should use getDefaultLocale for no_prefix strategy', () => {
|
|
205
|
+
const noPrefixConfig = { ...mockI18nConfig, strategy: 'no_prefix' as const }
|
|
206
|
+
routeService = new RouteService(
|
|
207
|
+
noPrefixConfig,
|
|
208
|
+
mockRouter,
|
|
209
|
+
null,
|
|
210
|
+
'de', // noPrefixDefault
|
|
211
|
+
navigateToMock,
|
|
212
|
+
() => 'de', // getter from plugin (cookie/state)
|
|
223
213
|
)
|
|
224
214
|
mockRouter.currentRoute.value.params = {} // Remove locale from params
|
|
225
215
|
mockRouter.currentRoute.value.path = '/' // No locale in path
|
|
226
216
|
const locale = routeService.getCurrentLocale()
|
|
227
|
-
|
|
217
|
+
// For no_prefix: getter/noPrefixDefault determines locale
|
|
218
|
+
expect(locale).toBe('de')
|
|
228
219
|
})
|
|
229
220
|
|
|
230
221
|
test('getCurrentLocale should prioritize route params over URL path', () => {
|
|
@@ -234,55 +225,19 @@ describe('RouteService', () => {
|
|
|
234
225
|
expect(locale).toBe('de') // Should use route params
|
|
235
226
|
})
|
|
236
227
|
|
|
237
|
-
test('getCurrentLocale should prioritize URL path over
|
|
228
|
+
test('getCurrentLocale should prioritize URL path over getDefaultLocale', () => {
|
|
238
229
|
routeService = new RouteService(
|
|
239
230
|
mockI18nConfig,
|
|
240
231
|
mockRouter,
|
|
241
232
|
null,
|
|
242
233
|
null,
|
|
243
234
|
navigateToMock,
|
|
244
|
-
|
|
245
|
-
'de', // cookie locale
|
|
246
|
-
'user-locale', // cookie name
|
|
235
|
+
() => 'de', // getter
|
|
247
236
|
)
|
|
248
237
|
mockRouter.currentRoute.value.params = {} // No locale in params
|
|
249
238
|
mockRouter.currentRoute.value.path = '/ru/sdfsdf' // URL has locale
|
|
250
239
|
const locale = routeService.getCurrentLocale()
|
|
251
|
-
expect(locale).toBe('ru') // Should use URL path, not
|
|
252
|
-
})
|
|
253
|
-
|
|
254
|
-
test('updateCookies should update cookie for regular strategy', () => {
|
|
255
|
-
routeService = new RouteService(
|
|
256
|
-
mockI18nConfig,
|
|
257
|
-
mockRouter,
|
|
258
|
-
null,
|
|
259
|
-
null,
|
|
260
|
-
navigateToMock,
|
|
261
|
-
setCookieMock,
|
|
262
|
-
null,
|
|
263
|
-
'user-locale', // cookie name
|
|
264
|
-
)
|
|
265
|
-
routeService.updateCookies('ru')
|
|
266
|
-
expect(setCookieMock).toHaveBeenCalledWith('user-locale', 'ru')
|
|
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')
|
|
240
|
+
expect(locale).toBe('ru') // Should use URL path, not getter
|
|
286
241
|
})
|
|
287
242
|
|
|
288
243
|
test('getCurrentName should return null if displayName is missing', () => {
|
|
@@ -294,7 +249,7 @@ describe('RouteService', () => {
|
|
|
294
249
|
{ code: 'ru', iso: 'ru-RU' },
|
|
295
250
|
],
|
|
296
251
|
}
|
|
297
|
-
routeService = new RouteService(mockI18nConfigWithoutDisplayName, mockRouter, null, null, navigateToMock
|
|
252
|
+
routeService = new RouteService(mockI18nConfigWithoutDisplayName, mockRouter, null, null, navigateToMock)
|
|
298
253
|
const route = { name: 'localized-about-en' } as RouteLocationNormalizedLoaded
|
|
299
254
|
const displayName = routeService.getCurrentName(route)
|
|
300
255
|
expect(displayName).toBeNull()
|
|
@@ -370,11 +325,6 @@ describe('RouteService', () => {
|
|
|
370
325
|
})
|
|
371
326
|
})
|
|
372
327
|
|
|
373
|
-
test('updateCookies should use default cookie when hashMode and noPrefixStrategy are disabled', () => {
|
|
374
|
-
routeService.updateCookies('de')
|
|
375
|
-
expect(setCookieMock).toHaveBeenCalledWith('user-locale', 'de')
|
|
376
|
-
})
|
|
377
|
-
|
|
378
328
|
test('getCurrentLocale should extract locale from fullPath with query params when route.path is missing', () => {
|
|
379
329
|
mockRouter.currentRoute.value.params = {}
|
|
380
330
|
mockRouter.currentRoute.value.path = '' // Simulate missing path (e.g. error state)
|