@bleedingdev/modern-js-plugin-i18n 3.8.3-ultramodern.6 → 3.9.0-ultramodern.10
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 +10 -21
- package/dist/cjs/cli/localisedRoutes.js +46 -0
- package/dist/cjs/runtime/Link.js +19 -32
- package/dist/cjs/runtime/context.js +26 -61
- package/dist/cjs/runtime/contextHelpers.js +5 -5
- package/dist/cjs/runtime/core.js +27 -14
- package/dist/cjs/runtime/hooks.js +7 -8
- package/dist/cjs/runtime/localizedPaths.js +11 -9
- package/dist/cjs/runtime/navigation.js +48 -0
- package/dist/cjs/runtime/providerComposition.js +37 -13
- package/dist/cjs/runtime/routerAdapter.js +35 -140
- package/dist/cjs/runtime/utils.js +21 -19
- package/dist/cjs/server/defaultUrlStrategy.js +48 -0
- package/dist/cjs/server/index.js +11 -7
- package/dist/cjs/server/mappedUrlStrategy.js +43 -0
- package/dist/cjs/server/redirectPolicy.js +33 -24
- package/dist/cjs/shared/mappedUrlStrategy.js +63 -0
- package/dist/cjs/shared/urlStrategy.js +48 -0
- package/dist/esm/cli/index.mjs +10 -21
- package/dist/esm/cli/localisedRoutes.mjs +8 -0
- package/dist/esm/runtime/Link.mjs +19 -32
- package/dist/esm/runtime/context.mjs +18 -62
- package/dist/esm/runtime/contextHelpers.mjs +5 -5
- package/dist/esm/runtime/core.mjs +13 -6
- package/dist/esm/runtime/hooks.mjs +7 -8
- package/dist/esm/runtime/localizedPaths.mjs +11 -9
- package/dist/esm/runtime/navigation.mjs +7 -0
- package/dist/esm/runtime/providerComposition.mjs +38 -14
- package/dist/esm/runtime/routerAdapter.mjs +30 -141
- package/dist/esm/runtime/utils.mjs +19 -17
- package/dist/esm/server/defaultUrlStrategy.mjs +7 -0
- package/dist/esm/server/index.mjs +11 -7
- package/dist/esm/server/mappedUrlStrategy.mjs +1 -0
- package/dist/esm/server/redirectPolicy.mjs +33 -24
- package/dist/esm/shared/mappedUrlStrategy.mjs +19 -0
- package/dist/esm/shared/urlStrategy.mjs +7 -0
- package/dist/esm-node/cli/index.mjs +10 -21
- package/dist/esm-node/cli/localisedRoutes.mjs +9 -0
- package/dist/esm-node/runtime/Link.mjs +19 -32
- package/dist/esm-node/runtime/context.mjs +18 -62
- package/dist/esm-node/runtime/contextHelpers.mjs +5 -5
- package/dist/esm-node/runtime/core.mjs +13 -6
- package/dist/esm-node/runtime/hooks.mjs +7 -8
- package/dist/esm-node/runtime/localizedPaths.mjs +11 -9
- package/dist/esm-node/runtime/navigation.mjs +8 -0
- package/dist/esm-node/runtime/providerComposition.mjs +38 -14
- package/dist/esm-node/runtime/routerAdapter.mjs +30 -141
- package/dist/esm-node/runtime/utils.mjs +19 -17
- package/dist/esm-node/server/defaultUrlStrategy.mjs +8 -0
- package/dist/esm-node/server/index.mjs +11 -7
- package/dist/esm-node/server/mappedUrlStrategy.mjs +2 -0
- package/dist/esm-node/server/redirectPolicy.mjs +33 -24
- package/dist/esm-node/shared/mappedUrlStrategy.mjs +20 -0
- package/dist/esm-node/shared/urlStrategy.mjs +8 -0
- package/dist/types/cli/index.d.ts +2 -2
- package/dist/types/cli/locales.d.ts +1 -1
- package/dist/types/cli/localisedRoutes.d.ts +18 -0
- package/dist/types/runtime/Link.d.ts +2 -2
- package/dist/types/runtime/context.d.ts +10 -13
- package/dist/types/runtime/contextHelpers.d.ts +3 -3
- package/dist/types/runtime/core.d.ts +23 -13
- package/dist/types/runtime/hooks.d.ts +5 -6
- package/dist/types/runtime/i18n/backend/config.d.ts +1 -1
- package/dist/types/runtime/i18n/backend/index.d.ts +2 -2
- package/dist/types/runtime/i18n/backend/middleware.common.d.ts +2 -2
- package/dist/types/runtime/i18n/backend/middleware.d.ts +2 -2
- package/dist/types/runtime/i18n/backend/middleware.node.d.ts +2 -2
- package/dist/types/runtime/i18n/backend/sdk-backend.d.ts +1 -1
- package/dist/types/runtime/i18n/detection/cache.d.ts +1 -1
- package/dist/types/runtime/i18n/detection/config.d.ts +1 -1
- package/dist/types/runtime/i18n/detection/detector.d.ts +2 -2
- package/dist/types/runtime/i18n/detection/index.d.ts +4 -4
- package/dist/types/runtime/i18n/detection/initOptions.d.ts +1 -1
- package/dist/types/runtime/i18n/detection/middleware.d.ts +1 -1
- package/dist/types/runtime/i18n/detection/middleware.node.d.ts +1 -1
- package/dist/types/runtime/i18n/detection/priority.d.ts +2 -2
- package/dist/types/runtime/i18n/detection/types.d.ts +1 -1
- package/dist/types/runtime/i18n/index.d.ts +3 -3
- package/dist/types/runtime/i18n/instance.d.ts +1 -1
- package/dist/types/runtime/i18n/react-i18next.d.ts +1 -1
- package/dist/types/runtime/i18n/utils.d.ts +2 -2
- package/dist/types/runtime/index.d.ts +2 -2
- package/dist/types/runtime/localizedPaths.d.ts +4 -4
- package/dist/types/runtime/navigation.d.ts +10 -0
- package/dist/types/runtime/no-react-i18next.d.ts +2 -2
- package/dist/types/runtime/pluginSetup.d.ts +3 -3
- package/dist/types/runtime/providerComposition.d.ts +15 -3
- package/dist/types/runtime/routerAdapter.d.ts +23 -6
- package/dist/types/runtime/types.d.ts +1 -1
- package/dist/types/runtime/utils.d.ts +4 -12
- package/dist/types/server/defaultUrlStrategy.d.ts +11 -0
- package/dist/types/server/detectorOptions.d.ts +1 -1
- package/dist/types/server/index.d.ts +3 -1
- package/dist/types/server/mappedUrlStrategy.d.ts +7 -0
- package/dist/types/server/redirectPolicy.d.ts +5 -13
- package/dist/types/shared/mappedUrlStrategy.d.ts +27 -0
- package/dist/types/shared/type.d.ts +8 -12
- package/dist/types/shared/urlStrategy.d.ts +18 -0
- package/dist/types/shared/utils.d.ts +1 -1
- package/package.json +30 -18
- package/rstest.config.mts +6 -5
- package/src/cli/index.ts +42 -43
- package/src/cli/localisedRoutes.ts +43 -0
- package/src/runtime/Link.tsx +20 -40
- package/src/runtime/context.tsx +34 -133
- package/src/runtime/contextHelpers.ts +7 -5
- package/src/runtime/core.tsx +53 -6
- package/src/runtime/hooks.ts +14 -9
- package/src/runtime/i18n/instance.ts +1 -2
- package/src/runtime/localizedPaths.ts +24 -19
- package/src/runtime/navigation.tsx +20 -0
- package/src/runtime/providerComposition.tsx +71 -23
- package/src/runtime/routerAdapter.tsx +73 -338
- package/src/runtime/utils.ts +49 -31
- package/src/server/defaultUrlStrategy.ts +27 -0
- package/src/server/index.ts +39 -9
- package/src/server/mappedUrlStrategy.ts +10 -0
- package/src/server/redirectPolicy.ts +85 -70
- package/src/shared/mappedUrlStrategy.ts +59 -0
- package/src/shared/type.ts +7 -11
- package/src/shared/urlStrategy.ts +38 -0
- package/tests/backendDefaults.test.ts +2 -23
- package/tests/defaultUrlStrategy.test.ts +107 -0
- package/tests/federatedI18nBoundaryResolver.test.tsx +65 -0
- package/tests/i18nUtils.test.ts +31 -65
- package/tests/link.test.tsx +13 -511
- package/tests/localisedRouteGeneration.test.ts +159 -0
- package/tests/localisedUrls.test.ts +16 -208
- package/tests/mappedUrlStrategyDerivation.test.ts +62 -0
- package/tests/navigation.test.tsx +365 -0
- package/tests/reactI18nextRuntimeBoundary.test.ts +1 -82
- package/tests/redirectPolicy.test.ts +89 -27
- package/tests/routerAdapter.test.tsx +98 -1171
- package/tests/urlStrategySerialization.test.ts +122 -0
- package/tests/federatedI18nBoundary.client.test.tsx +0 -129
- package/tests/federatedI18nBoundary.test.tsx +0 -132
- package/tests/linkTypes.test.ts +0 -59
- package/tests/localisedUrlRewriteMatrix.fork.test.ts +0 -147
- package/tests/peerCohort.test.ts +0 -25
- package/tests/type-fixture/fixture-globals.d.ts +0 -11
- package/tests/type-fixture/i18nInstanceTypes.fixture.ts +0 -29
- package/tests/type-fixture/linkTypes.fixture.tsx +0 -51
- package/tests/type-fixture/tsconfig.json +0 -16
|
@@ -1,150 +1,39 @@
|
|
|
1
|
+
import { jsx } from "react/jsx-runtime";
|
|
1
2
|
import { RuntimeContext, isBrowser } from "@modern-js/runtime";
|
|
2
|
-
import { InternalRuntimeContext
|
|
3
|
-
import {
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
return {
|
|
13
|
-
pathname: locationValue.pathname,
|
|
14
|
-
search: normalizeUrlPart('string' == typeof locationValue.search ? locationValue.search : locationValue.searchStr, '?'),
|
|
15
|
-
hash: normalizeUrlPart(locationValue.hash, '#')
|
|
16
|
-
};
|
|
17
|
-
};
|
|
18
|
-
const getWindowLocation = ()=>{
|
|
19
|
-
if (!isBrowser()) return null;
|
|
20
|
-
return {
|
|
21
|
-
pathname: window.location.pathname,
|
|
22
|
-
search: window.location.search,
|
|
23
|
-
hash: window.location.hash
|
|
24
|
-
};
|
|
25
|
-
};
|
|
26
|
-
const getRouterFramework = (runtimeContext, internalContext, inReactRouter)=>{
|
|
27
|
-
const framework = getRouterRuntimeState(internalContext)?.framework || getRouterRuntimeState(runtimeContext)?.framework;
|
|
28
|
-
if (framework) return framework;
|
|
29
|
-
if (internalContext.router?.useRouter || runtimeContext.router?.useRouter) return 'tanstack';
|
|
30
|
-
if (internalContext.router?.useLocation || internalContext.router?.useHref || runtimeContext.router?.useLocation || runtimeContext.router?.useHref) return 'react-router';
|
|
31
|
-
if (inReactRouter) return 'react-router';
|
|
32
|
-
};
|
|
33
|
-
const getRouterInstance = (internalContext, contextRouter)=>{
|
|
34
|
-
if (contextRouter) return contextRouter;
|
|
35
|
-
const router = getRouterRuntimeState(internalContext)?.instance;
|
|
36
|
-
if (!router || 'object' != typeof router) return null;
|
|
37
|
-
return router;
|
|
38
|
-
};
|
|
39
|
-
const getRouterStateLocation = (internalContext, contextRouter)=>{
|
|
40
|
-
const router = getRouterInstance(internalContext, contextRouter);
|
|
41
|
-
return normalizeLocation(router?.stores?.location?.get?.()) || normalizeLocation(router?.state?.location);
|
|
42
|
-
};
|
|
43
|
-
const getRouterParams = (internalContext, contextRouter)=>{
|
|
44
|
-
const router = getRouterInstance(internalContext, contextRouter);
|
|
45
|
-
const matches = router?.stores?.matches?.get?.() || router?.state?.matches;
|
|
46
|
-
if (!Array.isArray(matches)) return {};
|
|
47
|
-
return matches.reduce((params, match)=>{
|
|
48
|
-
if (match?.params) Object.assign(params, match.params);
|
|
49
|
-
return params;
|
|
50
|
-
}, {});
|
|
51
|
-
};
|
|
52
|
-
const getRouterSnapshot = (internalContext, contextRouter)=>{
|
|
53
|
-
const location = getRouterStateLocation(internalContext, contextRouter);
|
|
54
|
-
const params = getRouterParams(internalContext, contextRouter);
|
|
55
|
-
return JSON.stringify([
|
|
56
|
-
location?.pathname ?? '',
|
|
57
|
-
location?.search ?? '',
|
|
58
|
-
location?.hash ?? '',
|
|
59
|
-
Object.entries(params).sort(([left], [right])=>left.localeCompare(right))
|
|
60
|
-
]);
|
|
61
|
-
};
|
|
62
|
-
const useI18nRouterAdapter = ()=>{
|
|
3
|
+
import { InternalRuntimeContext } from "@modern-js/runtime/context";
|
|
4
|
+
import { createContext, useContext } from "react";
|
|
5
|
+
const navigationContextKey = Symbol.for('@modern-js/plugin-i18n/runtime/I18nNavigationContext');
|
|
6
|
+
const contextStore = globalThis;
|
|
7
|
+
const I18nNavigationContext = contextStore[navigationContextKey] ??= /*#__PURE__*/ createContext(null);
|
|
8
|
+
const I18nNavigationProvider = ({ children, value })=>/*#__PURE__*/ jsx(I18nNavigationContext.Provider, {
|
|
9
|
+
value: value,
|
|
10
|
+
children: children
|
|
11
|
+
});
|
|
12
|
+
const useNativeI18nRouterAdapter = ()=>{
|
|
63
13
|
const runtimeContext = useContext(RuntimeContext);
|
|
64
14
|
const internalContext = useContext(InternalRuntimeContext);
|
|
65
15
|
const routerApi = internalContext.router || runtimeContext.router;
|
|
66
|
-
const
|
|
67
|
-
const
|
|
68
|
-
const
|
|
69
|
-
const
|
|
70
|
-
const
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
}
|
|
75
|
-
const hasRouter = 'tanstack' === framework || 'react-router' === framework || Boolean(reactRouterNavigate);
|
|
76
|
-
const subscribeToRouter = useCallback((update)=>{
|
|
77
|
-
const router = getRouterInstance(internalContext, contextRouter);
|
|
78
|
-
if (!router) return ()=>void 0;
|
|
79
|
-
const unsubscribers = [];
|
|
80
|
-
if ('react-router' === framework && !inReactRouter && 'function' == typeof router.subscribe) {
|
|
81
|
-
const subscribe = router.subscribe;
|
|
82
|
-
const unsubscribe = subscribe.call(router, update);
|
|
83
|
-
if ('function' == typeof unsubscribe) unsubscribers.push(unsubscribe);
|
|
84
|
-
}
|
|
85
|
-
if ('tanstack' === framework && 'function' == typeof router.stores?.location?.subscribe) {
|
|
86
|
-
const unsubscribe = router.stores.location.subscribe(update);
|
|
87
|
-
if ('function' == typeof unsubscribe) unsubscribers.push(unsubscribe);
|
|
88
|
-
}
|
|
89
|
-
if ('tanstack' === framework && 'function' == typeof router.subscribe) {
|
|
90
|
-
const subscribe = router.subscribe;
|
|
91
|
-
for (const eventType of [
|
|
92
|
-
'onBeforeNavigate',
|
|
93
|
-
'onBeforeLoad'
|
|
94
|
-
]){
|
|
95
|
-
const unsubscribe = subscribe.call(router, eventType, update);
|
|
96
|
-
if ('function' == typeof unsubscribe) unsubscribers.push(unsubscribe);
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
return ()=>{
|
|
100
|
-
for (const unsubscribe of unsubscribers)unsubscribe();
|
|
101
|
-
};
|
|
102
|
-
}, [
|
|
103
|
-
contextRouter,
|
|
104
|
-
framework,
|
|
105
|
-
inReactRouter,
|
|
106
|
-
internalContext
|
|
107
|
-
]);
|
|
108
|
-
const getSnapshot = useCallback(()=>getRouterSnapshot(internalContext, contextRouter), [
|
|
109
|
-
contextRouter,
|
|
110
|
-
internalContext
|
|
111
|
-
]);
|
|
112
|
-
useSyncExternalStore(subscribeToRouter, getSnapshot, getSnapshot);
|
|
113
|
-
const navigate = useCallback((href, options)=>{
|
|
114
|
-
const router = getRouterInstance(internalContext, contextRouter);
|
|
115
|
-
const activeFramework = getRouterFramework(runtimeContext, internalContext, inReactRouter);
|
|
116
|
-
if ('tanstack' === activeFramework) {
|
|
117
|
-
if ('function' == typeof router?.navigate) return router.navigate({
|
|
118
|
-
to: href,
|
|
119
|
-
replace: options?.replace,
|
|
120
|
-
...options?.state === void 0 ? {} : {
|
|
121
|
-
state: options.state
|
|
122
|
-
}
|
|
123
|
-
});
|
|
124
|
-
throw new Error('TanStack router instance is not available.');
|
|
125
|
-
}
|
|
126
|
-
if (reactRouterNavigate) return reactRouterNavigate(href, options);
|
|
127
|
-
if ('react-router' === activeFramework) {
|
|
128
|
-
if ('function' == typeof router?.navigate) return router.navigate(href, options);
|
|
129
|
-
throw new Error('React Router instance is not available.');
|
|
130
|
-
}
|
|
131
|
-
}, [
|
|
132
|
-
contextRouter,
|
|
133
|
-
internalContext,
|
|
134
|
-
inReactRouter,
|
|
135
|
-
reactRouterNavigate,
|
|
136
|
-
runtimeContext
|
|
137
|
-
]);
|
|
138
|
-
const location = (reactRouterLocation ? normalizeLocation(reactRouterLocation) : getRouterStateLocation(internalContext, contextRouter)) || getWindowLocation();
|
|
139
|
-
const params = inReactRouter ? reactRouterParams : getRouterParams(internalContext, contextRouter);
|
|
140
|
-
const Link = 'tanstack' === framework ? internalContext.router?.Link || runtimeContext.router?.Link || null : 'react-router' === framework || inReactRouter ? routerApi?.Link ?? null : null;
|
|
16
|
+
const hasRouter = routerApi?.useInRouterContext?.() ?? false;
|
|
17
|
+
const navigate = hasRouter ? routerApi?.useNavigate?.() ?? null : null;
|
|
18
|
+
const routerLocation = hasRouter ? routerApi?.useLocation?.() ?? null : null;
|
|
19
|
+
const params = hasRouter ? routerApi?.useParams?.() ?? {} : {};
|
|
20
|
+
const location = routerLocation ?? (isBrowser() ? {
|
|
21
|
+
pathname: window.location.pathname,
|
|
22
|
+
search: window.location.search,
|
|
23
|
+
hash: window.location.hash
|
|
24
|
+
} : null);
|
|
141
25
|
return {
|
|
142
|
-
framework,
|
|
26
|
+
framework: hasRouter ? 'react-router' : void 0,
|
|
143
27
|
hasRouter,
|
|
28
|
+
navigate,
|
|
144
29
|
location,
|
|
145
|
-
|
|
146
|
-
Link
|
|
147
|
-
params
|
|
30
|
+
params,
|
|
31
|
+
Link: hasRouter ? routerApi?.Link ?? null : null
|
|
148
32
|
};
|
|
149
33
|
};
|
|
150
|
-
|
|
34
|
+
const useI18nRouterAdapter = ()=>{
|
|
35
|
+
const provided = useContext(I18nNavigationContext);
|
|
36
|
+
const native = useNativeI18nRouterAdapter();
|
|
37
|
+
return provided ?? native;
|
|
38
|
+
};
|
|
39
|
+
export { I18nNavigationProvider, useI18nRouterAdapter, useNativeI18nRouterAdapter };
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import { localiseTargetPathname, shouldSkipLocaleRedirect } from "@modern-js/i18n-runtime-extensions";
|
|
2
1
|
import { isBrowser } from "@modern-js/runtime";
|
|
3
2
|
import { getGlobalBasename } from "@modern-js/runtime/context";
|
|
3
|
+
import { splitUrlTarget } from "@modern-js/runtime-utils/url";
|
|
4
|
+
import { isDefaultLocaleRedirectSkipPath } from "../shared/mappedUrlStrategy.mjs";
|
|
5
|
+
import { asI18nUrlStrategy } from "../shared/urlStrategy.mjs";
|
|
4
6
|
const getPathname = (context)=>{
|
|
5
7
|
if (isBrowser()) return window.location.pathname;
|
|
6
8
|
return context.ssrContext?.request?.pathname || '/';
|
|
@@ -16,22 +18,14 @@ const getLanguageFromPath = (pathname, languages, fallbackLanguage)=>{
|
|
|
16
18
|
if (languages.includes(firstSegment)) return firstSegment;
|
|
17
19
|
return fallbackLanguage;
|
|
18
20
|
};
|
|
19
|
-
const
|
|
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)=>{
|
|
21
|
+
const buildLocalizedUrl = (target, language, languages, urlStrategy)=>{
|
|
33
22
|
const { pathname, search, hash } = splitUrlTarget(target);
|
|
34
|
-
const
|
|
23
|
+
const strategy = asI18nUrlStrategy(urlStrategy);
|
|
24
|
+
if (strategy) return `${strategy.localizePathname(pathname, language, languages)}${search}${hash}`;
|
|
25
|
+
const segments = pathname.split('/').filter(Boolean);
|
|
26
|
+
if (segments[0] && languages.some((item)=>item.toLowerCase() === segments[0].toLowerCase())) segments[0] = language;
|
|
27
|
+
else segments.unshift(language);
|
|
28
|
+
const localizedPathname = `/${segments.join('/')}`;
|
|
35
29
|
return `${localizedPathname}${search}${hash}`;
|
|
36
30
|
};
|
|
37
31
|
const detectLanguageFromPath = (pathname, languages, localePathRedirect)=>{
|
|
@@ -51,5 +45,13 @@ const detectLanguageFromPath = (pathname, languages, localePathRedirect)=>{
|
|
|
51
45
|
detected: false
|
|
52
46
|
};
|
|
53
47
|
};
|
|
54
|
-
const shouldIgnoreRedirect = (pathname, languages, ignoreRedirectRoutes
|
|
48
|
+
const shouldIgnoreRedirect = (pathname, languages, ignoreRedirectRoutes, urlStrategy)=>{
|
|
49
|
+
if (isDefaultLocaleRedirectSkipPath(pathname, languages)) return true;
|
|
50
|
+
if (asI18nUrlStrategy(urlStrategy)?.shouldSkipRedirect?.(pathname, languages)) return true;
|
|
51
|
+
if (!ignoreRedirectRoutes) return false;
|
|
52
|
+
const segments = pathname.split('/').filter(Boolean);
|
|
53
|
+
if (segments[0] && languages.some((item)=>item.toLowerCase() === segments[0].toLowerCase())) segments.shift();
|
|
54
|
+
const normalizedPath = `/${segments.join('/')}`;
|
|
55
|
+
return 'function' == typeof ignoreRedirectRoutes ? ignoreRedirectRoutes(normalizedPath) : ignoreRedirectRoutes.some((pattern)=>normalizedPath === pattern || normalizedPath.startsWith(`${pattern}/`));
|
|
56
|
+
};
|
|
55
57
|
export { buildLocalizedUrl, detectLanguageFromPath, getEntryPath, getLanguageFromPath, getPathname, shouldIgnoreRedirect, splitUrlTarget };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { createMappedUrlStrategy } from "./mappedUrlStrategy.mjs";
|
|
2
|
+
const createDefaultUrlStrategy = (localisedUrls)=>createMappedUrlStrategy(localisedUrls);
|
|
3
|
+
const resolveDefaultUrlStrategy = (localisedUrls)=>{
|
|
4
|
+
if (!localisedUrls || 'object' != typeof localisedUrls || 0 === Object.keys(localisedUrls).length) return;
|
|
5
|
+
return createDefaultUrlStrategy(localisedUrls);
|
|
6
|
+
};
|
|
7
|
+
export { createDefaultUrlStrategy, resolveDefaultUrlStrategy };
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { asI18nUrlStrategy } from "../shared/urlStrategy.mjs";
|
|
2
2
|
import { getLocaleDetectionOptions } from "../shared/utils.mjs";
|
|
3
3
|
import { collectApiPrefixes, matchesApiPrefix } from "./apiPrefix.mjs";
|
|
4
|
+
import { resolveDefaultUrlStrategy } from "./defaultUrlStrategy.mjs";
|
|
4
5
|
import { convertToHonoLanguageDetectorOptions } from "./detectorOptions.mjs";
|
|
5
6
|
import { buildLocalizedUrl, createLocaleRedirectResponse, getLanguageFromPath, isStaticResourceRequest, shouldIgnoreRedirect } from "./redirectPolicy.mjs";
|
|
6
7
|
import * as __rspack_external__modern_js_server_core_hono_0af3baa7 from "@modern-js/server-core/hono";
|
|
@@ -24,7 +25,10 @@ const i18nServerPlugin = (options)=>({
|
|
|
24
25
|
const { entryName } = route;
|
|
25
26
|
if (!entryName) return;
|
|
26
27
|
if (!options.localeDetection) return;
|
|
27
|
-
const
|
|
28
|
+
const entryLocaleDetection = getLocaleDetectionOptions(entryName, options.localeDetection);
|
|
29
|
+
const { localePathRedirect, i18nextDetector = true, languages = [], fallbackLanguage = 'en', detection, ignoreRedirectRoutes } = entryLocaleDetection;
|
|
30
|
+
const legacyLocalisedUrls = Reflect.get(entryLocaleDetection, 'localisedUrls');
|
|
31
|
+
const urlStrategy = asI18nUrlStrategy(options.resolveUrlStrategy?.(entryName)) ?? resolveDefaultUrlStrategy(legacyLocalisedUrls);
|
|
28
32
|
const staticRoutePrefixes = options.staticRoutePrefixes;
|
|
29
33
|
const originUrlPath = route.urlPath;
|
|
30
34
|
const urlPath = originUrlPath.endsWith('/') ? `${originUrlPath}*` : `${originUrlPath}/*`;
|
|
@@ -39,6 +43,7 @@ const i18nServerPlugin = (options)=>({
|
|
|
39
43
|
const url = new URL(c.req.url);
|
|
40
44
|
const pathname = url.pathname;
|
|
41
45
|
if (matchesApiPrefix(pathname, apiPrefixes)) return await next();
|
|
46
|
+
if (shouldIgnoreRedirect(pathname, urlPath, ignoreRedirectRoutes, urlStrategy, languages)) return await next();
|
|
42
47
|
if (isStaticResourceRequest(pathname, staticRoutePrefixes, languages)) return await next();
|
|
43
48
|
if ('/' === originUrlPath) {
|
|
44
49
|
const pathSegments = pathname.split('/').filter(Boolean);
|
|
@@ -59,7 +64,7 @@ const i18nServerPlugin = (options)=>({
|
|
|
59
64
|
const pathname = url.pathname;
|
|
60
65
|
if (matchesApiPrefix(pathname, apiPrefixes)) return await next();
|
|
61
66
|
if (isStaticResourceRequest(pathname, staticRoutePrefixes, languages)) return await next();
|
|
62
|
-
if (shouldIgnoreRedirect(pathname, urlPath, ignoreRedirectRoutes)) return await next();
|
|
67
|
+
if (shouldIgnoreRedirect(pathname, urlPath, ignoreRedirectRoutes, urlStrategy, languages)) return await next();
|
|
63
68
|
if ('/' === originUrlPath) {
|
|
64
69
|
const pathSegments = pathname.split('/').filter(Boolean);
|
|
65
70
|
if (pathSegments.length > 0) {
|
|
@@ -72,12 +77,11 @@ const i18nServerPlugin = (options)=>({
|
|
|
72
77
|
let detectedLanguage = null;
|
|
73
78
|
if (i18nextDetector) detectedLanguage = c.get('language') || null;
|
|
74
79
|
const targetLanguage = detectedLanguage || fallbackLanguage;
|
|
75
|
-
const localizedUrl = buildLocalizedUrl(c.req, originUrlPath, targetLanguage, languages,
|
|
80
|
+
const localizedUrl = buildLocalizedUrl(c.req, originUrlPath, targetLanguage, languages, urlStrategy);
|
|
76
81
|
return createLocaleRedirectResponse(localizedUrl);
|
|
77
82
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
const expectedUrl = buildLocalizedUrl(c.req, originUrlPath, language, languages, localisedUrls);
|
|
83
|
+
if (urlStrategy) {
|
|
84
|
+
const expectedUrl = buildLocalizedUrl(c.req, originUrlPath, language, languages, urlStrategy);
|
|
81
85
|
if (expectedUrl !== `${pathname}${url.search}${url.hash}`) return createLocaleRedirectResponse(expectedUrl);
|
|
82
86
|
}
|
|
83
87
|
await next();
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { createMappedUrlStrategy, resolveMappedUrlStrategy } from "../shared/mappedUrlStrategy.mjs";
|
|
@@ -1,39 +1,48 @@
|
|
|
1
|
-
import { isDefaultLocaleRedirectSkipPath
|
|
1
|
+
import { isDefaultLocaleRedirectSkipPath } from "../shared/mappedUrlStrategy.mjs";
|
|
2
|
+
import { asI18nUrlStrategy } from "../shared/urlStrategy.mjs";
|
|
2
3
|
const stripUrlPathPrefix = (pathname, urlPath)=>{
|
|
3
4
|
const basePath = urlPath.replace('/*', '');
|
|
4
5
|
if (!basePath || '/' === basePath) return pathname;
|
|
5
|
-
|
|
6
|
-
return remainingPath || '/';
|
|
6
|
+
return pathname === basePath || pathname.startsWith(`${basePath}/`) ? pathname.slice(basePath.length) || '/' : pathname;
|
|
7
7
|
};
|
|
8
|
-
const shouldIgnoreRedirect = (pathname, urlPath, ignoreRedirectRoutes)=>{
|
|
8
|
+
const shouldIgnoreRedirect = (pathname, urlPath, ignoreRedirectRoutes, urlStrategy, languages = [])=>{
|
|
9
9
|
const remainingPath = stripUrlPathPrefix(pathname, urlPath);
|
|
10
|
-
|
|
10
|
+
if (isDefaultLocaleRedirectSkipPath(remainingPath, languages)) return true;
|
|
11
|
+
if (asI18nUrlStrategy(urlStrategy)?.shouldSkipRedirect?.(remainingPath, languages)) return true;
|
|
12
|
+
if (!ignoreRedirectRoutes) return false;
|
|
13
|
+
const segments = remainingPath.split('/').filter(Boolean);
|
|
14
|
+
if (segments[0] && languages.some((language)=>language.toLowerCase() === segments[0].toLowerCase())) segments.shift();
|
|
15
|
+
const normalizedPath = `/${segments.join('/')}`;
|
|
16
|
+
return 'function' == typeof ignoreRedirectRoutes ? ignoreRedirectRoutes(normalizedPath) : ignoreRedirectRoutes.some((pattern)=>normalizedPath === pattern || normalizedPath.startsWith(`${pattern}/`));
|
|
11
17
|
};
|
|
12
18
|
const isStaticResourceRequest = (pathname, staticRoutePrefixes, languages = [])=>{
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
19
|
+
const prefixes = [
|
|
20
|
+
...staticRoutePrefixes,
|
|
21
|
+
'/static',
|
|
22
|
+
'/upload'
|
|
23
|
+
];
|
|
24
|
+
const matches = (target)=>prefixes.some((prefix)=>target === prefix || target.startsWith(`${prefix}/`));
|
|
25
|
+
if (matches(pathname)) return true;
|
|
26
|
+
const segments = pathname.split('/').filter(Boolean);
|
|
27
|
+
if (segments[0] && languages.some((language)=>language.toLowerCase() === segments[0].toLowerCase())) return matches(`/${segments.slice(1).join('/')}`);
|
|
28
|
+
return false;
|
|
18
29
|
};
|
|
19
30
|
const getLanguageFromPath = (req, urlPath, languages)=>{
|
|
20
|
-
const
|
|
21
|
-
const
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
const firstSegment = segments[0];
|
|
25
|
-
if (languages.includes(firstSegment)) return firstSegment;
|
|
26
|
-
return null;
|
|
31
|
+
const host = req.header?.().host;
|
|
32
|
+
const url = new URL(req.url, host ? `http://${host}` : void 0);
|
|
33
|
+
const firstSegment = stripUrlPathPrefix(url.pathname, urlPath).split('/').filter(Boolean)[0];
|
|
34
|
+
return languages.find((language)=>language.toLowerCase() === firstSegment?.toLowerCase()) ?? null;
|
|
27
35
|
};
|
|
28
|
-
const buildLocalizedUrl = (req, urlPath, language, languages,
|
|
36
|
+
const buildLocalizedUrl = (req, urlPath, language, languages, urlStrategy)=>{
|
|
29
37
|
const url = new URL(req.url);
|
|
30
|
-
const pathname = url.pathname;
|
|
31
38
|
const basePath = urlPath.replace('/*', '');
|
|
32
|
-
const remainingPath = stripUrlPathPrefix(pathname, urlPath);
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
39
|
+
const remainingPath = stripUrlPathPrefix(url.pathname, urlPath);
|
|
40
|
+
const segments = remainingPath.split('/').filter(Boolean);
|
|
41
|
+
if (segments[0] && languages.some((item)=>item.toLowerCase() === segments[0].toLowerCase())) segments[0] = language;
|
|
42
|
+
else segments.unshift(language);
|
|
43
|
+
const strategy = asI18nUrlStrategy(urlStrategy);
|
|
44
|
+
const pathname = strategy ? strategy.localizePathname(remainingPath, language, languages) : `/${segments.join('/')}`;
|
|
45
|
+
return `${'/' === basePath ? '' : basePath}${pathname}${url.search}${url.hash}`;
|
|
37
46
|
};
|
|
38
47
|
const createLocaleRedirectResponse = (location)=>new Response(null, {
|
|
39
48
|
status: 302,
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { canonicalTargetPathname, isDefaultLocaleRedirectSkipPath, localiseTargetPathname } from "@modern-js/i18n-runtime-extensions";
|
|
2
|
+
const createMappedUrlStrategy = (localisedUrls)=>({
|
|
3
|
+
localizePathname: (pathname, language, languages)=>localiseTargetPathname(pathname, language, [
|
|
4
|
+
...languages
|
|
5
|
+
], localisedUrls),
|
|
6
|
+
canonicalPathname: (pathname, languages)=>canonicalTargetPathname(pathname, [
|
|
7
|
+
...languages
|
|
8
|
+
], localisedUrls),
|
|
9
|
+
shouldSkipRedirect: (pathname, languages)=>isDefaultLocaleRedirectSkipPath(pathname, [
|
|
10
|
+
...languages
|
|
11
|
+
])
|
|
12
|
+
});
|
|
13
|
+
const resolveMappedUrlStrategy = (localeDetection)=>{
|
|
14
|
+
if (!localeDetection || 'object' != typeof localeDetection) return;
|
|
15
|
+
const localisedUrls = Reflect.get(localeDetection, 'localisedUrls');
|
|
16
|
+
if (!localisedUrls || 'object' != typeof localisedUrls || 0 === Object.keys(localisedUrls).length) return;
|
|
17
|
+
return createMappedUrlStrategy(localisedUrls);
|
|
18
|
+
};
|
|
19
|
+
export { createMappedUrlStrategy, isDefaultLocaleRedirectSkipPath, resolveMappedUrlStrategy };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
const asI18nUrlStrategy = (candidate)=>{
|
|
2
|
+
if (!candidate || 'object' != typeof candidate) return;
|
|
3
|
+
const strategy = candidate;
|
|
4
|
+
return 'function' == typeof strategy.localizePathname && 'function' == typeof strategy.canonicalPathname ? strategy : void 0;
|
|
5
|
+
};
|
|
6
|
+
const isUnusableI18nUrlStrategy = (candidate)=>Boolean(candidate) && 'object' == typeof candidate && void 0 === asI18nUrlStrategy(candidate);
|
|
7
|
+
export { asI18nUrlStrategy, isUnusableI18nUrlStrategy };
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import "node:module";
|
|
2
|
-
import { applyLocalisedUrlsToRoutes, resolveLocalisedUrlsConfig } from "@modern-js/i18n-runtime-extensions";
|
|
3
2
|
import { getPublicDirRoutePrefixes } from "@modern-js/server-core";
|
|
3
|
+
import { logger } from "@modern-js/utils";
|
|
4
|
+
import { isUnusableI18nUrlStrategy } from "../shared/urlStrategy.mjs";
|
|
4
5
|
import { getBackendOptions, getLocaleDetectionOptions } from "../shared/utils.mjs";
|
|
5
6
|
import { applyDetectedBackendPaths, detectLocalesDirectory } from "./locales.mjs";
|
|
7
|
+
import { applyLocalisedRoutes } from "./localisedRoutes.mjs";
|
|
6
8
|
import "../runtime/types.mjs";
|
|
7
9
|
const i18nPlugin = (options = {})=>({
|
|
8
10
|
name: '@modern-js/plugin-i18n',
|
|
@@ -32,6 +34,10 @@ const i18nPlugin = (options = {})=>({
|
|
|
32
34
|
backend: backendOptions,
|
|
33
35
|
...extendedConfig
|
|
34
36
|
};
|
|
37
|
+
if (isUnusableI18nUrlStrategy(config.urlStrategy)) {
|
|
38
|
+
delete config.urlStrategy;
|
|
39
|
+
logger.warn("[i18n] `urlStrategy` cannot be passed through `modern.config.ts`: the generated runtime registration carries plugin options as JSON, which drops its methods. Configure `localeDetection.localisedUrls` instead — the runtime and the server both derive the URL policy from it — or register a runtime plugin that supplies the strategy directly.");
|
|
40
|
+
}
|
|
35
41
|
const { reactI18next } = extendedConfig;
|
|
36
42
|
const runtimePluginPath = customPlugin?.runtime?.path || (false === reactI18next ? `@${metaName}/plugin-i18n/runtime/no-react-i18next` : `@${metaName}/plugin-i18n/runtime`);
|
|
37
43
|
plugins.push({
|
|
@@ -44,27 +50,10 @@ const i18nPlugin = (options = {})=>({
|
|
|
44
50
|
plugins
|
|
45
51
|
};
|
|
46
52
|
});
|
|
47
|
-
api.modifyFileSystemRoutes(({ entrypoint, routes })=>{
|
|
48
|
-
if (!localeDetection) return {
|
|
53
|
+
api.modifyFileSystemRoutes(({ entrypoint, routes })=>({
|
|
49
54
|
entrypoint,
|
|
50
|
-
routes
|
|
51
|
-
};
|
|
52
|
-
const localeDetectionOptions = getLocaleDetectionOptions(entrypoint.entryName, localeDetection);
|
|
53
|
-
const { localePathRedirect, languages = [], localisedUrls } = localeDetectionOptions;
|
|
54
|
-
if (!localePathRedirect || 0 === languages.length) return {
|
|
55
|
-
entrypoint,
|
|
56
|
-
routes
|
|
57
|
-
};
|
|
58
|
-
const localisedUrlsConfig = resolveLocalisedUrlsConfig(localisedUrls);
|
|
59
|
-
if (!localisedUrlsConfig.enabled) return {
|
|
60
|
-
entrypoint,
|
|
61
|
-
routes
|
|
62
|
-
};
|
|
63
|
-
return {
|
|
64
|
-
entrypoint,
|
|
65
|
-
routes: applyLocalisedUrlsToRoutes(routes, languages, localisedUrlsConfig.map)
|
|
66
|
-
};
|
|
67
|
-
});
|
|
55
|
+
routes: applyLocalisedRoutes(routes, localeDetection ? getLocaleDetectionOptions(entrypoint.entryName, localeDetection) : void 0)
|
|
56
|
+
}));
|
|
68
57
|
api._internalServerPlugins(({ plugins })=>{
|
|
69
58
|
const { serverRoutes, metaName } = api.getAppContext();
|
|
70
59
|
const normalizedConfig = api.getNormalizedConfig();
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
import { applyLocalisedUrlsToRoutes, resolveLocalisedUrlsConfig } from "@modern-js/i18n-runtime-extensions";
|
|
3
|
+
const applyLocalisedRoutes = (routes, localeDetection)=>{
|
|
4
|
+
const { localePathRedirect, languages = [], localisedUrls } = localeDetection ?? {};
|
|
5
|
+
const resolved = resolveLocalisedUrlsConfig(localisedUrls);
|
|
6
|
+
if (!localePathRedirect || !languages.length || !resolved.enabled) return routes;
|
|
7
|
+
return applyLocalisedUrlsToRoutes(routes, languages, resolved.map);
|
|
8
|
+
};
|
|
9
|
+
export { applyLocalisedRoutes };
|
|
@@ -10,10 +10,10 @@ const EXTERNAL_TARGET_RE = /^(?:[a-z][a-z0-9+.-]*:|\/\/)/i;
|
|
|
10
10
|
const Link = (props)=>{
|
|
11
11
|
const { to, params, children, hash: hashProp, search: searchProp, hashScrollIntoView, activeOptions, activeProps, prefetch, preload, ...rest } = props;
|
|
12
12
|
const adapter = useI18nRouterAdapter();
|
|
13
|
-
const { language, supportedLanguages,
|
|
13
|
+
const { language, supportedLanguages, urlStrategy } = useModernI18n();
|
|
14
14
|
const config = {
|
|
15
15
|
languages: supportedLanguages,
|
|
16
|
-
|
|
16
|
+
urlStrategy
|
|
17
17
|
};
|
|
18
18
|
const isExternal = EXTERNAL_TARGET_RE.test(to);
|
|
19
19
|
const isBareHash = to.startsWith('#');
|
|
@@ -23,7 +23,7 @@ const Link = (props)=>{
|
|
|
23
23
|
const interpolated = interpolateRouteParams(pathname || '/', params);
|
|
24
24
|
const firstSegment = interpolated.split('/').filter(Boolean)[0];
|
|
25
25
|
if (firstSegment && supportedLanguages.includes(firstSegment)) warnOnce(`lang-prefix:${to}`, `[plugin-i18n] <Link to="${to}"> starts with a language prefix. Write language-agnostic canonical paths; the Link localizes them automatically.`);
|
|
26
|
-
const localizedPathname = buildLocalizedUrl(interpolated, language, supportedLanguages,
|
|
26
|
+
const localizedPathname = buildLocalizedUrl(interpolated, language, supportedLanguages, urlStrategy);
|
|
27
27
|
const hash = hashProp ?? (hashFromTo ? hashFromTo.slice(1) : '');
|
|
28
28
|
const { searchString, searchObject } = normalizeSearch(searchProp, searchFromTo);
|
|
29
29
|
return {
|
|
@@ -43,7 +43,7 @@ const Link = (props)=>{
|
|
|
43
43
|
isBareHash,
|
|
44
44
|
language,
|
|
45
45
|
supportedLanguages,
|
|
46
|
-
|
|
46
|
+
urlStrategy
|
|
47
47
|
]);
|
|
48
48
|
const isActive = useMemo(()=>{
|
|
49
49
|
if (!target || !adapter.location) return false;
|
|
@@ -58,7 +58,7 @@ const Link = (props)=>{
|
|
|
58
58
|
adapter.location,
|
|
59
59
|
activeOptions?.exact,
|
|
60
60
|
supportedLanguages,
|
|
61
|
-
|
|
61
|
+
urlStrategy
|
|
62
62
|
]);
|
|
63
63
|
const resolvedActiveProps = splitActiveProps(isActive, activeProps);
|
|
64
64
|
const activeAttributes = isActive ? {
|
|
@@ -73,7 +73,7 @@ const Link = (props)=>{
|
|
|
73
73
|
children: children
|
|
74
74
|
});
|
|
75
75
|
}
|
|
76
|
-
const { Link: RouterLink, hasRouter
|
|
76
|
+
const { Link: RouterLink, hasRouter } = adapter;
|
|
77
77
|
if (!hasRouter || !RouterLink) {
|
|
78
78
|
const { replace: _replace, ...anchorProps } = rest;
|
|
79
79
|
const { className: activeClassName, style: activeStyle, ...activeRest } = resolvedActiveProps;
|
|
@@ -96,38 +96,25 @@ const Link = (props)=>{
|
|
|
96
96
|
...rest.style,
|
|
97
97
|
...activeStyle
|
|
98
98
|
};
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
} : {},
|
|
109
|
-
...void 0 === hashScrollIntoView ? {} : {
|
|
110
|
-
hashScrollIntoView
|
|
111
|
-
},
|
|
112
|
-
...void 0 === tanstackPreload ? {} : {
|
|
113
|
-
preload: tanstackPreload
|
|
114
|
-
},
|
|
115
|
-
...rest,
|
|
116
|
-
...activeRest,
|
|
117
|
-
...activeAttributes,
|
|
118
|
-
className: mergedClassName,
|
|
119
|
-
style: mergedStyle,
|
|
120
|
-
children: children
|
|
121
|
-
});
|
|
122
|
-
}
|
|
123
|
-
return /*#__PURE__*/ jsx(RouterLink, {
|
|
99
|
+
const routerProps = adapter.createLinkProps?.({
|
|
100
|
+
pathname: target.localizedPathname,
|
|
101
|
+
href: target.href,
|
|
102
|
+
search: target.searchObject,
|
|
103
|
+
hash: target.hash,
|
|
104
|
+
hashScrollIntoView,
|
|
105
|
+
prefetch,
|
|
106
|
+
preload
|
|
107
|
+
}) ?? {
|
|
124
108
|
to: target.href,
|
|
125
109
|
...void 0 === prefetch ? {} : {
|
|
126
110
|
prefetch
|
|
127
111
|
},
|
|
128
112
|
...void 0 === preload ? {} : {
|
|
129
113
|
preload
|
|
130
|
-
}
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
return /*#__PURE__*/ jsx(RouterLink, {
|
|
117
|
+
...routerProps,
|
|
131
118
|
...rest,
|
|
132
119
|
...activeRest,
|
|
133
120
|
...activeAttributes,
|