@bleedingdev/modern-js-plugin-i18n 3.2.0-ultramodern.12 → 3.2.0-ultramodern.121
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/README.md +221 -11
- package/dist/cjs/cli/index.js +32 -5
- package/dist/cjs/runtime/I18nLink.js +17 -28
- package/dist/cjs/runtime/Link.js +264 -0
- package/dist/cjs/runtime/canonicalRoutes.js +18 -0
- package/dist/cjs/runtime/context.js +41 -10
- package/dist/cjs/runtime/hooks.js +17 -10
- package/dist/cjs/runtime/i18n/backend/config.js +9 -5
- package/dist/cjs/runtime/i18n/backend/defaults.js +15 -10
- package/dist/cjs/runtime/i18n/backend/defaults.node.js +47 -8
- package/dist/cjs/runtime/i18n/backend/index.js +9 -5
- package/dist/cjs/runtime/i18n/backend/middleware.common.js +9 -5
- package/dist/cjs/runtime/i18n/backend/middleware.js +9 -5
- package/dist/cjs/runtime/i18n/backend/middleware.node.js +13 -9
- package/dist/cjs/runtime/i18n/backend/sdk-backend.js +9 -5
- package/dist/cjs/runtime/i18n/backend/sdk-event.js +16 -11
- package/dist/cjs/runtime/i18n/detection/config.js +9 -5
- package/dist/cjs/runtime/i18n/detection/index.js +9 -5
- package/dist/cjs/runtime/i18n/detection/middleware.js +9 -5
- package/dist/cjs/runtime/i18n/detection/middleware.node.js +9 -5
- package/dist/cjs/runtime/i18n/index.js +9 -5
- package/dist/cjs/runtime/i18n/instance.js +17 -37
- package/dist/cjs/runtime/i18n/react-i18next.js +53 -0
- package/dist/cjs/runtime/i18n/utils.js +9 -17
- package/dist/cjs/runtime/index.js +50 -15
- package/dist/cjs/runtime/localizedPaths.js +102 -0
- package/dist/cjs/runtime/routerAdapter.js +167 -0
- package/dist/cjs/runtime/utils.js +80 -97
- package/dist/cjs/server/index.js +62 -14
- package/dist/cjs/shared/deepMerge.js +12 -8
- package/dist/cjs/shared/detection.js +9 -5
- package/dist/cjs/shared/localisedUrls.js +351 -0
- package/dist/cjs/shared/utils.js +15 -11
- package/dist/esm/cli/index.mjs +23 -0
- package/dist/esm/runtime/I18nLink.mjs +7 -22
- package/dist/esm/runtime/Link.mjs +221 -0
- package/dist/esm/runtime/canonicalRoutes.mjs +0 -0
- 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/defaults.node.mjs +24 -3
- 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 +15 -0
- package/dist/esm/runtime/i18n/utils.mjs +0 -12
- package/dist/esm/runtime/index.mjs +23 -13
- package/dist/esm/runtime/localizedPaths.mjs +55 -0
- package/dist/esm/runtime/routerAdapter.mjs +129 -0
- package/dist/esm/runtime/utils.mjs +19 -31
- package/dist/esm/server/index.mjs +46 -8
- package/dist/esm/shared/localisedUrls.mjs +283 -0
- package/dist/esm-node/cli/index.mjs +23 -0
- package/dist/esm-node/runtime/I18nLink.mjs +7 -22
- package/dist/esm-node/runtime/Link.mjs +222 -0
- package/dist/esm-node/runtime/canonicalRoutes.mjs +1 -0
- 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/defaults.node.mjs +24 -3
- 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 +16 -0
- package/dist/esm-node/runtime/i18n/utils.mjs +0 -12
- package/dist/esm-node/runtime/index.mjs +23 -13
- package/dist/esm-node/runtime/localizedPaths.mjs +56 -0
- package/dist/esm-node/runtime/routerAdapter.mjs +130 -0
- package/dist/esm-node/runtime/utils.mjs +19 -31
- package/dist/esm-node/server/index.mjs +46 -8
- package/dist/esm-node/shared/localisedUrls.mjs +284 -0
- package/dist/types/cli/index.d.ts +1 -0
- package/dist/types/runtime/I18nLink.d.ts +6 -0
- package/dist/types/runtime/Link.d.ts +66 -0
- package/dist/types/runtime/canonicalRoutes.d.ts +60 -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/defaults.node.d.ts +3 -2
- package/dist/types/runtime/i18n/backend/middleware.node.d.ts +1 -1
- package/dist/types/runtime/i18n/instance.d.ts +4 -6
- package/dist/types/runtime/i18n/react-i18next.d.ts +7 -0
- package/dist/types/runtime/index.d.ts +6 -1
- package/dist/types/runtime/localizedPaths.d.ts +39 -0
- package/dist/types/runtime/routerAdapter.d.ts +26 -0
- package/dist/types/runtime/types.d.ts +1 -1
- package/dist/types/runtime/utils.d.ts +13 -9
- package/dist/types/server/index.d.ts +6 -0
- package/dist/types/shared/localisedUrls.d.ts +36 -0
- package/dist/types/shared/type.d.ts +14 -0
- package/package.json +24 -24
- package/rstest.config.mts +44 -0
- package/src/cli/index.ts +44 -1
- package/src/runtime/I18nLink.tsx +14 -51
- package/src/runtime/Link.tsx +430 -0
- package/src/runtime/canonicalRoutes.ts +93 -0
- package/src/runtime/context.tsx +45 -7
- package/src/runtime/hooks.ts +13 -4
- package/src/runtime/i18n/backend/defaults.node.ts +40 -2
- 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 +3 -30
- package/src/runtime/i18n/react-i18next.ts +25 -0
- package/src/runtime/i18n/utils.ts +4 -26
- package/src/runtime/index.tsx +47 -12
- package/src/runtime/localizedPaths.ts +107 -0
- package/src/runtime/routerAdapter.tsx +332 -0
- package/src/runtime/types.ts +1 -1
- package/src/runtime/utils.ts +33 -38
- package/src/server/index.ts +108 -11
- package/src/shared/localisedUrls.ts +623 -0
- package/src/shared/type.ts +14 -0
- package/tests/backendDefaults.test.ts +51 -0
- package/tests/i18nUtils.test.ts +59 -0
- package/tests/link.test.tsx +525 -0
- package/tests/linkTypes.test.ts +28 -0
- package/tests/localisedUrls.test.ts +536 -0
- package/tests/routerAdapter.test.tsx +456 -0
- package/tests/type-fixture/linkTypes.fixture.tsx +51 -0
- package/tests/type-fixture/tsconfig.json +15 -0
- package/dist/esm/rslib-runtime.mjs +0 -18
- package/dist/esm-node/rslib-runtime.mjs +0 -19
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
import { RuntimeContext, isBrowser } from "@modern-js/runtime";
|
|
3
|
+
import { InternalRuntimeContext, getRouterRuntimeState } from "@modern-js/runtime/context";
|
|
4
|
+
import { Link as router_Link, useInRouterContext, useLocation, useNavigate, useParams } from "@modern-js/runtime/router";
|
|
5
|
+
import { useCallback, useContext, useEffect, useState } from "react";
|
|
6
|
+
const normalizeUrlPart = (value, prefix)=>{
|
|
7
|
+
if ('string' != typeof value || !value) return '';
|
|
8
|
+
return value.startsWith(prefix) ? value : `${prefix}${value}`;
|
|
9
|
+
};
|
|
10
|
+
const normalizeLocation = (location)=>{
|
|
11
|
+
if (!location || 'object' != typeof location) return null;
|
|
12
|
+
const locationValue = location;
|
|
13
|
+
if ('string' != typeof locationValue.pathname) return null;
|
|
14
|
+
return {
|
|
15
|
+
pathname: locationValue.pathname,
|
|
16
|
+
search: normalizeUrlPart('string' == typeof locationValue.search ? locationValue.search : locationValue.searchStr, '?'),
|
|
17
|
+
hash: normalizeUrlPart(locationValue.hash, '#')
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
const getWindowLocation = ()=>{
|
|
21
|
+
if (!isBrowser()) return null;
|
|
22
|
+
return {
|
|
23
|
+
pathname: window.location.pathname,
|
|
24
|
+
search: window.location.search,
|
|
25
|
+
hash: window.location.hash
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
const getRouterFramework = (runtimeContext, internalContext, inReactRouter)=>{
|
|
29
|
+
const framework = getRouterRuntimeState(internalContext)?.framework || getRouterRuntimeState(runtimeContext)?.framework;
|
|
30
|
+
if (framework) return framework;
|
|
31
|
+
if (internalContext.router?.useRouter || runtimeContext.router?.useRouter) return 'tanstack';
|
|
32
|
+
if (internalContext.router?.useLocation || internalContext.router?.useHref || runtimeContext.router?.useLocation || runtimeContext.router?.useHref) return 'react-router';
|
|
33
|
+
if (inReactRouter) return 'react-router';
|
|
34
|
+
};
|
|
35
|
+
const getRouterInstance = (internalContext, contextRouter)=>{
|
|
36
|
+
if (contextRouter) return contextRouter;
|
|
37
|
+
const router = getRouterRuntimeState(internalContext)?.instance;
|
|
38
|
+
if (!router || 'object' != typeof router) return null;
|
|
39
|
+
return router;
|
|
40
|
+
};
|
|
41
|
+
const getRouterStateLocation = (internalContext, contextRouter)=>{
|
|
42
|
+
const router = getRouterInstance(internalContext, contextRouter);
|
|
43
|
+
return normalizeLocation(router?.stores?.location?.get?.()) || normalizeLocation(router?.state?.location);
|
|
44
|
+
};
|
|
45
|
+
const getRouterParams = (internalContext, contextRouter)=>{
|
|
46
|
+
const router = getRouterInstance(internalContext, contextRouter);
|
|
47
|
+
const matches = router?.stores?.matches?.get?.() || router?.state?.matches;
|
|
48
|
+
if (!Array.isArray(matches)) return {};
|
|
49
|
+
return matches.reduce((params, match)=>{
|
|
50
|
+
if (match?.params) Object.assign(params, match.params);
|
|
51
|
+
return params;
|
|
52
|
+
}, {});
|
|
53
|
+
};
|
|
54
|
+
const useI18nRouterAdapter = ()=>{
|
|
55
|
+
const runtimeContext = useContext(RuntimeContext);
|
|
56
|
+
const internalContext = useContext(InternalRuntimeContext);
|
|
57
|
+
const inReactRouter = useInRouterContext();
|
|
58
|
+
const reactRouterNavigate = inReactRouter ? useNavigate() : null;
|
|
59
|
+
const reactRouterLocation = inReactRouter ? useLocation() : null;
|
|
60
|
+
const reactRouterParams = inReactRouter ? useParams() : {};
|
|
61
|
+
const framework = getRouterFramework(runtimeContext, internalContext, inReactRouter);
|
|
62
|
+
const contextUseRouter = inReactRouter || 'tanstack' !== framework ? void 0 : internalContext.router?.useRouter || runtimeContext.router?.useRouter;
|
|
63
|
+
const contextRouter = contextUseRouter ? contextUseRouter({
|
|
64
|
+
warn: false
|
|
65
|
+
}) : null;
|
|
66
|
+
const [, setRouterVersion] = useState(0);
|
|
67
|
+
const hasRouter = 'tanstack' === framework || 'react-router' === framework || Boolean(reactRouterNavigate);
|
|
68
|
+
useEffect(()=>{
|
|
69
|
+
if ('tanstack' !== framework) return;
|
|
70
|
+
const router = getRouterInstance(internalContext, contextRouter);
|
|
71
|
+
if (!router) return;
|
|
72
|
+
const update = ()=>setRouterVersion((version)=>version + 1);
|
|
73
|
+
const unsubscribers = [];
|
|
74
|
+
if ('function' == typeof router.stores?.location?.subscribe) {
|
|
75
|
+
const unsubscribe = router.stores.location.subscribe(update);
|
|
76
|
+
if ('function' == typeof unsubscribe) unsubscribers.push(unsubscribe);
|
|
77
|
+
}
|
|
78
|
+
if ('function' == typeof router.subscribe) for (const eventType of [
|
|
79
|
+
'onBeforeNavigate',
|
|
80
|
+
'onBeforeLoad'
|
|
81
|
+
]){
|
|
82
|
+
const unsubscribe = router.subscribe(eventType, update);
|
|
83
|
+
if ('function' == typeof unsubscribe) unsubscribers.push(unsubscribe);
|
|
84
|
+
}
|
|
85
|
+
return ()=>{
|
|
86
|
+
for (const unsubscribe of unsubscribers)unsubscribe();
|
|
87
|
+
};
|
|
88
|
+
}, [
|
|
89
|
+
contextRouter,
|
|
90
|
+
framework,
|
|
91
|
+
internalContext
|
|
92
|
+
]);
|
|
93
|
+
const navigate = useCallback((href, options)=>{
|
|
94
|
+
const router = getRouterInstance(internalContext, contextRouter);
|
|
95
|
+
const activeFramework = getRouterFramework(runtimeContext, internalContext, inReactRouter);
|
|
96
|
+
if ('tanstack' === activeFramework) {
|
|
97
|
+
if ('function' == typeof router?.navigate) return router.navigate({
|
|
98
|
+
to: href,
|
|
99
|
+
replace: options?.replace,
|
|
100
|
+
...options?.state === void 0 ? {} : {
|
|
101
|
+
state: options.state
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
throw new Error('TanStack router instance is not available.');
|
|
105
|
+
}
|
|
106
|
+
if (reactRouterNavigate) return reactRouterNavigate(href, options);
|
|
107
|
+
if ('react-router' === activeFramework) {
|
|
108
|
+
if ('function' == typeof router?.navigate) return router.navigate(href, options);
|
|
109
|
+
throw new Error('React Router instance is not available.');
|
|
110
|
+
}
|
|
111
|
+
}, [
|
|
112
|
+
contextRouter,
|
|
113
|
+
internalContext,
|
|
114
|
+
inReactRouter,
|
|
115
|
+
reactRouterNavigate,
|
|
116
|
+
runtimeContext
|
|
117
|
+
]);
|
|
118
|
+
const location = (reactRouterLocation ? normalizeLocation(reactRouterLocation) : getRouterStateLocation(internalContext, contextRouter)) || getWindowLocation();
|
|
119
|
+
const params = inReactRouter ? reactRouterParams : getRouterParams(internalContext, contextRouter);
|
|
120
|
+
const Link = 'tanstack' === framework ? internalContext.router?.Link || runtimeContext.router?.Link || null : 'react-router' === framework || inReactRouter ? router_Link : null;
|
|
121
|
+
return {
|
|
122
|
+
framework,
|
|
123
|
+
hasRouter,
|
|
124
|
+
location,
|
|
125
|
+
navigate: hasRouter ? navigate : null,
|
|
126
|
+
Link,
|
|
127
|
+
params
|
|
128
|
+
};
|
|
129
|
+
};
|
|
130
|
+
export { useI18nRouterAdapter };
|
|
@@ -1,13 +1,7 @@
|
|
|
1
1
|
import "node:module";
|
|
2
2
|
import { isBrowser } from "@modern-js/runtime";
|
|
3
3
|
import { getGlobalBasename } from "@modern-js/runtime/context";
|
|
4
|
-
import {
|
|
5
|
-
import * as __rspack_external__modern_js_runtime_router_2dfd0c78 from "@modern-js/runtime/router";
|
|
6
|
-
__webpack_require__.add({
|
|
7
|
-
"@modern-js/runtime/router?f1fa" (module) {
|
|
8
|
-
module.exports = __rspack_external__modern_js_runtime_router_2dfd0c78;
|
|
9
|
-
}
|
|
10
|
-
});
|
|
4
|
+
import { localiseTargetPathname } from "../shared/localisedUrls.mjs";
|
|
11
5
|
const getPathname = (context)=>{
|
|
12
6
|
if (isBrowser()) return window.location.pathname;
|
|
13
7
|
return context.ssrContext?.request?.pathname || '/';
|
|
@@ -23,11 +17,23 @@ const getLanguageFromPath = (pathname, languages, fallbackLanguage)=>{
|
|
|
23
17
|
if (languages.includes(firstSegment)) return firstSegment;
|
|
24
18
|
return fallbackLanguage;
|
|
25
19
|
};
|
|
26
|
-
const
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
20
|
+
const splitUrlTarget = (target)=>{
|
|
21
|
+
const hashIndex = target.indexOf('#');
|
|
22
|
+
const hash = hashIndex >= 0 ? target.slice(hashIndex) : '';
|
|
23
|
+
const beforeHash = hashIndex >= 0 ? target.slice(0, hashIndex) : target;
|
|
24
|
+
const searchIndex = beforeHash.indexOf('?');
|
|
25
|
+
const search = searchIndex >= 0 ? beforeHash.slice(searchIndex) : '';
|
|
26
|
+
const pathname = searchIndex >= 0 ? beforeHash.slice(0, searchIndex) : beforeHash;
|
|
27
|
+
return {
|
|
28
|
+
pathname,
|
|
29
|
+
search,
|
|
30
|
+
hash
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
const buildLocalizedUrl = (target, language, languages, localisedUrls)=>{
|
|
34
|
+
const { pathname, search, hash } = splitUrlTarget(target);
|
|
35
|
+
const localizedPathname = localiseTargetPathname(pathname, language, languages, localisedUrls);
|
|
36
|
+
return `${localizedPathname}${search}${hash}`;
|
|
31
37
|
};
|
|
32
38
|
const detectLanguageFromPath = (pathname, languages, localePathRedirect)=>{
|
|
33
39
|
if (!localePathRedirect) return {
|
|
@@ -55,22 +61,4 @@ const shouldIgnoreRedirect = (pathname, languages, ignoreRedirectRoutes)=>{
|
|
|
55
61
|
if ('function' == typeof ignoreRedirectRoutes) return ignoreRedirectRoutes(normalizedPath);
|
|
56
62
|
return ignoreRedirectRoutes.some((pattern)=>normalizedPath === pattern || normalizedPath.startsWith(`${pattern}/`));
|
|
57
63
|
};
|
|
58
|
-
|
|
59
|
-
try {
|
|
60
|
-
const { useLocation, useNavigate, useParams } = __webpack_require__("@modern-js/runtime/router?f1fa");
|
|
61
|
-
return {
|
|
62
|
-
navigate: useNavigate(),
|
|
63
|
-
location: useLocation(),
|
|
64
|
-
params: useParams(),
|
|
65
|
-
hasRouter: true
|
|
66
|
-
};
|
|
67
|
-
} catch (error) {
|
|
68
|
-
return {
|
|
69
|
-
navigate: null,
|
|
70
|
-
location: null,
|
|
71
|
-
params: {},
|
|
72
|
-
hasRouter: false
|
|
73
|
-
};
|
|
74
|
-
}
|
|
75
|
-
};
|
|
76
|
-
export { buildLocalizedUrl, detectLanguageFromPath, getEntryPath, getLanguageFromPath, getPathname, shouldIgnoreRedirect, useRouterHooks };
|
|
64
|
+
export { buildLocalizedUrl, detectLanguageFromPath, getEntryPath, getLanguageFromPath, getPathname, shouldIgnoreRedirect, splitUrlTarget };
|
|
@@ -1,8 +1,39 @@
|
|
|
1
1
|
import "node:module";
|
|
2
2
|
import { DEFAULT_I18NEXT_DETECTION_OPTIONS, mergeDetectionOptions } from "../runtime/i18n/detection/config.mjs";
|
|
3
|
+
import { localiseTargetPathname, resolveLocalisedUrlsConfig } from "../shared/localisedUrls.mjs";
|
|
3
4
|
import { getLocaleDetectionOptions } from "../shared/utils.mjs";
|
|
4
5
|
import * as __rspack_external__modern_js_server_core_hono_a76ca254 from "@modern-js/server-core/hono";
|
|
5
6
|
const { languageDetector: languageDetector } = __rspack_external__modern_js_server_core_hono_a76ca254;
|
|
7
|
+
const normalizeApiPrefix = (prefix)=>{
|
|
8
|
+
const trimmedPrefix = prefix.trim();
|
|
9
|
+
if (!trimmedPrefix) return null;
|
|
10
|
+
const prefixedPath = trimmedPrefix.startsWith('/') ? trimmedPrefix : `/${trimmedPrefix}`;
|
|
11
|
+
const withoutWildcard = prefixedPath.replace(/\/\*$/, '');
|
|
12
|
+
const normalizedPrefix = withoutWildcard.length > 1 ? withoutWildcard.replace(/\/+$/, '') : withoutWildcard;
|
|
13
|
+
return '/' === normalizedPrefix ? null : normalizedPrefix;
|
|
14
|
+
};
|
|
15
|
+
const collectApiPrefixes = (routes, bffPrefix)=>{
|
|
16
|
+
const prefixes = new Set();
|
|
17
|
+
for (const route of routes){
|
|
18
|
+
if (!route.isApi || !route.urlPath) continue;
|
|
19
|
+
const normalizedPrefix = normalizeApiPrefix(route.urlPath);
|
|
20
|
+
if (normalizedPrefix) prefixes.add(normalizedPrefix);
|
|
21
|
+
}
|
|
22
|
+
const bffPrefixes = Array.isArray(bffPrefix) ? bffPrefix : bffPrefix ? [
|
|
23
|
+
bffPrefix
|
|
24
|
+
] : [];
|
|
25
|
+
for (const prefix of bffPrefixes){
|
|
26
|
+
const normalizedPrefix = normalizeApiPrefix(prefix);
|
|
27
|
+
if (normalizedPrefix) prefixes.add(normalizedPrefix);
|
|
28
|
+
}
|
|
29
|
+
return [
|
|
30
|
+
...prefixes
|
|
31
|
+
];
|
|
32
|
+
};
|
|
33
|
+
const matchesApiPrefix = (pathname, apiPrefixes)=>{
|
|
34
|
+
const normalizedPathname = pathname.startsWith('/') ? pathname : `/${pathname}`;
|
|
35
|
+
return apiPrefixes.some((prefix)=>normalizedPathname === prefix || normalizedPathname.startsWith(`${prefix}/`));
|
|
36
|
+
};
|
|
6
37
|
const convertToHonoLanguageDetectorOptions = (languages, fallbackLanguage, detectionOptions)=>{
|
|
7
38
|
const mergedDetection = detectionOptions ? mergeDetectionOptions(detectionOptions) : DEFAULT_I18NEXT_DETECTION_OPTIONS;
|
|
8
39
|
const order = (mergedDetection.order || []).filter((item)=>![
|
|
@@ -92,15 +123,12 @@ const getLanguageFromPath = (req, urlPath, languages)=>{
|
|
|
92
123
|
if (languages.includes(firstSegment)) return firstSegment;
|
|
93
124
|
return null;
|
|
94
125
|
};
|
|
95
|
-
const buildLocalizedUrl = (req, urlPath, language, languages)=>{
|
|
126
|
+
const buildLocalizedUrl = (req, urlPath, language, languages, localisedUrls)=>{
|
|
96
127
|
const url = new URL(req.url);
|
|
97
128
|
const pathname = url.pathname;
|
|
98
129
|
const basePath = urlPath.replace('/*', '');
|
|
99
130
|
const remainingPath = pathname.startsWith(basePath) ? pathname.slice(basePath.length) : pathname;
|
|
100
|
-
const
|
|
101
|
-
if (segments.length > 0 && languages.includes(segments[0])) segments[0] = language;
|
|
102
|
-
else segments.unshift(language);
|
|
103
|
-
const newPathname = `/${segments.join('/')}`;
|
|
131
|
+
const newPathname = localiseTargetPathname(remainingPath, language, languages, localisedUrls);
|
|
104
132
|
const suffix = `${url.search}${url.hash}`;
|
|
105
133
|
const localizedUrl = '/' === basePath ? newPathname + suffix : basePath + newPathname + suffix;
|
|
106
134
|
return localizedUrl;
|
|
@@ -110,6 +138,9 @@ const i18nServerPlugin = (options)=>({
|
|
|
110
138
|
setup: (api)=>{
|
|
111
139
|
api.onPrepare(()=>{
|
|
112
140
|
const { middlewares, routes } = api.getServerContext();
|
|
141
|
+
const serverConfig = api.getServerConfig();
|
|
142
|
+
const bffPrefix = serverConfig?.bff ? serverConfig.bff.prefix ?? '/api' : void 0;
|
|
143
|
+
const apiPrefixes = collectApiPrefixes(routes, bffPrefix);
|
|
113
144
|
const entryPaths = new Set();
|
|
114
145
|
routes.forEach((route)=>{
|
|
115
146
|
if (route.entryName && route.urlPath && '/' !== route.urlPath) {
|
|
@@ -121,7 +152,7 @@ const i18nServerPlugin = (options)=>({
|
|
|
121
152
|
const { entryName } = route;
|
|
122
153
|
if (!entryName) return;
|
|
123
154
|
if (!options.localeDetection) return;
|
|
124
|
-
const { localePathRedirect, i18nextDetector = true, languages = [], fallbackLanguage = 'en', detection, ignoreRedirectRoutes } = getLocaleDetectionOptions(entryName, options.localeDetection);
|
|
155
|
+
const { localePathRedirect, i18nextDetector = true, languages = [], fallbackLanguage = 'en', detection, ignoreRedirectRoutes, localisedUrls } = getLocaleDetectionOptions(entryName, options.localeDetection);
|
|
125
156
|
const staticRoutePrefixes = options.staticRoutePrefixes;
|
|
126
157
|
const originUrlPath = route.urlPath;
|
|
127
158
|
const urlPath = originUrlPath.endsWith('/') ? `${originUrlPath}*` : `${originUrlPath}/*`;
|
|
@@ -135,6 +166,7 @@ const i18nServerPlugin = (options)=>({
|
|
|
135
166
|
handler: async (c, next)=>{
|
|
136
167
|
const url = new URL(c.req.url);
|
|
137
168
|
const pathname = url.pathname;
|
|
169
|
+
if (matchesApiPrefix(pathname, apiPrefixes)) return await next();
|
|
138
170
|
if (isStaticResourceRequest(pathname, staticRoutePrefixes, languages)) return await next();
|
|
139
171
|
if ('/' === originUrlPath) {
|
|
140
172
|
const pathSegments = pathname.split('/').filter(Boolean);
|
|
@@ -153,6 +185,7 @@ const i18nServerPlugin = (options)=>({
|
|
|
153
185
|
handler: async (c, next)=>{
|
|
154
186
|
const url = new URL(c.req.url);
|
|
155
187
|
const pathname = url.pathname;
|
|
188
|
+
if (matchesApiPrefix(pathname, apiPrefixes)) return await next();
|
|
156
189
|
if (isStaticResourceRequest(pathname, staticRoutePrefixes, languages)) return await next();
|
|
157
190
|
if (shouldIgnoreRedirect(pathname, urlPath, ignoreRedirectRoutes)) return await next();
|
|
158
191
|
if ('/' === originUrlPath) {
|
|
@@ -167,9 +200,14 @@ const i18nServerPlugin = (options)=>({
|
|
|
167
200
|
let detectedLanguage = null;
|
|
168
201
|
if (i18nextDetector) detectedLanguage = c.get('language') || null;
|
|
169
202
|
const targetLanguage = detectedLanguage || fallbackLanguage;
|
|
170
|
-
const localizedUrl = buildLocalizedUrl(c.req, originUrlPath, targetLanguage, languages);
|
|
203
|
+
const localizedUrl = buildLocalizedUrl(c.req, originUrlPath, targetLanguage, languages, localisedUrls);
|
|
171
204
|
return c.redirect(localizedUrl);
|
|
172
205
|
}
|
|
206
|
+
const localisedUrlsConfig = resolveLocalisedUrlsConfig(localisedUrls);
|
|
207
|
+
if (localisedUrlsConfig.enabled) {
|
|
208
|
+
const expectedUrl = buildLocalizedUrl(c.req, originUrlPath, language, languages, localisedUrls);
|
|
209
|
+
if (expectedUrl !== `${pathname}${url.search}${url.hash}`) return c.redirect(expectedUrl);
|
|
210
|
+
}
|
|
173
211
|
await next();
|
|
174
212
|
}
|
|
175
213
|
});
|
|
@@ -180,4 +218,4 @@ const i18nServerPlugin = (options)=>({
|
|
|
180
218
|
});
|
|
181
219
|
const server = i18nServerPlugin;
|
|
182
220
|
export default server;
|
|
183
|
-
export { i18nServerPlugin };
|
|
221
|
+
export { collectApiPrefixes, i18nServerPlugin, matchesApiPrefix };
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
const LOCALE_PARAM_NAMES = new Set([
|
|
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 };
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { AppTools, CliPlugin } from '@modern-js/app-tools';
|
|
2
2
|
import type { Entrypoint } from '@modern-js/types';
|
|
3
3
|
import type { BackendOptions, LocaleDetectionOptions } from '../shared/type';
|
|
4
|
+
import '../runtime/types';
|
|
4
5
|
export type TransformRuntimeConfigFn = (extendedConfig: Record<string, any>, entrypoint: Entrypoint) => Record<string, any>;
|
|
5
6
|
export interface I18nPluginOptions {
|
|
6
7
|
localeDetection?: LocaleDetectionOptions;
|
|
@@ -4,5 +4,11 @@ export interface I18nLinkProps {
|
|
|
4
4
|
children: React.ReactNode;
|
|
5
5
|
[key: string]: any;
|
|
6
6
|
}
|
|
7
|
+
/**
|
|
8
|
+
* @deprecated Use {@link Link} from `@modern-js/plugin-i18n/runtime` instead.
|
|
9
|
+
* `Link` accepts the same language-agnostic `to` values and additionally
|
|
10
|
+
* supports `#hash`/`?query` targets, typed canonical routes, `params`
|
|
11
|
+
* interpolation and language-invariant active state.
|
|
12
|
+
*/
|
|
7
13
|
export declare const I18nLink: React.FC<I18nLinkProps>;
|
|
8
14
|
export default I18nLink;
|