@bleedingdev/modern-js-plugin-i18n 3.2.0-ultramodern.12 → 3.2.0-ultramodern.120

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.
Files changed (117) hide show
  1. package/README.md +221 -11
  2. package/dist/cjs/cli/index.js +32 -5
  3. package/dist/cjs/runtime/I18nLink.js +17 -28
  4. package/dist/cjs/runtime/Link.js +252 -0
  5. package/dist/cjs/runtime/canonicalRoutes.js +18 -0
  6. package/dist/cjs/runtime/context.js +41 -10
  7. package/dist/cjs/runtime/hooks.js +17 -10
  8. package/dist/cjs/runtime/i18n/backend/config.js +9 -5
  9. package/dist/cjs/runtime/i18n/backend/defaults.js +15 -10
  10. package/dist/cjs/runtime/i18n/backend/defaults.node.js +16 -11
  11. package/dist/cjs/runtime/i18n/backend/index.js +9 -5
  12. package/dist/cjs/runtime/i18n/backend/middleware.common.js +9 -5
  13. package/dist/cjs/runtime/i18n/backend/middleware.js +9 -5
  14. package/dist/cjs/runtime/i18n/backend/middleware.node.js +13 -9
  15. package/dist/cjs/runtime/i18n/backend/sdk-backend.js +9 -5
  16. package/dist/cjs/runtime/i18n/backend/sdk-event.js +16 -11
  17. package/dist/cjs/runtime/i18n/detection/config.js +9 -5
  18. package/dist/cjs/runtime/i18n/detection/index.js +9 -5
  19. package/dist/cjs/runtime/i18n/detection/middleware.js +9 -5
  20. package/dist/cjs/runtime/i18n/detection/middleware.node.js +9 -5
  21. package/dist/cjs/runtime/i18n/index.js +9 -5
  22. package/dist/cjs/runtime/i18n/instance.js +17 -37
  23. package/dist/cjs/runtime/i18n/react-i18next.js +53 -0
  24. package/dist/cjs/runtime/i18n/utils.js +9 -17
  25. package/dist/cjs/runtime/index.js +50 -15
  26. package/dist/cjs/runtime/localizedPaths.js +105 -0
  27. package/dist/cjs/runtime/routerAdapter.js +167 -0
  28. package/dist/cjs/runtime/utils.js +87 -97
  29. package/dist/cjs/server/index.js +69 -13
  30. package/dist/cjs/shared/deepMerge.js +12 -8
  31. package/dist/cjs/shared/detection.js +9 -5
  32. package/dist/cjs/shared/localisedUrls.js +271 -0
  33. package/dist/cjs/shared/utils.js +15 -11
  34. package/dist/esm/cli/index.mjs +23 -0
  35. package/dist/esm/runtime/I18nLink.mjs +7 -22
  36. package/dist/esm/runtime/Link.mjs +209 -0
  37. package/dist/esm/runtime/canonicalRoutes.mjs +0 -0
  38. package/dist/esm/runtime/context.mjs +34 -7
  39. package/dist/esm/runtime/hooks.mjs +9 -6
  40. package/dist/esm/runtime/i18n/backend/defaults.mjs +1 -1
  41. package/dist/esm/runtime/i18n/backend/defaults.node.mjs +2 -2
  42. package/dist/esm/runtime/i18n/backend/middleware.node.mjs +3 -3
  43. package/dist/esm/runtime/i18n/instance.mjs +1 -19
  44. package/dist/esm/runtime/i18n/react-i18next.mjs +15 -0
  45. package/dist/esm/runtime/i18n/utils.mjs +0 -12
  46. package/dist/esm/runtime/index.mjs +23 -13
  47. package/dist/esm/runtime/localizedPaths.mjs +58 -0
  48. package/dist/esm/runtime/routerAdapter.mjs +129 -0
  49. package/dist/esm/runtime/utils.mjs +25 -30
  50. package/dist/esm/server/index.mjs +53 -7
  51. package/dist/esm/shared/localisedUrls.mjs +212 -0
  52. package/dist/esm-node/cli/index.mjs +23 -0
  53. package/dist/esm-node/runtime/I18nLink.mjs +7 -22
  54. package/dist/esm-node/runtime/Link.mjs +210 -0
  55. package/dist/esm-node/runtime/canonicalRoutes.mjs +1 -0
  56. package/dist/esm-node/runtime/context.mjs +34 -7
  57. package/dist/esm-node/runtime/hooks.mjs +9 -6
  58. package/dist/esm-node/runtime/i18n/backend/defaults.mjs +1 -1
  59. package/dist/esm-node/runtime/i18n/backend/defaults.node.mjs +2 -2
  60. package/dist/esm-node/runtime/i18n/backend/middleware.node.mjs +3 -3
  61. package/dist/esm-node/runtime/i18n/instance.mjs +1 -19
  62. package/dist/esm-node/runtime/i18n/react-i18next.mjs +16 -0
  63. package/dist/esm-node/runtime/i18n/utils.mjs +0 -12
  64. package/dist/esm-node/runtime/index.mjs +23 -13
  65. package/dist/esm-node/runtime/localizedPaths.mjs +59 -0
  66. package/dist/esm-node/runtime/routerAdapter.mjs +130 -0
  67. package/dist/esm-node/runtime/utils.mjs +25 -30
  68. package/dist/esm-node/server/index.mjs +53 -7
  69. package/dist/esm-node/shared/localisedUrls.mjs +213 -0
  70. package/dist/types/cli/index.d.ts +1 -0
  71. package/dist/types/runtime/I18nLink.d.ts +6 -0
  72. package/dist/types/runtime/Link.d.ts +56 -0
  73. package/dist/types/runtime/canonicalRoutes.d.ts +60 -0
  74. package/dist/types/runtime/context.d.ts +3 -0
  75. package/dist/types/runtime/hooks.d.ts +4 -2
  76. package/dist/types/runtime/i18n/backend/middleware.node.d.ts +1 -1
  77. package/dist/types/runtime/i18n/instance.d.ts +4 -6
  78. package/dist/types/runtime/i18n/react-i18next.d.ts +7 -0
  79. package/dist/types/runtime/index.d.ts +6 -1
  80. package/dist/types/runtime/localizedPaths.d.ts +39 -0
  81. package/dist/types/runtime/routerAdapter.d.ts +26 -0
  82. package/dist/types/runtime/types.d.ts +1 -1
  83. package/dist/types/runtime/utils.d.ts +13 -9
  84. package/dist/types/server/index.d.ts +6 -0
  85. package/dist/types/shared/localisedUrls.d.ts +21 -0
  86. package/dist/types/shared/type.d.ts +12 -0
  87. package/package.json +24 -28
  88. package/rstest.config.mts +39 -0
  89. package/src/cli/index.ts +44 -1
  90. package/src/runtime/I18nLink.tsx +14 -51
  91. package/src/runtime/Link.tsx +414 -0
  92. package/src/runtime/canonicalRoutes.ts +93 -0
  93. package/src/runtime/context.tsx +45 -7
  94. package/src/runtime/hooks.ts +13 -4
  95. package/src/runtime/i18n/backend/defaults.node.ts +2 -2
  96. package/src/runtime/i18n/backend/defaults.ts +3 -1
  97. package/src/runtime/i18n/backend/middleware.node.ts +1 -1
  98. package/src/runtime/i18n/instance.ts +3 -30
  99. package/src/runtime/i18n/react-i18next.ts +25 -0
  100. package/src/runtime/i18n/utils.ts +4 -26
  101. package/src/runtime/index.tsx +47 -12
  102. package/src/runtime/localizedPaths.ts +118 -0
  103. package/src/runtime/routerAdapter.tsx +333 -0
  104. package/src/runtime/types.ts +1 -1
  105. package/src/runtime/utils.ts +44 -37
  106. package/src/server/index.ts +117 -10
  107. package/src/shared/localisedUrls.ts +453 -0
  108. package/src/shared/type.ts +12 -0
  109. package/tests/i18nUtils.test.ts +52 -0
  110. package/tests/link.test.tsx +475 -0
  111. package/tests/linkTypes.test.ts +28 -0
  112. package/tests/localisedUrls.test.ts +312 -0
  113. package/tests/routerAdapter.test.tsx +452 -0
  114. package/tests/type-fixture/linkTypes.fixture.tsx +51 -0
  115. package/tests/type-fixture/tsconfig.json +15 -0
  116. package/dist/esm/rslib-runtime.mjs +0 -18
  117. package/dist/esm-node/rslib-runtime.mjs +0 -19
