@bleedingdev/modern-js-plugin-i18n 3.2.0-ultramodern.3 → 3.2.0-ultramodern.30

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 (85) hide show
  1. package/dist/cjs/cli/index.js +22 -0
  2. package/dist/cjs/runtime/I18nLink.js +4 -12
  3. package/dist/cjs/runtime/context.js +32 -5
  4. package/dist/cjs/runtime/hooks.js +8 -5
  5. package/dist/cjs/runtime/i18n/backend/defaults.js +1 -1
  6. package/dist/cjs/runtime/i18n/backend/middleware.node.js +4 -4
  7. package/dist/cjs/runtime/i18n/instance.js +4 -2
  8. package/dist/cjs/runtime/index.js +7 -6
  9. package/dist/cjs/runtime/routerAdapter.js +163 -0
  10. package/dist/cjs/runtime/utils.js +63 -94
  11. package/dist/cjs/server/index.js +60 -8
  12. package/dist/cjs/shared/localisedUrls.js +237 -0
  13. package/dist/esm/cli/index.mjs +22 -0
  14. package/dist/esm/runtime/I18nLink.mjs +4 -12
  15. package/dist/esm/runtime/context.mjs +34 -7
  16. package/dist/esm/runtime/hooks.mjs +9 -6
  17. package/dist/esm/runtime/i18n/backend/defaults.mjs +1 -1
  18. package/dist/esm/runtime/i18n/backend/middleware.node.mjs +3 -3
  19. package/dist/esm/runtime/i18n/instance.mjs +4 -2
  20. package/dist/esm/runtime/index.mjs +7 -6
  21. package/dist/esm/runtime/routerAdapter.mjs +129 -0
  22. package/dist/esm/runtime/utils.mjs +11 -30
  23. package/dist/esm/server/index.mjs +53 -7
  24. package/dist/esm/shared/localisedUrls.mjs +191 -0
  25. package/dist/esm-node/cli/index.mjs +22 -0
  26. package/dist/esm-node/runtime/I18nLink.mjs +4 -12
  27. package/dist/esm-node/runtime/context.mjs +34 -7
  28. package/dist/esm-node/runtime/hooks.mjs +9 -6
  29. package/dist/esm-node/runtime/i18n/backend/defaults.mjs +1 -1
  30. package/dist/esm-node/runtime/i18n/backend/middleware.node.mjs +3 -3
  31. package/dist/esm-node/runtime/i18n/instance.mjs +4 -2
  32. package/dist/esm-node/runtime/index.mjs +7 -6
  33. package/dist/esm-node/runtime/routerAdapter.mjs +130 -0
  34. package/dist/esm-node/runtime/utils.mjs +11 -30
  35. package/dist/esm-node/server/index.mjs +53 -7
  36. package/dist/esm-node/shared/localisedUrls.mjs +192 -0
  37. package/dist/types/cli/index.d.ts +21 -0
  38. package/dist/types/runtime/I18nLink.d.ts +23 -0
  39. package/dist/types/runtime/context.d.ts +41 -0
  40. package/dist/types/runtime/hooks.d.ts +30 -0
  41. package/dist/types/runtime/i18n/backend/config.d.ts +2 -0
  42. package/dist/types/runtime/i18n/backend/defaults.d.ts +13 -0
  43. package/dist/types/runtime/i18n/backend/defaults.node.d.ts +8 -0
  44. package/dist/types/runtime/i18n/backend/index.d.ts +3 -0
  45. package/dist/types/runtime/i18n/backend/middleware.common.d.ts +14 -0
  46. package/dist/types/runtime/i18n/backend/middleware.d.ts +12 -0
  47. package/dist/types/runtime/i18n/backend/middleware.node.d.ts +13 -0
  48. package/dist/types/runtime/i18n/backend/sdk-backend.d.ts +53 -0
  49. package/dist/types/runtime/i18n/backend/sdk-event.d.ts +9 -0
  50. package/dist/types/runtime/i18n/detection/config.d.ts +11 -0
  51. package/dist/types/runtime/i18n/detection/index.d.ts +50 -0
  52. package/dist/types/runtime/i18n/detection/middleware.d.ts +24 -0
  53. package/dist/types/runtime/i18n/detection/middleware.node.d.ts +17 -0
  54. package/dist/types/runtime/i18n/index.d.ts +3 -0
  55. package/dist/types/runtime/i18n/instance.d.ts +96 -0
  56. package/dist/types/runtime/i18n/utils.d.ts +29 -0
  57. package/dist/types/runtime/index.d.ts +21 -0
  58. package/dist/types/runtime/routerAdapter.d.ts +26 -0
  59. package/dist/types/runtime/types.d.ts +15 -0
  60. package/dist/types/runtime/utils.d.ts +28 -0
  61. package/dist/types/server/index.d.ts +14 -0
  62. package/dist/types/shared/deepMerge.d.ts +1 -0
  63. package/dist/types/shared/detection.d.ts +11 -0
  64. package/dist/types/shared/localisedUrls.d.ts +13 -0
  65. package/dist/types/shared/type.d.ts +168 -0
  66. package/dist/types/shared/utils.d.ts +5 -0
  67. package/package.json +15 -15
  68. package/rstest.config.mts +39 -0
  69. package/src/cli/index.ts +43 -1
  70. package/src/runtime/I18nLink.tsx +10 -16
  71. package/src/runtime/context.tsx +45 -7
  72. package/src/runtime/hooks.ts +13 -4
  73. package/src/runtime/i18n/backend/defaults.ts +3 -1
  74. package/src/runtime/i18n/backend/middleware.node.ts +1 -1
  75. package/src/runtime/i18n/instance.ts +14 -5
  76. package/src/runtime/index.tsx +10 -2
  77. package/src/runtime/routerAdapter.tsx +333 -0
  78. package/src/runtime/utils.ts +22 -34
  79. package/src/server/index.ts +117 -10
  80. package/src/shared/localisedUrls.ts +393 -0
  81. package/src/shared/type.ts +12 -0
  82. package/tests/localisedUrls.test.ts +278 -0
  83. package/tests/routerAdapter.test.tsx +278 -0
  84. package/dist/esm/rslib-runtime.mjs +0 -18
  85. package/dist/esm-node/rslib-runtime.mjs +0 -19
