@bleedingdev/modern-js-i18n-runtime-extensions 3.9.0-ultramodern.11 → 3.9.0-ultramodern.13
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/index.js +10 -10
- package/dist/cjs/localeRedirectPolicy.js +6 -6
- package/dist/cjs/paths.js +3 -3
- package/dist/cjs/router-navigation/adapter.js +24 -166
- package/dist/esm/index.mjs +1 -1
- package/dist/esm/localeRedirectPolicy.mjs +1 -1
- package/dist/esm/paths.mjs +1 -1
- package/dist/esm/router-navigation/adapter.mjs +26 -168
- package/dist/esm-node/index.mjs +1 -1
- package/dist/esm-node/localeRedirectPolicy.mjs +1 -1
- package/dist/esm-node/paths.mjs +1 -1
- package/dist/esm-node/router-navigation/adapter.mjs +26 -168
- package/dist/types/index.d.ts +1 -1
- package/dist/types/localeRedirectPolicy.d.ts +1 -1
- package/dist/types/paths.d.ts +1 -1
- package/dist/types/router-navigation/adapter.d.ts +7 -55
- package/package.json +9 -9
- package/src/index.ts +1 -1
- package/src/localeRedirectPolicy.ts +2 -2
- package/src/paths.ts +2 -2
- package/src/router-navigation/adapter.tsx +62 -478
- package/tests/routerNavigation.test.tsx +80 -0
- package/dist/cjs/localisedUrls/config.js +0 -49
- package/dist/cjs/localisedUrls/index.js +0 -98
- package/dist/cjs/localisedUrls/normalise.js +0 -87
- package/dist/cjs/localisedUrls/pathname.js +0 -68
- package/dist/cjs/localisedUrls/patterns.js +0 -214
- package/dist/cjs/localisedUrls/redirect.js +0 -100
- package/dist/cjs/localisedUrls/resolve.js +0 -78
- package/dist/cjs/localisedUrls/routeIdentity.js +0 -118
- package/dist/cjs/localisedUrls/routes.js +0 -210
- package/dist/cjs/localisedUrls/types.js +0 -18
- package/dist/esm/localisedUrls/config.mjs +0 -11
- package/dist/esm/localisedUrls/index.mjs +0 -8
- package/dist/esm/localisedUrls/normalise.mjs +0 -37
- package/dist/esm/localisedUrls/pathname.mjs +0 -24
- package/dist/esm/localisedUrls/patterns.mjs +0 -152
- package/dist/esm/localisedUrls/redirect.mjs +0 -53
- package/dist/esm/localisedUrls/resolve.mjs +0 -37
- package/dist/esm/localisedUrls/routeIdentity.mjs +0 -77
- package/dist/esm/localisedUrls/routes.mjs +0 -169
- package/dist/esm/localisedUrls/types.mjs +0 -0
- package/dist/esm-node/localisedUrls/config.mjs +0 -12
- package/dist/esm-node/localisedUrls/index.mjs +0 -9
- package/dist/esm-node/localisedUrls/normalise.mjs +0 -38
- package/dist/esm-node/localisedUrls/pathname.mjs +0 -25
- package/dist/esm-node/localisedUrls/patterns.mjs +0 -153
- package/dist/esm-node/localisedUrls/redirect.mjs +0 -54
- package/dist/esm-node/localisedUrls/resolve.mjs +0 -38
- package/dist/esm-node/localisedUrls/routeIdentity.mjs +0 -78
- package/dist/esm-node/localisedUrls/routes.mjs +0 -170
- package/dist/esm-node/localisedUrls/types.mjs +0 -1
- package/dist/types/localisedUrls/config.d.ts +0 -9
- package/dist/types/localisedUrls/index.d.ts +0 -10
- package/dist/types/localisedUrls/normalise.d.ts +0 -10
- package/dist/types/localisedUrls/pathname.d.ts +0 -4
- package/dist/types/localisedUrls/patterns.d.ts +0 -11
- package/dist/types/localisedUrls/redirect.d.ts +0 -19
- package/dist/types/localisedUrls/resolve.d.ts +0 -7
- package/dist/types/localisedUrls/routeIdentity.d.ts +0 -24
- package/dist/types/localisedUrls/routes.d.ts +0 -3
- package/dist/types/localisedUrls/types.d.ts +0 -14
- package/src/localisedUrls/config.ts +0 -18
- package/src/localisedUrls/index.ts +0 -28
- package/src/localisedUrls/normalise.ts +0 -62
- package/src/localisedUrls/pathname.ts +0 -60
- package/src/localisedUrls/patterns.ts +0 -283
- package/src/localisedUrls/redirect.ts +0 -107
- package/src/localisedUrls/resolve.ts +0 -106
- package/src/localisedUrls/routeIdentity.ts +0 -107
- package/src/localisedUrls/routes.ts +0 -432
- package/src/localisedUrls/types.ts +0 -18
- package/tests/localisedUrls.test.ts +0 -532
|
@@ -1,432 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
getLeadingLocaleParam,
|
|
3
|
-
normalisePathPattern,
|
|
4
|
-
normaliseRoutePath,
|
|
5
|
-
stripLeadingLocaleParam,
|
|
6
|
-
} from './normalise';
|
|
7
|
-
import {
|
|
8
|
-
doPathPatternsOverlap,
|
|
9
|
-
getOrderedPathParamSignature,
|
|
10
|
-
getPathParamSignature,
|
|
11
|
-
getPathPatternValidationIssue,
|
|
12
|
-
getPhysicalPathPattern,
|
|
13
|
-
haveEqualPatternSpecificity,
|
|
14
|
-
} from './patterns';
|
|
15
|
-
import type {
|
|
16
|
-
LocalisedRoute,
|
|
17
|
-
LocalisedUrlPathMap,
|
|
18
|
-
LocalisedUrlsMap,
|
|
19
|
-
} from './types';
|
|
20
|
-
|
|
21
|
-
const isLocalisableRoutePath = (path?: string): path is string => {
|
|
22
|
-
const pathWithoutLocale = stripLeadingLocaleParam(path);
|
|
23
|
-
|
|
24
|
-
if (
|
|
25
|
-
!pathWithoutLocale ||
|
|
26
|
-
pathWithoutLocale === '/' ||
|
|
27
|
-
pathWithoutLocale === '*'
|
|
28
|
-
) {
|
|
29
|
-
return false;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
return true;
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
const joinPath = (parentPath: string, routePath?: string): string => {
|
|
36
|
-
if (!isLocalisableRoutePath(routePath)) {
|
|
37
|
-
return parentPath;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
const segment = normaliseRoutePath(stripLeadingLocaleParam(routePath) || '');
|
|
41
|
-
return normalisePathPattern(`${parentPath}/${segment}`);
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
const isFrameworkInternalRoutePath = (canonicalPath: string) =>
|
|
45
|
-
canonicalPath === '/_mf' || canonicalPath.startsWith('/_mf/');
|
|
46
|
-
|
|
47
|
-
const legalRouteIdPart = (value: string): string =>
|
|
48
|
-
value.replace(/[^a-zA-Z0-9_$-]+/g, '_').replace(/^_+|_+$/g, '') || 'index';
|
|
49
|
-
|
|
50
|
-
const formatParamSignature = (signature: string[]): string =>
|
|
51
|
-
signature.length > 0 ? signature.join(', ') : '(none)';
|
|
52
|
-
|
|
53
|
-
const validateLocalisedUrlPatterns = (
|
|
54
|
-
languages: string[],
|
|
55
|
-
localisedUrls: LocalisedUrlsMap,
|
|
56
|
-
) => {
|
|
57
|
-
const physicalPatterns = new Map<
|
|
58
|
-
string,
|
|
59
|
-
{ canonicalPath: string; source: string }
|
|
60
|
-
>();
|
|
61
|
-
const claimedPatterns: Array<{
|
|
62
|
-
canonicalPath: string;
|
|
63
|
-
pattern: string;
|
|
64
|
-
source: string;
|
|
65
|
-
}> = [];
|
|
66
|
-
|
|
67
|
-
const claimPhysicalPattern = (
|
|
68
|
-
sourcePattern: string,
|
|
69
|
-
canonicalPath: string,
|
|
70
|
-
source: string,
|
|
71
|
-
) => {
|
|
72
|
-
const physicalPattern = getPhysicalPathPattern(sourcePattern);
|
|
73
|
-
const existing = physicalPatterns.get(physicalPattern);
|
|
74
|
-
if (existing && existing.canonicalPath !== canonicalPath) {
|
|
75
|
-
throw new Error(
|
|
76
|
-
`localisedUrls["${existing.canonicalPath}"] ${existing.source} and localisedUrls["${canonicalPath}"] ${source} generate the same physical route pattern "${physicalPattern}". Localised route patterns must identify exactly one canonical route.`,
|
|
77
|
-
);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
const overlapping = claimedPatterns.find(
|
|
81
|
-
claimed =>
|
|
82
|
-
claimed.canonicalPath !== canonicalPath &&
|
|
83
|
-
haveEqualPatternSpecificity(claimed.pattern, sourcePattern) &&
|
|
84
|
-
doPathPatternsOverlap(claimed.pattern, sourcePattern),
|
|
85
|
-
);
|
|
86
|
-
if (overlapping) {
|
|
87
|
-
throw new Error(
|
|
88
|
-
`localisedUrls["${overlapping.canonicalPath}"] ${overlapping.source} pattern "${overlapping.pattern}" and localisedUrls["${canonicalPath}"] ${source} pattern "${sourcePattern}" are equal-specificity overlapping route patterns. Localised route patterns must identify exactly one canonical route.`,
|
|
89
|
-
);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
physicalPatterns.set(physicalPattern, { canonicalPath, source });
|
|
93
|
-
claimedPatterns.push({ canonicalPath, pattern: sourcePattern, source });
|
|
94
|
-
};
|
|
95
|
-
|
|
96
|
-
for (const [canonicalPath, entry] of Object.entries(localisedUrls).sort(
|
|
97
|
-
([left], [right]) => (left < right ? -1 : left > right ? 1 : 0),
|
|
98
|
-
)) {
|
|
99
|
-
const canonicalIssue = getPathPatternValidationIssue(canonicalPath);
|
|
100
|
-
if (canonicalIssue) {
|
|
101
|
-
throw new Error(
|
|
102
|
-
`localisedUrls["${canonicalPath}"] canonical pattern has ${canonicalIssue}.`,
|
|
103
|
-
);
|
|
104
|
-
}
|
|
105
|
-
const expectedSignature = getPathParamSignature(canonicalPath);
|
|
106
|
-
const expectedOrderedSignature =
|
|
107
|
-
getOrderedPathParamSignature(canonicalPath);
|
|
108
|
-
claimPhysicalPattern(canonicalPath, canonicalPath, 'canonical pattern');
|
|
109
|
-
|
|
110
|
-
for (const language of languages) {
|
|
111
|
-
const localisedPattern = entry[language];
|
|
112
|
-
if (!localisedPattern) {
|
|
113
|
-
continue;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
const localisedIssue = getPathPatternValidationIssue(localisedPattern);
|
|
117
|
-
if (localisedIssue) {
|
|
118
|
-
throw new Error(
|
|
119
|
-
`localisedUrls["${canonicalPath}"].${language} has ${localisedIssue}.`,
|
|
120
|
-
);
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
const actualSignature = getPathParamSignature(localisedPattern);
|
|
124
|
-
if (
|
|
125
|
-
actualSignature.length !== expectedSignature.length ||
|
|
126
|
-
actualSignature.some(
|
|
127
|
-
(parameter, index) => parameter !== expectedSignature[index],
|
|
128
|
-
)
|
|
129
|
-
) {
|
|
130
|
-
throw new Error(
|
|
131
|
-
`localisedUrls["${canonicalPath}"].${language} must use the same parameter signature as "${canonicalPath}": expected ${formatParamSignature(
|
|
132
|
-
expectedSignature,
|
|
133
|
-
)}; received ${formatParamSignature(actualSignature)}.`,
|
|
134
|
-
);
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
const actualOrderedSignature =
|
|
138
|
-
getOrderedPathParamSignature(localisedPattern);
|
|
139
|
-
if (
|
|
140
|
-
expectedOrderedSignature.some(
|
|
141
|
-
parameter => parameter === '*' || parameter.endsWith('?'),
|
|
142
|
-
) &&
|
|
143
|
-
actualOrderedSignature.some(
|
|
144
|
-
(parameter, index) => parameter !== expectedOrderedSignature[index],
|
|
145
|
-
)
|
|
146
|
-
) {
|
|
147
|
-
throw new Error(
|
|
148
|
-
`localisedUrls["${canonicalPath}"].${language} must keep optional and splat parameters in canonical order: expected ${formatParamSignature(
|
|
149
|
-
expectedOrderedSignature,
|
|
150
|
-
)}; received ${formatParamSignature(actualOrderedSignature)}.`,
|
|
151
|
-
);
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
claimPhysicalPattern(localisedPattern, canonicalPath, `.${language}`);
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
};
|
|
158
|
-
|
|
159
|
-
const ensureLocalisedUrlsForPath = (
|
|
160
|
-
canonicalPath: string,
|
|
161
|
-
languages: string[],
|
|
162
|
-
localisedUrls: LocalisedUrlsMap,
|
|
163
|
-
): LocalisedUrlPathMap => {
|
|
164
|
-
const entry = localisedUrls[canonicalPath];
|
|
165
|
-
if (!entry) {
|
|
166
|
-
throw new Error(
|
|
167
|
-
`localisedUrls is enabled, but route "${canonicalPath}" does not define localised URLs for languages: ${languages.join(
|
|
168
|
-
', ',
|
|
169
|
-
)}. Add localisedUrls["${canonicalPath}"] or set localeDetection.localisedUrls to false.`,
|
|
170
|
-
);
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
const missingLanguages = languages.filter(language => !entry[language]);
|
|
174
|
-
if (missingLanguages.length > 0) {
|
|
175
|
-
throw new Error(
|
|
176
|
-
`localisedUrls["${canonicalPath}"] is missing languages: ${missingLanguages.join(
|
|
177
|
-
', ',
|
|
178
|
-
)}. Every configured language must have a localised URL.`,
|
|
179
|
-
);
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
return entry;
|
|
183
|
-
};
|
|
184
|
-
|
|
185
|
-
export const validateLocalisedUrls = (
|
|
186
|
-
routes: LocalisedRoute[],
|
|
187
|
-
languages: string[],
|
|
188
|
-
localisedUrls: LocalisedUrlsMap,
|
|
189
|
-
) => {
|
|
190
|
-
validateLocalisedUrlPatterns(languages, localisedUrls);
|
|
191
|
-
|
|
192
|
-
const visit = (route: LocalisedRoute, parentPath: string) => {
|
|
193
|
-
const canonicalPath = joinPath(parentPath, route.path);
|
|
194
|
-
if (
|
|
195
|
-
isLocalisableRoutePath(route.path) &&
|
|
196
|
-
!isFrameworkInternalRoutePath(canonicalPath)
|
|
197
|
-
) {
|
|
198
|
-
ensureLocalisedUrlsForPath(canonicalPath, languages, localisedUrls);
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
if ('children' in route && route.children) {
|
|
202
|
-
route.children.forEach(child => visit(child, canonicalPath));
|
|
203
|
-
}
|
|
204
|
-
};
|
|
205
|
-
|
|
206
|
-
routes.forEach(route => visit(route, ''));
|
|
207
|
-
};
|
|
208
|
-
|
|
209
|
-
const getLocalisedRouteIdSuffixes = (
|
|
210
|
-
paths: string[],
|
|
211
|
-
): Array<string | undefined> => {
|
|
212
|
-
const usedSuffixes = new Set<string>();
|
|
213
|
-
|
|
214
|
-
return paths.map((path, index) => {
|
|
215
|
-
if (index === 0) {
|
|
216
|
-
return undefined;
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
const baseSuffix = legalRouteIdPart(path);
|
|
220
|
-
let suffix = baseSuffix;
|
|
221
|
-
let disambiguator = 2;
|
|
222
|
-
while (usedSuffixes.has(suffix)) {
|
|
223
|
-
suffix = `${baseSuffix}_${disambiguator++}`;
|
|
224
|
-
}
|
|
225
|
-
usedSuffixes.add(suffix);
|
|
226
|
-
return suffix;
|
|
227
|
-
});
|
|
228
|
-
};
|
|
229
|
-
|
|
230
|
-
const getLocalisedRoutePaths = (
|
|
231
|
-
canonicalPath: string,
|
|
232
|
-
parentLocalisedPaths: Record<string, string>,
|
|
233
|
-
languages: string[],
|
|
234
|
-
entry: LocalisedUrlPathMap,
|
|
235
|
-
): string[] => {
|
|
236
|
-
const paths = languages.map(language => {
|
|
237
|
-
const fullPath = normalisePathPattern(entry[language]);
|
|
238
|
-
const parentPath = normalisePathPattern(
|
|
239
|
-
parentLocalisedPaths[language] || '/',
|
|
240
|
-
);
|
|
241
|
-
if (parentPath === '/') {
|
|
242
|
-
return normaliseRoutePath(fullPath) || undefined;
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
const parentPrefix = `${parentPath}/`;
|
|
246
|
-
if (!fullPath.startsWith(parentPrefix)) {
|
|
247
|
-
throw new Error(
|
|
248
|
-
`localisedUrls["${canonicalPath}"].${language} must be nested under "${parentPath}" because its parent route is localised there.`,
|
|
249
|
-
);
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
return normaliseRoutePath(fullPath.slice(parentPath.length));
|
|
253
|
-
});
|
|
254
|
-
|
|
255
|
-
const uniquePaths = Array.from(new Set(paths.filter(Boolean) as string[]));
|
|
256
|
-
return uniquePaths;
|
|
257
|
-
};
|
|
258
|
-
|
|
259
|
-
const transformLocalisedRoute = (
|
|
260
|
-
route: LocalisedRoute,
|
|
261
|
-
parentCanonicalPath: string,
|
|
262
|
-
parentLocalisedPaths: Record<string, string>,
|
|
263
|
-
languages: string[],
|
|
264
|
-
localisedUrls: LocalisedUrlsMap,
|
|
265
|
-
): LocalisedRoute[] => {
|
|
266
|
-
const canonicalPath = joinPath(parentCanonicalPath, route.path);
|
|
267
|
-
const localisedUrlEntry =
|
|
268
|
-
isLocalisableRoutePath(route.path) &&
|
|
269
|
-
!isFrameworkInternalRoutePath(canonicalPath)
|
|
270
|
-
? ensureLocalisedUrlsForPath(canonicalPath, languages, localisedUrls)
|
|
271
|
-
: undefined;
|
|
272
|
-
const routeLocalisedPaths = localisedUrlEntry
|
|
273
|
-
? languages.reduce<Record<string, string>>((acc, language) => {
|
|
274
|
-
acc[language] = normalisePathPattern(localisedUrlEntry[language]);
|
|
275
|
-
return acc;
|
|
276
|
-
}, {})
|
|
277
|
-
: parentLocalisedPaths;
|
|
278
|
-
|
|
279
|
-
const children =
|
|
280
|
-
'children' in route && route.children
|
|
281
|
-
? route.children.flatMap(child =>
|
|
282
|
-
transformLocalisedRoute(
|
|
283
|
-
child,
|
|
284
|
-
canonicalPath,
|
|
285
|
-
routeLocalisedPaths,
|
|
286
|
-
languages,
|
|
287
|
-
localisedUrls,
|
|
288
|
-
),
|
|
289
|
-
)
|
|
290
|
-
: undefined;
|
|
291
|
-
|
|
292
|
-
const baseRoute = {
|
|
293
|
-
...route,
|
|
294
|
-
...(!isFrameworkInternalRoutePath(canonicalPath)
|
|
295
|
-
? {
|
|
296
|
-
modernLocalisedRoute: {
|
|
297
|
-
id: route.id,
|
|
298
|
-
path: route.path,
|
|
299
|
-
canonicalPath,
|
|
300
|
-
paths: localisedUrlEntry,
|
|
301
|
-
},
|
|
302
|
-
}
|
|
303
|
-
: {}),
|
|
304
|
-
...(children ? { children } : {}),
|
|
305
|
-
} as LocalisedRoute;
|
|
306
|
-
|
|
307
|
-
if (!localisedUrlEntry) {
|
|
308
|
-
return [baseRoute];
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
const localisedPaths = getLocalisedRoutePaths(
|
|
312
|
-
canonicalPath,
|
|
313
|
-
parentLocalisedPaths,
|
|
314
|
-
languages,
|
|
315
|
-
localisedUrlEntry,
|
|
316
|
-
);
|
|
317
|
-
const leadingLocaleParam = getLeadingLocaleParam(route.path);
|
|
318
|
-
const routeIdPaths = leadingLocaleParam
|
|
319
|
-
? localisedPaths.map(path =>
|
|
320
|
-
normaliseRoutePath(`${leadingLocaleParam}/${path}`),
|
|
321
|
-
)
|
|
322
|
-
: localisedPaths;
|
|
323
|
-
const routeIdSuffixes = getLocalisedRouteIdSuffixes(routeIdPaths);
|
|
324
|
-
|
|
325
|
-
return localisedPaths.map((localisedPath, index) =>
|
|
326
|
-
cloneRouteWithLocalisedPath(
|
|
327
|
-
baseRoute,
|
|
328
|
-
localisedPath,
|
|
329
|
-
routeIdSuffixes[index],
|
|
330
|
-
canonicalPath,
|
|
331
|
-
),
|
|
332
|
-
);
|
|
333
|
-
};
|
|
334
|
-
|
|
335
|
-
const suffixRouteIds = <T extends LocalisedRoute>(
|
|
336
|
-
route: T,
|
|
337
|
-
suffix: string,
|
|
338
|
-
): T => {
|
|
339
|
-
const children =
|
|
340
|
-
'children' in route && route.children
|
|
341
|
-
? route.children.map(child => suffixRouteIds(child, suffix))
|
|
342
|
-
: undefined;
|
|
343
|
-
|
|
344
|
-
return {
|
|
345
|
-
...route,
|
|
346
|
-
...(route.id ? { id: `${route.id}__localised_${suffix}` } : {}),
|
|
347
|
-
...(children ? { children } : {}),
|
|
348
|
-
};
|
|
349
|
-
};
|
|
350
|
-
|
|
351
|
-
const collectRouteIdCounts = (
|
|
352
|
-
routes: LocalisedRoute[],
|
|
353
|
-
counts = new Map<string, number>(),
|
|
354
|
-
): Map<string, number> => {
|
|
355
|
-
for (const route of routes) {
|
|
356
|
-
if (route.id) {
|
|
357
|
-
counts.set(route.id, (counts.get(route.id) ?? 0) + 1);
|
|
358
|
-
}
|
|
359
|
-
if ('children' in route && route.children) {
|
|
360
|
-
collectRouteIdCounts(route.children, counts);
|
|
361
|
-
}
|
|
362
|
-
}
|
|
363
|
-
return counts;
|
|
364
|
-
};
|
|
365
|
-
|
|
366
|
-
const validateNoNewRouteIdCollisions = (
|
|
367
|
-
sourceRoutes: LocalisedRoute[],
|
|
368
|
-
localisedRoutes: LocalisedRoute[],
|
|
369
|
-
) => {
|
|
370
|
-
const sourceCounts = collectRouteIdCounts(sourceRoutes);
|
|
371
|
-
const localisedCounts = collectRouteIdCounts(localisedRoutes);
|
|
372
|
-
|
|
373
|
-
for (const [routeId, count] of localisedCounts) {
|
|
374
|
-
if (count > 1 && count > (sourceCounts.get(routeId) ?? 0)) {
|
|
375
|
-
throw new Error(
|
|
376
|
-
`Localised route expansion generated duplicate route ID "${routeId}". Change the source route ID or localised path so every generated route remains identifiable.`,
|
|
377
|
-
);
|
|
378
|
-
}
|
|
379
|
-
}
|
|
380
|
-
};
|
|
381
|
-
|
|
382
|
-
const cloneRouteWithLocalisedPath = (
|
|
383
|
-
route: LocalisedRoute,
|
|
384
|
-
path: string,
|
|
385
|
-
routeIdSuffix: string | undefined,
|
|
386
|
-
canonicalPath: string,
|
|
387
|
-
): LocalisedRoute => {
|
|
388
|
-
const leadingLocaleParam = getLeadingLocaleParam(route.path);
|
|
389
|
-
const localisedPath = leadingLocaleParam
|
|
390
|
-
? normaliseRoutePath(`${leadingLocaleParam}/${path}`)
|
|
391
|
-
: path;
|
|
392
|
-
const routeWithPath = {
|
|
393
|
-
...route,
|
|
394
|
-
path: localisedPath,
|
|
395
|
-
} as LocalisedRoute;
|
|
396
|
-
// Language-agnostic source pattern; lets downstream codegen collapse the
|
|
397
|
-
// localized physical variants back to one canonical route.
|
|
398
|
-
(routeWithPath as { modernCanonicalPath?: string }).modernCanonicalPath =
|
|
399
|
-
canonicalPath;
|
|
400
|
-
|
|
401
|
-
return routeIdSuffix
|
|
402
|
-
? suffixRouteIds(routeWithPath, routeIdSuffix)
|
|
403
|
-
: routeWithPath;
|
|
404
|
-
};
|
|
405
|
-
|
|
406
|
-
export const applyLocalisedUrlsToRoutes = (
|
|
407
|
-
routes: LocalisedRoute[],
|
|
408
|
-
languages: string[],
|
|
409
|
-
localisedUrls: LocalisedUrlsMap,
|
|
410
|
-
): LocalisedRoute[] => {
|
|
411
|
-
const rootLocalisedPaths = languages.reduce<Record<string, string>>(
|
|
412
|
-
(acc, language) => {
|
|
413
|
-
acc[language] = '/';
|
|
414
|
-
return acc;
|
|
415
|
-
},
|
|
416
|
-
{},
|
|
417
|
-
);
|
|
418
|
-
|
|
419
|
-
validateLocalisedUrls(routes, languages, localisedUrls);
|
|
420
|
-
|
|
421
|
-
const localisedRoutes = routes.flatMap(route =>
|
|
422
|
-
transformLocalisedRoute(
|
|
423
|
-
route,
|
|
424
|
-
'',
|
|
425
|
-
rootLocalisedPaths,
|
|
426
|
-
languages,
|
|
427
|
-
localisedUrls,
|
|
428
|
-
),
|
|
429
|
-
);
|
|
430
|
-
validateNoNewRouteIdCollisions(routes, localisedRoutes);
|
|
431
|
-
return localisedRoutes;
|
|
432
|
-
};
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
export type LocalisedRoute = {
|
|
2
|
-
type: 'nested' | 'page';
|
|
3
|
-
path?: string;
|
|
4
|
-
id?: string;
|
|
5
|
-
children?: LocalisedRoute[];
|
|
6
|
-
[key: string]: any;
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
export type LocalisedUrlPathMap = Record<string, string>;
|
|
10
|
-
|
|
11
|
-
export type LocalisedUrlsMap = Record<string, LocalisedUrlPathMap>;
|
|
12
|
-
|
|
13
|
-
export type LocalisedUrlsOption = boolean | LocalisedUrlsMap;
|
|
14
|
-
|
|
15
|
-
export interface ResolvedLocalisedUrlsConfig {
|
|
16
|
-
enabled: boolean;
|
|
17
|
-
map: LocalisedUrlsMap;
|
|
18
|
-
}
|