@@ -0,0 +1,453 @@
1
+ import type { NestedRouteForCli, PageRoute } from '@modern-js/types';
2
+
3
+ export type LocalisedUrlPathMap = Record<string, string>;
4
+ export type LocalisedUrlsMap = Record<string, LocalisedUrlPathMap>;
5
+ export type LocalisedUrlsOption = boolean | LocalisedUrlsMap;
6
+
7
+ export interface ResolvedLocalisedUrlsConfig {
8
+ enabled: boolean;
9
+ map: LocalisedUrlsMap;
10
+ }
11
+
12
+ const LOCALE_PARAM_NAMES = new Set(['lang', 'locale', 'language']);
13
+
14
+ export const normalisePathPattern = (path: string): string => {
15
+ const withoutDuplicateSlashes = path.replace(/\/+/g, '/');
16
+ const withLeadingSlash = withoutDuplicateSlashes.startsWith('/')
17
+ ? withoutDuplicateSlashes
18
+ : `/${withoutDuplicateSlashes}`;
19
+ const withoutTrailingSlash =
20
+ withLeadingSlash.length > 1
21
+ ? withLeadingSlash.replace(/\/+$/, '')
22
+ : withLeadingSlash;
23
+
24
+ return withoutTrailingSlash.replace(/\[(.+?)\]/g, ':$1');
25
+ };
26
+
27
+ const normaliseRoutePath = (path: string): string => {
28
+ const normalized = normalisePathPattern(path);
29
+ return normalized === '/' ? '' : normalized.slice(1);
30
+ };
31
+
32
+ const getLocaleParamSegment = (segment: string): string | null => {
33
+ if (!segment.startsWith(':')) {
34
+ return null;
35
+ }
36
+
37
+ const paramName = segment.slice(1).replace(/\?$/, '');
38
+ return LOCALE_PARAM_NAMES.has(paramName) ? segment : null;
39
+ };
40
+
41
+ const splitPathSegments = (path?: string): string[] => {
42
+ if (!path) {
43
+ return [];
44
+ }
45
+
46
+ return normalisePathPattern(path).split('/').filter(Boolean);
47
+ };
48
+
49
+ const stripLeadingLocaleParam = (path?: string): string | undefined => {
50
+ const segments = splitPathSegments(path);
51
+ const leadingLocaleParam = getLocaleParamSegment(segments[0] || '');
52
+
53
+ if (!leadingLocaleParam) {
54
+ return path;
55
+ }
56
+
57
+ const remainingPath = segments.slice(1).join('/');
58
+ return remainingPath ? `/${remainingPath}` : undefined;
59
+ };
60
+
61
+ const getLeadingLocaleParam = (path?: string): string | null => {
62
+ const segments = splitPathSegments(path);
63
+ return getLocaleParamSegment(segments[0] || '');
64
+ };
65
+
66
+ export const resolveLocalisedUrlsConfig = (
67
+ option: LocalisedUrlsOption | undefined,
68
+ ): ResolvedLocalisedUrlsConfig => {
69
+ if (option === false) {
70
+ return { enabled: false, map: {} };
71
+ }
72
+
73
+ if (option && typeof option === 'object') {
74
+ return { enabled: true, map: option };
75
+ }
76
+
77
+ return { enabled: true, map: {} };
78
+ };
79
+
80
+ const isLocaleParamPath = (path?: string): boolean => {
81
+ const segments = splitPathSegments(path);
82
+ return segments.length === 1 && Boolean(getLocaleParamSegment(segments[0]));
83
+ };
84
+
85
+ const isLocalisableRoutePath = (path?: string): path is string => {
86
+ const pathWithoutLocale = stripLeadingLocaleParam(path);
87
+
88
+ if (
89
+ !pathWithoutLocale ||
90
+ pathWithoutLocale === '/' ||
91
+ pathWithoutLocale === '*'
92
+ ) {
93
+ return false;
94
+ }
95
+
96
+ return true;
97
+ };
98
+
99
+ const joinPath = (parentPath: string, routePath?: string): string => {
100
+ if (!isLocalisableRoutePath(routePath)) {
101
+ return parentPath;
102
+ }
103
+
104
+ const segment = normaliseRoutePath(stripLeadingLocaleParam(routePath) || '');
105
+ return normalisePathPattern(`${parentPath}/${segment}`);
106
+ };
107
+
108
+ const ensureLocalisedUrlsForPath = (
109
+ canonicalPath: string,
110
+ languages: string[],
111
+ localisedUrls: LocalisedUrlsMap,
112
+ ): LocalisedUrlPathMap => {
113
+ const entry = localisedUrls[canonicalPath];
114
+ if (!entry) {
115
+ throw new Error(
116
+ `localisedUrls is enabled, but route "${canonicalPath}" does not define localised URLs for languages: ${languages.join(
117
+ ', ',
118
+ )}. Add localisedUrls["${canonicalPath}"] or set localeDetection.localisedUrls to false.`,
119
+ );
120
+ }
121
+
122
+ const missingLanguages = languages.filter(language => !entry[language]);
123
+ if (missingLanguages.length > 0) {
124
+ throw new Error(
125
+ `localisedUrls["${canonicalPath}"] is missing languages: ${missingLanguages.join(
126
+ ', ',
127
+ )}. Every configured language must have a localised URL.`,
128
+ );
129
+ }
130
+
131
+ return entry;
132
+ };
133
+
134
+ export const validateLocalisedUrls = (
135
+ routes: (NestedRouteForCli | PageRoute)[],
136
+ languages: string[],
137
+ localisedUrls: LocalisedUrlsMap,
138
+ ) => {
139
+ const visit = (route: NestedRouteForCli | PageRoute, parentPath: string) => {
140
+ const canonicalPath = joinPath(parentPath, route.path);
141
+ if (isLocalisableRoutePath(route.path)) {
142
+ ensureLocalisedUrlsForPath(canonicalPath, languages, localisedUrls);
143
+ }
144
+
145
+ if ('children' in route && route.children) {
146
+ route.children.forEach(child => visit(child, canonicalPath));
147
+ }
148
+ };
149
+
150
+ routes.forEach(route => visit(route, ''));
151
+ };
152
+
153
+ const getLocalisedRoutePaths = (
154
+ canonicalPath: string,
155
+ parentLocalisedPaths: Record<string, string>,
156
+ languages: string[],
157
+ entry: LocalisedUrlPathMap,
158
+ ): string[] => {
159
+ const paths = languages.map(language => {
160
+ const fullPath = normalisePathPattern(entry[language]);
161
+ const parentPath = normalisePathPattern(
162
+ parentLocalisedPaths[language] || '/',
163
+ );
164
+ if (parentPath === '/') {
165
+ return normaliseRoutePath(fullPath) || undefined;
166
+ }
167
+
168
+ const parentPrefix = `${parentPath}/`;
169
+ if (!fullPath.startsWith(parentPrefix)) {
170
+ throw new Error(
171
+ `localisedUrls["${canonicalPath}"].${language} must be nested under "${parentPath}" because its parent route is localised there.`,
172
+ );
173
+ }
174
+
175
+ return normaliseRoutePath(fullPath.slice(parentPath.length));
176
+ });
177
+
178
+ return Array.from(new Set(paths.filter(Boolean) as string[]));
179
+ };
180
+
181
+ const transformLocalisedRoute = (
182
+ route: NestedRouteForCli | PageRoute,
183
+ parentCanonicalPath: string,
184
+ parentLocalisedPaths: Record<string, string>,
185
+ languages: string[],
186
+ localisedUrls: LocalisedUrlsMap,
187
+ ): (NestedRouteForCli | PageRoute)[] => {
188
+ const canonicalPath = joinPath(parentCanonicalPath, route.path);
189
+ const localisedUrlEntry = isLocalisableRoutePath(route.path)
190
+ ? ensureLocalisedUrlsForPath(canonicalPath, languages, localisedUrls)
191
+ : undefined;
192
+ const routeLocalisedPaths = localisedUrlEntry
193
+ ? languages.reduce<Record<string, string>>((acc, language) => {
194
+ acc[language] = normalisePathPattern(localisedUrlEntry[language]);
195
+ return acc;
196
+ }, {})
197
+ : parentLocalisedPaths;
198
+
199
+ const children =
200
+ 'children' in route && route.children
201
+ ? route.children.flatMap(child =>
202
+ transformLocalisedRoute(
203
+ child,
204
+ canonicalPath,
205
+ routeLocalisedPaths,
206
+ languages,
207
+ localisedUrls,
208
+ ),
209
+ )
210
+ : undefined;
211
+
212
+ const baseRoute = {
213
+ ...route,
214
+ ...(children ? { children } : {}),
215
+ } as NestedRouteForCli | PageRoute;
216
+
217
+ if (!localisedUrlEntry) {
218
+ return [baseRoute];
219
+ }
220
+
221
+ return getLocalisedRoutePaths(
222
+ canonicalPath,
223
+ parentLocalisedPaths,
224
+ languages,
225
+ localisedUrlEntry,
226
+ ).map((localisedPath, index) =>
227
+ cloneRouteWithLocalisedPath(baseRoute, localisedPath, index, canonicalPath),
228
+ );
229
+ };
230
+
231
+ const legalRouteIdPart = (value: string): string =>
232
+ value.replace(/[^a-zA-Z0-9_$-]+/g, '_').replace(/^_+|_+$/g, '') || 'index';
233
+
234
+ const suffixRouteIds = <T extends NestedRouteForCli | PageRoute>(
235
+ route: T,
236
+ suffix: string,
237
+ ): T => {
238
+ const children =
239
+ 'children' in route && route.children
240
+ ? route.children.map(child => suffixRouteIds(child, suffix))
241
+ : undefined;
242
+
243
+ return {
244
+ ...route,
245
+ ...(route.id ? { id: `${route.id}__localised_${suffix}` } : {}),
246
+ ...(children ? { children } : {}),
247
+ };
248
+ };
249
+
250
+ const cloneRouteWithLocalisedPath = (
251
+ route: NestedRouteForCli | PageRoute,
252
+ path: string,
253
+ index: number,
254
+ canonicalPath: string,
255
+ ): NestedRouteForCli | PageRoute => {
256
+ const leadingLocaleParam = getLeadingLocaleParam(route.path);
257
+ const localisedPath = leadingLocaleParam
258
+ ? normaliseRoutePath(`${leadingLocaleParam}/${path}`)
259
+ : path;
260
+ const routeWithPath = {
261
+ ...route,
262
+ path: localisedPath,
263
+ } as NestedRouteForCli | PageRoute;
264
+ // Language-agnostic source pattern; lets downstream codegen collapse the
265
+ // localized physical variants back to one canonical route.
266
+ (routeWithPath as { modernCanonicalPath?: string }).modernCanonicalPath =
267
+ canonicalPath;
268
+
269
+ return index === 0
270
+ ? routeWithPath
271
+ : suffixRouteIds(routeWithPath, legalRouteIdPart(localisedPath));
272
+ };
273
+
274
+ export const applyLocalisedUrlsToRoutes = (
275
+ routes: (NestedRouteForCli | PageRoute)[],
276
+ languages: string[],
277
+ localisedUrls: LocalisedUrlsMap,
278
+ ): (NestedRouteForCli | PageRoute)[] => {
279
+ const rootLocalisedPaths = languages.reduce<Record<string, string>>(
280
+ (acc, language) => {
281
+ acc[language] = '/';
282
+ return acc;
283
+ },
284
+ {},
285
+ );
286
+
287
+ validateLocalisedUrls(routes, languages, localisedUrls);
288
+
289
+ return routes.flatMap(route =>
290
+ transformLocalisedRoute(
291
+ route,
292
+ '',
293
+ rootLocalisedPaths,
294
+ languages,
295
+ localisedUrls,
296
+ ),
297
+ );
298
+ };
299
+
300
+ const escapeRegExp = (value: string): string =>
301
+ value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
302
+
303
+ const getParamName = (segment: string): string =>
304
+ segment.slice(1).replace(/\?$/, '');
305
+
306
+ const compilePathPattern = (pattern: string) => {
307
+ const names: string[] = [];
308
+ const segments = normalisePathPattern(pattern).split('/').filter(Boolean);
309
+ const source = segments
310
+ .map(segment => {
311
+ if (segment.startsWith(':')) {
312
+ names.push(getParamName(segment));
313
+ const paramPattern = '([^/]+)';
314
+ return segment.endsWith('?')
315
+ ? `(?:/${paramPattern})?`
316
+ : `/${paramPattern}`;
317
+ }
318
+ if (segment === '*') {
319
+ names.push('*');
320
+ return '/(.*)';
321
+ }
322
+ return `/${escapeRegExp(segment)}`;
323
+ })
324
+ .join('');
325
+
326
+ return {
327
+ names,
328
+ regexp: new RegExp(`^${source || '/'}$`),
329
+ };
330
+ };
331
+
332
+ export const matchPathPattern = (
333
+ pathname: string,
334
+ pattern: string,
335
+ ): Record<string, string> | null => {
336
+ const { names, regexp } = compilePathPattern(pattern);
337
+ const match = regexp.exec(normalisePathPattern(pathname));
338
+ if (!match) {
339
+ return null;
340
+ }
341
+
342
+ return names.reduce<Record<string, string>>((params, name, index) => {
343
+ params[name] = decodeURIComponent(match[index + 1] || '');
344
+ return params;
345
+ }, {});
346
+ };
347
+
348
+ export const buildPathFromPattern = (
349
+ pattern: string,
350
+ params: Record<string, string>,
351
+ ): string => {
352
+ const segments = normalisePathPattern(pattern).split('/').filter(Boolean);
353
+ const path = segments
354
+ .map(segment => {
355
+ if (segment.startsWith(':')) {
356
+ const param = params[getParamName(segment)];
357
+ return param ? encodeURIComponent(param) : '';
358
+ }
359
+ if (segment === '*') {
360
+ return params['*'] || '';
361
+ }
362
+ return segment;
363
+ })
364
+ .filter(Boolean)
365
+ .join('/');
366
+
367
+ return `/${path}`;
368
+ };
369
+
370
+ export const resolveLocalisedPath = (
371
+ pathname: string,
372
+ targetLanguage: string,
373
+ languages: string[],
374
+ localisedUrls: LocalisedUrlsMap,
375
+ ): string => {
376
+ const normalizedPathname = normalisePathPattern(pathname);
377
+
378
+ // Canonical keys take precedence: authors write language-agnostic paths,
379
+ // which are the map keys, even when no language pattern equals the key.
380
+ for (const [canonicalPattern, localisedUrlEntry] of Object.entries(
381
+ localisedUrls,
382
+ )) {
383
+ const targetPattern = localisedUrlEntry[targetLanguage];
384
+ if (!targetPattern) {
385
+ continue;
386
+ }
387
+
388
+ const params = matchPathPattern(normalizedPathname, canonicalPattern);
389
+ if (params) {
390
+ return buildPathFromPattern(targetPattern, params);
391
+ }
392
+ }
393
+
394
+ for (const localisedUrlEntry of Object.values(localisedUrls)) {
395
+ const targetPattern = localisedUrlEntry[targetLanguage];
396
+ if (!targetPattern) {
397
+ continue;
398
+ }
399
+
400
+ for (const language of languages) {
401
+ const sourcePattern = localisedUrlEntry[language];
402
+ if (!sourcePattern) {
403
+ continue;
404
+ }
405
+
406
+ const params = matchPathPattern(normalizedPathname, sourcePattern);
407
+ if (params) {
408
+ return buildPathFromPattern(targetPattern, params);
409
+ }
410
+ }
411
+ }
412
+
413
+ return normalizedPathname;
414
+ };
415
+
416
+ /**
417
+ * Reverse-map a language-specific pathname (without language prefix) back to
418
+ * the canonical, language-agnostic path: localized slug patterns are matched
419
+ * against every language variant and rebuilt from the canonical map key.
420
+ */
421
+ export const resolveCanonicalLocalisedPath = (
422
+ pathname: string,
423
+ languages: string[],
424
+ localisedUrls: LocalisedUrlsMap,
425
+ ): string => {
426
+ const normalizedPathname = normalisePathPattern(pathname);
427
+
428
+ for (const [canonicalPattern, localisedUrlEntry] of Object.entries(
429
+ localisedUrls,
430
+ )) {
431
+ const canonicalParams = matchPathPattern(
432
+ normalizedPathname,
433
+ canonicalPattern,
434
+ );
435
+ if (canonicalParams) {
436
+ return buildPathFromPattern(canonicalPattern, canonicalParams);
437
+ }
438
+
439
+ for (const language of languages) {
440
+ const sourcePattern = localisedUrlEntry[language];
441
+ if (!sourcePattern) {
442
+ continue;
443
+ }
444
+
445
+ const params = matchPathPattern(normalizedPathname, sourcePattern);
446
+ if (params) {
447
+ return buildPathFromPattern(canonicalPattern, params);
448
+ }
449
+ }
450
+ }
451
+
452
+ return normalizedPathname;
453
+ };
@@ -2,6 +2,7 @@ import type {
2
2
  LanguageDetectorOptions,
3
3
  Resources,
4
4
  } from '../runtime/i18n/instance';