@@ -0,0 +1,393 @@
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),
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
+ ): NestedRouteForCli | PageRoute => {
255
+ const leadingLocaleParam = getLeadingLocaleParam(route.path);
256
+ const localisedPath = leadingLocaleParam
257
+ ? normaliseRoutePath(`${leadingLocaleParam}/${path}`)
258
+ : path;
259
+ const routeWithPath = {
260
+ ...route,
261
+ path: localisedPath,
262
+ } as NestedRouteForCli | PageRoute;
263
+
264
+ return index === 0
265
+ ? routeWithPath
266
+ : suffixRouteIds(routeWithPath, legalRouteIdPart(localisedPath));
267
+ };
268
+
269
+ export const applyLocalisedUrlsToRoutes = (
270
+ routes: (NestedRouteForCli | PageRoute)[],
271
+ languages: string[],
272
+ localisedUrls: LocalisedUrlsMap,
273
+ ): (NestedRouteForCli | PageRoute)[] => {
274
+ const rootLocalisedPaths = languages.reduce<Record<string, string>>(
275
+ (acc, language) => {
276
+ acc[language] = '/';
277
+ return acc;
278
+ },
279
+ {},
280
+ );
281
+
282
+ validateLocalisedUrls(routes, languages, localisedUrls);
283
+
284
+ return routes.flatMap(route =>
285
+ transformLocalisedRoute(
286
+ route,
287
+ '',
288
+ rootLocalisedPaths,
289
+ languages,
290
+ localisedUrls,
291
+ ),
292
+ );
293
+ };
294
+
295
+ const escapeRegExp = (value: string): string =>
296
+ value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
297
+
298
+ const getParamName = (segment: string): string =>
299
+ segment.slice(1).replace(/\?$/, '');
300
+
301
+ const compilePathPattern = (pattern: string) => {
302
+ const names: string[] = [];
303
+ const segments = normalisePathPattern(pattern).split('/').filter(Boolean);
304
+ const source = segments
305
+ .map(segment => {
306
+ if (segment.startsWith(':')) {
307
+ names.push(getParamName(segment));
308
+ const paramPattern = '([^/]+)';
309
+ return segment.endsWith('?')
310
+ ? `(?:/${paramPattern})?`
311
+ : `/${paramPattern}`;
312
+ }
313
+ if (segment === '*') {
314
+ names.push('*');
315
+ return '/(.*)';
316
+ }
317
+ return `/${escapeRegExp(segment)}`;
318
+ })
319
+ .join('');
320
+
321
+ return {
322
+ names,
323
+ regexp: new RegExp(`^${source || '/'}$`),
324
+ };
325
+ };
326
+
327
+ const matchPathPattern = (
328
+ pathname: string,
329
+ pattern: string,
330
+ ): Record<string, string> | null => {
331
+ const { names, regexp } = compilePathPattern(pattern);
332
+ const match = regexp.exec(normalisePathPattern(pathname));
333
+ if (!match) {
334
+ return null;
335
+ }
336
+
337
+ return names.reduce<Record<string, string>>((params, name, index) => {
338
+ params[name] = decodeURIComponent(match[index + 1] || '');
339
+ return params;
340
+ }, {});
341
+ };
342
+
343
+ const buildPathFromPattern = (
344
+ pattern: string,
345
+ params: Record<string, string>,
346
+ ): string => {
347
+ const segments = normalisePathPattern(pattern).split('/').filter(Boolean);
348
+ const path = segments
349
+ .map(segment => {
350
+ if (segment.startsWith(':')) {
351
+ const param = params[getParamName(segment)];
352
+ return param ? encodeURIComponent(param) : '';
353
+ }
354
+ if (segment === '*') {
355
+ return params['*'] || '';
356
+ }
357
+ return segment;
358
+ })
359
+ .filter(Boolean)
360
+ .join('/');
361
+
362
+ return `/${path}`;
363
+ };
364
+
365
+ export const resolveLocalisedPath = (
366
+ pathname: string,
367
+ targetLanguage: string,
368
+ languages: string[],
369
+ localisedUrls: LocalisedUrlsMap,
370
+ ): string => {
371
+ const normalizedPathname = normalisePathPattern(pathname);
372
+
373
+ for (const localisedUrlEntry of Object.values(localisedUrls)) {
374
+ const targetPattern = localisedUrlEntry[targetLanguage];
375
+ if (!targetPattern) {
376
+ continue;
377
+ }
378
+
379
+ for (const language of languages) {
380
+ const sourcePattern = localisedUrlEntry[language];
381
+ if (!sourcePattern) {
382
+ continue;
383
+ }
384
+
385
+ const params = matchPathPattern(normalizedPathname, sourcePattern);
386
+ if (params) {
387
+ return buildPathFromPattern(targetPattern, params);
388
+ }
389
+ }
390
+ }
391
+
392
+ return normalizedPathname;
393
+ };
@@ -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,278 @@
1
+ import type { NestedRouteForCli } from '@modern-js/types';
2
+ import { describe, expect, test } from '@rstest/core';
3
+ import {
4
+ collectApiPrefixes,
5
+ i18nServerPlugin,
6
+ matchesApiPrefix,
7
+ } from '../src/server';
8
+ import {
9
+ applyLocalisedUrlsToRoutes,
10
+ resolveLocalisedPath,
11
+ validateLocalisedUrls,
12
+ } from '../src/shared/localisedUrls';
13
+
14
+ const createRoute = (
15
+ path: string,
16
+ children?: NestedRouteForCli[],
17
+ ): NestedRouteForCli => ({
18
+ id: path,
19
+ path,
20
+ type: 'nested',
21
+ origin: 'file-system',
22
+ routeType: children ? 'layout' : 'page',
23
+ _component: `${path}.tsx`,
24
+ children,
25
+ });
26
+
27
+ const createRequestContext = (pathname: string) =>
28
+ ({
29
+ req: {
30
+ url: `http://localhost${pathname}`,
31
+ },
32
+ }) as any;
33
+
34
+ describe('localisedUrls', () => {
35
+ test('requires every localisable route path to define every language', () => {
36
+ const routes = [createRoute(':lang', [createRoute('terms-of-service')])];
37
+
38
+ expect(() =>
39
+ validateLocalisedUrls(routes, ['en', 'cs'], {
40
+ '/terms-of-service': {
41
+ en: '/terms-of-service',
42
+ },
43
+ }),
44
+ ).toThrow('missing languages: cs');
45
+ });
46
+
47
+ test('expands route paths to localised aliases', () => {
48
+ const routes = [
49
+ createRoute(':lang', [
50
+ createRoute('terms-of-service'),
51
+ createRoute('products', [createRoute(':slug')]),
52
+ ]),
53
+ ];
54
+
55
+ const localisedRoutes = applyLocalisedUrlsToRoutes(routes, ['en', 'cs'], {
56
+ '/terms-of-service': {
57
+ en: '/terms-of-service',
58
+ cs: '/podminky-pouzivani',
59
+ },
60
+ '/products': {
61
+ en: '/products',
62
+ cs: '/produkty',
63
+ },
64
+ '/products/:slug': {
65
+ en: '/products/:slug',
66
+ cs: '/produkty/:slug',
67
+ },
68
+ });
69
+
70
+ const localeRoute = localisedRoutes[0] as NestedRouteForCli;
71
+ expect(localeRoute.children?.map(route => route.path)).toEqual([
72
+ 'terms-of-service',
73
+ 'podminky-pouzivani',
74
+ 'products',
75
+ 'produkty',
76
+ ]);
77
+
78
+ const productRoutes = localeRoute.children?.filter(
79
+ route => route.path === 'products' || route.path === 'produkty',
80
+ );
81
+ expect(productRoutes?.[0].children?.map(route => route.path)).toEqual([
82
+ ':slug',
83
+ ]);
84
+ expect(productRoutes?.[1].children?.map(route => route.path)).toEqual([
85
+ ':slug',
86
+ ]);
87
+ });
88
+
89
+ test('expands flat locale-prefixed route paths with canonical keys', () => {
90
+ const routes = [
91
+ createRoute('/:lang/about'),
92
+ createRoute('/:lang/products/:slug'),
93
+ ];
94
+
95
+ const localisedRoutes = applyLocalisedUrlsToRoutes(routes, ['en', 'cs'], {
96
+ '/about': {
97
+ en: '/about',
98
+ cs: '/o-nas',
99
+ },
100
+ '/products/:slug': {
101
+ en: '/products/:slug',
102
+ cs: '/produkty/:slug',
103
+ },
104
+ });
105
+
106
+ expect(localisedRoutes.map(route => route.path)).toEqual([
107
+ ':lang/about',
108
+ ':lang/o-nas',
109
+ ':lang/products/:slug',
110
+ ':lang/produkty/:slug',
111
+ ]);
112
+ });
113
+
114
+ test('resolves current localized path to the target language', () => {
115
+ const localisedUrls = {
116
+ '/terms-of-service': {
117
+ en: '/terms-of-service',
118
+ cs: '/podminky-pouzivani',
119
+ },
120
+ '/products/:slug': {
121
+ en: '/products/:slug',
122
+ cs: '/produkty/:slug',
123
+ },
124
+ };
125
+
126
+ expect(
127
+ resolveLocalisedPath(
128
+ '/terms-of-service',
129
+ 'cs',
130
+ ['en', 'cs'],
131
+ localisedUrls,
132
+ ),
133
+ ).toBe('/podminky-pouzivani');
134
+ expect(
135
+ resolveLocalisedPath('/produkty/cervena-bota', 'en', ['en', 'cs'], {
136
+ ...localisedUrls,
137
+ '/products/:slug': {
138
+ en: '/products/:slug',
139
+ cs: '/produkty/:slug',
140
+ },
141
+ }),
142
+ ).toBe('/products/cervena-bota');
143
+ });
144
+
145
+ test('resolves optional route params', () => {
146
+ const localisedUrls = {
147
+ '/products/:slug?': {
148
+ en: '/products/:slug?',
149
+ cs: '/produkty/:slug?',
150
+ },
151
+ };
152
+
153
+ expect(
154
+ resolveLocalisedPath('/products', 'cs', ['en', 'cs'], localisedUrls),
155
+ ).toBe('/produkty');
156
+ expect(
157
+ resolveLocalisedPath(
158
+ '/produkty/cervena-bota',
159
+ 'en',
160
+ ['en', 'cs'],
161
+ localisedUrls,
162
+ ),
163
+ ).toBe('/products/cervena-bota');
164
+ });
165
+ });
166
+
167
+ describe('i18n server API prefix skips', () => {
168
+ test('collects API route prefixes and normalized BFF config prefixes', () => {
169
+ expect(
170
+ collectApiPrefixes(
171
+ [
172
+ { entryName: 'main', isApi: false, urlPath: '/' },
173
+ { isApi: true, urlPath: '/bff-api' },
174
+ { isApi: true, urlPath: '/rpc/*' },
175
+ { isApi: true, urlPath: '/' },
176
+ { isApi: true },
177
+ ],
178
+ ['bff-api/', '/internal-api'],
179
+ ),
180
+ ).toEqual(['/bff-api', '/rpc', '/internal-api']);
181
+ });
182
+
183
+ test('matches API prefixes by exact path or slash-delimited segment', () => {
184
+ const prefixes = ['/bff-api'];
185
+
186
+ expect(matchesApiPrefix('/bff-api', prefixes)).toBe(true);
187
+ expect(matchesApiPrefix('/bff-api/ping', prefixes)).toBe(true);
188
+ expect(matchesApiPrefix('/bff-api-v2', prefixes)).toBe(false);
189
+ expect(matchesApiPrefix('/bff-api-v2/ping', prefixes)).toBe(false);
190
+ });
191
+
192
+ test('skips language detector and redirect middleware for API routes', async () => {
193
+ const middlewares: any[] = [];
194
+ const routes = [
195
+ { entryName: 'main', entryPath: '', urlPath: '/' },
196
+ { entryPath: '', isApi: true, urlPath: '/bff-api' },
197
+ ];
198
+ let prepare: (() => void) | undefined;
199
+
200
+ i18nServerPlugin({
201
+ localeDetection: {
202
+ fallbackLanguage: 'en',
203
+ languages: ['en', 'cs'],
204
+ localePathRedirect: true,
205
+ },
206
+ staticRoutePrefixes: [],
207
+ }).setup({
208
+ getServerConfig: () => ({}),
209
+ getServerContext: () => ({ middlewares, routes }),
210
+ onPrepare: fn => {
211
+ prepare = fn;
212
+ },
213
+ } as any);
214
+
215
+ prepare?.();
216
+
217
+ const detectorMiddleware = middlewares.find(
218
+ middleware => middleware.name === 'i18n-language-detector',
219
+ );
220
+ const redirectMiddleware = middlewares.find(
221
+ middleware => middleware.name === 'i18n-server-middleware',
222
+ );
223
+
224
+ expect(detectorMiddleware).toBeDefined();
225
+ expect(redirectMiddleware).toBeDefined();
226
+
227
+ for (const middleware of [detectorMiddleware, redirectMiddleware]) {
228
+ let nextCalls = 0;
229
+ const response = await middleware.handler(
230
+ createRequestContext('/bff-api/ping'),
231
+ async () => {
232
+ nextCalls++;
233
+ },
234
+ );
235
+
236
+ expect(response).toBeUndefined();
237
+ expect(nextCalls).toBe(1);
238
+ }
239
+ });
240
+
241
+ test('uses /api as the BFF prefix when BFF config is present without prefix', async () => {
242
+ const middlewares: any[] = [];
243
+ const routes = [{ entryName: 'main', entryPath: '', urlPath: '/' }];
244
+ let prepare: (() => void) | undefined;
245
+
246
+ i18nServerPlugin({
247
+ localeDetection: {
248
+ fallbackLanguage: 'en',
249
+ languages: ['en', 'cs'],
250
+ localePathRedirect: true,
251
+ },
252
+ staticRoutePrefixes: [],
253
+ }).setup({
254
+ getServerConfig: () => ({ bff: {} }),
255
+ getServerContext: () => ({ middlewares, routes }),
256
+ onPrepare: fn => {
257
+ prepare = fn;
258
+ },
259
+ } as any);
260
+
261
+ prepare?.();
262
+
263
+ const redirectMiddleware = middlewares.find(
264
+ middleware => middleware.name === 'i18n-server-middleware',
265
+ );
266
+
267
+ let nextCalls = 0;
268
+ const response = await redirectMiddleware.handler(
269
+ createRequestContext('/api/ping'),
270
+ async () => {
271
+ nextCalls++;
272
+ },
273
+ );
274
+
275
+ expect(response).toBeUndefined();
276
+ expect(nextCalls).toBe(1);
277
+ });
278
+ });