@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,129 @@
|
|
|
1
|
+
import { RuntimeContext, isBrowser } from "@modern-js/runtime";
|
|
2
|
+
import { InternalRuntimeContext, getRouterRuntimeState } from "@modern-js/runtime/context";
|
|
3
|
+
import { Link as router_Link, useInRouterContext, useLocation, useNavigate, useParams } from "@modern-js/runtime/router";
|
|
4
|
+
import { useCallback, useContext, useEffect, useState } from "react";
|
|
5
|
+
const normalizeUrlPart = (value, prefix)=>{
|
|
6
|
+
if ('string' != typeof value || !value) return '';
|
|
7
|
+
return value.startsWith(prefix) ? value : `${prefix}${value}`;
|
|
8
|
+
};
|
|
9
|
+
const normalizeLocation = (location)=>{
|
|
10
|
+
if (!location || 'object' != typeof location) return null;
|
|
11
|
+
const locationValue = location;
|
|
12
|
+
if ('string' != typeof locationValue.pathname) return null;
|
|
13
|
+
return {
|
|
14
|
+
pathname: locationValue.pathname,
|
|
15
|
+
search: normalizeUrlPart('string' == typeof locationValue.search ? locationValue.search : locationValue.searchStr, '?'),
|
|
16
|
+
hash: normalizeUrlPart(locationValue.hash, '#')
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
const getWindowLocation = ()=>{
|
|
20
|
+
if (!isBrowser()) return null;
|
|
21
|
+
return {
|
|
22
|
+
pathname: window.location.pathname,
|
|
23
|
+
search: window.location.search,
|
|
24
|
+
hash: window.location.hash
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
const getRouterFramework = (runtimeContext, internalContext, inReactRouter)=>{
|
|
28
|
+
const framework = getRouterRuntimeState(internalContext)?.framework || getRouterRuntimeState(runtimeContext)?.framework;
|
|
29
|
+
if (framework) return framework;
|
|
30
|
+
if (internalContext.router?.useRouter || runtimeContext.router?.useRouter) return 'tanstack';
|
|
31
|
+
if (internalContext.router?.useLocation || internalContext.router?.useHref || runtimeContext.router?.useLocation || runtimeContext.router?.useHref) return 'react-router';
|
|
32
|
+
if (inReactRouter) return 'react-router';
|
|
33
|
+
};
|
|
34
|
+
const getRouterInstance = (internalContext, contextRouter)=>{
|
|
35
|
+
if (contextRouter) return contextRouter;
|
|
36
|
+
const router = getRouterRuntimeState(internalContext)?.instance;
|
|
37
|
+
if (!router || 'object' != typeof router) return null;
|
|
38
|
+
return router;
|
|
39
|
+
};
|
|
40
|
+
const getRouterStateLocation = (internalContext, contextRouter)=>{
|
|
41
|
+
const router = getRouterInstance(internalContext, contextRouter);
|
|
42
|
+
return normalizeLocation(router?.stores?.location?.get?.()) || normalizeLocation(router?.state?.location);
|
|
43
|
+
};
|
|
44
|
+
const getRouterParams = (internalContext, contextRouter)=>{
|
|
45
|
+
const router = getRouterInstance(internalContext, contextRouter);
|
|
46
|
+
const matches = router?.stores?.matches?.get?.() || router?.state?.matches;
|
|
47
|
+
if (!Array.isArray(matches)) return {};
|
|
48
|
+
return matches.reduce((params, match)=>{
|
|
49
|
+
if (match?.params) Object.assign(params, match.params);
|
|
50
|
+
return params;
|
|
51
|
+
}, {});
|
|
52
|
+
};
|
|
53
|
+
const useI18nRouterAdapter = ()=>{
|
|
54
|
+
const runtimeContext = useContext(RuntimeContext);
|
|
55
|
+
const internalContext = useContext(InternalRuntimeContext);
|
|
56
|
+
const inReactRouter = useInRouterContext();
|
|
57
|
+
const reactRouterNavigate = inReactRouter ? useNavigate() : null;
|
|
58
|
+
const reactRouterLocation = inReactRouter ? useLocation() : null;
|
|
59
|
+
const reactRouterParams = inReactRouter ? useParams() : {};
|
|
60
|
+
const framework = getRouterFramework(runtimeContext, internalContext, inReactRouter);
|
|
61
|
+
const contextUseRouter = inReactRouter || 'tanstack' !== framework ? void 0 : internalContext.router?.useRouter || runtimeContext.router?.useRouter;
|
|
62
|
+
const contextRouter = contextUseRouter ? contextUseRouter({
|
|
63
|
+
warn: false
|
|
64
|
+
}) : null;
|
|
65
|
+
const [, setRouterVersion] = useState(0);
|
|
66
|
+
const hasRouter = 'tanstack' === framework || 'react-router' === framework || Boolean(reactRouterNavigate);
|
|
67
|
+
useEffect(()=>{
|
|
68
|
+
if ('tanstack' !== framework) return;
|
|
69
|
+
const router = getRouterInstance(internalContext, contextRouter);
|
|
70
|
+
if (!router) return;
|
|
71
|
+
const update = ()=>setRouterVersion((version)=>version + 1);
|
|
72
|
+
const unsubscribers = [];
|
|
73
|
+
if ('function' == typeof router.stores?.location?.subscribe) {
|
|
74
|
+
const unsubscribe = router.stores.location.subscribe(update);
|
|
75
|
+
if ('function' == typeof unsubscribe) unsubscribers.push(unsubscribe);
|
|
76
|
+
}
|
|
77
|
+
if ('function' == typeof router.subscribe) for (const eventType of [
|
|
78
|
+
'onBeforeNavigate',
|
|
79
|
+
'onBeforeLoad'
|
|
80
|
+
]){
|
|
81
|
+
const unsubscribe = router.subscribe(eventType, update);
|
|
82
|
+
if ('function' == typeof unsubscribe) unsubscribers.push(unsubscribe);
|
|
83
|
+
}
|
|
84
|
+
return ()=>{
|
|
85
|
+
for (const unsubscribe of unsubscribers)unsubscribe();
|
|
86
|
+
};
|
|
87
|
+
}, [
|
|
88
|
+
contextRouter,
|
|
89
|
+
framework,
|
|
90
|
+
internalContext
|
|
91
|
+
]);
|
|
92
|
+
const navigate = useCallback((href, options)=>{
|
|
93
|
+
const router = getRouterInstance(internalContext, contextRouter);
|
|
94
|
+
const activeFramework = getRouterFramework(runtimeContext, internalContext, inReactRouter);
|
|
95
|
+
if ('tanstack' === activeFramework) {
|
|
96
|
+
if ('function' == typeof router?.navigate) return router.navigate({
|
|
97
|
+
to: href,
|
|
98
|
+
replace: options?.replace,
|
|
99
|
+
...options?.state === void 0 ? {} : {
|
|
100
|
+
state: options.state
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
throw new Error('TanStack router instance is not available.');
|
|
104
|
+
}
|
|
105
|
+
if (reactRouterNavigate) return reactRouterNavigate(href, options);
|
|
106
|
+
if ('react-router' === activeFramework) {
|
|
107
|
+
if ('function' == typeof router?.navigate) return router.navigate(href, options);
|
|
108
|
+
throw new Error('React Router instance is not available.');
|
|
109
|
+
}
|
|
110
|
+
}, [
|
|
111
|
+
contextRouter,
|
|
112
|
+
internalContext,
|
|
113
|
+
inReactRouter,
|
|
114
|
+
reactRouterNavigate,
|
|
115
|
+
runtimeContext
|
|
116
|
+
]);
|
|
117
|
+
const location = (reactRouterLocation ? normalizeLocation(reactRouterLocation) : getRouterStateLocation(internalContext, contextRouter)) || getWindowLocation();
|
|
118
|
+
const params = inReactRouter ? reactRouterParams : getRouterParams(internalContext, contextRouter);
|
|
119
|
+
const Link = 'tanstack' === framework ? internalContext.router?.Link || runtimeContext.router?.Link || null : 'react-router' === framework || inReactRouter ? router_Link : null;
|
|
120
|
+
return {
|
|
121
|
+
framework,
|
|
122
|
+
hasRouter,
|
|
123
|
+
location,
|
|
124
|
+
navigate: hasRouter ? navigate : null,
|
|
125
|
+
Link,
|
|
126
|
+
params
|
|
127
|
+
};
|
|
128
|
+
};
|
|
129
|
+
export { useI18nRouterAdapter };
|
|
@@ -1,12 +1,6 @@
|
|
|
1
1
|
import { isBrowser } from "@modern-js/runtime";
|
|
2
2
|
import { getGlobalBasename } from "@modern-js/runtime/context";
|
|
3
|
-
import {
|
|
4
|
-
import * as __rspack_external__modern_js_runtime_router_2dfd0c78 from "@modern-js/runtime/router";
|
|
5
|
-
__webpack_require__.add({
|
|
6
|
-
"@modern-js/runtime/router?f1fa" (module) {
|
|
7
|
-
module.exports = __rspack_external__modern_js_runtime_router_2dfd0c78;
|
|
8
|
-
}
|
|
9
|
-
});
|
|
3
|
+
import { localiseTargetPathname } from "../shared/localisedUrls.mjs";
|
|
10
4
|
const getPathname = (context)=>{
|
|
11
5
|
if (isBrowser()) return window.location.pathname;
|
|
12
6
|
return context.ssrContext?.request?.pathname || '/';
|
|
@@ -22,11 +16,23 @@ const getLanguageFromPath = (pathname, languages, fallbackLanguage)=>{
|
|
|
22
16
|
if (languages.includes(firstSegment)) return firstSegment;
|
|
23
17
|
return fallbackLanguage;
|
|
24
18
|
};
|
|
25
|
-
const
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
19
|
+
const splitUrlTarget = (target)=>{
|
|
20
|
+
const hashIndex = target.indexOf('#');
|
|
21
|
+
const hash = hashIndex >= 0 ? target.slice(hashIndex) : '';
|
|
22
|
+
const beforeHash = hashIndex >= 0 ? target.slice(0, hashIndex) : target;
|
|
23
|
+
const searchIndex = beforeHash.indexOf('?');
|
|
24
|
+
const search = searchIndex >= 0 ? beforeHash.slice(searchIndex) : '';
|
|
25
|
+
const pathname = searchIndex >= 0 ? beforeHash.slice(0, searchIndex) : beforeHash;
|
|
26
|
+
return {
|
|
27
|
+
pathname,
|
|
28
|
+
search,
|
|
29
|
+
hash
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
const buildLocalizedUrl = (target, language, languages, localisedUrls)=>{
|
|
33
|
+
const { pathname, search, hash } = splitUrlTarget(target);
|
|
34
|
+
const localizedPathname = localiseTargetPathname(pathname, language, languages, localisedUrls);
|
|
35
|
+
return `${localizedPathname}${search}${hash}`;
|
|
30
36
|
};
|
|
31
37
|
const detectLanguageFromPath = (pathname, languages, localePathRedirect)=>{
|
|
32
38
|
if (!localePathRedirect) return {
|
|
@@ -54,22 +60,4 @@ const shouldIgnoreRedirect = (pathname, languages, ignoreRedirectRoutes)=>{
|
|
|
54
60
|
if ('function' == typeof ignoreRedirectRoutes) return ignoreRedirectRoutes(normalizedPath);
|
|
55
61
|
return ignoreRedirectRoutes.some((pattern)=>normalizedPath === pattern || normalizedPath.startsWith(`${pattern}/`));
|
|
56
62
|
};
|
|
57
|
-
|
|
58
|
-
try {
|
|
59
|
-
const { useLocation, useNavigate, useParams } = __webpack_require__("@modern-js/runtime/router?f1fa");
|
|
60
|
-
return {
|
|
61
|
-
navigate: useNavigate(),
|
|
62
|
-
location: useLocation(),
|
|
63
|
-
params: useParams(),
|
|
64
|
-
hasRouter: true
|
|
65
|
-
};
|
|
66
|
-
} catch (error) {
|
|
67
|
-
return {
|
|
68
|
-
navigate: null,
|
|
69
|
-
location: null,
|
|
70
|
-
params: {},
|
|
71
|
-
hasRouter: false
|
|
72
|
-
};
|
|
73
|
-
}
|
|
74
|
-
};
|
|
75
|
-
export { buildLocalizedUrl, detectLanguageFromPath, getEntryPath, getLanguageFromPath, getPathname, shouldIgnoreRedirect, useRouterHooks };
|
|
63
|
+
export { buildLocalizedUrl, detectLanguageFromPath, getEntryPath, getLanguageFromPath, getPathname, shouldIgnoreRedirect, splitUrlTarget };
|
|
@@ -1,7 +1,38 @@
|
|
|
1
1
|
import { DEFAULT_I18NEXT_DETECTION_OPTIONS, mergeDetectionOptions } from "../runtime/i18n/detection/config.mjs";
|
|
2
|
+
import { localiseTargetPathname, resolveLocalisedUrlsConfig } from "../shared/localisedUrls.mjs";
|
|
2
3
|
import { getLocaleDetectionOptions } from "../shared/utils.mjs";
|
|
3
4
|
import * as __rspack_external__modern_js_server_core_hono_a76ca254 from "@modern-js/server-core/hono";
|
|
4
5
|
const { languageDetector: languageDetector } = __rspack_external__modern_js_server_core_hono_a76ca254;
|
|
6
|
+
const normalizeApiPrefix = (prefix)=>{
|
|
7
|
+
const trimmedPrefix = prefix.trim();
|
|
8
|
+
if (!trimmedPrefix) return null;
|
|
9
|
+
const prefixedPath = trimmedPrefix.startsWith('/') ? trimmedPrefix : `/${trimmedPrefix}`;
|
|
10
|
+
const withoutWildcard = prefixedPath.replace(/\/\*$/, '');
|
|
11
|
+
const normalizedPrefix = withoutWildcard.length > 1 ? withoutWildcard.replace(/\/+$/, '') : withoutWildcard;
|
|
12
|
+
return '/' === normalizedPrefix ? null : normalizedPrefix;
|
|
13
|
+
};
|
|
14
|
+
const collectApiPrefixes = (routes, bffPrefix)=>{
|
|
15
|
+
const prefixes = new Set();
|
|
16
|
+
for (const route of routes){
|
|
17
|
+
if (!route.isApi || !route.urlPath) continue;
|
|
18
|
+
const normalizedPrefix = normalizeApiPrefix(route.urlPath);
|
|
19
|
+
if (normalizedPrefix) prefixes.add(normalizedPrefix);
|
|
20
|
+
}
|
|
21
|
+
const bffPrefixes = Array.isArray(bffPrefix) ? bffPrefix : bffPrefix ? [
|
|
22
|
+
bffPrefix
|
|
23
|
+
] : [];
|
|
24
|
+
for (const prefix of bffPrefixes){
|
|
25
|
+
const normalizedPrefix = normalizeApiPrefix(prefix);
|
|
26
|
+
if (normalizedPrefix) prefixes.add(normalizedPrefix);
|
|
27
|
+
}
|
|
28
|
+
return [
|
|
29
|
+
...prefixes
|
|
30
|
+
];
|
|
31
|
+
};
|
|
32
|
+
const matchesApiPrefix = (pathname, apiPrefixes)=>{
|
|
33
|
+
const normalizedPathname = pathname.startsWith('/') ? pathname : `/${pathname}`;
|
|
34
|
+
return apiPrefixes.some((prefix)=>normalizedPathname === prefix || normalizedPathname.startsWith(`${prefix}/`));
|
|
35
|
+
};
|
|
5
36
|
const convertToHonoLanguageDetectorOptions = (languages, fallbackLanguage, detectionOptions)=>{
|
|
6
37
|
const mergedDetection = detectionOptions ? mergeDetectionOptions(detectionOptions) : DEFAULT_I18NEXT_DETECTION_OPTIONS;
|
|
7
38
|
const order = (mergedDetection.order || []).filter((item)=>![
|
|
@@ -91,15 +122,12 @@ const getLanguageFromPath = (req, urlPath, languages)=>{
|
|
|
91
122
|
if (languages.includes(firstSegment)) return firstSegment;
|
|
92
123
|
return null;
|
|
93
124
|
};
|
|
94
|
-
const buildLocalizedUrl = (req, urlPath, language, languages)=>{
|
|
125
|
+
const buildLocalizedUrl = (req, urlPath, language, languages, localisedUrls)=>{
|
|
95
126
|
const url = new URL(req.url);
|
|
96
127
|
const pathname = url.pathname;
|
|
97
128
|
const basePath = urlPath.replace('/*', '');
|
|
98
129
|
const remainingPath = pathname.startsWith(basePath) ? pathname.slice(basePath.length) : pathname;
|
|
99
|
-
const
|
|
100
|
-
if (segments.length > 0 && languages.includes(segments[0])) segments[0] = language;
|
|
101
|
-
else segments.unshift(language);
|
|
102
|
-
const newPathname = `/${segments.join('/')}`;
|
|
130
|
+
const newPathname = localiseTargetPathname(remainingPath, language, languages, localisedUrls);
|
|
103
131
|
const suffix = `${url.search}${url.hash}`;
|
|
104
132
|
const localizedUrl = '/' === basePath ? newPathname + suffix : basePath + newPathname + suffix;
|
|
105
133
|
return localizedUrl;
|
|
@@ -109,6 +137,9 @@ const i18nServerPlugin = (options)=>({
|
|
|
109
137
|
setup: (api)=>{
|
|
110
138
|
api.onPrepare(()=>{
|
|
111
139
|
const { middlewares, routes } = api.getServerContext();
|
|
140
|
+
const serverConfig = api.getServerConfig();
|
|
141
|
+
const bffPrefix = serverConfig?.bff ? serverConfig.bff.prefix ?? '/api' : void 0;
|
|
142
|
+
const apiPrefixes = collectApiPrefixes(routes, bffPrefix);
|
|
112
143
|
const entryPaths = new Set();
|
|
113
144
|
routes.forEach((route)=>{
|
|
114
145
|
if (route.entryName && route.urlPath && '/' !== route.urlPath) {
|
|
@@ -120,7 +151,7 @@ const i18nServerPlugin = (options)=>({
|
|
|
120
151
|
const { entryName } = route;
|
|
121
152
|
if (!entryName) return;
|
|
122
153
|
if (!options.localeDetection) return;
|
|
123
|
-
const { localePathRedirect, i18nextDetector = true, languages = [], fallbackLanguage = 'en', detection, ignoreRedirectRoutes } = getLocaleDetectionOptions(entryName, options.localeDetection);
|
|
154
|
+
const { localePathRedirect, i18nextDetector = true, languages = [], fallbackLanguage = 'en', detection, ignoreRedirectRoutes, localisedUrls } = getLocaleDetectionOptions(entryName, options.localeDetection);
|
|
124
155
|
const staticRoutePrefixes = options.staticRoutePrefixes;
|
|
125
156
|
const originUrlPath = route.urlPath;
|
|
126
157
|
const urlPath = originUrlPath.endsWith('/') ? `${originUrlPath}*` : `${originUrlPath}/*`;
|
|
@@ -134,6 +165,7 @@ const i18nServerPlugin = (options)=>({
|
|
|
134
165
|
handler: async (c, next)=>{
|
|
135
166
|
const url = new URL(c.req.url);
|
|
136
167
|
const pathname = url.pathname;
|
|
168
|
+
if (matchesApiPrefix(pathname, apiPrefixes)) return await next();
|
|
137
169
|
if (isStaticResourceRequest(pathname, staticRoutePrefixes, languages)) return await next();
|
|
138
170
|
if ('/' === originUrlPath) {
|
|
139
171
|
const pathSegments = pathname.split('/').filter(Boolean);
|
|
@@ -152,6 +184,7 @@ const i18nServerPlugin = (options)=>({
|
|
|
152
184
|
handler: async (c, next)=>{
|
|
153
185
|
const url = new URL(c.req.url);
|
|
154
186
|
const pathname = url.pathname;
|
|
187
|
+
if (matchesApiPrefix(pathname, apiPrefixes)) return await next();
|
|
155
188
|
if (isStaticResourceRequest(pathname, staticRoutePrefixes, languages)) return await next();
|
|
156
189
|
if (shouldIgnoreRedirect(pathname, urlPath, ignoreRedirectRoutes)) return await next();
|
|
157
190
|
if ('/' === originUrlPath) {
|
|
@@ -166,9 +199,14 @@ const i18nServerPlugin = (options)=>({
|
|
|
166
199
|
let detectedLanguage = null;
|
|
167
200
|
if (i18nextDetector) detectedLanguage = c.get('language') || null;
|
|
168
201
|
const targetLanguage = detectedLanguage || fallbackLanguage;
|
|
169
|
-
const localizedUrl = buildLocalizedUrl(c.req, originUrlPath, targetLanguage, languages);
|
|
202
|
+
const localizedUrl = buildLocalizedUrl(c.req, originUrlPath, targetLanguage, languages, localisedUrls);
|
|
170
203
|
return c.redirect(localizedUrl);
|
|
171
204
|
}
|
|
205
|
+
const localisedUrlsConfig = resolveLocalisedUrlsConfig(localisedUrls);
|
|
206
|
+
if (localisedUrlsConfig.enabled) {
|
|
207
|
+
const expectedUrl = buildLocalizedUrl(c.req, originUrlPath, language, languages, localisedUrls);
|
|
208
|
+
if (expectedUrl !== `${pathname}${url.search}${url.hash}`) return c.redirect(expectedUrl);
|
|
209
|
+
}
|
|
172
210
|
await next();
|
|
173
211
|
}
|
|
174
212
|
});
|
|
@@ -179,4 +217,4 @@ const i18nServerPlugin = (options)=>({
|
|
|
179
217
|
});
|
|
180
218
|
const server = i18nServerPlugin;
|
|
181
219
|
export default server;
|
|
182
|
-
export { i18nServerPlugin };
|
|
220
|
+
export { collectApiPrefixes, i18nServerPlugin, matchesApiPrefix };
|
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
const LOCALE_PARAM_NAMES = new Set([
|
|
2
|
+
'lang',
|
|
3
|
+
'locale',
|
|
4
|
+
'language'
|
|
5
|
+
]);
|
|
6
|
+
const normaliseSlashes = (path)=>{
|
|
7
|
+
const withoutDuplicateSlashes = path.replace(/\/+/g, '/');
|
|
8
|
+
const withLeadingSlash = withoutDuplicateSlashes.startsWith('/') ? withoutDuplicateSlashes : `/${withoutDuplicateSlashes}`;
|
|
9
|
+
return withLeadingSlash.length > 1 ? withLeadingSlash.replace(/\/+$/, '') : withLeadingSlash;
|
|
10
|
+
};
|
|
11
|
+
const normalisePathPattern = (path)=>normaliseSlashes(path).replace(/\[(.+?)\]/g, ':$1');
|
|
12
|
+
const normalisePathname = (pathname)=>normaliseSlashes(pathname);
|
|
13
|
+
const normaliseRoutePath = (path)=>{
|
|
14
|
+
const normalized = normalisePathPattern(path);
|
|
15
|
+
return '/' === normalized ? '' : normalized.slice(1);
|
|
16
|
+
};
|
|
17
|
+
const getLocaleParamSegment = (segment)=>{
|
|
18
|
+
if (!segment.startsWith(':')) return null;
|
|
19
|
+
const paramName = segment.slice(1).replace(/\?$/, '');
|
|
20
|
+
return LOCALE_PARAM_NAMES.has(paramName) ? segment : null;
|
|
21
|
+
};
|
|
22
|
+
const splitPathSegments = (path)=>{
|
|
23
|
+
if (!path) return [];
|
|
24
|
+
return normalisePathPattern(path).split('/').filter(Boolean);
|
|
25
|
+
};
|
|
26
|
+
const stripLeadingLocaleParam = (path)=>{
|
|
27
|
+
const segments = splitPathSegments(path);
|
|
28
|
+
const leadingLocaleParam = getLocaleParamSegment(segments[0] || '');
|
|
29
|
+
if (!leadingLocaleParam) return path;
|
|
30
|
+
const remainingPath = segments.slice(1).join('/');
|
|
31
|
+
return remainingPath ? `/${remainingPath}` : void 0;
|
|
32
|
+
};
|
|
33
|
+
const getLeadingLocaleParam = (path)=>{
|
|
34
|
+
const segments = splitPathSegments(path);
|
|
35
|
+
return getLocaleParamSegment(segments[0] || '');
|
|
36
|
+
};
|
|
37
|
+
const resolveLocalisedUrlsConfig = (option)=>{
|
|
38
|
+
if (option && 'object' == typeof option && Object.keys(option).length > 0) return {
|
|
39
|
+
enabled: true,
|
|
40
|
+
map: option
|
|
41
|
+
};
|
|
42
|
+
return {
|
|
43
|
+
enabled: false,
|
|
44
|
+
map: {}
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
const isLocalisableRoutePath = (path)=>{
|
|
48
|
+
const pathWithoutLocale = stripLeadingLocaleParam(path);
|
|
49
|
+
if (!pathWithoutLocale || '/' === pathWithoutLocale || '*' === pathWithoutLocale) return false;
|
|
50
|
+
return true;
|
|
51
|
+
};
|
|
52
|
+
const joinPath = (parentPath, routePath)=>{
|
|
53
|
+
if (!isLocalisableRoutePath(routePath)) return parentPath;
|
|
54
|
+
const segment = normaliseRoutePath(stripLeadingLocaleParam(routePath) || '');
|
|
55
|
+
return normalisePathPattern(`${parentPath}/${segment}`);
|
|
56
|
+
};
|
|
57
|
+
const ensureLocalisedUrlsForPath = (canonicalPath, languages, localisedUrls)=>{
|
|
58
|
+
const entry = localisedUrls[canonicalPath];
|
|
59
|
+
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.`);
|
|
60
|
+
const missingLanguages = languages.filter((language)=>!entry[language]);
|
|
61
|
+
if (missingLanguages.length > 0) throw new Error(`localisedUrls["${canonicalPath}"] is missing languages: ${missingLanguages.join(', ')}. Every configured language must have a localised URL.`);
|
|
62
|
+
return entry;
|
|
63
|
+
};
|
|
64
|
+
const validateLocalisedUrls = (routes, languages, localisedUrls)=>{
|
|
65
|
+
const visit = (route, parentPath)=>{
|
|
66
|
+
const canonicalPath = joinPath(parentPath, route.path);
|
|
67
|
+
if (isLocalisableRoutePath(route.path)) ensureLocalisedUrlsForPath(canonicalPath, languages, localisedUrls);
|
|
68
|
+
if ('children' in route && route.children) route.children.forEach((child)=>visit(child, canonicalPath));
|
|
69
|
+
};
|
|
70
|
+
routes.forEach((route)=>visit(route, ''));
|
|
71
|
+
};
|
|
72
|
+
const getLocalisedRoutePaths = (canonicalPath, parentLocalisedPaths, languages, entry)=>{
|
|
73
|
+
const paths = languages.map((language)=>{
|
|
74
|
+
const fullPath = normalisePathPattern(entry[language]);
|
|
75
|
+
const parentPath = normalisePathPattern(parentLocalisedPaths[language] || '/');
|
|
76
|
+
if ('/' === parentPath) return normaliseRoutePath(fullPath) || void 0;
|
|
77
|
+
const parentPrefix = `${parentPath}/`;
|
|
78
|
+
if (!fullPath.startsWith(parentPrefix)) throw new Error(`localisedUrls["${canonicalPath}"].${language} must be nested under "${parentPath}" because its parent route is localised there.`);
|
|
79
|
+
return normaliseRoutePath(fullPath.slice(parentPath.length));
|
|
80
|
+
});
|
|
81
|
+
return Array.from(new Set(paths.filter(Boolean)));
|
|
82
|
+
};
|
|
83
|
+
const transformLocalisedRoute = (route, parentCanonicalPath, parentLocalisedPaths, languages, localisedUrls)=>{
|
|
84
|
+
const canonicalPath = joinPath(parentCanonicalPath, route.path);
|
|
85
|
+
const localisedUrlEntry = isLocalisableRoutePath(route.path) ? ensureLocalisedUrlsForPath(canonicalPath, languages, localisedUrls) : void 0;
|
|
86
|
+
const routeLocalisedPaths = localisedUrlEntry ? languages.reduce((acc, language)=>{
|
|
87
|
+
acc[language] = normalisePathPattern(localisedUrlEntry[language]);
|
|
88
|
+
return acc;
|
|
89
|
+
}, {}) : parentLocalisedPaths;
|
|
90
|
+
const children = 'children' in route && route.children ? route.children.flatMap((child)=>transformLocalisedRoute(child, canonicalPath, routeLocalisedPaths, languages, localisedUrls)) : void 0;
|
|
91
|
+
const baseRoute = {
|
|
92
|
+
...route,
|
|
93
|
+
...children ? {
|
|
94
|
+
children
|
|
95
|
+
} : {}
|
|
96
|
+
};
|
|
97
|
+
if (!localisedUrlEntry) return [
|
|
98
|
+
baseRoute
|
|
99
|
+
];
|
|
100
|
+
return getLocalisedRoutePaths(canonicalPath, parentLocalisedPaths, languages, localisedUrlEntry).map((localisedPath, index)=>cloneRouteWithLocalisedPath(baseRoute, localisedPath, index, canonicalPath));
|
|
101
|
+
};
|
|
102
|
+
const legalRouteIdPart = (value)=>value.replace(/[^a-zA-Z0-9_$-]+/g, '_').replace(/^_+|_+$/g, '') || 'index';
|
|
103
|
+
const suffixRouteIds = (route, suffix)=>{
|
|
104
|
+
const children = 'children' in route && route.children ? route.children.map((child)=>suffixRouteIds(child, suffix)) : void 0;
|
|
105
|
+
return {
|
|
106
|
+
...route,
|
|
107
|
+
...route.id ? {
|
|
108
|
+
id: `${route.id}__localised_${suffix}`
|
|
109
|
+
} : {},
|
|
110
|
+
...children ? {
|
|
111
|
+
children
|
|
112
|
+
} : {}
|
|
113
|
+
};
|
|
114
|
+
};
|
|
115
|
+
const cloneRouteWithLocalisedPath = (route, path, index, canonicalPath)=>{
|
|
116
|
+
const leadingLocaleParam = getLeadingLocaleParam(route.path);
|
|
117
|
+
const localisedPath = leadingLocaleParam ? normaliseRoutePath(`${leadingLocaleParam}/${path}`) : path;
|
|
118
|
+
const routeWithPath = {
|
|
119
|
+
...route,
|
|
120
|
+
path: localisedPath
|
|
121
|
+
};
|
|
122
|
+
routeWithPath.modernCanonicalPath = canonicalPath;
|
|
123
|
+
return 0 === index ? routeWithPath : suffixRouteIds(routeWithPath, legalRouteIdPart(localisedPath));
|
|
124
|
+
};
|
|
125
|
+
const applyLocalisedUrlsToRoutes = (routes, languages, localisedUrls)=>{
|
|
126
|
+
const rootLocalisedPaths = languages.reduce((acc, language)=>{
|
|
127
|
+
acc[language] = '/';
|
|
128
|
+
return acc;
|
|
129
|
+
}, {});
|
|
130
|
+
validateLocalisedUrls(routes, languages, localisedUrls);
|
|
131
|
+
return routes.flatMap((route)=>transformLocalisedRoute(route, '', rootLocalisedPaths, languages, localisedUrls));
|
|
132
|
+
};
|
|
133
|
+
const escapeRegExp = (value)=>value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
134
|
+
const getParamName = (segment)=>segment.slice(1).replace(/\?$/, '');
|
|
135
|
+
const compiledPathPatternCache = new Map();
|
|
136
|
+
const compilePathPattern = (pattern)=>{
|
|
137
|
+
const normalizedPattern = normalisePathPattern(pattern);
|
|
138
|
+
const cached = compiledPathPatternCache.get(normalizedPattern);
|
|
139
|
+
if (cached) return cached;
|
|
140
|
+
const names = [];
|
|
141
|
+
const segments = normalizedPattern.split('/').filter(Boolean);
|
|
142
|
+
const source = segments.map((segment)=>{
|
|
143
|
+
if (segment.startsWith(':')) {
|
|
144
|
+
names.push(getParamName(segment));
|
|
145
|
+
const paramPattern = '([^/]+)';
|
|
146
|
+
return segment.endsWith('?') ? `(?:/${paramPattern})?` : `/${paramPattern}`;
|
|
147
|
+
}
|
|
148
|
+
if ('*' === segment) {
|
|
149
|
+
names.push('*');
|
|
150
|
+
return '/(.*)';
|
|
151
|
+
}
|
|
152
|
+
return `/${escapeRegExp(segment)}`;
|
|
153
|
+
}).join('');
|
|
154
|
+
const compiled = {
|
|
155
|
+
names,
|
|
156
|
+
regexp: new RegExp(`^${source || '/'}$`)
|
|
157
|
+
};
|
|
158
|
+
compiledPathPatternCache.set(normalizedPattern, compiled);
|
|
159
|
+
return compiled;
|
|
160
|
+
};
|
|
161
|
+
const getPatternSpecificity = (pattern)=>{
|
|
162
|
+
const segments = normalisePathPattern(pattern).split('/').filter(Boolean);
|
|
163
|
+
let staticSegments = 0;
|
|
164
|
+
let dynamicSegments = 0;
|
|
165
|
+
let splatSegments = 0;
|
|
166
|
+
for (const segment of segments)if ('*' === segment) splatSegments++;
|
|
167
|
+
else if (segment.startsWith(':')) dynamicSegments++;
|
|
168
|
+
else staticSegments++;
|
|
169
|
+
return {
|
|
170
|
+
staticSegments,
|
|
171
|
+
dynamicSegments,
|
|
172
|
+
splatSegments,
|
|
173
|
+
totalSegments: segments.length
|
|
174
|
+
};
|
|
175
|
+
};
|
|
176
|
+
const comparePatternSpecificity = (left, right)=>{
|
|
177
|
+
const a = getPatternSpecificity(left);
|
|
178
|
+
const b = getPatternSpecificity(right);
|
|
179
|
+
return b.staticSegments - a.staticSegments || b.totalSegments - a.totalSegments || a.splatSegments - b.splatSegments || a.dynamicSegments - b.dynamicSegments;
|
|
180
|
+
};
|
|
181
|
+
const sortPatternsBySpecificity = (patterns)=>patterns.map((pattern, index)=>({
|
|
182
|
+
pattern,
|
|
183
|
+
index
|
|
184
|
+
})).sort((left, right)=>comparePatternSpecificity(left.pattern.pattern, right.pattern.pattern) || left.index - right.index).map(({ pattern })=>pattern);
|
|
185
|
+
const decodePathParam = (value)=>{
|
|
186
|
+
try {
|
|
187
|
+
return decodeURIComponent(value);
|
|
188
|
+
} catch {
|
|
189
|
+
return null;
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
const matchPathPattern = (pathname, pattern)=>{
|
|
193
|
+
const { names, regexp } = compilePathPattern(pattern);
|
|
194
|
+
const match = regexp.exec(normalisePathname(pathname));
|
|
195
|
+
if (!match) return null;
|
|
196
|
+
const params = {};
|
|
197
|
+
for(let index = 0; index < names.length; index++){
|
|
198
|
+
const decoded = decodePathParam(match[index + 1] || '');
|
|
199
|
+
if (null === decoded) return null;
|
|
200
|
+
params[names[index]] = decoded;
|
|
201
|
+
}
|
|
202
|
+
return params;
|
|
203
|
+
};
|
|
204
|
+
const buildPathFromPattern = (pattern, params)=>{
|
|
205
|
+
const segments = normalisePathPattern(pattern).split('/').filter(Boolean);
|
|
206
|
+
const path = segments.map((segment)=>{
|
|
207
|
+
if (segment.startsWith(':')) {
|
|
208
|
+
const param = params[getParamName(segment)];
|
|
209
|
+
return param ? encodeURIComponent(param) : '';
|
|
210
|
+
}
|
|
211
|
+
if ('*' === segment) return params['*'] || '';
|
|
212
|
+
return segment;
|
|
213
|
+
}).filter(Boolean).join('/');
|
|
214
|
+
return `/${path}`;
|
|
215
|
+
};
|
|
216
|
+
const resolveLocalisedPath = (pathname, targetLanguage, languages, localisedUrls)=>{
|
|
217
|
+
const normalizedPathname = normalisePathname(pathname);
|
|
218
|
+
const canonicalCandidates = sortPatternsBySpecificity(Object.entries(localisedUrls).map(([canonicalPattern, localisedUrlEntry])=>({
|
|
219
|
+
pattern: canonicalPattern,
|
|
220
|
+
canonicalPattern,
|
|
221
|
+
localisedUrlEntry
|
|
222
|
+
})));
|
|
223
|
+
for (const { canonicalPattern, localisedUrlEntry } of canonicalCandidates){
|
|
224
|
+
const targetPattern = localisedUrlEntry[targetLanguage];
|
|
225
|
+
if (!targetPattern) continue;
|
|
226
|
+
const params = matchPathPattern(normalizedPathname, canonicalPattern);
|
|
227
|
+
if (params) return buildPathFromPattern(targetPattern, params);
|
|
228
|
+
}
|
|
229
|
+
const localisedCandidates = sortPatternsBySpecificity(Object.values(localisedUrls).flatMap((localisedUrlEntry)=>{
|
|
230
|
+
const targetPattern = localisedUrlEntry[targetLanguage];
|
|
231
|
+
if (!targetPattern) return [];
|
|
232
|
+
return languages.map((language)=>localisedUrlEntry[language]).filter((sourcePattern)=>Boolean(sourcePattern)).map((sourcePattern)=>({
|
|
233
|
+
pattern: sourcePattern,
|
|
234
|
+
sourcePattern,
|
|
235
|
+
targetPattern
|
|
236
|
+
}));
|
|
237
|
+
}));
|
|
238
|
+
for (const { sourcePattern, targetPattern } of localisedCandidates){
|
|
239
|
+
const params = matchPathPattern(normalizedPathname, sourcePattern);
|
|
240
|
+
if (params) return buildPathFromPattern(targetPattern, params);
|
|
241
|
+
}
|
|
242
|
+
return normalizedPathname;
|
|
243
|
+
};
|
|
244
|
+
const resolveCanonicalLocalisedPath = (pathname, languages, localisedUrls)=>{
|
|
245
|
+
const normalizedPathname = normalisePathname(pathname);
|
|
246
|
+
const canonicalCandidates = sortPatternsBySpecificity(Object.entries(localisedUrls).map(([canonicalPattern, localisedUrlEntry])=>({
|
|
247
|
+
pattern: canonicalPattern,
|
|
248
|
+
canonicalPattern,
|
|
249
|
+
localisedUrlEntry
|
|
250
|
+
})));
|
|
251
|
+
for (const { canonicalPattern, localisedUrlEntry } of canonicalCandidates){
|
|
252
|
+
const canonicalParams = matchPathPattern(normalizedPathname, canonicalPattern);
|
|
253
|
+
if (canonicalParams) return buildPathFromPattern(canonicalPattern, canonicalParams);
|
|
254
|
+
for (const language of languages){
|
|
255
|
+
const sourcePattern = localisedUrlEntry[language];
|
|
256
|
+
if (!sourcePattern) continue;
|
|
257
|
+
const params = matchPathPattern(normalizedPathname, sourcePattern);
|
|
258
|
+
if (params) return buildPathFromPattern(canonicalPattern, params);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
return normalizedPathname;
|
|
262
|
+
};
|
|
263
|
+
const stripLanguagePrefix = (pathname, languages)=>{
|
|
264
|
+
const segments = pathname.split('/').filter(Boolean);
|
|
265
|
+
if (segments.length > 0 && languages.includes(segments[0])) return `/${segments.slice(1).join('/')}`;
|
|
266
|
+
return pathname || '/';
|
|
267
|
+
};
|
|
268
|
+
const localiseTargetPathname = (pathname, language, languages, localisedUrls)=>{
|
|
269
|
+
const pathWithoutLanguage = stripLanguagePrefix(pathname, languages);
|
|
270
|
+
const localisedUrlsConfig = resolveLocalisedUrlsConfig(localisedUrls);
|
|
271
|
+
const resolvedPath = localisedUrlsConfig.enabled ? resolveLocalisedPath(pathWithoutLanguage, language, languages, localisedUrlsConfig.map) : pathWithoutLanguage;
|
|
272
|
+
const resolvedSegments = resolvedPath.split('/').filter(Boolean);
|
|
273
|
+
return `/${[
|
|
274
|
+
language,
|
|
275
|
+
...resolvedSegments
|
|
276
|
+
].join('/')}`;
|
|
277
|
+
};
|
|
278
|
+
const canonicalTargetPathname = (pathname, languages, localisedUrls)=>{
|
|
279
|
+
const pathWithoutLanguage = stripLanguagePrefix(pathname, languages);
|
|
280
|
+
const localisedUrlsConfig = resolveLocalisedUrlsConfig(localisedUrls);
|
|
281
|
+
return localisedUrlsConfig.enabled ? resolveCanonicalLocalisedPath(pathWithoutLanguage, languages, localisedUrlsConfig.map) : pathWithoutLanguage;
|
|
282
|
+
};
|
|
283
|
+
export { applyLocalisedUrlsToRoutes, buildPathFromPattern, canonicalTargetPathname, localiseTargetPathname, matchPathPattern, normalisePathPattern, normalisePathname, resolveCanonicalLocalisedPath, resolveLocalisedPath, resolveLocalisedUrlsConfig, validateLocalisedUrls };
|
|
@@ -2,7 +2,9 @@ import "node:module";
|
|
|
2
2
|
import { getPublicDirRoutePrefixes } from "@modern-js/server-core";
|
|
3
3
|
import fs from "fs";
|
|
4
4
|
import path from "path";
|
|
5
|
+
import { applyLocalisedUrlsToRoutes, resolveLocalisedUrlsConfig } from "../shared/localisedUrls.mjs";
|
|
5
6
|
import { getBackendOptions, getLocaleDetectionOptions } from "../shared/utils.mjs";
|
|
7
|
+
import "../runtime/types.mjs";
|
|
6
8
|
function hasJsonFiles(dirPath) {
|
|
7
9
|
try {
|
|
8
10
|
if (!fs.existsSync(dirPath) || !fs.statSync(dirPath).isDirectory()) return false;
|
|
@@ -81,6 +83,27 @@ const i18nPlugin = (options = {})=>({
|
|
|
81
83
|
plugins
|
|
82
84
|
};
|
|
83
85
|
});
|
|
86
|
+
api.modifyFileSystemRoutes(({ entrypoint, routes })=>{
|
|
87
|
+
if (!localeDetection) return {
|
|
88
|
+
entrypoint,
|
|
89
|
+
routes
|
|
90
|
+
};
|
|
91
|
+
const localeDetectionOptions = getLocaleDetectionOptions(entrypoint.entryName, localeDetection);
|
|
92
|
+
const { localePathRedirect, languages = [], localisedUrls } = localeDetectionOptions;
|
|
93
|
+
if (!localePathRedirect || 0 === languages.length) return {
|
|
94
|
+
entrypoint,
|
|
95
|
+
routes
|
|
96
|
+
};
|
|
97
|
+
const localisedUrlsConfig = resolveLocalisedUrlsConfig(localisedUrls);
|
|
98
|
+
if (!localisedUrlsConfig.enabled) return {
|
|
99
|
+
entrypoint,
|
|
100
|
+
routes
|
|
101
|
+
};
|
|
102
|
+
return {
|
|
103
|
+
entrypoint,
|
|
104
|
+
routes: applyLocalisedUrlsToRoutes(routes, languages, localisedUrlsConfig.map)
|
|
105
|
+
};
|
|
106
|
+
});
|
|
84
107
|
api._internalServerPlugins(({ plugins })=>{
|
|
85
108
|
const { serverRoutes, metaName } = api.getAppContext();
|
|
86
109
|
const normalizedConfig = api.getNormalizedConfig();
|
|
@@ -1,29 +1,14 @@
|
|
|
1
1
|
import "node:module";
|
|
2
2
|
import { jsx } from "react/jsx-runtime";
|
|
3
|
-
import { Link
|
|
4
|
-
|
|
5
|
-
import { buildLocalizedUrl } from "./utils.mjs";
|
|
6
|
-
const useRouterHooks = ()=>{
|
|
7
|
-
const inRouter = useInRouterContext();
|
|
8
|
-
return {
|
|
9
|
-
Link: inRouter ? router_Link : null,
|
|
10
|
-
params: inRouter ? useParams() : {},
|
|
11
|
-
hasRouter: inRouter
|
|
12
|
-
};
|
|
13
|
-
};
|
|
3
|
+
import { Link } from "./Link.mjs";
|
|
4
|
+
let warnedDeprecation = false;
|
|
14
5
|
const I18nLink = ({ to, children, ...props })=>{
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
if ('development' === process.env.NODE_ENV && hasRouter && !params.lang) console.warn("I18nLink is being used outside of a :lang dynamic route context. This may cause unexpected behavior. Please ensure I18nLink is used within a route that has a :lang parameter.");
|
|
20
|
-
if (!hasRouter || !Link) return /*#__PURE__*/ jsx("a", {
|
|
21
|
-
href: localizedTo,
|
|
22
|
-
...props,
|
|
23
|
-
children: children
|
|
24
|
-
});
|
|
6
|
+
if ('development' === process.env.NODE_ENV && !warnedDeprecation) {
|
|
7
|
+
warnedDeprecation = true;
|
|
8
|
+
console.warn("[plugin-i18n] I18nLink is deprecated. Import { Link } from '@modern-js/plugin-i18n/runtime' instead — it accepts the same language-agnostic `to` values.");
|
|
9
|
+
}
|
|
25
10
|
return /*#__PURE__*/ jsx(Link, {
|
|
26
|
-
to:
|
|
11
|
+
to: to,
|
|
27
12
|
...props,
|
|
28
13
|
children: children
|
|
29
14
|
});
|