@bleedingdev/modern-js-plugin-i18n 3.2.0-ultramodern.8 → 3.2.0-ultramodern.80
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/cjs/cli/index.js +22 -0
- package/dist/cjs/runtime/I18nLink.js +4 -12
- package/dist/cjs/runtime/context.js +32 -5
- package/dist/cjs/runtime/hooks.js +8 -5
- package/dist/cjs/runtime/i18n/backend/defaults.js +1 -1
- package/dist/cjs/runtime/i18n/backend/middleware.node.js +4 -4
- package/dist/cjs/runtime/i18n/instance.js +0 -24
- package/dist/cjs/runtime/i18n/react-i18next.js +52 -0
- package/dist/cjs/runtime/i18n/utils.js +0 -12
- package/dist/cjs/runtime/index.js +13 -7
- package/dist/cjs/runtime/routerAdapter.js +163 -0
- package/dist/cjs/runtime/utils.js +63 -94
- package/dist/cjs/server/index.js +60 -8
- package/dist/cjs/shared/localisedUrls.js +237 -0
- package/dist/esm/cli/index.mjs +22 -0
- package/dist/esm/runtime/I18nLink.mjs +4 -12
- package/dist/esm/runtime/context.mjs +34 -7
- package/dist/esm/runtime/hooks.mjs +9 -6
- package/dist/esm/runtime/i18n/backend/defaults.mjs +1 -1
- package/dist/esm/runtime/i18n/backend/middleware.node.mjs +3 -3
- package/dist/esm/runtime/i18n/instance.mjs +1 -19
- package/dist/esm/runtime/i18n/react-i18next.mjs +18 -0
- package/dist/esm/runtime/i18n/utils.mjs +0 -12
- package/dist/esm/runtime/index.mjs +14 -8
- package/dist/esm/runtime/routerAdapter.mjs +129 -0
- package/dist/esm/runtime/utils.mjs +11 -30
- package/dist/esm/server/index.mjs +53 -7
- package/dist/esm/shared/localisedUrls.mjs +191 -0
- package/dist/esm-node/cli/index.mjs +22 -0
- package/dist/esm-node/runtime/I18nLink.mjs +4 -12
- package/dist/esm-node/runtime/context.mjs +34 -7
- package/dist/esm-node/runtime/hooks.mjs +9 -6
- package/dist/esm-node/runtime/i18n/backend/defaults.mjs +1 -1
- package/dist/esm-node/runtime/i18n/backend/middleware.node.mjs +3 -3
- package/dist/esm-node/runtime/i18n/instance.mjs +1 -19
- package/dist/esm-node/runtime/i18n/react-i18next.mjs +19 -0
- package/dist/esm-node/runtime/i18n/utils.mjs +0 -12
- package/dist/esm-node/runtime/index.mjs +14 -8
- package/dist/esm-node/runtime/routerAdapter.mjs +130 -0
- package/dist/esm-node/runtime/utils.mjs +11 -30
- package/dist/esm-node/server/index.mjs +53 -7
- package/dist/esm-node/shared/localisedUrls.mjs +192 -0
- package/dist/types/runtime/I18nLink.d.ts +15 -0
- package/dist/types/runtime/context.d.ts +3 -0
- package/dist/types/runtime/hooks.d.ts +4 -2
- package/dist/types/runtime/i18n/backend/middleware.node.d.ts +1 -1
- package/dist/types/runtime/i18n/instance.d.ts +0 -5
- package/dist/types/runtime/i18n/react-i18next.d.ts +7 -0
- package/dist/types/runtime/index.d.ts +1 -0
- package/dist/types/runtime/routerAdapter.d.ts +26 -0
- package/dist/types/runtime/utils.d.ts +2 -7
- package/dist/types/server/index.d.ts +6 -0
- package/dist/types/shared/localisedUrls.d.ts +13 -0
- package/dist/types/shared/type.d.ts +12 -0
- package/package.json +15 -15
- package/rstest.config.mts +39 -0
- package/src/cli/index.ts +43 -1
- package/src/runtime/I18nLink.tsx +10 -16
- package/src/runtime/context.tsx +45 -7
- package/src/runtime/hooks.ts +13 -4
- package/src/runtime/i18n/backend/defaults.ts +3 -1
- package/src/runtime/i18n/backend/middleware.node.ts +1 -1
- package/src/runtime/i18n/instance.ts +0 -29
- package/src/runtime/i18n/react-i18next.ts +31 -0
- package/src/runtime/i18n/utils.ts +4 -26
- package/src/runtime/index.tsx +21 -9
- package/src/runtime/routerAdapter.tsx +333 -0
- package/src/runtime/utils.ts +22 -34
- package/src/server/index.ts +117 -10
- package/src/shared/localisedUrls.ts +393 -0
- package/src/shared/type.ts +12 -0
- package/tests/i18nUtils.test.ts +45 -0
- package/tests/localisedUrls.test.ts +312 -0
- package/tests/routerAdapter.test.tsx +278 -0
- package/dist/esm/rslib-runtime.mjs +0 -18
- 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
|
+
};
|
package/src/shared/type.ts
CHANGED
|
@@ -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,45 @@
|
|
|
1
|
+
import { describe, expect, test } from '@rstest/core';
|
|
2
|
+
import type { I18nInstance } from '../src/runtime/i18n';
|
|
3
|
+
import { initializeI18nInstance } from '../src/runtime/i18n/utils';
|
|
4
|
+
|
|
5
|
+
function createBackendI18nInstance(): I18nInstance {
|
|
6
|
+
return {
|
|
7
|
+
language: 'en',
|
|
8
|
+
isInitialized: false,
|
|
9
|
+
init: async () => undefined,
|
|
10
|
+
use: () => {},
|
|
11
|
+
options: {},
|
|
12
|
+
store: {
|
|
13
|
+
data: {},
|
|
14
|
+
},
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
describe('i18n runtime utils', () => {
|
|
19
|
+
test('does not poll for backend resources after init', async () => {
|
|
20
|
+
const i18nInstance = createBackendI18nInstance();
|
|
21
|
+
const init = rstest.fn(async () => {
|
|
22
|
+
i18nInstance.isInitialized = true;
|
|
23
|
+
});
|
|
24
|
+
i18nInstance.init = init as I18nInstance['init'];
|
|
25
|
+
const setTimeoutSpy = rstest.spyOn(globalThis, 'setTimeout');
|
|
26
|
+
|
|
27
|
+
await initializeI18nInstance(
|
|
28
|
+
i18nInstance,
|
|
29
|
+
'cs',
|
|
30
|
+
'en',
|
|
31
|
+
['en', 'cs'],
|
|
32
|
+
{},
|
|
33
|
+
{
|
|
34
|
+
enabled: true,
|
|
35
|
+
loadPath: '/locales/{{lng}}/{{ns}}.json',
|
|
36
|
+
},
|
|
37
|
+
{},
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
expect(init).toHaveBeenCalledTimes(1);
|
|
41
|
+
expect(setTimeoutSpy).not.toHaveBeenCalled();
|
|
42
|
+
|
|
43
|
+
setTimeoutSpy.mockRestore();
|
|
44
|
+
});
|
|
45
|
+
});
|