5
+ import type { LocalisedUrlsOption } from './localisedUrls';
5
6
 
6
7
  export interface BaseLocaleDetectionOptions {
7
8
  localePathRedirect?: boolean;
@@ -10,6 +11,17 @@ export interface BaseLocaleDetectionOptions {
10
11
  fallbackLanguage?: string;
11
12
  detection?: LanguageDetectorOptions;
12
13
  ignoreRedirectRoutes?: string[] | ((pathname: string) => boolean);
14
+ /**
15
+ * Enables localised pathnames in addition to the locale prefix.
16
+ *
17
+ * - `false`: keep only locale-prefix behavior (`/en/about`).
18
+ * - object: map canonical route paths to every configured language.
19
+ *
20
+ * Defaults to `true` when `localePathRedirect` is enabled, so route
21
+ * generation validates that every localisable route path has entries for all
22
+ * configured languages.
23
+ */
24
+ localisedUrls?: LocalisedUrlsOption;
13
25
  }
14
26
 
15
27
  export interface LocaleDetectionOptions extends BaseLocaleDetectionOptions {
@@ -0,0 +1,52 @@
1
+ import { describe, expect, test } from '@rstest/core';
2
+ import type { I18nInstance } from '../src/runtime/i18n';
3
+ import { DEFAULT_I18NEXT_BACKEND_OPTIONS as NODE_DEFAULT_I18NEXT_BACKEND_OPTIONS } from '../src/runtime/i18n/backend/defaults.node';
4
+ import { initializeI18nInstance } from '../src/runtime/i18n/utils';
5
+
6
+ function createBackendI18nInstance(): I18nInstance {
7
+ return {
8
+ language: 'en',
9
+ isInitialized: false,
10
+ init: async () => undefined,
11
+ use: () => {},
12
+ options: {},
13
+ store: {
14
+ data: {},
15
+ },
16
+ };
17
+ }
18
+
19
+ describe('i18n runtime utils', () => {
20
+ test('uses the generated public locale directory for node fs backend defaults', () => {
21
+ expect(NODE_DEFAULT_I18NEXT_BACKEND_OPTIONS.loadPath).toBe(
22
+ './config/public/locales/{{lng}}/{{ns}}.json',
23
+ );
24
+ });
25
+
26
+ test('does not poll for backend resources after init', async () => {
27
+ const i18nInstance = createBackendI18nInstance();
28
+ const init = rstest.fn(async () => {
29
+ i18nInstance.isInitialized = true;
30
+ });
31
+ i18nInstance.init = init as I18nInstance['init'];
32
+ const setTimeoutSpy = rstest.spyOn(globalThis, 'setTimeout');
33
+
34
+ await initializeI18nInstance(
35
+ i18nInstance,
36
+ 'cs',
37
+ 'en',
38
+ ['en', 'cs'],
39
+ {},
40
+ {
41
+ enabled: true,
42
+ loadPath: '/locales/{{lng}}/{{ns}}.json',
43
+ },
44
+ {},
45
+ );
46
+
47
+ expect(init).toHaveBeenCalledTimes(1);
48
+ expect(setTimeoutSpy).not.toHaveBeenCalled();
49
+
50
+ setTimeoutSpy.mockRestore();
51
+ });
52
+ });