@bleedingdev/modern-js-plugin-i18n 3.5.0-ultramodern.4 → 3.5.0-ultramodern.41
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 +2 -1
- package/dist/cjs/cli/locales.js +15 -15
- package/dist/cjs/runtime/Link.js +8 -89
- package/dist/cjs/runtime/context.js +18 -82
- package/dist/cjs/runtime/contextHelpers.js +137 -0
- package/dist/cjs/runtime/core.js +29 -132
- package/dist/cjs/runtime/i18n/backend/defaults.node.js +7 -7
- package/dist/cjs/runtime/i18n/detection/cache.js +150 -0
- package/dist/cjs/runtime/i18n/detection/detector.js +100 -0
- package/dist/cjs/runtime/i18n/detection/index.js +8 -263
- package/dist/cjs/runtime/i18n/detection/initOptions.js +69 -0
- package/dist/cjs/runtime/i18n/detection/language.js +62 -0
- package/dist/cjs/runtime/i18n/detection/path.js +49 -0
- package/dist/cjs/runtime/i18n/detection/priority.js +66 -0
- package/dist/cjs/runtime/i18n/detection/ssr.js +70 -0
- package/dist/cjs/runtime/i18n/detection/types.js +18 -0
- package/dist/cjs/runtime/i18n/instance.js +7 -7
- package/dist/cjs/runtime/linkHelpers.js +148 -0
- package/dist/cjs/runtime/pluginSetup.js +102 -0
- package/dist/cjs/runtime/providerComposition.js +114 -0
- package/dist/cjs/runtime/reactI18next.js +43 -0
- package/dist/cjs/runtime/utils.js +1 -9
- package/dist/cjs/server/apiPrefix.js +72 -0
- package/dist/cjs/server/detectorOptions.js +97 -0
- package/dist/cjs/server/index.js +17 -144
- package/dist/cjs/server/redirectPolicy.js +96 -0
- package/dist/cjs/shared/detection.js +2 -104
- package/dist/cjs/shared/localisedUrls/config.js +49 -0
- package/dist/cjs/shared/localisedUrls/index.js +91 -0
- package/dist/cjs/shared/localisedUrls/normalise.js +87 -0
- package/dist/cjs/shared/localisedUrls/pathname.js +68 -0
- package/dist/cjs/shared/localisedUrls/patterns.js +130 -0
- package/dist/cjs/shared/localisedUrls/redirect.js +95 -0
- package/dist/cjs/shared/localisedUrls/resolve.js +88 -0
- package/dist/cjs/shared/localisedUrls/routes.js +129 -0
- package/dist/cjs/shared/localisedUrls/types.js +18 -0
- package/dist/cjs/shared/localisedUrls.js +28 -294
- package/dist/cjs/shared/utils.js +2 -8
- package/dist/esm/cli/index.mjs +2 -1
- package/dist/esm/cli/locales.mjs +13 -13
- package/dist/esm/runtime/Link.mjs +1 -82
- package/dist/esm/runtime/context.mjs +18 -82
- package/dist/esm/runtime/contextHelpers.mjs +84 -0
- package/dist/esm/runtime/core.mjs +30 -133
- package/dist/esm/runtime/i18n/backend/defaults.node.mjs +5 -5
- package/dist/esm/runtime/i18n/detection/cache.mjs +103 -0
- package/dist/esm/runtime/i18n/detection/detector.mjs +62 -0
- package/dist/esm/runtime/i18n/detection/index.mjs +4 -260
- package/dist/esm/runtime/i18n/detection/initOptions.mjs +28 -0
- package/dist/esm/runtime/i18n/detection/language.mjs +21 -0
- package/dist/esm/runtime/i18n/detection/path.mjs +11 -0
- package/dist/esm/runtime/i18n/detection/priority.mjs +28 -0
- package/dist/esm/runtime/i18n/detection/ssr.mjs +26 -0
- package/dist/esm/runtime/i18n/detection/types.mjs +0 -0
- package/dist/esm/runtime/i18n/instance.mjs +8 -5
- package/dist/esm/runtime/linkHelpers.mjs +98 -0
- package/dist/esm/runtime/pluginSetup.mjs +64 -0
- package/dist/esm/runtime/providerComposition.mjs +76 -0
- package/dist/esm/runtime/reactI18next.mjs +5 -0
- package/dist/esm/runtime/utils.mjs +2 -10
- package/dist/esm/server/apiPrefix.mjs +31 -0
- package/dist/esm/server/detectorOptions.mjs +59 -0
- package/dist/esm/server/index.mjs +8 -135
- package/dist/esm/server/redirectPolicy.mjs +46 -0
- package/dist/esm/shared/detection.mjs +1 -71
- package/dist/esm/shared/localisedUrls/config.mjs +11 -0
- package/dist/esm/shared/localisedUrls/index.mjs +7 -0
- package/dist/esm/shared/localisedUrls/normalise.mjs +37 -0
- package/dist/esm/shared/localisedUrls/pathname.mjs +24 -0
- package/dist/esm/shared/localisedUrls/patterns.mjs +86 -0
- package/dist/esm/shared/localisedUrls/redirect.mjs +48 -0
- package/dist/esm/shared/localisedUrls/resolve.mjs +47 -0
- package/dist/esm/shared/localisedUrls/routes.mjs +88 -0
- package/dist/esm/shared/localisedUrls/types.mjs +0 -0
- package/dist/esm/shared/localisedUrls.mjs +1 -283
- package/dist/esm/shared/utils.mjs +1 -1
- package/dist/esm-node/cli/index.mjs +2 -1
- package/dist/esm-node/cli/locales.mjs +13 -13
- package/dist/esm-node/runtime/Link.mjs +1 -82
- package/dist/esm-node/runtime/context.mjs +18 -82
- package/dist/esm-node/runtime/contextHelpers.mjs +85 -0
- package/dist/esm-node/runtime/core.mjs +30 -133
- package/dist/esm-node/runtime/i18n/backend/defaults.node.mjs +5 -5
- package/dist/esm-node/runtime/i18n/detection/cache.mjs +104 -0
- package/dist/esm-node/runtime/i18n/detection/detector.mjs +63 -0
- package/dist/esm-node/runtime/i18n/detection/index.mjs +4 -260
- package/dist/esm-node/runtime/i18n/detection/initOptions.mjs +29 -0
- package/dist/esm-node/runtime/i18n/detection/language.mjs +22 -0
- package/dist/esm-node/runtime/i18n/detection/path.mjs +12 -0
- package/dist/esm-node/runtime/i18n/detection/priority.mjs +29 -0
- package/dist/esm-node/runtime/i18n/detection/ssr.mjs +27 -0
- package/dist/esm-node/runtime/i18n/detection/types.mjs +1 -0
- package/dist/esm-node/runtime/i18n/instance.mjs +8 -5
- package/dist/esm-node/runtime/linkHelpers.mjs +99 -0
- package/dist/esm-node/runtime/pluginSetup.mjs +65 -0
- package/dist/esm-node/runtime/providerComposition.mjs +77 -0
- package/dist/esm-node/runtime/reactI18next.mjs +6 -0
- package/dist/esm-node/runtime/utils.mjs +2 -10
- package/dist/esm-node/server/apiPrefix.mjs +32 -0
- package/dist/esm-node/server/detectorOptions.mjs +60 -0
- package/dist/esm-node/server/index.mjs +8 -135
- package/dist/esm-node/server/redirectPolicy.mjs +47 -0
- package/dist/esm-node/shared/detection.mjs +1 -71
- package/dist/esm-node/shared/localisedUrls/config.mjs +12 -0
- package/dist/esm-node/shared/localisedUrls/index.mjs +8 -0
- package/dist/esm-node/shared/localisedUrls/normalise.mjs +38 -0
- package/dist/esm-node/shared/localisedUrls/pathname.mjs +25 -0
- package/dist/esm-node/shared/localisedUrls/patterns.mjs +87 -0
- package/dist/esm-node/shared/localisedUrls/redirect.mjs +49 -0
- package/dist/esm-node/shared/localisedUrls/resolve.mjs +48 -0
- package/dist/esm-node/shared/localisedUrls/routes.mjs +89 -0
- package/dist/esm-node/shared/localisedUrls/types.mjs +1 -0
- package/dist/esm-node/shared/localisedUrls.mjs +1 -283
- package/dist/esm-node/shared/utils.mjs +1 -1
- package/dist/types/cli/locales.d.ts +1 -1
- package/dist/types/runtime/Link.d.ts +3 -7
- package/dist/types/runtime/context.d.ts +6 -5
- package/dist/types/runtime/contextHelpers.d.ts +28 -0
- package/dist/types/runtime/core.d.ts +3 -7
- package/dist/types/runtime/hooks.d.ts +5 -0
- package/dist/types/runtime/i18n/backend/middleware.common.d.ts +2 -1
- package/dist/types/runtime/i18n/detection/cache.d.ts +14 -0
- package/dist/types/runtime/i18n/detection/detector.d.ts +8 -0
- package/dist/types/runtime/i18n/detection/index.d.ts +5 -50
- package/dist/types/runtime/i18n/detection/initOptions.d.ts +10 -0
- package/dist/types/runtime/i18n/detection/language.d.ts +11 -0
- package/dist/types/runtime/i18n/detection/middleware.d.ts +4 -4
- package/dist/types/runtime/i18n/detection/middleware.node.d.ts +4 -4
- package/dist/types/runtime/i18n/detection/path.d.ts +5 -0
- package/dist/types/runtime/i18n/detection/priority.d.ts +10 -0
- package/dist/types/runtime/i18n/detection/ssr.d.ts +9 -0
- package/dist/types/runtime/i18n/detection/types.d.ts +32 -0
- package/dist/types/runtime/i18n/instance.d.ts +28 -26
- package/dist/types/runtime/i18n/react-i18next.d.ts +1 -6
- package/dist/types/runtime/i18n/utils.d.ts +14 -5
- package/dist/types/runtime/linkHelpers.d.ts +17 -0
- package/dist/types/runtime/pluginSetup.d.ts +32 -0
- package/dist/types/runtime/providerComposition.d.ts +16 -0
- package/dist/types/runtime/reactI18next.d.ts +7 -0
- package/dist/types/runtime/routerAdapter.d.ts +7 -6
- package/dist/types/runtime/utils.d.ts +7 -2
- package/dist/types/server/apiPrefix.d.ts +7 -0
- package/dist/types/server/detectorOptions.d.ts +21 -0
- package/dist/types/server/index.d.ts +1 -6
- package/dist/types/server/redirectPolicy.d.ts +22 -0
- package/dist/types/shared/detection.d.ts +1 -11
- package/dist/types/shared/localisedUrls/config.d.ts +9 -0
- package/dist/types/shared/localisedUrls/index.d.ts +9 -0
- package/dist/types/shared/localisedUrls/normalise.d.ts +10 -0
- package/dist/types/shared/localisedUrls/pathname.d.ts +4 -0
- package/dist/types/shared/localisedUrls/patterns.d.ts +5 -0
- package/dist/types/shared/localisedUrls/redirect.d.ts +11 -0
- package/dist/types/shared/localisedUrls/resolve.d.ts +7 -0
- package/dist/types/shared/localisedUrls/routes.d.ts +3 -0
- package/dist/types/shared/localisedUrls/types.d.ts +14 -0
- package/dist/types/shared/localisedUrls.d.ts +2 -36
- package/dist/types/shared/utils.d.ts +0 -2
- package/package.json +16 -18
- package/rstest.config.mts +1 -0
- package/src/cli/index.ts +5 -4
- package/src/cli/locales.ts +3 -3
- package/src/runtime/Link.tsx +11 -138
- package/src/runtime/context.tsx +45 -199
- package/src/runtime/contextHelpers.ts +232 -0
- package/src/runtime/core.tsx +43 -274
- package/src/runtime/hooks.ts +13 -2
- package/src/runtime/i18n/backend/defaults.node.ts +2 -2
- package/src/runtime/i18n/backend/middleware.common.ts +5 -4
- package/src/runtime/i18n/detection/cache.ts +189 -0
- package/src/runtime/i18n/detection/detector.ts +166 -0
- package/src/runtime/i18n/detection/index.ts +10 -641
- package/src/runtime/i18n/detection/initOptions.ts +74 -0
- package/src/runtime/i18n/detection/language.ts +64 -0
- package/src/runtime/i18n/detection/middleware.node.ts +26 -7
- package/src/runtime/i18n/detection/middleware.ts +15 -7
- package/src/runtime/i18n/detection/path.ts +41 -0
- package/src/runtime/i18n/detection/priority.ts +82 -0
- package/src/runtime/i18n/detection/ssr.ts +47 -0
- package/src/runtime/i18n/detection/types.ts +41 -0
- package/src/runtime/i18n/instance.ts +56 -31
- package/src/runtime/i18n/react-i18next.ts +1 -6
- package/src/runtime/i18n/utils.ts +18 -9
- package/src/runtime/linkHelpers.ts +174 -0
- package/src/runtime/pluginSetup.ts +189 -0
- package/src/runtime/providerComposition.tsx +144 -0
- package/src/runtime/reactI18next.ts +20 -0
- package/src/runtime/routerAdapter.tsx +6 -6
- package/src/runtime/utils.ts +13 -35
- package/src/server/apiPrefix.ts +67 -0
- package/src/server/detectorOptions.ts +105 -0
- package/src/server/index.ts +14 -319
- package/src/server/redirectPolicy.ts +127 -0
- package/src/shared/detection.ts +1 -1
- package/src/shared/localisedUrls/config.ts +18 -0
- package/src/shared/localisedUrls/index.ts +24 -0
- package/src/shared/localisedUrls/normalise.ts +67 -0
- package/src/shared/localisedUrls/pathname.ts +60 -0
- package/src/shared/localisedUrls/patterns.ts +160 -0
- package/src/shared/localisedUrls/redirect.ts +91 -0
- package/src/shared/localisedUrls/resolve.ts +135 -0
- package/src/shared/localisedUrls/routes.ts +226 -0
- package/src/shared/localisedUrls/types.ts +18 -0
- package/src/shared/localisedUrls.ts +26 -623
- package/src/shared/utils.ts +2 -2
- package/tests/fixtures/localised-routes.golden.json +156 -0
- package/tests/link.test.tsx +111 -0
- package/tests/linkTypes.test.ts +15 -4
- package/tests/localisedUrlRewriteMatrix.fork.test.ts +147 -0
- package/tests/localisedUrls.test.ts +129 -2
- package/tests/reactI18nextRuntimeBoundary.test.ts +6 -3
- package/tests/redirectPolicy.test.ts +43 -0
- package/tests/routerAdapter.test.tsx +85 -0
- package/tests/type-fixture/fixture-globals.d.ts +11 -0
- package/tests/type-fixture/tsconfig.json +1 -0
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
import { normalisePathname } from "./normalise.mjs";
|
|
3
|
+
import { buildPathFromPattern, matchPathPattern, sortPatternsBySpecificity } from "./patterns.mjs";
|
|
4
|
+
const getCanonicalCandidates = (localisedUrls)=>sortPatternsBySpecificity(Object.entries(localisedUrls).map(([canonicalPattern, localisedUrlEntry])=>({
|
|
5
|
+
pattern: canonicalPattern,
|
|
6
|
+
canonicalPattern,
|
|
7
|
+
localisedUrlEntry
|
|
8
|
+
})));
|
|
9
|
+
const resolveLocalisedUrlPath = (pathname, languages, localisedUrls, options)=>{
|
|
10
|
+
const normalizedPathname = normalisePathname(pathname);
|
|
11
|
+
const canonicalCandidates = getCanonicalCandidates(localisedUrls);
|
|
12
|
+
for (const { canonicalPattern, localisedUrlEntry } of canonicalCandidates){
|
|
13
|
+
const targetPattern = 'localised' === options.mode ? localisedUrlEntry[options.targetLanguage] : canonicalPattern;
|
|
14
|
+
if (!targetPattern) continue;
|
|
15
|
+
const canonicalParams = matchPathPattern(normalizedPathname, canonicalPattern);
|
|
16
|
+
if (canonicalParams) return buildPathFromPattern(targetPattern, canonicalParams);
|
|
17
|
+
if ('canonical' === options.mode) for (const language of languages){
|
|
18
|
+
const sourcePattern = localisedUrlEntry[language];
|
|
19
|
+
if (!sourcePattern) continue;
|
|
20
|
+
const params = matchPathPattern(normalizedPathname, sourcePattern);
|
|
21
|
+
if (params) return buildPathFromPattern(canonicalPattern, params);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
if ('localised' === options.mode) {
|
|
25
|
+
const localisedCandidates = sortPatternsBySpecificity(Object.values(localisedUrls).flatMap((localisedUrlEntry)=>{
|
|
26
|
+
const targetPattern = localisedUrlEntry[options.targetLanguage];
|
|
27
|
+
if (!targetPattern) return [];
|
|
28
|
+
return languages.map((language)=>localisedUrlEntry[language]).filter((sourcePattern)=>Boolean(sourcePattern)).map((sourcePattern)=>({
|
|
29
|
+
pattern: sourcePattern,
|
|
30
|
+
sourcePattern,
|
|
31
|
+
targetPattern
|
|
32
|
+
}));
|
|
33
|
+
}));
|
|
34
|
+
for (const { sourcePattern, targetPattern } of localisedCandidates){
|
|
35
|
+
const params = matchPathPattern(normalizedPathname, sourcePattern);
|
|
36
|
+
if (params) return buildPathFromPattern(targetPattern, params);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return normalizedPathname;
|
|
40
|
+
};
|
|
41
|
+
const resolveLocalisedPath = (pathname, targetLanguage, languages, localisedUrls)=>resolveLocalisedUrlPath(pathname, languages, localisedUrls, {
|
|
42
|
+
mode: 'localised',
|
|
43
|
+
targetLanguage
|
|
44
|
+
});
|
|
45
|
+
const resolveCanonicalLocalisedPath = (pathname, languages, localisedUrls)=>resolveLocalisedUrlPath(pathname, languages, localisedUrls, {
|
|
46
|
+
mode: 'canonical'
|
|
47
|
+
});
|
|
48
|
+
export { resolveCanonicalLocalisedPath, resolveLocalisedPath };
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
import { getLeadingLocaleParam, normalisePathPattern, normaliseRoutePath, stripLeadingLocaleParam } from "./normalise.mjs";
|
|
3
|
+
const isLocalisableRoutePath = (path)=>{
|
|
4
|
+
const pathWithoutLocale = stripLeadingLocaleParam(path);
|
|
5
|
+
if (!pathWithoutLocale || '/' === pathWithoutLocale || '*' === pathWithoutLocale) return false;
|
|
6
|
+
return true;
|
|
7
|
+
};
|
|
8
|
+
const joinPath = (parentPath, routePath)=>{
|
|
9
|
+
if (!isLocalisableRoutePath(routePath)) return parentPath;
|
|
10
|
+
const segment = normaliseRoutePath(stripLeadingLocaleParam(routePath) || '');
|
|
11
|
+
return normalisePathPattern(`${parentPath}/${segment}`);
|
|
12
|
+
};
|
|
13
|
+
const ensureLocalisedUrlsForPath = (canonicalPath, languages, localisedUrls)=>{
|
|
14
|
+
const entry = localisedUrls[canonicalPath];
|
|
15
|
+
if (!entry) throw new Error(`localisedUrls is enabled, but route "${canonicalPath}" does not define localised URLs for languages: ${languages.join(', ')}. Add localisedUrls["${canonicalPath}"] or set localeDetection.localisedUrls to false.`);
|
|
16
|
+
const missingLanguages = languages.filter((language)=>!entry[language]);
|
|
17
|
+
if (missingLanguages.length > 0) throw new Error(`localisedUrls["${canonicalPath}"] is missing languages: ${missingLanguages.join(', ')}. Every configured language must have a localised URL.`);
|
|
18
|
+
return entry;
|
|
19
|
+
};
|
|
20
|
+
const validateLocalisedUrls = (routes, languages, localisedUrls)=>{
|
|
21
|
+
const visit = (route, parentPath)=>{
|
|
22
|
+
const canonicalPath = joinPath(parentPath, route.path);
|
|
23
|
+
if (isLocalisableRoutePath(route.path)) ensureLocalisedUrlsForPath(canonicalPath, languages, localisedUrls);
|
|
24
|
+
if ('children' in route && route.children) route.children.forEach((child)=>visit(child, canonicalPath));
|
|
25
|
+
};
|
|
26
|
+
routes.forEach((route)=>visit(route, ''));
|
|
27
|
+
};
|
|
28
|
+
const getLocalisedRoutePaths = (canonicalPath, parentLocalisedPaths, languages, entry)=>{
|
|
29
|
+
const paths = languages.map((language)=>{
|
|
30
|
+
const fullPath = normalisePathPattern(entry[language]);
|
|
31
|
+
const parentPath = normalisePathPattern(parentLocalisedPaths[language] || '/');
|
|
32
|
+
if ('/' === parentPath) return normaliseRoutePath(fullPath) || void 0;
|
|
33
|
+
const parentPrefix = `${parentPath}/`;
|
|
34
|
+
if (!fullPath.startsWith(parentPrefix)) throw new Error(`localisedUrls["${canonicalPath}"].${language} must be nested under "${parentPath}" because its parent route is localised there.`);
|
|
35
|
+
return normaliseRoutePath(fullPath.slice(parentPath.length));
|
|
36
|
+
});
|
|
37
|
+
return Array.from(new Set(paths.filter(Boolean)));
|
|
38
|
+
};
|
|
39
|
+
const transformLocalisedRoute = (route, parentCanonicalPath, parentLocalisedPaths, languages, localisedUrls)=>{
|
|
40
|
+
const canonicalPath = joinPath(parentCanonicalPath, route.path);
|
|
41
|
+
const localisedUrlEntry = isLocalisableRoutePath(route.path) ? ensureLocalisedUrlsForPath(canonicalPath, languages, localisedUrls) : void 0;
|
|
42
|
+
const routeLocalisedPaths = localisedUrlEntry ? languages.reduce((acc, language)=>{
|
|
43
|
+
acc[language] = normalisePathPattern(localisedUrlEntry[language]);
|
|
44
|
+
return acc;
|
|
45
|
+
}, {}) : parentLocalisedPaths;
|
|
46
|
+
const children = 'children' in route && route.children ? route.children.flatMap((child)=>transformLocalisedRoute(child, canonicalPath, routeLocalisedPaths, languages, localisedUrls)) : void 0;
|
|
47
|
+
const baseRoute = {
|
|
48
|
+
...route,
|
|
49
|
+
...children ? {
|
|
50
|
+
children
|
|
51
|
+
} : {}
|
|
52
|
+
};
|
|
53
|
+
if (!localisedUrlEntry) return [
|
|
54
|
+
baseRoute
|
|
55
|
+
];
|
|
56
|
+
return getLocalisedRoutePaths(canonicalPath, parentLocalisedPaths, languages, localisedUrlEntry).map((localisedPath, index)=>cloneRouteWithLocalisedPath(baseRoute, localisedPath, index, canonicalPath));
|
|
57
|
+
};
|
|
58
|
+
const legalRouteIdPart = (value)=>value.replace(/[^a-zA-Z0-9_$-]+/g, '_').replace(/^_+|_+$/g, '') || 'index';
|
|
59
|
+
const suffixRouteIds = (route, suffix)=>{
|
|
60
|
+
const children = 'children' in route && route.children ? route.children.map((child)=>suffixRouteIds(child, suffix)) : void 0;
|
|
61
|
+
return {
|
|
62
|
+
...route,
|
|
63
|
+
...route.id ? {
|
|
64
|
+
id: `${route.id}__localised_${suffix}`
|
|
65
|
+
} : {},
|
|
66
|
+
...children ? {
|
|
67
|
+
children
|
|
68
|
+
} : {}
|
|
69
|
+
};
|
|
70
|
+
};
|
|
71
|
+
const cloneRouteWithLocalisedPath = (route, path, index, canonicalPath)=>{
|
|
72
|
+
const leadingLocaleParam = getLeadingLocaleParam(route.path);
|
|
73
|
+
const localisedPath = leadingLocaleParam ? normaliseRoutePath(`${leadingLocaleParam}/${path}`) : path;
|
|
74
|
+
const routeWithPath = {
|
|
75
|
+
...route,
|
|
76
|
+
path: localisedPath
|
|
77
|
+
};
|
|
78
|
+
routeWithPath.modernCanonicalPath = canonicalPath;
|
|
79
|
+
return 0 === index ? routeWithPath : suffixRouteIds(routeWithPath, legalRouteIdPart(localisedPath));
|
|
80
|
+
};
|
|
81
|
+
const applyLocalisedUrlsToRoutes = (routes, languages, localisedUrls)=>{
|
|
82
|
+
const rootLocalisedPaths = languages.reduce((acc, language)=>{
|
|
83
|
+
acc[language] = '/';
|
|
84
|
+
return acc;
|
|
85
|
+
}, {});
|
|
86
|
+
validateLocalisedUrls(routes, languages, localisedUrls);
|
|
87
|
+
return routes.flatMap((route)=>transformLocalisedRoute(route, '', rootLocalisedPaths, languages, localisedUrls));
|
|
88
|
+
};
|
|
89
|
+
export { applyLocalisedUrlsToRoutes, validateLocalisedUrls };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import "node:module";
|
|
@@ -1,284 +1,2 @@
|
|
|
1
1
|
import "node:module";
|
|
2
|
-
|
|
3
|
-
'lang',
|
|
4
|
-
'locale',
|
|
5
|
-
'language'
|
|
6
|
-
]);
|
|
7
|
-
const normaliseSlashes = (path)=>{
|
|
8
|
-
const withoutDuplicateSlashes = path.replace(/\/+/g, '/');
|
|
9
|
-
const withLeadingSlash = withoutDuplicateSlashes.startsWith('/') ? withoutDuplicateSlashes : `/${withoutDuplicateSlashes}`;
|
|
10
|
-
return withLeadingSlash.length > 1 ? withLeadingSlash.replace(/\/+$/, '') : withLeadingSlash;
|
|
11
|
-
};
|
|
12
|
-
const normalisePathPattern = (path)=>normaliseSlashes(path).replace(/\[(.+?)\]/g, ':$1');
|
|
13
|
-
const normalisePathname = (pathname)=>normaliseSlashes(pathname);
|
|
14
|
-
const normaliseRoutePath = (path)=>{
|
|
15
|
-
const normalized = normalisePathPattern(path);
|
|
16
|
-
return '/' === normalized ? '' : normalized.slice(1);
|
|
17
|
-
};
|
|
18
|
-
const getLocaleParamSegment = (segment)=>{
|
|
19
|
-
if (!segment.startsWith(':')) return null;
|
|
20
|
-
const paramName = segment.slice(1).replace(/\?$/, '');
|
|
21
|
-
return LOCALE_PARAM_NAMES.has(paramName) ? segment : null;
|
|
22
|
-
};
|
|
23
|
-
const splitPathSegments = (path)=>{
|
|
24
|
-
if (!path) return [];
|
|
25
|
-
return normalisePathPattern(path).split('/').filter(Boolean);
|
|
26
|
-
};
|
|
27
|
-
const stripLeadingLocaleParam = (path)=>{
|
|
28
|
-
const segments = splitPathSegments(path);
|
|
29
|
-
const leadingLocaleParam = getLocaleParamSegment(segments[0] || '');
|
|
30
|
-
if (!leadingLocaleParam) return path;
|
|
31
|
-
const remainingPath = segments.slice(1).join('/');
|
|
32
|
-
return remainingPath ? `/${remainingPath}` : void 0;
|
|
33
|
-
};
|
|
34
|
-
const getLeadingLocaleParam = (path)=>{
|
|
35
|
-
const segments = splitPathSegments(path);
|
|
36
|
-
return getLocaleParamSegment(segments[0] || '');
|
|
37
|
-
};
|
|
38
|
-
const resolveLocalisedUrlsConfig = (option)=>{
|
|
39
|
-
if (option && 'object' == typeof option && Object.keys(option).length > 0) return {
|
|
40
|
-
enabled: true,
|
|
41
|
-
map: option
|
|
42
|
-
};
|
|
43
|
-
return {
|
|
44
|
-
enabled: false,
|
|
45
|
-
map: {}
|
|
46
|
-
};
|
|
47
|
-
};
|
|
48
|
-
const isLocalisableRoutePath = (path)=>{
|
|
49
|
-
const pathWithoutLocale = stripLeadingLocaleParam(path);
|
|
50
|
-
if (!pathWithoutLocale || '/' === pathWithoutLocale || '*' === pathWithoutLocale) return false;
|
|
51
|
-
return true;
|
|
52
|
-
};
|
|
53
|
-
const joinPath = (parentPath, routePath)=>{
|
|
54
|
-
if (!isLocalisableRoutePath(routePath)) return parentPath;
|
|
55
|
-
const segment = normaliseRoutePath(stripLeadingLocaleParam(routePath) || '');
|
|
56
|
-
return normalisePathPattern(`${parentPath}/${segment}`);
|
|
57
|
-
};
|
|
58
|
-
const ensureLocalisedUrlsForPath = (canonicalPath, languages, localisedUrls)=>{
|
|
59
|
-
const entry = localisedUrls[canonicalPath];
|
|
60
|
-
if (!entry) throw new Error(`localisedUrls is enabled, but route "${canonicalPath}" does not define localised URLs for languages: ${languages.join(', ')}. Add localisedUrls["${canonicalPath}"] or set localeDetection.localisedUrls to false.`);
|
|
61
|
-
const missingLanguages = languages.filter((language)=>!entry[language]);
|
|
62
|
-
if (missingLanguages.length > 0) throw new Error(`localisedUrls["${canonicalPath}"] is missing languages: ${missingLanguages.join(', ')}. Every configured language must have a localised URL.`);
|
|
63
|
-
return entry;
|
|
64
|
-
};
|
|
65
|
-
const validateLocalisedUrls = (routes, languages, localisedUrls)=>{
|
|
66
|
-
const visit = (route, parentPath)=>{
|
|
67
|
-
const canonicalPath = joinPath(parentPath, route.path);
|
|
68
|
-
if (isLocalisableRoutePath(route.path)) ensureLocalisedUrlsForPath(canonicalPath, languages, localisedUrls);
|
|
69
|
-
if ('children' in route && route.children) route.children.forEach((child)=>visit(child, canonicalPath));
|
|
70
|
-
};
|
|
71
|
-
routes.forEach((route)=>visit(route, ''));
|
|
72
|
-
};
|
|
73
|
-
const getLocalisedRoutePaths = (canonicalPath, parentLocalisedPaths, languages, entry)=>{
|
|
74
|
-
const paths = languages.map((language)=>{
|
|
75
|
-
const fullPath = normalisePathPattern(entry[language]);
|
|
76
|
-
const parentPath = normalisePathPattern(parentLocalisedPaths[language] || '/');
|
|
77
|
-
if ('/' === parentPath) return normaliseRoutePath(fullPath) || void 0;
|
|
78
|
-
const parentPrefix = `${parentPath}/`;
|
|
79
|
-
if (!fullPath.startsWith(parentPrefix)) throw new Error(`localisedUrls["${canonicalPath}"].${language} must be nested under "${parentPath}" because its parent route is localised there.`);
|
|
80
|
-
return normaliseRoutePath(fullPath.slice(parentPath.length));
|
|
81
|
-
});
|
|
82
|
-
return Array.from(new Set(paths.filter(Boolean)));
|
|
83
|
-
};
|
|
84
|
-
const transformLocalisedRoute = (route, parentCanonicalPath, parentLocalisedPaths, languages, localisedUrls)=>{
|
|
85
|
-
const canonicalPath = joinPath(parentCanonicalPath, route.path);
|
|
86
|
-
const localisedUrlEntry = isLocalisableRoutePath(route.path) ? ensureLocalisedUrlsForPath(canonicalPath, languages, localisedUrls) : void 0;
|
|
87
|
-
const routeLocalisedPaths = localisedUrlEntry ? languages.reduce((acc, language)=>{
|
|
88
|
-
acc[language] = normalisePathPattern(localisedUrlEntry[language]);
|
|
89
|
-
return acc;
|
|
90
|
-
}, {}) : parentLocalisedPaths;
|
|
91
|
-
const children = 'children' in route && route.children ? route.children.flatMap((child)=>transformLocalisedRoute(child, canonicalPath, routeLocalisedPaths, languages, localisedUrls)) : void 0;
|
|
92
|
-
const baseRoute = {
|
|
93
|
-
...route,
|
|
94
|
-
...children ? {
|
|
95
|
-
children
|
|
96
|
-
} : {}
|
|
97
|
-
};
|
|
98
|
-
if (!localisedUrlEntry) return [
|
|
99
|
-
baseRoute
|
|
100
|
-
];
|
|
101
|
-
return getLocalisedRoutePaths(canonicalPath, parentLocalisedPaths, languages, localisedUrlEntry).map((localisedPath, index)=>cloneRouteWithLocalisedPath(baseRoute, localisedPath, index, canonicalPath));
|
|
102
|
-
};
|
|
103
|
-
const legalRouteIdPart = (value)=>value.replace(/[^a-zA-Z0-9_$-]+/g, '_').replace(/^_+|_+$/g, '') || 'index';
|
|
104
|
-
const suffixRouteIds = (route, suffix)=>{
|
|
105
|
-
const children = 'children' in route && route.children ? route.children.map((child)=>suffixRouteIds(child, suffix)) : void 0;
|
|
106
|
-
return {
|
|
107
|
-
...route,
|
|
108
|
-
...route.id ? {
|
|
109
|
-
id: `${route.id}__localised_${suffix}`
|
|
110
|
-
} : {},
|
|
111
|
-
...children ? {
|
|
112
|
-
children
|
|
113
|
-
} : {}
|
|
114
|
-
};
|
|
115
|
-
};
|
|
116
|
-
const cloneRouteWithLocalisedPath = (route, path, index, canonicalPath)=>{
|
|
117
|
-
const leadingLocaleParam = getLeadingLocaleParam(route.path);
|
|
118
|
-
const localisedPath = leadingLocaleParam ? normaliseRoutePath(`${leadingLocaleParam}/${path}`) : path;
|
|
119
|
-
const routeWithPath = {
|
|
120
|
-
...route,
|
|
121
|
-
path: localisedPath
|
|
122
|
-
};
|
|
123
|
-
routeWithPath.modernCanonicalPath = canonicalPath;
|
|
124
|
-
return 0 === index ? routeWithPath : suffixRouteIds(routeWithPath, legalRouteIdPart(localisedPath));
|
|
125
|
-
};
|
|
126
|
-
const applyLocalisedUrlsToRoutes = (routes, languages, localisedUrls)=>{
|
|
127
|
-
const rootLocalisedPaths = languages.reduce((acc, language)=>{
|
|
128
|
-
acc[language] = '/';
|
|
129
|
-
return acc;
|
|
130
|
-
}, {});
|
|
131
|
-
validateLocalisedUrls(routes, languages, localisedUrls);
|
|
132
|
-
return routes.flatMap((route)=>transformLocalisedRoute(route, '', rootLocalisedPaths, languages, localisedUrls));
|
|
133
|
-
};
|
|
134
|
-
const escapeRegExp = (value)=>value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
135
|
-
const getParamName = (segment)=>segment.slice(1).replace(/\?$/, '');
|
|
136
|
-
const compiledPathPatternCache = new Map();
|
|
137
|
-
const compilePathPattern = (pattern)=>{
|
|
138
|
-
const normalizedPattern = normalisePathPattern(pattern);
|
|
139
|
-
const cached = compiledPathPatternCache.get(normalizedPattern);
|
|
140
|
-
if (cached) return cached;
|
|
141
|
-
const names = [];
|
|
142
|
-
const segments = normalizedPattern.split('/').filter(Boolean);
|
|
143
|
-
const source = segments.map((segment)=>{
|
|
144
|
-
if (segment.startsWith(':')) {
|
|
145
|
-
names.push(getParamName(segment));
|
|
146
|
-
const paramPattern = '([^/]+)';
|
|
147
|
-
return segment.endsWith('?') ? `(?:/${paramPattern})?` : `/${paramPattern}`;
|
|
148
|
-
}
|
|
149
|
-
if ('*' === segment) {
|
|
150
|
-
names.push('*');
|
|
151
|
-
return '/(.*)';
|
|
152
|
-
}
|
|
153
|
-
return `/${escapeRegExp(segment)}`;
|
|
154
|
-
}).join('');
|
|
155
|
-
const compiled = {
|
|
156
|
-
names,
|
|
157
|
-
regexp: new RegExp(`^${source || '/'}$`)
|
|
158
|
-
};
|
|
159
|
-
compiledPathPatternCache.set(normalizedPattern, compiled);
|
|
160
|
-
return compiled;
|
|
161
|
-
};
|
|
162
|
-
const getPatternSpecificity = (pattern)=>{
|
|
163
|
-
const segments = normalisePathPattern(pattern).split('/').filter(Boolean);
|
|
164
|
-
let staticSegments = 0;
|
|
165
|
-
let dynamicSegments = 0;
|
|
166
|
-
let splatSegments = 0;
|
|
167
|
-
for (const segment of segments)if ('*' === segment) splatSegments++;
|
|
168
|
-
else if (segment.startsWith(':')) dynamicSegments++;
|
|
169
|
-
else staticSegments++;
|
|
170
|
-
return {
|
|
171
|
-
staticSegments,
|
|
172
|
-
dynamicSegments,
|
|
173
|
-
splatSegments,
|
|
174
|
-
totalSegments: segments.length
|
|
175
|
-
};
|
|
176
|
-
};
|
|
177
|
-
const comparePatternSpecificity = (left, right)=>{
|
|
178
|
-
const a = getPatternSpecificity(left);
|
|
179
|
-
const b = getPatternSpecificity(right);
|
|
180
|
-
return b.staticSegments - a.staticSegments || b.totalSegments - a.totalSegments || a.splatSegments - b.splatSegments || a.dynamicSegments - b.dynamicSegments;
|
|
181
|
-
};
|
|
182
|
-
const sortPatternsBySpecificity = (patterns)=>patterns.map((pattern, index)=>({
|
|
183
|
-
pattern,
|
|
184
|
-
index
|
|
185
|
-
})).sort((left, right)=>comparePatternSpecificity(left.pattern.pattern, right.pattern.pattern) || left.index - right.index).map(({ pattern })=>pattern);
|
|
186
|
-
const decodePathParam = (value)=>{
|
|
187
|
-
try {
|
|
188
|
-
return decodeURIComponent(value);
|
|
189
|
-
} catch {
|
|
190
|
-
return null;
|
|
191
|
-
}
|
|
192
|
-
};
|
|
193
|
-
const matchPathPattern = (pathname, pattern)=>{
|
|
194
|
-
const { names, regexp } = compilePathPattern(pattern);
|
|
195
|
-
const match = regexp.exec(normalisePathname(pathname));
|
|
196
|
-
if (!match) return null;
|
|
197
|
-
const params = {};
|
|
198
|
-
for(let index = 0; index < names.length; index++){
|
|
199
|
-
const decoded = decodePathParam(match[index + 1] || '');
|
|
200
|
-
if (null === decoded) return null;
|
|
201
|
-
params[names[index]] = decoded;
|
|
202
|
-
}
|
|
203
|
-
return params;
|
|
204
|
-
};
|
|
205
|
-
const buildPathFromPattern = (pattern, params)=>{
|
|
206
|
-
const segments = normalisePathPattern(pattern).split('/').filter(Boolean);
|
|
207
|
-
const path = segments.map((segment)=>{
|
|
208
|
-
if (segment.startsWith(':')) {
|
|
209
|
-
const param = params[getParamName(segment)];
|
|
210
|
-
return param ? encodeURIComponent(param) : '';
|
|
211
|
-
}
|
|
212
|
-
if ('*' === segment) return params['*'] || '';
|
|
213
|
-
return segment;
|
|
214
|
-
}).filter(Boolean).join('/');
|
|
215
|
-
return `/${path}`;
|
|
216
|
-
};
|
|
217
|
-
const resolveLocalisedPath = (pathname, targetLanguage, languages, localisedUrls)=>{
|
|
218
|
-
const normalizedPathname = normalisePathname(pathname);
|
|
219
|
-
const canonicalCandidates = sortPatternsBySpecificity(Object.entries(localisedUrls).map(([canonicalPattern, localisedUrlEntry])=>({
|
|
220
|
-
pattern: canonicalPattern,
|
|
221
|
-
canonicalPattern,
|
|
222
|
-
localisedUrlEntry
|
|
223
|
-
})));
|
|
224
|
-
for (const { canonicalPattern, localisedUrlEntry } of canonicalCandidates){
|
|
225
|
-
const targetPattern = localisedUrlEntry[targetLanguage];
|
|
226
|
-
if (!targetPattern) continue;
|
|
227
|
-
const params = matchPathPattern(normalizedPathname, canonicalPattern);
|
|
228
|
-
if (params) return buildPathFromPattern(targetPattern, params);
|
|
229
|
-
}
|
|
230
|
-
const localisedCandidates = sortPatternsBySpecificity(Object.values(localisedUrls).flatMap((localisedUrlEntry)=>{
|
|
231
|
-
const targetPattern = localisedUrlEntry[targetLanguage];
|
|
232
|
-
if (!targetPattern) return [];
|
|
233
|
-
return languages.map((language)=>localisedUrlEntry[language]).filter((sourcePattern)=>Boolean(sourcePattern)).map((sourcePattern)=>({
|
|
234
|
-
pattern: sourcePattern,
|
|
235
|
-
sourcePattern,
|
|
236
|
-
targetPattern
|
|
237
|
-
}));
|
|
238
|
-
}));
|
|
239
|
-
for (const { sourcePattern, targetPattern } of localisedCandidates){
|
|
240
|
-
const params = matchPathPattern(normalizedPathname, sourcePattern);
|
|
241
|
-
if (params) return buildPathFromPattern(targetPattern, params);
|
|
242
|
-
}
|
|
243
|
-
return normalizedPathname;
|
|
244
|
-
};
|
|
245
|
-
const resolveCanonicalLocalisedPath = (pathname, languages, localisedUrls)=>{
|
|
246
|
-
const normalizedPathname = normalisePathname(pathname);
|
|
247
|
-
const canonicalCandidates = sortPatternsBySpecificity(Object.entries(localisedUrls).map(([canonicalPattern, localisedUrlEntry])=>({
|
|
248
|
-
pattern: canonicalPattern,
|
|
249
|
-
canonicalPattern,
|
|
250
|
-
localisedUrlEntry
|
|
251
|
-
})));
|
|
252
|
-
for (const { canonicalPattern, localisedUrlEntry } of canonicalCandidates){
|
|
253
|
-
const canonicalParams = matchPathPattern(normalizedPathname, canonicalPattern);
|
|
254
|
-
if (canonicalParams) return buildPathFromPattern(canonicalPattern, canonicalParams);
|
|
255
|
-
for (const language of languages){
|
|
256
|
-
const sourcePattern = localisedUrlEntry[language];
|
|
257
|
-
if (!sourcePattern) continue;
|
|
258
|
-
const params = matchPathPattern(normalizedPathname, sourcePattern);
|
|
259
|
-
if (params) return buildPathFromPattern(canonicalPattern, params);
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
return normalizedPathname;
|
|
263
|
-
};
|
|
264
|
-
const stripLanguagePrefix = (pathname, languages)=>{
|
|
265
|
-
const segments = pathname.split('/').filter(Boolean);
|
|
266
|
-
if (segments.length > 0 && languages.includes(segments[0])) return `/${segments.slice(1).join('/')}`;
|
|
267
|
-
return pathname || '/';
|
|
268
|
-
};
|
|
269
|
-
const localiseTargetPathname = (pathname, language, languages, localisedUrls)=>{
|
|
270
|
-
const pathWithoutLanguage = stripLanguagePrefix(pathname, languages);
|
|
271
|
-
const localisedUrlsConfig = resolveLocalisedUrlsConfig(localisedUrls);
|
|
272
|
-
const resolvedPath = localisedUrlsConfig.enabled ? resolveLocalisedPath(pathWithoutLanguage, language, languages, localisedUrlsConfig.map) : pathWithoutLanguage;
|
|
273
|
-
const resolvedSegments = resolvedPath.split('/').filter(Boolean);
|
|
274
|
-
return `/${[
|
|
275
|
-
language,
|
|
276
|
-
...resolvedSegments
|
|
277
|
-
].join('/')}`;
|
|
278
|
-
};
|
|
279
|
-
const canonicalTargetPathname = (pathname, languages, localisedUrls)=>{
|
|
280
|
-
const pathWithoutLanguage = stripLanguagePrefix(pathname, languages);
|
|
281
|
-
const localisedUrlsConfig = resolveLocalisedUrlsConfig(localisedUrls);
|
|
282
|
-
return localisedUrlsConfig.enabled ? resolveCanonicalLocalisedPath(pathWithoutLanguage, languages, localisedUrlsConfig.map) : pathWithoutLanguage;
|
|
283
|
-
};
|
|
284
|
-
export { applyLocalisedUrlsToRoutes, buildPathFromPattern, canonicalTargetPathname, localiseTargetPathname, matchPathPattern, normalisePathPattern, normalisePathname, resolveCanonicalLocalisedPath, resolveLocalisedPath, resolveLocalisedUrlsConfig, validateLocalisedUrls };
|
|
2
|
+
export { DEFAULT_LOCALE_REDIRECT_SKIP_RULES, applyLocalisedUrlsToRoutes, buildPathFromPattern, canonicalTargetPathname, isDefaultLocaleRedirectSkipPath, localiseTargetPathname, matchPathPattern, matchesPathPrefix, normalisePathPattern, normalisePathname, resolveCanonicalLocalisedPath, resolveLocalisedPath, resolveLocalisedUrlsConfig, shouldSkipLocaleRedirect, stripLanguagePrefix, validateLocalisedUrls } from "./localisedUrls/index.mjs";
|
|
@@ -33,4 +33,4 @@ function getBackendOptions(entryName, backend) {
|
|
|
33
33
|
if ('backendOptionsByEntry' in fullConfig) return removeEntryConfigKey(fullConfig, 'backendOptionsByEntry');
|
|
34
34
|
return backend;
|
|
35
35
|
}
|
|
36
|
-
export { getBackendOptions,
|
|
36
|
+
export { getBackendOptions, getLocaleDetectionOptions };
|
|
@@ -1,12 +1,8 @@
|
|
|
1
1
|
import type React from 'react';
|
|
2
2
|
import type { LinkParamsProp, LinkTargetPathname, ValidateLinkTo } from './canonicalRoutes';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
* (`$` / `*`) segments with concrete values before localization, so
|
|
7
|
-
* pattern-mapped slugs localize correctly.
|
|
8
|
-
*/
|
|
9
|
-
export declare const interpolateRouteParams: (pathname: string, params?: LinkParams) => string;
|
|
3
|
+
import { interpolateRouteParams, type LinkParams } from './linkHelpers';
|
|
4
|
+
export type { LinkParams };
|
|
5
|
+
export { interpolateRouteParams };
|
|
10
6
|
export interface LinkActiveOptions {
|
|
11
7
|
/**
|
|
12
8
|
* `true`: active only when the location matches the target exactly.
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { FC, ReactNode } from 'react';
|
|
2
2
|
import type { LocalisedUrlsOption } from '../shared/localisedUrls';
|
|
3
3
|
import type { I18nInstance } from './i18n';
|
|
4
|
-
|
|
4
|
+
interface ModernI18nContextValue {
|
|
5
5
|
language: string;
|
|
6
6
|
i18nInstance: I18nInstance;
|
|
7
7
|
entryName?: string;
|
|
@@ -11,12 +11,12 @@ export interface ModernI18nContextValue {
|
|
|
11
11
|
localisedUrls?: LocalisedUrlsOption;
|
|
12
12
|
updateLanguage?: (newLang: string) => void;
|
|
13
13
|
}
|
|
14
|
-
|
|
14
|
+
interface ModernI18nProviderProps {
|
|
15
15
|
children: ReactNode;
|
|
16
16
|
value: ModernI18nContextValue;
|
|
17
17
|
}
|
|
18
18
|
export declare const ModernI18nProvider: FC<ModernI18nProviderProps>;
|
|
19
|
-
|
|
19
|
+
interface UseModernI18nReturn {
|
|
20
20
|
language: string;
|
|
21
21
|
changeLanguage: (newLang: string) => Promise<void>;
|
|
22
22
|
t: (key: string | string[], ...args: any[]) => string;
|
|
@@ -32,11 +32,12 @@ export interface UseModernI18nReturn {
|
|
|
32
32
|
* This hook provides:
|
|
33
33
|
* - Current language from URL params or i18n context
|
|
34
34
|
* - changeLanguage function that updates both i18n instance and URL
|
|
35
|
-
* - Direct access to
|
|
35
|
+
* - Direct access to i18n instance
|
|
36
36
|
* - List of supported languages
|
|
37
|
-
* - Helper function to check if
|
|
37
|
+
* - Helper function to check if language is supported
|
|
38
38
|
*
|
|
39
39
|
* @param options - Optional configuration to override context settings
|
|
40
40
|
* @returns Object containing i18n functionality and utilities
|
|
41
41
|
*/
|
|
42
42
|
export declare const useModernI18n: () => UseModernI18nReturn;
|
|
43
|
+
export {};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { LocalisedUrlsOption } from '../shared/localisedUrls';
|
|
2
|
+
import type { I18nInstance } from './i18n';
|
|
3
|
+
type Navigate = (url: string, options?: {
|
|
4
|
+
replace?: boolean;
|
|
5
|
+
}) => Promise<void> | void;
|
|
6
|
+
type LocationLike = {
|
|
7
|
+
pathname: string;
|
|
8
|
+
search: string;
|
|
9
|
+
hash: string;
|
|
10
|
+
};
|
|
11
|
+
export declare function getPathLanguage(pathname: string | undefined, languages: string[] | undefined, localePathRedirect: boolean | undefined): string | undefined;
|
|
12
|
+
export declare function cacheI18nLanguage(i18nInstance: I18nInstance, language: string): void;
|
|
13
|
+
interface ChangeModernI18nLanguageOptions {
|
|
14
|
+
i18nInstance: I18nInstance;
|
|
15
|
+
updateLanguage?: (newLang: string) => void;
|
|
16
|
+
localePathRedirect?: boolean;
|
|
17
|
+
ignoreRedirectRoutes?: string[] | ((pathname: string) => boolean);
|
|
18
|
+
localisedUrls?: LocalisedUrlsOption;
|
|
19
|
+
languages?: string[];
|
|
20
|
+
hasRouter: boolean;
|
|
21
|
+
navigate?: Navigate | null;
|
|
22
|
+
location?: LocationLike | null;
|
|
23
|
+
}
|
|
24
|
+
export declare function changeModernI18nLanguage(newLang: string, options: ChangeModernI18nLanguageOptions): Promise<void>;
|
|
25
|
+
export declare function translateI18n(i18nInstance: I18nInstance, key: string | string[], ...args: unknown[]): string;
|
|
26
|
+
export declare function isI18nLanguageSupported(languages: string[] | undefined, lang: string): boolean;
|
|
27
|
+
export declare function isI18nResourcesReady(i18nInstance: I18nInstance, currentLanguage: string): boolean;
|
|
28
|
+
export {};
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import type React from 'react';
|
|
1
|
+
import type { RuntimePlugin } from '@modern-js/runtime';
|
|
3
2
|
import type { BaseBackendOptions, BaseLocaleDetectionOptions } from '../shared/type';
|
|
4
3
|
import type { I18nInitOptions, I18nInstance } from './i18n';
|
|
4
|
+
import { type LoadReactI18nextIntegration } from './reactI18next';
|
|
5
5
|
import './types';
|
|
6
6
|
export type { I18nSdkLoader, I18nSdkLoadOptions } from '../shared/type';
|
|
7
7
|
export type { Resources } from './i18n/instance';
|
|
8
|
+
export type { LoadReactI18nextIntegration, ReactI18nextIntegration, } from './reactI18next';
|
|
8
9
|
export interface I18nPluginOptions {
|
|
9
10
|
entryName?: string;
|
|
10
11
|
localeDetection?: BaseLocaleDetectionOptions;
|
|
@@ -16,11 +17,6 @@ export interface I18nPluginOptions {
|
|
|
16
17
|
reactI18next?: boolean;
|
|
17
18
|
[key: string]: any;
|
|
18
19
|
}
|
|
19
|
-
export interface ReactI18nextIntegration {
|
|
20
|
-
I18nextProvider: React.ComponentType<any> | null;
|
|
21
|
-
initReactI18next: any | null;
|
|
22
|
-
}
|
|
23
|
-
export type LoadReactI18nextIntegration = () => Promise<ReactI18nextIntegration | null>;
|
|
24
20
|
export declare const createI18nPlugin: (loadReactI18nextIntegration?: LoadReactI18nextIntegration) => ((options: I18nPluginOptions) => RuntimePlugin);
|
|
25
21
|
export type { AllowedLinkTarget, CanonicalRoutePath, UltramodernCanonicalRoutes, } from './canonicalRoutes';
|
|
26
22
|
export { useModernI18n } from './context';
|
|
@@ -4,6 +4,11 @@ import type { LocalisedUrlsOption } from '../shared/localisedUrls';
|
|
|
4
4
|
import type { I18nInstance } from './i18n';
|
|
5
5
|
interface RuntimeContextWithI18n extends TRuntimeContext {
|
|
6
6
|
i18nInstance?: I18nInstance;
|
|
7
|
+
ssrContext?: {
|
|
8
|
+
request?: {
|
|
9
|
+
pathname?: string;
|
|
10
|
+
};
|
|
11
|
+
};
|
|
7
12
|
}
|
|
8
13
|
export declare function createContextValue(lang: string, i18nInstance: I18nInstance | undefined, entryName: string | undefined, languages: string[], localePathRedirect: boolean, ignoreRedirectRoutes: string[] | ((pathname: string) => boolean) | undefined, localisedUrls: LocalisedUrlsOption | undefined, setLang: (lang: string) => void): {
|
|
9
14
|
language: string;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { BaseBackendOptions, ChainedBackendConfig } from '../../../shared/type';
|
|
2
2
|
import type { I18nInstance } from '../instance';
|
|
3
3
|
type BackendConfigWithChained = BaseBackendOptions & Partial<ChainedBackendConfig>;
|
|
4
|
+
type BackendConstructor = new (...args: never[]) => unknown;
|
|
4
5
|
/**
|
|
5
6
|
* Common logic for using i18next backend
|
|
6
7
|
* This function handles the backend selection and chained backend configuration
|
|
@@ -10,5 +11,5 @@ type BackendConfigWithChained = BaseBackendOptions & Partial<ChainedBackendConfi
|
|
|
10
11
|
* @param BackendBase - The base backend class (for non-chained use)
|
|
11
12
|
* @param backend - Optional backend configuration
|
|
12
13
|
*/
|
|
13
|
-
export declare function useI18nextBackendCommon(i18nInstance: I18nInstance, BackendWithSave:
|
|
14
|
+
export declare function useI18nextBackendCommon(i18nInstance: I18nInstance, BackendWithSave: BackendConstructor, BackendBase: BackendConstructor, backend?: BackendConfigWithChained): void;
|
|
14
15
|
export {};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { I18nInitOptions, I18nInstance, LanguageDetectorOptions } from '../instance';
|
|
2
|
+
interface DetectorCacheEntry {
|
|
3
|
+
instance: I18nInstance;
|
|
4
|
+
isTemporary: boolean;
|
|
5
|
+
configKey: string;
|
|
6
|
+
}
|
|
7
|
+
export declare const detectorInstanceCache: WeakMap<I18nInstance, DetectorCacheEntry>;
|
|
8
|
+
export declare const buildDetectorConfigKey: (languages: string[], fallbackLanguage: string, mergedDetection: LanguageDetectorOptions) => string;
|
|
9
|
+
export declare const pickSafeDetectionOptions: (userInitOptions?: I18nInitOptions) => Partial<I18nInitOptions> & Record<string, unknown>;
|
|
10
|
+
export declare const createDetectorInstance: (baseInstance: I18nInstance, configKey: string) => {
|
|
11
|
+
instance: I18nInstance;
|
|
12
|
+
isTemporary: boolean;
|
|
13
|
+
};
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { I18nInstance } from '../instance';
|
|
2
|
+
import type { BaseLanguageDetectionOptions, LanguageDetectionSsrContext } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* Priority 3: Detect language using i18next detector
|
|
5
|
+
*/
|
|
6
|
+
export declare const detectLanguageFromI18nextDetector: (i18nInstance: I18nInstance, options: BaseLanguageDetectionOptions & {
|
|
7
|
+
ssrContext?: LanguageDetectionSsrContext;
|
|
8
|
+
}) => Promise<string | undefined>;
|