@bleedingdev/modern-js-plugin-i18n 3.9.0-ultramodern.3 → 3.9.0-ultramodern.5
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 +0 -22
- 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 +17 -14
- package/dist/cjs/runtime/hooks.js +7 -8
- package/dist/cjs/runtime/localizedPaths.js +11 -9
- package/dist/cjs/runtime/providerComposition.js +37 -13
- package/dist/cjs/runtime/routerAdapter.js +35 -140
- package/dist/cjs/runtime/utils.js +17 -19
- package/dist/cjs/server/index.js +10 -7
- package/dist/cjs/server/redirectPolicy.js +36 -31
- package/dist/cjs/shared/urlStrategy.js +18 -0
- package/dist/esm/cli/index.mjs +0 -22
- 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 +8 -5
- package/dist/esm/runtime/hooks.mjs +7 -8
- package/dist/esm/runtime/localizedPaths.mjs +11 -9
- package/dist/esm/runtime/providerComposition.mjs +38 -14
- package/dist/esm/runtime/routerAdapter.mjs +30 -141
- package/dist/esm/runtime/utils.mjs +15 -17
- package/dist/esm/server/index.mjs +10 -7
- package/dist/esm/server/redirectPolicy.mjs +29 -24
- package/dist/esm/shared/urlStrategy.mjs +0 -0
- package/dist/esm-node/cli/index.mjs +0 -22
- 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 +8 -5
- package/dist/esm-node/runtime/hooks.mjs +7 -8
- package/dist/esm-node/runtime/localizedPaths.mjs +11 -9
- 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 +15 -17
- package/dist/esm-node/server/index.mjs +10 -7
- package/dist/esm-node/server/redirectPolicy.mjs +29 -24
- package/dist/esm-node/shared/urlStrategy.mjs +1 -0
- package/dist/types/runtime/context.d.ts +8 -11
- package/dist/types/runtime/contextHelpers.d.ts +2 -2
- package/dist/types/runtime/core.d.ts +11 -2
- package/dist/types/runtime/hooks.d.ts +4 -5
- package/dist/types/runtime/localizedPaths.d.ts +4 -4
- package/dist/types/runtime/providerComposition.d.ts +13 -1
- package/dist/types/runtime/routerAdapter.d.ts +23 -6
- package/dist/types/runtime/utils.d.ts +4 -12
- package/dist/types/server/index.d.ts +2 -0
- package/dist/types/server/redirectPolicy.d.ts +5 -13
- package/dist/types/shared/type.d.ts +0 -14
- package/dist/types/shared/urlStrategy.d.ts +7 -0
- package/package.json +21 -11
- package/rstest.config.mts +2 -8
- package/src/cli/index.ts +1 -43
- 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 +26 -6
- package/src/runtime/hooks.ts +14 -9
- package/src/runtime/localizedPaths.ts +24 -19
- package/src/runtime/providerComposition.tsx +71 -23
- package/src/runtime/routerAdapter.tsx +73 -338
- package/src/runtime/utils.ts +40 -31
- package/src/server/index.ts +44 -9
- package/src/server/redirectPolicy.ts +70 -70
- package/src/shared/type.ts +0 -14
- package/src/shared/urlStrategy.ts +11 -0
- package/tests/federatedI18nBoundaryResolver.test.tsx +71 -0
- package/tests/i18nUtils.test.ts +31 -65
- package/tests/link.test.tsx +13 -511
- package/tests/linkTypes.test.ts +12 -3
- package/tests/localisedUrlRewriteMatrix.fork.test.ts +4 -96
- package/tests/localisedUrls.test.ts +7 -216
- package/tests/reactI18nextRuntimeBoundary.test.ts +1 -82
- package/tests/redirectPolicy.test.ts +39 -30
- package/tests/routerAdapter.test.tsx +95 -1055
- package/tests/federatedI18nBoundary.client.test.tsx +0 -129
- package/tests/federatedI18nBoundary.test.tsx +0 -132
- package/tests/peerCohort.test.ts +0 -25
|
@@ -28,160 +28,55 @@ var __webpack_require__ = {};
|
|
|
28
28
|
var __webpack_exports__ = {};
|
|
29
29
|
__webpack_require__.r(__webpack_exports__);
|
|
30
30
|
__webpack_require__.d(__webpack_exports__, {
|
|
31
|
-
|
|
31
|
+
I18nNavigationProvider: ()=>I18nNavigationProvider,
|
|
32
|
+
useI18nRouterAdapter: ()=>useI18nRouterAdapter,
|
|
33
|
+
useNativeI18nRouterAdapter: ()=>useNativeI18nRouterAdapter
|
|
32
34
|
});
|
|
35
|
+
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
|
|
33
36
|
const runtime_namespaceObject = require("@modern-js/runtime");
|
|
34
37
|
const context_namespaceObject = require("@modern-js/runtime/context");
|
|
35
38
|
const external_react_namespaceObject = require("react");
|
|
36
|
-
const
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
return {
|
|
45
|
-
pathname: locationValue.pathname,
|
|
46
|
-
search: normalizeUrlPart('string' == typeof locationValue.search ? locationValue.search : locationValue.searchStr, '?'),
|
|
47
|
-
hash: normalizeUrlPart(locationValue.hash, '#')
|
|
48
|
-
};
|
|
49
|
-
};
|
|
50
|
-
const getWindowLocation = ()=>{
|
|
51
|
-
if (!(0, runtime_namespaceObject.isBrowser)()) return null;
|
|
52
|
-
return {
|
|
53
|
-
pathname: window.location.pathname,
|
|
54
|
-
search: window.location.search,
|
|
55
|
-
hash: window.location.hash
|
|
56
|
-
};
|
|
57
|
-
};
|
|
58
|
-
const getRouterFramework = (runtimeContext, internalContext, inReactRouter)=>{
|
|
59
|
-
const framework = (0, context_namespaceObject.getRouterRuntimeState)(internalContext)?.framework || (0, context_namespaceObject.getRouterRuntimeState)(runtimeContext)?.framework;
|
|
60
|
-
if (framework) return framework;
|
|
61
|
-
if (internalContext.router?.useRouter || runtimeContext.router?.useRouter) return 'tanstack';
|
|
62
|
-
if (internalContext.router?.useLocation || internalContext.router?.useHref || runtimeContext.router?.useLocation || runtimeContext.router?.useHref) return 'react-router';
|
|
63
|
-
if (inReactRouter) return 'react-router';
|
|
64
|
-
};
|
|
65
|
-
const getRouterInstance = (internalContext, contextRouter)=>{
|
|
66
|
-
if (contextRouter) return contextRouter;
|
|
67
|
-
const router = (0, context_namespaceObject.getRouterRuntimeState)(internalContext)?.instance;
|
|
68
|
-
if (!router || 'object' != typeof router) return null;
|
|
69
|
-
return router;
|
|
70
|
-
};
|
|
71
|
-
const getRouterStateLocation = (internalContext, contextRouter)=>{
|
|
72
|
-
const router = getRouterInstance(internalContext, contextRouter);
|
|
73
|
-
return normalizeLocation(router?.stores?.location?.get?.()) || normalizeLocation(router?.state?.location);
|
|
74
|
-
};
|
|
75
|
-
const getRouterParams = (internalContext, contextRouter)=>{
|
|
76
|
-
const router = getRouterInstance(internalContext, contextRouter);
|
|
77
|
-
const matches = router?.stores?.matches?.get?.() || router?.state?.matches;
|
|
78
|
-
if (!Array.isArray(matches)) return {};
|
|
79
|
-
return matches.reduce((params, match)=>{
|
|
80
|
-
if (match?.params) Object.assign(params, match.params);
|
|
81
|
-
return params;
|
|
82
|
-
}, {});
|
|
83
|
-
};
|
|
84
|
-
const getRouterSnapshot = (internalContext, contextRouter)=>{
|
|
85
|
-
const location = getRouterStateLocation(internalContext, contextRouter);
|
|
86
|
-
const params = getRouterParams(internalContext, contextRouter);
|
|
87
|
-
return JSON.stringify([
|
|
88
|
-
location?.pathname ?? '',
|
|
89
|
-
location?.search ?? '',
|
|
90
|
-
location?.hash ?? '',
|
|
91
|
-
Object.entries(params).sort(([left], [right])=>left.localeCompare(right))
|
|
92
|
-
]);
|
|
93
|
-
};
|
|
94
|
-
const useI18nRouterAdapter = ()=>{
|
|
39
|
+
const navigationContextKey = Symbol.for('@modern-js/plugin-i18n/runtime/I18nNavigationContext');
|
|
40
|
+
const contextStore = globalThis;
|
|
41
|
+
const I18nNavigationContext = contextStore[navigationContextKey] ??= /*#__PURE__*/ (0, external_react_namespaceObject.createContext)(null);
|
|
42
|
+
const I18nNavigationProvider = ({ children, value })=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(I18nNavigationContext.Provider, {
|
|
43
|
+
value: value,
|
|
44
|
+
children: children
|
|
45
|
+
});
|
|
46
|
+
const useNativeI18nRouterAdapter = ()=>{
|
|
95
47
|
const runtimeContext = (0, external_react_namespaceObject.useContext)(runtime_namespaceObject.RuntimeContext);
|
|
96
48
|
const internalContext = (0, external_react_namespaceObject.useContext)(context_namespaceObject.InternalRuntimeContext);
|
|
97
49
|
const routerApi = internalContext.router || runtimeContext.router;
|
|
98
|
-
const
|
|
99
|
-
const
|
|
100
|
-
const
|
|
101
|
-
const
|
|
102
|
-
const
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
}
|
|
107
|
-
const hasRouter = 'tanstack' === framework || 'react-router' === framework || Boolean(reactRouterNavigate);
|
|
108
|
-
const subscribeToRouter = (0, external_react_namespaceObject.useCallback)((update)=>{
|
|
109
|
-
const router = getRouterInstance(internalContext, contextRouter);
|
|
110
|
-
if (!router) return ()=>void 0;
|
|
111
|
-
const unsubscribers = [];
|
|
112
|
-
if ('react-router' === framework && !inReactRouter && 'function' == typeof router.subscribe) {
|
|
113
|
-
const subscribe = router.subscribe;
|
|
114
|
-
const unsubscribe = subscribe.call(router, update);
|
|
115
|
-
if ('function' == typeof unsubscribe) unsubscribers.push(unsubscribe);
|
|
116
|
-
}
|
|
117
|
-
if ('tanstack' === framework && 'function' == typeof router.stores?.location?.subscribe) {
|
|
118
|
-
const unsubscribe = router.stores.location.subscribe(update);
|
|
119
|
-
if ('function' == typeof unsubscribe) unsubscribers.push(unsubscribe);
|
|
120
|
-
}
|
|
121
|
-
if ('tanstack' === framework && 'function' == typeof router.subscribe) {
|
|
122
|
-
const subscribe = router.subscribe;
|
|
123
|
-
for (const eventType of [
|
|
124
|
-
'onBeforeNavigate',
|
|
125
|
-
'onBeforeLoad'
|
|
126
|
-
]){
|
|
127
|
-
const unsubscribe = subscribe.call(router, eventType, update);
|
|
128
|
-
if ('function' == typeof unsubscribe) unsubscribers.push(unsubscribe);
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
return ()=>{
|
|
132
|
-
for (const unsubscribe of unsubscribers)unsubscribe();
|
|
133
|
-
};
|
|
134
|
-
}, [
|
|
135
|
-
contextRouter,
|
|
136
|
-
framework,
|
|
137
|
-
inReactRouter,
|
|
138
|
-
internalContext
|
|
139
|
-
]);
|
|
140
|
-
const getSnapshot = (0, external_react_namespaceObject.useCallback)(()=>getRouterSnapshot(internalContext, contextRouter), [
|
|
141
|
-
contextRouter,
|
|
142
|
-
internalContext
|
|
143
|
-
]);
|
|
144
|
-
(0, external_react_namespaceObject.useSyncExternalStore)(subscribeToRouter, getSnapshot, getSnapshot);
|
|
145
|
-
const navigate = (0, external_react_namespaceObject.useCallback)((href, options)=>{
|
|
146
|
-
const router = getRouterInstance(internalContext, contextRouter);
|
|
147
|
-
const activeFramework = getRouterFramework(runtimeContext, internalContext, inReactRouter);
|
|
148
|
-
if ('tanstack' === activeFramework) {
|
|
149
|
-
if ('function' == typeof router?.navigate) return router.navigate({
|
|
150
|
-
to: href,
|
|
151
|
-
replace: options?.replace,
|
|
152
|
-
...options?.state === void 0 ? {} : {
|
|
153
|
-
state: options.state
|
|
154
|
-
}
|
|
155
|
-
});
|
|
156
|
-
throw new Error('TanStack router instance is not available.');
|
|
157
|
-
}
|
|
158
|
-
if (reactRouterNavigate) return reactRouterNavigate(href, options);
|
|
159
|
-
if ('react-router' === activeFramework) {
|
|
160
|
-
if ('function' == typeof router?.navigate) return router.navigate(href, options);
|
|
161
|
-
throw new Error('React Router instance is not available.');
|
|
162
|
-
}
|
|
163
|
-
}, [
|
|
164
|
-
contextRouter,
|
|
165
|
-
internalContext,
|
|
166
|
-
inReactRouter,
|
|
167
|
-
reactRouterNavigate,
|
|
168
|
-
runtimeContext
|
|
169
|
-
]);
|
|
170
|
-
const location = (reactRouterLocation ? normalizeLocation(reactRouterLocation) : getRouterStateLocation(internalContext, contextRouter)) || getWindowLocation();
|
|
171
|
-
const params = inReactRouter ? reactRouterParams : getRouterParams(internalContext, contextRouter);
|
|
172
|
-
const Link = 'tanstack' === framework ? internalContext.router?.Link || runtimeContext.router?.Link || null : 'react-router' === framework || inReactRouter ? routerApi?.Link ?? null : null;
|
|
50
|
+
const hasRouter = routerApi?.useInRouterContext?.() ?? false;
|
|
51
|
+
const navigate = hasRouter ? routerApi?.useNavigate?.() ?? null : null;
|
|
52
|
+
const routerLocation = hasRouter ? routerApi?.useLocation?.() ?? null : null;
|
|
53
|
+
const params = hasRouter ? routerApi?.useParams?.() ?? {} : {};
|
|
54
|
+
const location = routerLocation ?? ((0, runtime_namespaceObject.isBrowser)() ? {
|
|
55
|
+
pathname: window.location.pathname,
|
|
56
|
+
search: window.location.search,
|
|
57
|
+
hash: window.location.hash
|
|
58
|
+
} : null);
|
|
173
59
|
return {
|
|
174
|
-
framework,
|
|
60
|
+
framework: hasRouter ? 'react-router' : void 0,
|
|
175
61
|
hasRouter,
|
|
62
|
+
navigate,
|
|
176
63
|
location,
|
|
177
|
-
|
|
178
|
-
Link
|
|
179
|
-
params
|
|
64
|
+
params,
|
|
65
|
+
Link: hasRouter ? routerApi?.Link ?? null : null
|
|
180
66
|
};
|
|
181
67
|
};
|
|
68
|
+
const useI18nRouterAdapter = ()=>{
|
|
69
|
+
const provided = (0, external_react_namespaceObject.useContext)(I18nNavigationContext);
|
|
70
|
+
const native = useNativeI18nRouterAdapter();
|
|
71
|
+
return provided ?? native;
|
|
72
|
+
};
|
|
73
|
+
exports.I18nNavigationProvider = __webpack_exports__.I18nNavigationProvider;
|
|
182
74
|
exports.useI18nRouterAdapter = __webpack_exports__.useI18nRouterAdapter;
|
|
75
|
+
exports.useNativeI18nRouterAdapter = __webpack_exports__.useNativeI18nRouterAdapter;
|
|
183
76
|
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
184
|
-
"
|
|
77
|
+
"I18nNavigationProvider",
|
|
78
|
+
"useI18nRouterAdapter",
|
|
79
|
+
"useNativeI18nRouterAdapter"
|
|
185
80
|
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
186
81
|
Object.defineProperty(exports, '__esModule', {
|
|
187
82
|
value: true
|
|
@@ -34,11 +34,11 @@ __webpack_require__.d(__webpack_exports__, {
|
|
|
34
34
|
getLanguageFromPath: ()=>getLanguageFromPath,
|
|
35
35
|
getPathname: ()=>getPathname,
|
|
36
36
|
shouldIgnoreRedirect: ()=>shouldIgnoreRedirect,
|
|
37
|
-
splitUrlTarget: ()=>splitUrlTarget
|
|
37
|
+
splitUrlTarget: ()=>url_namespaceObject.splitUrlTarget
|
|
38
38
|
});
|
|
39
|
-
const i18n_runtime_extensions_namespaceObject = require("@modern-js/i18n-runtime-extensions");
|
|
40
39
|
const runtime_namespaceObject = require("@modern-js/runtime");
|
|
41
40
|
const context_namespaceObject = require("@modern-js/runtime/context");
|
|
41
|
+
const url_namespaceObject = require("@modern-js/runtime-utils/url");
|
|
42
42
|
const getPathname = (context)=>{
|
|
43
43
|
if ((0, runtime_namespaceObject.isBrowser)()) return window.location.pathname;
|
|
44
44
|
return context.ssrContext?.request?.pathname || '/';
|
|
@@ -54,22 +54,13 @@ const getLanguageFromPath = (pathname, languages, fallbackLanguage)=>{
|
|
|
54
54
|
if (languages.includes(firstSegment)) return firstSegment;
|
|
55
55
|
return fallbackLanguage;
|
|
56
56
|
};
|
|
57
|
-
const
|
|
58
|
-
const
|
|
59
|
-
|
|
60
|
-
const
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
const
|
|
64
|
-
return {
|
|
65
|
-
pathname,
|
|
66
|
-
search,
|
|
67
|
-
hash
|
|
68
|
-
};
|
|
69
|
-
};
|
|
70
|
-
const buildLocalizedUrl = (target, language, languages, localisedUrls)=>{
|
|
71
|
-
const { pathname, search, hash } = splitUrlTarget(target);
|
|
72
|
-
const localizedPathname = (0, i18n_runtime_extensions_namespaceObject.localiseTargetPathname)(pathname, language, languages, localisedUrls);
|
|
57
|
+
const buildLocalizedUrl = (target, language, languages, urlStrategy)=>{
|
|
58
|
+
const { pathname, search, hash } = (0, url_namespaceObject.splitUrlTarget)(target);
|
|
59
|
+
if (urlStrategy) return `${urlStrategy.localizePathname(pathname, language, languages)}${search}${hash}`;
|
|
60
|
+
const segments = pathname.split('/').filter(Boolean);
|
|
61
|
+
if (segments[0] && languages.some((item)=>item.toLowerCase() === segments[0].toLowerCase())) segments[0] = language;
|
|
62
|
+
else segments.unshift(language);
|
|
63
|
+
const localizedPathname = `/${segments.join('/')}`;
|
|
73
64
|
return `${localizedPathname}${search}${hash}`;
|
|
74
65
|
};
|
|
75
66
|
const detectLanguageFromPath = (pathname, languages, localePathRedirect)=>{
|
|
@@ -89,7 +80,14 @@ const detectLanguageFromPath = (pathname, languages, localePathRedirect)=>{
|
|
|
89
80
|
detected: false
|
|
90
81
|
};
|
|
91
82
|
};
|
|
92
|
-
const shouldIgnoreRedirect = (pathname, languages, ignoreRedirectRoutes
|
|
83
|
+
const shouldIgnoreRedirect = (pathname, languages, ignoreRedirectRoutes, urlStrategy)=>{
|
|
84
|
+
if (urlStrategy?.shouldSkipRedirect?.(pathname, languages)) return true;
|
|
85
|
+
if (!ignoreRedirectRoutes) return false;
|
|
86
|
+
const segments = pathname.split('/').filter(Boolean);
|
|
87
|
+
if (segments[0] && languages.some((item)=>item.toLowerCase() === segments[0].toLowerCase())) segments.shift();
|
|
88
|
+
const normalizedPath = `/${segments.join('/')}`;
|
|
89
|
+
return 'function' == typeof ignoreRedirectRoutes ? ignoreRedirectRoutes(normalizedPath) : ignoreRedirectRoutes.some((pattern)=>normalizedPath === pattern || normalizedPath.startsWith(`${pattern}/`));
|
|
90
|
+
};
|
|
93
91
|
exports.buildLocalizedUrl = __webpack_exports__.buildLocalizedUrl;
|
|
94
92
|
exports.detectLanguageFromPath = __webpack_exports__.detectLanguageFromPath;
|
|
95
93
|
exports.getEntryPath = __webpack_exports__.getEntryPath;
|
package/dist/cjs/server/index.js
CHANGED
|
@@ -33,7 +33,6 @@ __webpack_require__.d(__webpack_exports__, {
|
|
|
33
33
|
i18nServerPlugin: ()=>i18nServerPlugin,
|
|
34
34
|
matchesApiPrefix: ()=>external_apiPrefix_js_namespaceObject.matchesApiPrefix
|
|
35
35
|
});
|
|
36
|
-
const i18n_runtime_extensions_namespaceObject = require("@modern-js/i18n-runtime-extensions");
|
|
37
36
|
const hono_namespaceObject = require("@modern-js/server-core/hono");
|
|
38
37
|
const utils_js_namespaceObject = require("../shared/utils.js");
|
|
39
38
|
const external_apiPrefix_js_namespaceObject = require("./apiPrefix.js");
|
|
@@ -59,7 +58,11 @@ const i18nServerPlugin = (options)=>({
|
|
|
59
58
|
const { entryName } = route;
|
|
60
59
|
if (!entryName) return;
|
|
61
60
|
if (!options.localeDetection) return;
|
|
62
|
-
const
|
|
61
|
+
const entryLocaleDetection = (0, utils_js_namespaceObject.getLocaleDetectionOptions)(entryName, options.localeDetection);
|
|
62
|
+
const { localePathRedirect, i18nextDetector = true, languages = [], fallbackLanguage = 'en', detection, ignoreRedirectRoutes } = entryLocaleDetection;
|
|
63
|
+
const legacyLocalisedUrls = Reflect.get(entryLocaleDetection, 'localisedUrls');
|
|
64
|
+
const urlStrategy = options.resolveUrlStrategy?.(entryName);
|
|
65
|
+
if (legacyLocalisedUrls && 'object' == typeof legacyLocalisedUrls && Object.keys(legacyLocalisedUrls).length > 0 && !urlStrategy) throw new Error('Mapped locale URLs require a URL strategy. Use the i18n integration server plugin or supply resolveUrlStrategy.');
|
|
63
66
|
const staticRoutePrefixes = options.staticRoutePrefixes;
|
|
64
67
|
const originUrlPath = route.urlPath;
|
|
65
68
|
const urlPath = originUrlPath.endsWith('/') ? `${originUrlPath}*` : `${originUrlPath}/*`;
|
|
@@ -74,6 +77,7 @@ const i18nServerPlugin = (options)=>({
|
|
|
74
77
|
const url = new URL(c.req.url);
|
|
75
78
|
const pathname = url.pathname;
|
|
76
79
|
if ((0, external_apiPrefix_js_namespaceObject.matchesApiPrefix)(pathname, apiPrefixes)) return await next();
|
|
80
|
+
if ((0, external_redirectPolicy_js_namespaceObject.shouldIgnoreRedirect)(pathname, urlPath, ignoreRedirectRoutes, urlStrategy, languages)) return await next();
|
|
77
81
|
if ((0, external_redirectPolicy_js_namespaceObject.isStaticResourceRequest)(pathname, staticRoutePrefixes, languages)) return await next();
|
|
78
82
|
if ('/' === originUrlPath) {
|
|
79
83
|
const pathSegments = pathname.split('/').filter(Boolean);
|
|
@@ -94,7 +98,7 @@ const i18nServerPlugin = (options)=>({
|
|
|
94
98
|
const pathname = url.pathname;
|
|
95
99
|
if ((0, external_apiPrefix_js_namespaceObject.matchesApiPrefix)(pathname, apiPrefixes)) return await next();
|
|
96
100
|
if ((0, external_redirectPolicy_js_namespaceObject.isStaticResourceRequest)(pathname, staticRoutePrefixes, languages)) return await next();
|
|
97
|
-
if ((0, external_redirectPolicy_js_namespaceObject.shouldIgnoreRedirect)(pathname, urlPath, ignoreRedirectRoutes)) return await next();
|
|
101
|
+
if ((0, external_redirectPolicy_js_namespaceObject.shouldIgnoreRedirect)(pathname, urlPath, ignoreRedirectRoutes, urlStrategy, languages)) return await next();
|
|
98
102
|
if ('/' === originUrlPath) {
|
|
99
103
|
const pathSegments = pathname.split('/').filter(Boolean);
|
|
100
104
|
if (pathSegments.length > 0) {
|
|
@@ -107,12 +111,11 @@ const i18nServerPlugin = (options)=>({
|
|
|
107
111
|
let detectedLanguage = null;
|
|
108
112
|
if (i18nextDetector) detectedLanguage = c.get('language') || null;
|
|
109
113
|
const targetLanguage = detectedLanguage || fallbackLanguage;
|
|
110
|
-
const localizedUrl = (0, external_redirectPolicy_js_namespaceObject.buildLocalizedUrl)(c.req, originUrlPath, targetLanguage, languages,
|
|
114
|
+
const localizedUrl = (0, external_redirectPolicy_js_namespaceObject.buildLocalizedUrl)(c.req, originUrlPath, targetLanguage, languages, urlStrategy);
|
|
111
115
|
return (0, external_redirectPolicy_js_namespaceObject.createLocaleRedirectResponse)(localizedUrl);
|
|
112
116
|
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
const expectedUrl = (0, external_redirectPolicy_js_namespaceObject.buildLocalizedUrl)(c.req, originUrlPath, language, languages, localisedUrls);
|
|
117
|
+
if (urlStrategy) {
|
|
118
|
+
const expectedUrl = (0, external_redirectPolicy_js_namespaceObject.buildLocalizedUrl)(c.req, originUrlPath, language, languages, urlStrategy);
|
|
116
119
|
if (expectedUrl !== `${pathname}${url.search}${url.hash}`) return (0, external_redirectPolicy_js_namespaceObject.createLocaleRedirectResponse)(expectedUrl);
|
|
117
120
|
}
|
|
118
121
|
await next();
|
|
@@ -27,49 +27,47 @@ var __webpack_require__ = {};
|
|
|
27
27
|
})();
|
|
28
28
|
var __webpack_exports__ = {};
|
|
29
29
|
__webpack_require__.r(__webpack_exports__);
|
|
30
|
-
__webpack_require__.d(__webpack_exports__, {
|
|
31
|
-
buildLocalizedUrl: ()=>buildLocalizedUrl,
|
|
32
|
-
createLocaleRedirectResponse: ()=>createLocaleRedirectResponse,
|
|
33
|
-
getLanguageFromPath: ()=>getLanguageFromPath,
|
|
34
|
-
isStaticResourceRequest: ()=>isStaticResourceRequest,
|
|
35
|
-
shouldIgnoreRedirect: ()=>shouldIgnoreRedirect
|
|
36
|
-
});
|
|
37
|
-
const i18n_runtime_extensions_namespaceObject = require("@modern-js/i18n-runtime-extensions");
|
|
38
30
|
const stripUrlPathPrefix = (pathname, urlPath)=>{
|
|
39
31
|
const basePath = urlPath.replace('/*', '');
|
|
40
32
|
if (!basePath || '/' === basePath) return pathname;
|
|
41
|
-
|
|
42
|
-
return remainingPath || '/';
|
|
33
|
+
return pathname === basePath || pathname.startsWith(`${basePath}/`) ? pathname.slice(basePath.length) || '/' : pathname;
|
|
43
34
|
};
|
|
44
|
-
const shouldIgnoreRedirect = (pathname, urlPath, ignoreRedirectRoutes)=>{
|
|
35
|
+
const shouldIgnoreRedirect = (pathname, urlPath, ignoreRedirectRoutes, urlStrategy, languages = [])=>{
|
|
45
36
|
const remainingPath = stripUrlPathPrefix(pathname, urlPath);
|
|
46
|
-
|
|
37
|
+
if (urlStrategy?.shouldSkipRedirect?.(remainingPath, languages)) return true;
|
|
38
|
+
if (!ignoreRedirectRoutes) return false;
|
|
39
|
+
const segments = remainingPath.split('/').filter(Boolean);
|
|
40
|
+
if (segments[0] && languages.some((language)=>language.toLowerCase() === segments[0].toLowerCase())) segments.shift();
|
|
41
|
+
const normalizedPath = `/${segments.join('/')}`;
|
|
42
|
+
return 'function' == typeof ignoreRedirectRoutes ? ignoreRedirectRoutes(normalizedPath) : ignoreRedirectRoutes.some((pattern)=>normalizedPath === pattern || normalizedPath.startsWith(`${pattern}/`));
|
|
47
43
|
};
|
|
48
44
|
const isStaticResourceRequest = (pathname, staticRoutePrefixes, languages = [])=>{
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
45
|
+
const prefixes = [
|
|
46
|
+
...staticRoutePrefixes,
|
|
47
|
+
'/static',
|
|
48
|
+
'/upload'
|
|
49
|
+
];
|
|
50
|
+
const matches = (target)=>prefixes.some((prefix)=>target === prefix || target.startsWith(`${prefix}/`));
|
|
51
|
+
if (matches(pathname)) return true;
|
|
52
|
+
const segments = pathname.split('/').filter(Boolean);
|
|
53
|
+
if (segments[0] && languages.some((language)=>language.toLowerCase() === segments[0].toLowerCase())) return matches(`/${segments.slice(1).join('/')}`);
|
|
54
|
+
return false;
|
|
54
55
|
};
|
|
55
56
|
const getLanguageFromPath = (req, urlPath, languages)=>{
|
|
56
|
-
const
|
|
57
|
-
const
|
|
58
|
-
const
|
|
59
|
-
|
|
60
|
-
const firstSegment = segments[0];
|
|
61
|
-
if (languages.includes(firstSegment)) return firstSegment;
|
|
62
|
-
return null;
|
|
57
|
+
const host = req.header?.().host;
|
|
58
|
+
const url = new URL(req.url, host ? `http://${host}` : void 0);
|
|
59
|
+
const firstSegment = stripUrlPathPrefix(url.pathname, urlPath).split('/').filter(Boolean)[0];
|
|
60
|
+
return languages.find((language)=>language.toLowerCase() === firstSegment?.toLowerCase()) ?? null;
|
|
63
61
|
};
|
|
64
|
-
const buildLocalizedUrl = (req, urlPath, language, languages,
|
|
62
|
+
const buildLocalizedUrl = (req, urlPath, language, languages, urlStrategy)=>{
|
|
65
63
|
const url = new URL(req.url);
|
|
66
|
-
const pathname = url.pathname;
|
|
67
64
|
const basePath = urlPath.replace('/*', '');
|
|
68
|
-
const remainingPath = stripUrlPathPrefix(pathname, urlPath);
|
|
69
|
-
const
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
65
|
+
const remainingPath = stripUrlPathPrefix(url.pathname, urlPath);
|
|
66
|
+
const segments = remainingPath.split('/').filter(Boolean);
|
|
67
|
+
if (segments[0] && languages.some((item)=>item.toLowerCase() === segments[0].toLowerCase())) segments[0] = language;
|
|
68
|
+
else segments.unshift(language);
|
|
69
|
+
const pathname = urlStrategy ? urlStrategy.localizePathname(remainingPath, language, languages) : `/${segments.join('/')}`;
|
|
70
|
+
return `${'/' === basePath ? '' : basePath}${pathname}${url.search}${url.hash}`;
|
|
73
71
|
};
|
|
74
72
|
const createLocaleRedirectResponse = (location)=>new Response(null, {
|
|
75
73
|
status: 302,
|
|
@@ -79,6 +77,13 @@ const createLocaleRedirectResponse = (location)=>new Response(null, {
|
|
|
79
77
|
Vary: 'Accept-Language, Cookie'
|
|
80
78
|
}
|
|
81
79
|
});
|
|
80
|
+
__webpack_require__.d(__webpack_exports__, {}, {
|
|
81
|
+
buildLocalizedUrl: buildLocalizedUrl,
|
|
82
|
+
createLocaleRedirectResponse: createLocaleRedirectResponse,
|
|
83
|
+
getLanguageFromPath: getLanguageFromPath,
|
|
84
|
+
isStaticResourceRequest: isStaticResourceRequest,
|
|
85
|
+
shouldIgnoreRedirect: shouldIgnoreRedirect
|
|
86
|
+
});
|
|
82
87
|
exports.buildLocalizedUrl = __webpack_exports__.buildLocalizedUrl;
|
|
83
88
|
exports.createLocaleRedirectResponse = __webpack_exports__.createLocaleRedirectResponse;
|
|
84
89
|
exports.getLanguageFromPath = __webpack_exports__.getLanguageFromPath;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __webpack_require__ = {};
|
|
3
|
+
(()=>{
|
|
4
|
+
__webpack_require__.r = (exports1)=>{
|
|
5
|
+
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
6
|
+
value: 'Module'
|
|
7
|
+
});
|
|
8
|
+
Object.defineProperty(exports1, '__esModule', {
|
|
9
|
+
value: true
|
|
10
|
+
});
|
|
11
|
+
};
|
|
12
|
+
})();
|
|
13
|
+
var __webpack_exports__ = {};
|
|
14
|
+
__webpack_require__.r(__webpack_exports__);
|
|
15
|
+
for(var __rspack_i in __webpack_exports__)exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
16
|
+
Object.defineProperty(exports, '__esModule', {
|
|
17
|
+
value: true
|
|
18
|
+
});
|
package/dist/esm/cli/index.mjs
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { applyLocalisedUrlsToRoutes, resolveLocalisedUrlsConfig } from "@modern-js/i18n-runtime-extensions";
|
|
2
1
|
import { getPublicDirRoutePrefixes } from "@modern-js/server-core";
|
|
3
2
|
import { getBackendOptions, getLocaleDetectionOptions } from "../shared/utils.mjs";
|
|
4
3
|
import { applyDetectedBackendPaths, detectLocalesDirectory } from "./locales.mjs";
|
|
@@ -43,27 +42,6 @@ const i18nPlugin = (options = {})=>({
|
|
|
43
42
|
plugins
|
|
44
43
|
};
|
|
45
44
|
});
|
|
46
|
-
api.modifyFileSystemRoutes(({ entrypoint, routes })=>{
|
|
47
|
-
if (!localeDetection) return {
|
|
48
|
-
entrypoint,
|
|
49
|
-
routes
|
|
50
|
-
};
|
|
51
|
-
const localeDetectionOptions = getLocaleDetectionOptions(entrypoint.entryName, localeDetection);
|
|
52
|
-
const { localePathRedirect, languages = [], localisedUrls } = localeDetectionOptions;
|
|
53
|
-
if (!localePathRedirect || 0 === languages.length) return {
|
|
54
|
-
entrypoint,
|
|
55
|
-
routes
|
|
56
|
-
};
|
|
57
|
-
const localisedUrlsConfig = resolveLocalisedUrlsConfig(localisedUrls);
|
|
58
|
-
if (!localisedUrlsConfig.enabled) return {
|
|
59
|
-
entrypoint,
|
|
60
|
-
routes
|
|
61
|
-
};
|
|
62
|
-
return {
|
|
63
|
-
entrypoint,
|
|
64
|
-
routes: applyLocalisedUrlsToRoutes(routes, languages, localisedUrlsConfig.map)
|
|
65
|
-
};
|
|
66
|
-
});
|
|
67
45
|
api._internalServerPlugins(({ plugins })=>{
|
|
68
46
|
const { serverRoutes, metaName } = api.getAppContext();
|
|
69
47
|
const normalizedConfig = api.getNormalizedConfig();
|
|
@@ -9,10 +9,10 @@ const EXTERNAL_TARGET_RE = /^(?:[a-z][a-z0-9+.-]*:|\/\/)/i;
|
|
|
9
9
|
const Link = (props)=>{
|
|
10
10
|
const { to, params, children, hash: hashProp, search: searchProp, hashScrollIntoView, activeOptions, activeProps, prefetch, preload, ...rest } = props;
|
|
11
11
|
const adapter = useI18nRouterAdapter();
|
|
12
|
-
const { language, supportedLanguages,
|
|
12
|
+
const { language, supportedLanguages, urlStrategy } = useModernI18n();
|
|
13
13
|
const config = {
|
|
14
14
|
languages: supportedLanguages,
|
|
15
|
-
|
|
15
|
+
urlStrategy
|
|
16
16
|
};
|
|
17
17
|
const isExternal = EXTERNAL_TARGET_RE.test(to);
|
|
18
18
|
const isBareHash = to.startsWith('#');
|
|
@@ -22,7 +22,7 @@ const Link = (props)=>{
|
|
|
22
22
|
const interpolated = interpolateRouteParams(pathname || '/', params);
|
|
23
23
|
const firstSegment = interpolated.split('/').filter(Boolean)[0];
|
|
24
24
|
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.`);
|
|
25
|
-
const localizedPathname = buildLocalizedUrl(interpolated, language, supportedLanguages,
|
|
25
|
+
const localizedPathname = buildLocalizedUrl(interpolated, language, supportedLanguages, urlStrategy);
|
|
26
26
|
const hash = hashProp ?? (hashFromTo ? hashFromTo.slice(1) : '');
|
|
27
27
|
const { searchString, searchObject } = normalizeSearch(searchProp, searchFromTo);
|
|
28
28
|
return {
|
|
@@ -42,7 +42,7 @@ const Link = (props)=>{
|
|
|
42
42
|
isBareHash,
|
|
43
43
|
language,
|
|
44
44
|
supportedLanguages,
|
|
45
|
-
|
|
45
|
+
urlStrategy
|
|
46
46
|
]);
|
|
47
47
|
const isActive = useMemo(()=>{
|
|
48
48
|
if (!target || !adapter.location) return false;
|
|
@@ -57,7 +57,7 @@ const Link = (props)=>{
|
|
|
57
57
|
adapter.location,
|
|
58
58
|
activeOptions?.exact,
|
|
59
59
|
supportedLanguages,
|
|
60
|
-
|
|
60
|
+
urlStrategy
|
|
61
61
|
]);
|
|
62
62
|
const resolvedActiveProps = splitActiveProps(isActive, activeProps);
|
|
63
63
|
const activeAttributes = isActive ? {
|
|
@@ -72,7 +72,7 @@ const Link = (props)=>{
|
|
|
72
72
|
children: children
|
|
73
73
|
});
|
|
74
74
|
}
|
|
75
|
-
const { Link: RouterLink, hasRouter
|
|
75
|
+
const { Link: RouterLink, hasRouter } = adapter;
|
|
76
76
|
if (!hasRouter || !RouterLink) {
|
|
77
77
|
const { replace: _replace, ...anchorProps } = rest;
|
|
78
78
|
const { className: activeClassName, style: activeStyle, ...activeRest } = resolvedActiveProps;
|
|
@@ -95,38 +95,25 @@ const Link = (props)=>{
|
|
|
95
95
|
...rest.style,
|
|
96
96
|
...activeStyle
|
|
97
97
|
};
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
} : {},
|
|
108
|
-
...void 0 === hashScrollIntoView ? {} : {
|
|
109
|
-
hashScrollIntoView
|
|
110
|
-
},
|
|
111
|
-
...void 0 === tanstackPreload ? {} : {
|
|
112
|
-
preload: tanstackPreload
|
|
113
|
-
},
|
|
114
|
-
...rest,
|
|
115
|
-
...activeRest,
|
|
116
|
-
...activeAttributes,
|
|
117
|
-
className: mergedClassName,
|
|
118
|
-
style: mergedStyle,
|
|
119
|
-
children: children
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
return /*#__PURE__*/ jsx(RouterLink, {
|
|
98
|
+
const routerProps = adapter.createLinkProps?.({
|
|
99
|
+
pathname: target.localizedPathname,
|
|
100
|
+
href: target.href,
|
|
101
|
+
search: target.searchObject,
|
|
102
|
+
hash: target.hash,
|
|
103
|
+
hashScrollIntoView,
|
|
104
|
+
prefetch,
|
|
105
|
+
preload
|
|
106
|
+
}) ?? {
|
|
123
107
|
to: target.href,
|
|
124
108
|
...void 0 === prefetch ? {} : {
|
|
125
109
|
prefetch
|
|
126
110
|
},
|
|
127
111
|
...void 0 === preload ? {} : {
|
|
128
112
|
preload
|
|
129
|
-
}
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
return /*#__PURE__*/ jsx(RouterLink, {
|
|
116
|
+
...routerProps,
|
|
130
117
|
...rest,
|
|
131
118
|
...activeRest,
|
|
132
119
|
...activeAttributes,
|