@bleedingdev/modern-js-plugin-i18n 3.5.0-ultramodern.5 → 3.5.0-ultramodern.75
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/cli/index.js +2 -1
- package/dist/cjs/cli/locales.js +15 -15
- package/dist/cjs/runtime/Link.js +8 -89
- package/dist/cjs/runtime/context.js +78 -91
- package/dist/cjs/runtime/contextHelpers.js +143 -0
- package/dist/cjs/runtime/core.js +32 -132
- package/dist/cjs/runtime/hooks.js +54 -21
- package/dist/cjs/runtime/i18n/backend/defaults.node.js +7 -7
- package/dist/cjs/runtime/i18n/detection/cache.js +150 -0
- package/dist/cjs/runtime/i18n/detection/detector.js +100 -0
- package/dist/cjs/runtime/i18n/detection/index.js +8 -263
- package/dist/cjs/runtime/i18n/detection/initOptions.js +69 -0
- package/dist/cjs/runtime/i18n/detection/language.js +62 -0
- package/dist/cjs/runtime/i18n/detection/path.js +49 -0
- package/dist/cjs/runtime/i18n/detection/priority.js +66 -0
- package/dist/cjs/runtime/i18n/detection/ssr.js +70 -0
- package/dist/cjs/runtime/i18n/detection/types.js +18 -0
- package/dist/cjs/runtime/i18n/instance.js +34 -10
- package/dist/cjs/runtime/linkHelpers.js +148 -0
- package/dist/cjs/runtime/pluginSetup.js +102 -0
- package/dist/cjs/runtime/providerComposition.js +115 -0
- package/dist/cjs/runtime/reactI18next.js +43 -0
- package/dist/cjs/runtime/routerAdapter.js +16 -8
- package/dist/cjs/runtime/utils.js +1 -9
- package/dist/cjs/server/apiPrefix.js +72 -0
- package/dist/cjs/server/detectorOptions.js +97 -0
- package/dist/cjs/server/index.js +17 -144
- package/dist/cjs/server/redirectPolicy.js +96 -0
- package/dist/cjs/shared/detection.js +2 -104
- package/dist/cjs/shared/localisedUrls/config.js +49 -0
- package/dist/cjs/shared/localisedUrls/index.js +91 -0
- package/dist/cjs/shared/localisedUrls/normalise.js +87 -0
- package/dist/cjs/shared/localisedUrls/pathname.js +68 -0
- package/dist/cjs/shared/localisedUrls/patterns.js +130 -0
- package/dist/cjs/shared/localisedUrls/redirect.js +103 -0
- package/dist/cjs/shared/localisedUrls/resolve.js +88 -0
- package/dist/cjs/shared/localisedUrls/routes.js +130 -0
- package/dist/cjs/shared/localisedUrls/types.js +18 -0
- package/dist/cjs/shared/localisedUrls.js +28 -294
- package/dist/cjs/shared/utils.js +2 -8
- package/dist/esm/cli/index.mjs +2 -1
- package/dist/esm/cli/locales.mjs +13 -13
- package/dist/esm/runtime/Link.mjs +1 -82
- package/dist/esm/runtime/context.mjs +76 -92
- package/dist/esm/runtime/contextHelpers.mjs +87 -0
- package/dist/esm/runtime/core.mjs +30 -133
- package/dist/esm/runtime/hooks.mjs +56 -23
- package/dist/esm/runtime/i18n/backend/defaults.node.mjs +5 -5
- package/dist/esm/runtime/i18n/detection/cache.mjs +103 -0
- package/dist/esm/runtime/i18n/detection/detector.mjs +62 -0
- package/dist/esm/runtime/i18n/detection/index.mjs +4 -260
- package/dist/esm/runtime/i18n/detection/initOptions.mjs +28 -0
- package/dist/esm/runtime/i18n/detection/language.mjs +21 -0
- package/dist/esm/runtime/i18n/detection/path.mjs +11 -0
- package/dist/esm/runtime/i18n/detection/priority.mjs +28 -0
- package/dist/esm/runtime/i18n/detection/ssr.mjs +26 -0
- package/dist/esm/runtime/i18n/detection/types.mjs +0 -0
- package/dist/esm/runtime/i18n/instance.mjs +35 -8
- package/dist/esm/runtime/linkHelpers.mjs +98 -0
- package/dist/esm/runtime/pluginSetup.mjs +64 -0
- package/dist/esm/runtime/providerComposition.mjs +77 -0
- package/dist/esm/runtime/reactI18next.mjs +5 -0
- package/dist/esm/runtime/routerAdapter.mjs +16 -8
- package/dist/esm/runtime/utils.mjs +2 -10
- package/dist/esm/server/apiPrefix.mjs +31 -0
- package/dist/esm/server/detectorOptions.mjs +59 -0
- package/dist/esm/server/index.mjs +8 -135
- package/dist/esm/server/redirectPolicy.mjs +46 -0
- package/dist/esm/shared/detection.mjs +1 -71
- package/dist/esm/shared/localisedUrls/config.mjs +11 -0
- package/dist/esm/shared/localisedUrls/index.mjs +7 -0
- package/dist/esm/shared/localisedUrls/normalise.mjs +37 -0
- package/dist/esm/shared/localisedUrls/pathname.mjs +24 -0
- package/dist/esm/shared/localisedUrls/patterns.mjs +86 -0
- package/dist/esm/shared/localisedUrls/redirect.mjs +56 -0
- package/dist/esm/shared/localisedUrls/resolve.mjs +47 -0
- package/dist/esm/shared/localisedUrls/routes.mjs +89 -0
- package/dist/esm/shared/localisedUrls/types.mjs +0 -0
- package/dist/esm/shared/localisedUrls.mjs +1 -283
- package/dist/esm/shared/utils.mjs +1 -1
- package/dist/esm-node/cli/index.mjs +2 -1
- package/dist/esm-node/cli/locales.mjs +13 -13
- package/dist/esm-node/runtime/Link.mjs +1 -82
- package/dist/esm-node/runtime/context.mjs +76 -92
- package/dist/esm-node/runtime/contextHelpers.mjs +88 -0
- package/dist/esm-node/runtime/core.mjs +30 -133
- package/dist/esm-node/runtime/hooks.mjs +56 -23
- package/dist/esm-node/runtime/i18n/backend/defaults.node.mjs +5 -5
- package/dist/esm-node/runtime/i18n/detection/cache.mjs +104 -0
- package/dist/esm-node/runtime/i18n/detection/detector.mjs +63 -0
- package/dist/esm-node/runtime/i18n/detection/index.mjs +4 -260
- package/dist/esm-node/runtime/i18n/detection/initOptions.mjs +29 -0
- package/dist/esm-node/runtime/i18n/detection/language.mjs +22 -0
- package/dist/esm-node/runtime/i18n/detection/path.mjs +12 -0
- package/dist/esm-node/runtime/i18n/detection/priority.mjs +29 -0
- package/dist/esm-node/runtime/i18n/detection/ssr.mjs +27 -0
- package/dist/esm-node/runtime/i18n/detection/types.mjs +1 -0
- package/dist/esm-node/runtime/i18n/instance.mjs +35 -8
- package/dist/esm-node/runtime/linkHelpers.mjs +99 -0
- package/dist/esm-node/runtime/pluginSetup.mjs +65 -0
- package/dist/esm-node/runtime/providerComposition.mjs +78 -0
- package/dist/esm-node/runtime/reactI18next.mjs +6 -0
- package/dist/esm-node/runtime/routerAdapter.mjs +16 -8
- package/dist/esm-node/runtime/utils.mjs +2 -10
- package/dist/esm-node/server/apiPrefix.mjs +32 -0
- package/dist/esm-node/server/detectorOptions.mjs +60 -0
- package/dist/esm-node/server/index.mjs +8 -135
- package/dist/esm-node/server/redirectPolicy.mjs +47 -0
- package/dist/esm-node/shared/detection.mjs +1 -71
- package/dist/esm-node/shared/localisedUrls/config.mjs +12 -0
- package/dist/esm-node/shared/localisedUrls/index.mjs +8 -0
- package/dist/esm-node/shared/localisedUrls/normalise.mjs +38 -0
- package/dist/esm-node/shared/localisedUrls/pathname.mjs +25 -0
- package/dist/esm-node/shared/localisedUrls/patterns.mjs +87 -0
- package/dist/esm-node/shared/localisedUrls/redirect.mjs +57 -0
- package/dist/esm-node/shared/localisedUrls/resolve.mjs +48 -0
- package/dist/esm-node/shared/localisedUrls/routes.mjs +90 -0
- package/dist/esm-node/shared/localisedUrls/types.mjs +1 -0
- package/dist/esm-node/shared/localisedUrls.mjs +1 -283
- package/dist/esm-node/shared/utils.mjs +1 -1
- package/dist/types/cli/locales.d.ts +1 -1
- package/dist/types/runtime/Link.d.ts +3 -7
- package/dist/types/runtime/context.d.ts +21 -5
- package/dist/types/runtime/contextHelpers.d.ts +29 -0
- package/dist/types/runtime/core.d.ts +4 -8
- package/dist/types/runtime/hooks.d.ts +3 -7
- package/dist/types/runtime/i18n/backend/middleware.common.d.ts +2 -1
- package/dist/types/runtime/i18n/detection/cache.d.ts +14 -0
- package/dist/types/runtime/i18n/detection/detector.d.ts +8 -0
- package/dist/types/runtime/i18n/detection/index.d.ts +5 -50
- package/dist/types/runtime/i18n/detection/initOptions.d.ts +10 -0
- package/dist/types/runtime/i18n/detection/language.d.ts +11 -0
- package/dist/types/runtime/i18n/detection/middleware.d.ts +4 -4
- package/dist/types/runtime/i18n/detection/middleware.node.d.ts +4 -4
- package/dist/types/runtime/i18n/detection/path.d.ts +5 -0
- package/dist/types/runtime/i18n/detection/priority.d.ts +10 -0
- package/dist/types/runtime/i18n/detection/ssr.d.ts +9 -0
- package/dist/types/runtime/i18n/detection/types.d.ts +32 -0
- package/dist/types/runtime/i18n/instance.d.ts +31 -27
- package/dist/types/runtime/i18n/react-i18next.d.ts +1 -6
- package/dist/types/runtime/i18n/utils.d.ts +14 -5
- package/dist/types/runtime/linkHelpers.d.ts +17 -0
- package/dist/types/runtime/pluginSetup.d.ts +32 -0
- package/dist/types/runtime/providerComposition.d.ts +16 -0
- package/dist/types/runtime/reactI18next.d.ts +7 -0
- package/dist/types/runtime/routerAdapter.d.ts +7 -6
- package/dist/types/runtime/utils.d.ts +7 -2
- package/dist/types/server/apiPrefix.d.ts +7 -0
- package/dist/types/server/detectorOptions.d.ts +21 -0
- package/dist/types/server/index.d.ts +1 -6
- package/dist/types/server/redirectPolicy.d.ts +22 -0
- package/dist/types/shared/detection.d.ts +1 -11
- package/dist/types/shared/localisedUrls/config.d.ts +9 -0
- package/dist/types/shared/localisedUrls/index.d.ts +9 -0
- package/dist/types/shared/localisedUrls/normalise.d.ts +10 -0
- package/dist/types/shared/localisedUrls/pathname.d.ts +4 -0
- package/dist/types/shared/localisedUrls/patterns.d.ts +5 -0
- package/dist/types/shared/localisedUrls/redirect.d.ts +11 -0
- package/dist/types/shared/localisedUrls/resolve.d.ts +7 -0
- package/dist/types/shared/localisedUrls/routes.d.ts +3 -0
- package/dist/types/shared/localisedUrls/types.d.ts +14 -0
- package/dist/types/shared/localisedUrls.d.ts +2 -36
- package/dist/types/shared/utils.d.ts +0 -2
- package/package.json +17 -20
- package/rstest.config.mts +8 -1
- package/src/cli/index.ts +5 -4
- package/src/cli/locales.ts +3 -3
- package/src/runtime/Link.tsx +11 -138
- package/src/runtime/context.tsx +170 -207
- package/src/runtime/contextHelpers.ts +242 -0
- package/src/runtime/core.tsx +48 -275
- package/src/runtime/hooks.ts +86 -24
- package/src/runtime/i18n/backend/defaults.node.ts +2 -2
- package/src/runtime/i18n/backend/middleware.common.ts +5 -4
- package/src/runtime/i18n/detection/cache.ts +189 -0
- package/src/runtime/i18n/detection/detector.ts +166 -0
- package/src/runtime/i18n/detection/index.ts +10 -641
- package/src/runtime/i18n/detection/initOptions.ts +74 -0
- package/src/runtime/i18n/detection/language.ts +64 -0
- package/src/runtime/i18n/detection/middleware.node.ts +26 -7
- package/src/runtime/i18n/detection/middleware.ts +15 -7
- package/src/runtime/i18n/detection/path.ts +41 -0
- package/src/runtime/i18n/detection/priority.ts +82 -0
- package/src/runtime/i18n/detection/ssr.ts +47 -0
- package/src/runtime/i18n/detection/types.ts +41 -0
- package/src/runtime/i18n/instance.ts +98 -34
- package/src/runtime/i18n/react-i18next.ts +1 -6
- package/src/runtime/i18n/utils.ts +18 -9
- package/src/runtime/linkHelpers.ts +174 -0
- package/src/runtime/pluginSetup.ts +189 -0
- package/src/runtime/providerComposition.tsx +148 -0
- package/src/runtime/reactI18next.ts +20 -0
- package/src/runtime/routerAdapter.tsx +36 -15
- package/src/runtime/utils.ts +13 -35
- package/src/server/apiPrefix.ts +67 -0
- package/src/server/detectorOptions.ts +105 -0
- package/src/server/index.ts +14 -319
- package/src/server/redirectPolicy.ts +127 -0
- package/src/shared/detection.ts +1 -1
- package/src/shared/localisedUrls/config.ts +18 -0
- package/src/shared/localisedUrls/index.ts +24 -0
- package/src/shared/localisedUrls/normalise.ts +67 -0
- package/src/shared/localisedUrls/pathname.ts +60 -0
- package/src/shared/localisedUrls/patterns.ts +160 -0
- package/src/shared/localisedUrls/redirect.ts +93 -0
- package/src/shared/localisedUrls/resolve.ts +135 -0
- package/src/shared/localisedUrls/routes.ts +234 -0
- package/src/shared/localisedUrls/types.ts +18 -0
- package/src/shared/localisedUrls.ts +26 -623
- package/src/shared/utils.ts +2 -2
- package/tests/federatedI18nBoundary.client.test.tsx +112 -0
- package/tests/federatedI18nBoundary.test.tsx +120 -0
- package/tests/fixtures/localised-routes.golden.json +156 -0
- package/tests/link.test.tsx +111 -0
- package/tests/linkTypes.test.ts +15 -4
- package/tests/localisedUrlRewriteMatrix.fork.test.ts +147 -0
- package/tests/localisedUrls.test.ts +148 -2
- package/tests/reactI18nextRuntimeBoundary.test.ts +6 -3
- package/tests/redirectPolicy.test.ts +45 -0
- package/tests/routerAdapter.test.tsx +603 -0
- package/tests/type-fixture/fixture-globals.d.ts +11 -0
- package/tests/type-fixture/tsconfig.json +1 -0
package/dist/cjs/cli/index.js
CHANGED
|
@@ -64,7 +64,8 @@ const i18nPlugin = (options = {})=>({
|
|
|
64
64
|
backend: backendOptions,
|
|
65
65
|
...extendedConfig
|
|
66
66
|
};
|
|
67
|
-
const
|
|
67
|
+
const { reactI18next } = extendedConfig;
|
|
68
|
+
const runtimePluginPath = customPlugin?.runtime?.path || (false === reactI18next ? `@${metaName}/plugin-i18n/runtime/no-react-i18next` : `@${metaName}/plugin-i18n/runtime`);
|
|
68
69
|
plugins.push({
|
|
69
70
|
name: customPlugin?.runtime?.name || 'i18n',
|
|
70
71
|
path: runtimePluginPath,
|
package/dist/cjs/cli/locales.js
CHANGED
|
@@ -40,19 +40,19 @@ __webpack_require__.d(__webpack_exports__, {
|
|
|
40
40
|
applyDetectedBackendPaths: ()=>applyDetectedBackendPaths,
|
|
41
41
|
detectLocalesDirectory: ()=>detectLocalesDirectory
|
|
42
42
|
});
|
|
43
|
+
const external_node_fs_namespaceObject = require("node:fs");
|
|
44
|
+
var external_node_fs_default = /*#__PURE__*/ __webpack_require__.n(external_node_fs_namespaceObject);
|
|
45
|
+
const external_node_path_namespaceObject = require("node:path");
|
|
46
|
+
var external_node_path_default = /*#__PURE__*/ __webpack_require__.n(external_node_path_namespaceObject);
|
|
43
47
|
const server_core_namespaceObject = require("@modern-js/server-core");
|
|
44
|
-
const external_fs_namespaceObject = require("fs");
|
|
45
|
-
var external_fs_default = /*#__PURE__*/ __webpack_require__.n(external_fs_namespaceObject);
|
|
46
|
-
const external_path_namespaceObject = require("path");
|
|
47
|
-
var external_path_default = /*#__PURE__*/ __webpack_require__.n(external_path_namespaceObject);
|
|
48
48
|
const LOCALES_RESOURCE_PATTERN = '{{lng}}/{{ns}}.json';
|
|
49
49
|
function hasJsonFiles(dirPath) {
|
|
50
50
|
try {
|
|
51
|
-
if (!
|
|
52
|
-
const entries =
|
|
51
|
+
if (!external_node_fs_default().existsSync(dirPath) || !external_node_fs_default().statSync(dirPath).isDirectory()) return false;
|
|
52
|
+
const entries = external_node_fs_default().readdirSync(dirPath);
|
|
53
53
|
for (const entry of entries){
|
|
54
|
-
const entryPath =
|
|
55
|
-
const stat =
|
|
54
|
+
const entryPath = external_node_path_default().join(dirPath, entry);
|
|
55
|
+
const stat = external_node_fs_default().statSync(entryPath);
|
|
56
56
|
if (stat.isFile() && entry.endsWith('.json')) return true;
|
|
57
57
|
if (stat.isDirectory() && hasJsonFiles(entryPath)) return true;
|
|
58
58
|
}
|
|
@@ -62,7 +62,7 @@ function hasJsonFiles(dirPath) {
|
|
|
62
62
|
}
|
|
63
63
|
}
|
|
64
64
|
function toPosixPath(filePath) {
|
|
65
|
-
return filePath.split(
|
|
65
|
+
return filePath.split(external_node_path_default().sep).join(external_node_path_default().posix.sep);
|
|
66
66
|
}
|
|
67
67
|
function getPublicDirOutputPath(publicDir) {
|
|
68
68
|
return (0, server_core_namespaceObject.normalizePublicDirPath)(publicDir);
|
|
@@ -89,20 +89,20 @@ function detectLocalesDirectory(appDirectory, normalizedConfig) {
|
|
|
89
89
|
const publicDirPrefixes = (0, server_core_namespaceObject.getPublicDirRoutePrefixes)(normalizedConfig?.server?.publicDir);
|
|
90
90
|
for(let index = 0; index < publicDirs.length; index++){
|
|
91
91
|
const publicDir = publicDirs[index];
|
|
92
|
-
if (
|
|
93
|
-
const publicDirPath =
|
|
92
|
+
if (external_node_path_default().isAbsolute(publicDir)) continue;
|
|
93
|
+
const publicDirPath = external_node_path_default().join(appDirectory, publicDir);
|
|
94
94
|
const publicDirPrefix = publicDirPrefixes[index] || `/${(0, server_core_namespaceObject.normalizePublicDirPath)(publicDir)}`;
|
|
95
95
|
const publicDirOutputPath = getPublicDirOutputPath(publicDir);
|
|
96
|
-
if ('locales' ===
|
|
97
|
-
const localesPath =
|
|
96
|
+
if ('locales' === external_node_path_default().basename((0, server_core_namespaceObject.normalizePublicDirPath)(publicDir)) && hasJsonFiles(publicDirPath)) return buildDetectedLocalesDirectory(publicDirPrefix, `./${publicDirOutputPath}`);
|
|
97
|
+
const localesPath = external_node_path_default().join(publicDirPath, 'locales');
|
|
98
98
|
if (hasJsonFiles(localesPath)) return buildDetectedLocalesDirectory(`${publicDirPrefix}/locales`, `./${publicDirOutputPath}/locales`);
|
|
99
99
|
}
|
|
100
|
-
const configPublicPath =
|
|
100
|
+
const configPublicPath = external_node_path_default().join(appDirectory, 'config', 'public', 'locales');
|
|
101
101
|
if (hasJsonFiles(configPublicPath)) return buildDetectedLocalesDirectory('/locales', './public/locales', [
|
|
102
102
|
'./config/public/locales',
|
|
103
103
|
'./public/locales'
|
|
104
104
|
]);
|
|
105
|
-
const rootLocalesPath =
|
|
105
|
+
const rootLocalesPath = external_node_path_default().join(appDirectory, 'locales');
|
|
106
106
|
if (hasJsonFiles(rootLocalesPath)) return buildDetectedLocalesDirectory('/locales', './locales');
|
|
107
107
|
}
|
|
108
108
|
function applyDetectedBackendPaths(backendOptions, detectedLocales) {
|
package/dist/cjs/runtime/Link.js
CHANGED
|
@@ -30,97 +30,16 @@ __webpack_require__.r(__webpack_exports__);
|
|
|
30
30
|
__webpack_require__.d(__webpack_exports__, {
|
|
31
31
|
Link: ()=>Link,
|
|
32
32
|
default: ()=>runtime_Link,
|
|
33
|
-
interpolateRouteParams: ()=>interpolateRouteParams
|
|
33
|
+
interpolateRouteParams: ()=>external_linkHelpers_js_namespaceObject.interpolateRouteParams
|
|
34
34
|
});
|
|
35
35
|
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
|
|
36
36
|
const external_react_namespaceObject = require("react");
|
|
37
37
|
const external_context_js_namespaceObject = require("./context.js");
|
|
38
|
+
const external_linkHelpers_js_namespaceObject = require("./linkHelpers.js");
|
|
38
39
|
const external_localizedPaths_js_namespaceObject = require("./localizedPaths.js");
|
|
39
40
|
const external_routerAdapter_js_namespaceObject = require("./routerAdapter.js");
|
|
40
41
|
const external_utils_js_namespaceObject = require("./utils.js");
|
|
41
42
|
const EXTERNAL_TARGET_RE = /^(?:[a-z][a-z0-9+.-]*:|\/\/)/i;
|
|
42
|
-
const warnedTargets = new Set();
|
|
43
|
-
const warnOnce = (key, message)=>{
|
|
44
|
-
if ('development' !== process.env.NODE_ENV || warnedTargets.has(key)) return;
|
|
45
|
-
warnedTargets.add(key);
|
|
46
|
-
console.warn(message);
|
|
47
|
-
};
|
|
48
|
-
const interpolateRouteParams = (pathname, params)=>{
|
|
49
|
-
if (!/[$:*{]/.test(pathname)) return pathname;
|
|
50
|
-
const resolveParam = (name)=>{
|
|
51
|
-
const value = params?.[name];
|
|
52
|
-
return void 0 === value ? void 0 : String(value);
|
|
53
|
-
};
|
|
54
|
-
const segments = pathname.split('/').map((segment)=>{
|
|
55
|
-
if (!segment) return segment;
|
|
56
|
-
if (segment.startsWith('{-$') && segment.endsWith('}')) {
|
|
57
|
-
const value = resolveParam(segment.slice(3, -1));
|
|
58
|
-
return void 0 === value ? null : encodeURIComponent(value);
|
|
59
|
-
}
|
|
60
|
-
if ('$' === segment || '*' === segment) {
|
|
61
|
-
const value = resolveParam('_splat') ?? resolveParam('*');
|
|
62
|
-
return void 0 === value ? null : value.split('/').map(encodeURIComponent).join('/');
|
|
63
|
-
}
|
|
64
|
-
if (segment.startsWith('$')) {
|
|
65
|
-
const value = resolveParam(segment.slice(1));
|
|
66
|
-
if (void 0 === value) {
|
|
67
|
-
warnOnce(`missing-param:${pathname}:${segment}`, `[plugin-i18n] <Link to="${pathname}"> is missing required param "${segment.slice(1)}".`);
|
|
68
|
-
return segment;
|
|
69
|
-
}
|
|
70
|
-
return encodeURIComponent(value);
|
|
71
|
-
}
|
|
72
|
-
if (segment.startsWith(':')) {
|
|
73
|
-
const optional = segment.endsWith('?');
|
|
74
|
-
const name = segment.slice(1, optional ? -1 : void 0);
|
|
75
|
-
const value = resolveParam(name);
|
|
76
|
-
if (void 0 === value) {
|
|
77
|
-
if (optional) return null;
|
|
78
|
-
warnOnce(`missing-param:${pathname}:${segment}`, `[plugin-i18n] <Link to="${pathname}"> is missing required param "${name}".`);
|
|
79
|
-
return segment;
|
|
80
|
-
}
|
|
81
|
-
return encodeURIComponent(value);
|
|
82
|
-
}
|
|
83
|
-
return segment;
|
|
84
|
-
}).filter((segment)=>null !== segment);
|
|
85
|
-
return segments.join('/') || '/';
|
|
86
|
-
};
|
|
87
|
-
const normalizeSearch = (search, searchFromTo)=>{
|
|
88
|
-
if (search && 'object' == typeof search) {
|
|
89
|
-
const entries = Object.entries(search).filter(([, value])=>null != value);
|
|
90
|
-
const searchObject = Object.fromEntries(entries.map(([key, value])=>[
|
|
91
|
-
key,
|
|
92
|
-
String(value)
|
|
93
|
-
]));
|
|
94
|
-
const params = new URLSearchParams(searchObject);
|
|
95
|
-
const serialized = params.toString();
|
|
96
|
-
return {
|
|
97
|
-
searchString: serialized ? `?${serialized}` : '',
|
|
98
|
-
searchObject
|
|
99
|
-
};
|
|
100
|
-
}
|
|
101
|
-
const raw = 'string' == typeof search && search ? search : searchFromTo;
|
|
102
|
-
if (!raw) return {
|
|
103
|
-
searchString: '',
|
|
104
|
-
searchObject: void 0
|
|
105
|
-
};
|
|
106
|
-
const searchString = raw.startsWith('?') ? raw : `?${raw}`;
|
|
107
|
-
const searchObject = {};
|
|
108
|
-
new URLSearchParams(searchString).forEach((value, key)=>{
|
|
109
|
-
searchObject[key] = value;
|
|
110
|
-
});
|
|
111
|
-
return {
|
|
112
|
-
searchString,
|
|
113
|
-
searchObject
|
|
114
|
-
};
|
|
115
|
-
};
|
|
116
|
-
const splitActiveProps = (active, activeProps)=>{
|
|
117
|
-
if (!active || !activeProps) return {};
|
|
118
|
-
return activeProps;
|
|
119
|
-
};
|
|
120
|
-
const mergeClassNames = (...values)=>{
|
|
121
|
-
const classNames = values.filter((value)=>'string' == typeof value && value.length > 0);
|
|
122
|
-
return classNames.length > 0 ? classNames.join(' ') : void 0;
|
|
123
|
-
};
|
|
124
43
|
const Link = (props)=>{
|
|
125
44
|
const { to, params, children, hash: hashProp, search: searchProp, hashScrollIntoView, activeOptions, activeProps, prefetch, preload, ...rest } = props;
|
|
126
45
|
const adapter = (0, external_routerAdapter_js_namespaceObject.useI18nRouterAdapter)();
|
|
@@ -134,12 +53,12 @@ const Link = (props)=>{
|
|
|
134
53
|
const target = (0, external_react_namespaceObject.useMemo)(()=>{
|
|
135
54
|
if (isExternal || isBareHash) return null;
|
|
136
55
|
const { pathname, search: searchFromTo, hash: hashFromTo } = (0, external_utils_js_namespaceObject.splitUrlTarget)(to);
|
|
137
|
-
const interpolated = interpolateRouteParams(pathname || '/', params);
|
|
56
|
+
const interpolated = (0, external_linkHelpers_js_namespaceObject.interpolateRouteParams)(pathname || '/', params);
|
|
138
57
|
const firstSegment = interpolated.split('/').filter(Boolean)[0];
|
|
139
|
-
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.`);
|
|
58
|
+
if (firstSegment && supportedLanguages.includes(firstSegment)) (0, external_linkHelpers_js_namespaceObject.warnOnce)(`lang-prefix:${to}`, `[plugin-i18n] <Link to="${to}"> starts with a language prefix. Write language-agnostic canonical paths; the Link localizes them automatically.`);
|
|
140
59
|
const localizedPathname = (0, external_utils_js_namespaceObject.buildLocalizedUrl)(interpolated, language, supportedLanguages, localisedUrls);
|
|
141
60
|
const hash = hashProp ?? (hashFromTo ? hashFromTo.slice(1) : '');
|
|
142
|
-
const { searchString, searchObject } = normalizeSearch(searchProp, searchFromTo);
|
|
61
|
+
const { searchString, searchObject } = (0, external_linkHelpers_js_namespaceObject.normalizeSearch)(searchProp, searchFromTo);
|
|
143
62
|
return {
|
|
144
63
|
canonicalPathname: interpolated,
|
|
145
64
|
localizedPathname,
|
|
@@ -174,7 +93,7 @@ const Link = (props)=>{
|
|
|
174
93
|
supportedLanguages,
|
|
175
94
|
localisedUrls
|
|
176
95
|
]);
|
|
177
|
-
const resolvedActiveProps = splitActiveProps(isActive, activeProps);
|
|
96
|
+
const resolvedActiveProps = (0, external_linkHelpers_js_namespaceObject.splitActiveProps)(isActive, activeProps);
|
|
178
97
|
const activeAttributes = isActive ? {
|
|
179
98
|
'data-status': 'active',
|
|
180
99
|
'aria-current': rest['aria-current'] ?? resolvedActiveProps['aria-current'] ?? 'page'
|
|
@@ -196,7 +115,7 @@ const Link = (props)=>{
|
|
|
196
115
|
...anchorProps,
|
|
197
116
|
...activeRest,
|
|
198
117
|
...activeAttributes,
|
|
199
|
-
className: mergeClassNames(rest.className, activeClassName),
|
|
118
|
+
className: (0, external_linkHelpers_js_namespaceObject.mergeClassNames)(rest.className, activeClassName),
|
|
200
119
|
style: {
|
|
201
120
|
...rest.style,
|
|
202
121
|
...activeStyle
|
|
@@ -205,7 +124,7 @@ const Link = (props)=>{
|
|
|
205
124
|
});
|
|
206
125
|
}
|
|
207
126
|
const { className: activeClassName, style: activeStyle, ...activeRest } = resolvedActiveProps;
|
|
208
|
-
const mergedClassName = mergeClassNames(rest.className, activeClassName);
|
|
127
|
+
const mergedClassName = (0, external_linkHelpers_js_namespaceObject.mergeClassNames)(rest.className, activeClassName);
|
|
209
128
|
const mergedStyle = {
|
|
210
129
|
...rest.style,
|
|
211
130
|
...activeStyle
|
|
@@ -28,15 +28,15 @@ var __webpack_require__ = {};
|
|
|
28
28
|
var __webpack_exports__ = {};
|
|
29
29
|
__webpack_require__.r(__webpack_exports__);
|
|
30
30
|
__webpack_require__.d(__webpack_exports__, {
|
|
31
|
+
FederatedI18nBoundary: ()=>FederatedI18nBoundary,
|
|
31
32
|
ModernI18nProvider: ()=>ModernI18nProvider,
|
|
32
33
|
useModernI18n: ()=>useModernI18n
|
|
33
34
|
});
|
|
34
35
|
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
|
|
35
|
-
const runtime_namespaceObject = require("@modern-js/runtime");
|
|
36
36
|
const external_react_namespaceObject = require("react");
|
|
37
|
-
const
|
|
37
|
+
const external_contextHelpers_js_namespaceObject = require("./contextHelpers.js");
|
|
38
|
+
const instance_js_namespaceObject = require("./i18n/instance.js");
|
|
38
39
|
const external_routerAdapter_js_namespaceObject = require("./routerAdapter.js");
|
|
39
|
-
const external_utils_js_namespaceObject = require("./utils.js");
|
|
40
40
|
const modernI18nContextKey = Symbol.for('@modern-js/plugin-i18n/runtime/ModernI18nContext');
|
|
41
41
|
const getModernI18nContext = ()=>{
|
|
42
42
|
const globalStore = globalThis;
|
|
@@ -48,76 +48,87 @@ const ModernI18nProvider = ({ children, value })=>/*#__PURE__*/ (0, jsx_runtime_
|
|
|
48
48
|
value: value,
|
|
49
49
|
children: children
|
|
50
50
|
});
|
|
51
|
+
const FederatedI18nBoundary = ({ children, defaultNamespace, fallbackLanguage, resources, supportedLanguages })=>{
|
|
52
|
+
const parent = (0, external_react_namespaceObject.useContext)(ModernI18nContext);
|
|
53
|
+
if (!parent) throw new Error('FederatedI18nBoundary must be used within ModernI18nProvider');
|
|
54
|
+
const languages = supportedLanguages ?? parent.languages ?? Object.keys(resources);
|
|
55
|
+
const scopedInstance = (0, external_react_namespaceObject.useMemo)(()=>{
|
|
56
|
+
const parentInstance = (0, instance_js_namespaceObject.getActualI18nextInstance)(parent.i18nInstance);
|
|
57
|
+
const clone = parentInstance.cloneInstance?.({
|
|
58
|
+
defaultNS: defaultNamespace,
|
|
59
|
+
fallbackLng: fallbackLanguage ?? languages[0] ?? parent.language,
|
|
60
|
+
forkResourceStore: true,
|
|
61
|
+
initImmediate: false,
|
|
62
|
+
lng: parent.language,
|
|
63
|
+
ns: [
|
|
64
|
+
defaultNamespace
|
|
65
|
+
],
|
|
66
|
+
resources,
|
|
67
|
+
supportedLngs: languages
|
|
68
|
+
});
|
|
69
|
+
if (!clone) throw new Error('FederatedI18nBoundary requires an i18n instance with cloneInstance support');
|
|
70
|
+
const resourceStore = clone.store;
|
|
71
|
+
const parentResourceStore = parentInstance.store;
|
|
72
|
+
const sharesNestedResourceState = Object.entries(resourceStore?.data ?? {}).some(([language, namespaces])=>{
|
|
73
|
+
const parentNamespaces = parentResourceStore?.data?.[language];
|
|
74
|
+
return namespaces === parentNamespaces || Object.entries(namespaces).some(([namespace, resource])=>null !== resource && 'object' == typeof resource && resource === parentNamespaces?.[namespace]);
|
|
75
|
+
});
|
|
76
|
+
if (clone === parentInstance || resourceStore === parentResourceStore || resourceStore?.data === parentResourceStore?.data || sharesNestedResourceState) throw new Error('FederatedI18nBoundary cloneInstance did not isolate the host resource store');
|
|
77
|
+
if (!resourceStore?.addResourceBundle || !clone.removeResourceBundle || !resourceStore.data) throw new Error('FederatedI18nBoundary requires an isolated mutable i18n resource store');
|
|
78
|
+
for (const [language, namespaces] of Object.entries(resourceStore.data))for (const namespace of Object.keys(namespaces))clone.removeResourceBundle(language, namespace);
|
|
79
|
+
for (const [language, namespaces] of Object.entries(resources))for (const [namespace, resource] of Object.entries(namespaces))resourceStore.addResourceBundle(language, namespace, resource, true, true);
|
|
80
|
+
return clone;
|
|
81
|
+
}, [
|
|
82
|
+
defaultNamespace,
|
|
83
|
+
fallbackLanguage,
|
|
84
|
+
languages,
|
|
85
|
+
parent.i18nInstance,
|
|
86
|
+
parent.language,
|
|
87
|
+
resources
|
|
88
|
+
]);
|
|
89
|
+
const value = (0, external_react_namespaceObject.useMemo)(()=>({
|
|
90
|
+
...parent,
|
|
91
|
+
i18nInstance: scopedInstance,
|
|
92
|
+
language: parent.language,
|
|
93
|
+
languages
|
|
94
|
+
}), [
|
|
95
|
+
languages,
|
|
96
|
+
parent,
|
|
97
|
+
scopedInstance
|
|
98
|
+
]);
|
|
99
|
+
return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(ModernI18nProvider, {
|
|
100
|
+
value: value,
|
|
101
|
+
children: children
|
|
102
|
+
});
|
|
103
|
+
};
|
|
51
104
|
const useModernI18n = ()=>{
|
|
52
105
|
const context = (0, external_react_namespaceObject.useContext)(ModernI18nContext);
|
|
53
|
-
if (!context) throw new Error('useModernI18n must be used within
|
|
54
|
-
const { language: contextLanguage, i18nInstance, languages, localePathRedirect, ignoreRedirectRoutes, localisedUrls, updateLanguage } = context;
|
|
106
|
+
if (!context) throw new Error('useModernI18n must be used within ModernI18nProvider');
|
|
107
|
+
const { language: contextLanguage, i18nInstance, languages, localePathRedirect, ignoreRedirectRoutes, localisedUrls, updateLanguage, synchronizeLanguage } = context;
|
|
55
108
|
const { navigate, location, hasRouter } = (0, external_routerAdapter_js_namespaceObject.useI18nRouterAdapter)();
|
|
56
|
-
const pathLanguage = (0, external_react_namespaceObject.useMemo)(()=>
|
|
57
|
-
if (!localePathRedirect || !location?.pathname) return;
|
|
58
|
-
const detected = (0, external_utils_js_namespaceObject.detectLanguageFromPath)(location.pathname, languages || [], localePathRedirect);
|
|
59
|
-
return detected.detected ? detected.language : void 0;
|
|
60
|
-
}, [
|
|
109
|
+
const pathLanguage = (0, external_react_namespaceObject.useMemo)(()=>(0, external_contextHelpers_js_namespaceObject.getPathLanguage)(location?.pathname, languages, localePathRedirect), [
|
|
61
110
|
languages,
|
|
62
111
|
localePathRedirect,
|
|
63
112
|
location?.pathname
|
|
64
113
|
]);
|
|
65
|
-
const currentLanguage = pathLanguage || contextLanguage;
|
|
66
114
|
(0, external_react_namespaceObject.useEffect)(()=>{
|
|
67
|
-
if (
|
|
68
|
-
updateLanguage?.(pathLanguage);
|
|
69
|
-
i18nInstance?.setLang?.(pathLanguage);
|
|
70
|
-
i18nInstance?.changeLanguage?.(pathLanguage);
|
|
71
|
-
if ((0, runtime_namespaceObject.isBrowser)()) {
|
|
72
|
-
const detectionOptions = i18nInstance.options?.detection;
|
|
73
|
-
(0, index_js_namespaceObject.cacheUserLanguage)(i18nInstance, pathLanguage, detectionOptions);
|
|
74
|
-
}
|
|
115
|
+
if (pathLanguage) synchronizeLanguage?.(pathLanguage);
|
|
75
116
|
}, [
|
|
76
|
-
contextLanguage,
|
|
77
|
-
i18nInstance,
|
|
78
117
|
pathLanguage,
|
|
79
|
-
|
|
118
|
+
synchronizeLanguage
|
|
80
119
|
]);
|
|
81
|
-
const
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
const relativePath = currentPath.replace(entryPath, '');
|
|
94
|
-
const pathLanguage = (0, external_utils_js_namespaceObject.detectLanguageFromPath)(currentPath, languages || [], localePathRedirect);
|
|
95
|
-
if (pathLanguage.detected && pathLanguage.language === newLang) return;
|
|
96
|
-
if (!(0, external_utils_js_namespaceObject.shouldIgnoreRedirect)(relativePath, languages || [], ignoreRedirectRoutes)) {
|
|
97
|
-
const newPath = (0, external_utils_js_namespaceObject.buildLocalizedUrl)(relativePath, newLang, languages || [], localisedUrls);
|
|
98
|
-
const newUrl = entryPath + newPath + location.search + location.hash;
|
|
99
|
-
await navigate(newUrl, {
|
|
100
|
-
replace: true
|
|
101
|
-
});
|
|
102
|
-
}
|
|
103
|
-
} else if (localePathRedirect && (0, runtime_namespaceObject.isBrowser)() && !hasRouter) {
|
|
104
|
-
const currentPath = window.location.pathname;
|
|
105
|
-
const entryPath = (0, external_utils_js_namespaceObject.getEntryPath)();
|
|
106
|
-
const relativePath = currentPath.replace(entryPath, '');
|
|
107
|
-
const pathLanguage = (0, external_utils_js_namespaceObject.detectLanguageFromPath)(currentPath, languages || [], localePathRedirect);
|
|
108
|
-
if (pathLanguage.detected && pathLanguage.language === newLang) return;
|
|
109
|
-
if (!(0, external_utils_js_namespaceObject.shouldIgnoreRedirect)(relativePath, languages || [], ignoreRedirectRoutes)) {
|
|
110
|
-
const newPath = (0, external_utils_js_namespaceObject.buildLocalizedUrl)(relativePath, newLang, languages || [], localisedUrls);
|
|
111
|
-
const newUrl = entryPath + newPath + window.location.search + window.location.hash;
|
|
112
|
-
window.history.pushState(null, '', newUrl);
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
if (updateLanguage) updateLanguage(newLang);
|
|
116
|
-
} catch (error) {
|
|
117
|
-
console.error('Failed to change language:', error);
|
|
118
|
-
throw error;
|
|
119
|
-
}
|
|
120
|
-
}, [
|
|
120
|
+
const currentLanguage = contextLanguage;
|
|
121
|
+
const changeLanguage = (0, external_react_namespaceObject.useCallback)((newLang)=>(0, external_contextHelpers_js_namespaceObject.changeModernI18nLanguage)(newLang, {
|
|
122
|
+
i18nInstance,
|
|
123
|
+
updateLanguage,
|
|
124
|
+
localePathRedirect,
|
|
125
|
+
ignoreRedirectRoutes,
|
|
126
|
+
localisedUrls,
|
|
127
|
+
languages,
|
|
128
|
+
hasRouter,
|
|
129
|
+
navigate,
|
|
130
|
+
location
|
|
131
|
+
}), [
|
|
121
132
|
i18nInstance,
|
|
122
133
|
updateLanguage,
|
|
123
134
|
localePathRedirect,
|
|
@@ -128,40 +139,14 @@ const useModernI18n = ()=>{
|
|
|
128
139
|
navigate,
|
|
129
140
|
location
|
|
130
141
|
]);
|
|
131
|
-
const t = (0, external_react_namespaceObject.useCallback)((key, ...args)=>
|
|
132
|
-
if ('function' != typeof i18nInstance.t) throw new Error('i18nInstance.t is required');
|
|
133
|
-
return i18nInstance.t(key, ...args);
|
|
134
|
-
}, [
|
|
142
|
+
const t = (0, external_react_namespaceObject.useCallback)((key, ...args)=>(0, external_contextHelpers_js_namespaceObject.translateI18n)(i18nInstance, key, ...args), [
|
|
135
143
|
currentLanguage,
|
|
136
144
|
i18nInstance
|
|
137
145
|
]);
|
|
138
|
-
const isLanguageSupported = (0, external_react_namespaceObject.useCallback)((lang)=>languages
|
|
146
|
+
const isLanguageSupported = (0, external_react_namespaceObject.useCallback)((lang)=>(0, external_contextHelpers_js_namespaceObject.isI18nLanguageSupported)(languages, lang), [
|
|
139
147
|
languages
|
|
140
148
|
]);
|
|
141
|
-
const isResourcesReady = (0, external_react_namespaceObject.useMemo)(()=>
|
|
142
|
-
if (!i18nInstance?.isInitialized) return false;
|
|
143
|
-
const backend = i18nInstance?.services?.backend;
|
|
144
|
-
if (backend && 'function' == typeof backend.isLoading) {
|
|
145
|
-
const loadingResources = backend.getLoadingResources();
|
|
146
|
-
const isCurrentLanguageLoading = loadingResources.some(({ language })=>language === currentLanguage);
|
|
147
|
-
if (isCurrentLanguageLoading) return false;
|
|
148
|
-
}
|
|
149
|
-
const store = i18nInstance.store;
|
|
150
|
-
if (!store?.data) return false;
|
|
151
|
-
const langData = store.data[currentLanguage];
|
|
152
|
-
if (!langData || 'object' != typeof langData) return false;
|
|
153
|
-
const options = i18nInstance.options;
|
|
154
|
-
const namespaces = options?.ns || options?.defaultNS || [
|
|
155
|
-
'translation'
|
|
156
|
-
];
|
|
157
|
-
const requiredNamespaces = Array.isArray(namespaces) ? namespaces : [
|
|
158
|
-
namespaces
|
|
159
|
-
];
|
|
160
|
-
return requiredNamespaces.every((ns)=>{
|
|
161
|
-
const nsData = langData[ns];
|
|
162
|
-
return nsData && 'object' == typeof nsData && Object.keys(nsData).length > 0;
|
|
163
|
-
});
|
|
164
|
-
}, [
|
|
149
|
+
const isResourcesReady = (0, external_react_namespaceObject.useMemo)(()=>(0, external_contextHelpers_js_namespaceObject.isI18nResourcesReady)(i18nInstance, currentLanguage), [
|
|
165
150
|
currentLanguage,
|
|
166
151
|
i18nInstance
|
|
167
152
|
]);
|
|
@@ -176,9 +161,11 @@ const useModernI18n = ()=>{
|
|
|
176
161
|
isResourcesReady
|
|
177
162
|
};
|
|
178
163
|
};
|
|
164
|
+
exports.FederatedI18nBoundary = __webpack_exports__.FederatedI18nBoundary;
|
|
179
165
|
exports.ModernI18nProvider = __webpack_exports__.ModernI18nProvider;
|
|
180
166
|
exports.useModernI18n = __webpack_exports__.useModernI18n;
|
|
181
167
|
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
168
|
+
"FederatedI18nBoundary",
|
|
182
169
|
"ModernI18nProvider",
|
|
183
170
|
"useModernI18n"
|
|
184
171
|
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __webpack_require__ = {};
|
|
3
|
+
(()=>{
|
|
4
|
+
__webpack_require__.d = (exports1, getters, values)=>{
|
|
5
|
+
var define = (defs, kind)=>{
|
|
6
|
+
for(var key in defs)if (__webpack_require__.o(defs, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
[kind]: defs[key]
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
define(getters, "get");
|
|
12
|
+
define(values, "value");
|
|
13
|
+
};
|
|
14
|
+
})();
|
|
15
|
+
(()=>{
|
|
16
|
+
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
17
|
+
})();
|
|
18
|
+
(()=>{
|
|
19
|
+
__webpack_require__.r = (exports1)=>{
|
|
20
|
+
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
21
|
+
value: 'Module'
|
|
22
|
+
});
|
|
23
|
+
Object.defineProperty(exports1, '__esModule', {
|
|
24
|
+
value: true
|
|
25
|
+
});
|
|
26
|
+
};
|
|
27
|
+
})();
|
|
28
|
+
var __webpack_exports__ = {};
|
|
29
|
+
__webpack_require__.r(__webpack_exports__);
|
|
30
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
31
|
+
cacheI18nLanguage: ()=>cacheI18nLanguage,
|
|
32
|
+
changeI18nInstanceLanguage: ()=>changeI18nInstanceLanguage,
|
|
33
|
+
changeModernI18nLanguage: ()=>changeModernI18nLanguage,
|
|
34
|
+
getPathLanguage: ()=>getPathLanguage,
|
|
35
|
+
isI18nLanguageSupported: ()=>isI18nLanguageSupported,
|
|
36
|
+
isI18nResourcesReady: ()=>isI18nResourcesReady,
|
|
37
|
+
translateI18n: ()=>translateI18n
|
|
38
|
+
});
|
|
39
|
+
const runtime_namespaceObject = require("@modern-js/runtime");
|
|
40
|
+
const index_js_namespaceObject = require("./i18n/detection/index.js");
|
|
41
|
+
const external_utils_js_namespaceObject = require("./utils.js");
|
|
42
|
+
function getPathLanguage(pathname, languages, localePathRedirect) {
|
|
43
|
+
if (!localePathRedirect || !pathname) return;
|
|
44
|
+
const detected = (0, external_utils_js_namespaceObject.detectLanguageFromPath)(pathname, languages || [], localePathRedirect);
|
|
45
|
+
return detected.detected ? detected.language : void 0;
|
|
46
|
+
}
|
|
47
|
+
function cacheI18nLanguage(i18nInstance, language) {
|
|
48
|
+
if ((0, runtime_namespaceObject.isBrowser)()) {
|
|
49
|
+
const detectionOptions = i18nInstance.options?.detection;
|
|
50
|
+
(0, index_js_namespaceObject.cacheUserLanguage)(i18nInstance, language, detectionOptions);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
async function changeI18nInstanceLanguage(i18nInstance, language) {
|
|
54
|
+
if (i18nInstance.setLang) await i18nInstance.setLang(language);
|
|
55
|
+
else await i18nInstance.changeLanguage?.(language);
|
|
56
|
+
}
|
|
57
|
+
async function changeModernI18nLanguage(newLang, options) {
|
|
58
|
+
const { i18nInstance, updateLanguage, localePathRedirect, ignoreRedirectRoutes, localisedUrls, languages, hasRouter, navigate, location } = options;
|
|
59
|
+
try {
|
|
60
|
+
if (!newLang || 'string' != typeof newLang) throw new Error('Language must be non-empty string');
|
|
61
|
+
await changeI18nInstanceLanguage(i18nInstance, newLang);
|
|
62
|
+
cacheI18nLanguage(i18nInstance, newLang);
|
|
63
|
+
if (localePathRedirect && (0, runtime_namespaceObject.isBrowser)() && hasRouter && navigate && location) {
|
|
64
|
+
const currentPath = location.pathname;
|
|
65
|
+
const entryPath = (0, external_utils_js_namespaceObject.getEntryPath)();
|
|
66
|
+
const relativePath = currentPath.replace(entryPath, '');
|
|
67
|
+
const pathLanguage = (0, external_utils_js_namespaceObject.detectLanguageFromPath)(currentPath, languages || [], localePathRedirect);
|
|
68
|
+
if (pathLanguage.detected && pathLanguage.language === newLang) return void updateLanguage?.(newLang);
|
|
69
|
+
if (!(0, external_utils_js_namespaceObject.shouldIgnoreRedirect)(relativePath, languages || [], ignoreRedirectRoutes)) {
|
|
70
|
+
const newPath = (0, external_utils_js_namespaceObject.buildLocalizedUrl)(relativePath, newLang, languages || [], localisedUrls);
|
|
71
|
+
const newUrl = entryPath + newPath + location.search + location.hash;
|
|
72
|
+
await navigate(newUrl, {
|
|
73
|
+
replace: true
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
} else if (localePathRedirect && (0, runtime_namespaceObject.isBrowser)() && !hasRouter) {
|
|
77
|
+
const currentPath = window.location.pathname;
|
|
78
|
+
const entryPath = (0, external_utils_js_namespaceObject.getEntryPath)();
|
|
79
|
+
const relativePath = currentPath.replace(entryPath, '');
|
|
80
|
+
const pathLanguage = (0, external_utils_js_namespaceObject.detectLanguageFromPath)(currentPath, languages || [], localePathRedirect);
|
|
81
|
+
if (pathLanguage.detected && pathLanguage.language === newLang) return void updateLanguage?.(newLang);
|
|
82
|
+
if (!(0, external_utils_js_namespaceObject.shouldIgnoreRedirect)(relativePath, languages || [], ignoreRedirectRoutes)) {
|
|
83
|
+
const newPath = (0, external_utils_js_namespaceObject.buildLocalizedUrl)(relativePath, newLang, languages || [], localisedUrls);
|
|
84
|
+
const newUrl = entryPath + newPath + window.location.search + window.location.hash;
|
|
85
|
+
window.history.pushState(null, '', newUrl);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
if (updateLanguage) updateLanguage(newLang);
|
|
89
|
+
} catch (error) {
|
|
90
|
+
console.error('Failed change language:', error);
|
|
91
|
+
throw error;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
function translateI18n(i18nInstance, key, ...args) {
|
|
95
|
+
if ('function' != typeof i18nInstance.t) throw new Error('i18nInstance.t required');
|
|
96
|
+
return i18nInstance.t(key, ...args);
|
|
97
|
+
}
|
|
98
|
+
function isI18nLanguageSupported(languages, lang) {
|
|
99
|
+
return languages?.includes(lang) || false;
|
|
100
|
+
}
|
|
101
|
+
function isI18nResourcesReady(i18nInstance, currentLanguage) {
|
|
102
|
+
if (!i18nInstance?.isInitialized) return false;
|
|
103
|
+
const backend = i18nInstance?.services?.backend;
|
|
104
|
+
if (backend && 'function' == typeof backend.isLoading) {
|
|
105
|
+
const loadingResources = backend.getLoadingResources();
|
|
106
|
+
const isCurrentLanguageLoading = loadingResources.some(({ language })=>language === currentLanguage);
|
|
107
|
+
if (isCurrentLanguageLoading) return false;
|
|
108
|
+
}
|
|
109
|
+
const store = i18nInstance.store;
|
|
110
|
+
if (!store?.data) return false;
|
|
111
|
+
const langData = store.data[currentLanguage];
|
|
112
|
+
if (!langData || 'object' != typeof langData) return false;
|
|
113
|
+
const options = i18nInstance.options;
|
|
114
|
+
const namespaces = options?.ns || options?.defaultNS || [
|
|
115
|
+
'translation'
|
|
116
|
+
];
|
|
117
|
+
const requiredNamespaces = Array.isArray(namespaces) ? namespaces : [
|
|
118
|
+
namespaces
|
|
119
|
+
];
|
|
120
|
+
return requiredNamespaces.every((ns)=>{
|
|
121
|
+
const nsData = langData[ns];
|
|
122
|
+
return nsData && 'object' == typeof nsData && Object.keys(nsData).length > 0;
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
exports.cacheI18nLanguage = __webpack_exports__.cacheI18nLanguage;
|
|
126
|
+
exports.changeI18nInstanceLanguage = __webpack_exports__.changeI18nInstanceLanguage;
|
|
127
|
+
exports.changeModernI18nLanguage = __webpack_exports__.changeModernI18nLanguage;
|
|
128
|
+
exports.getPathLanguage = __webpack_exports__.getPathLanguage;
|
|
129
|
+
exports.isI18nLanguageSupported = __webpack_exports__.isI18nLanguageSupported;
|
|
130
|
+
exports.isI18nResourcesReady = __webpack_exports__.isI18nResourcesReady;
|
|
131
|
+
exports.translateI18n = __webpack_exports__.translateI18n;
|
|
132
|
+
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
133
|
+
"cacheI18nLanguage",
|
|
134
|
+
"changeI18nInstanceLanguage",
|
|
135
|
+
"changeModernI18nLanguage",
|
|
136
|
+
"getPathLanguage",
|
|
137
|
+
"isI18nLanguageSupported",
|
|
138
|
+
"isI18nResourcesReady",
|
|
139
|
+
"translateI18n"
|
|
140
|
+
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
141
|
+
Object.defineProperty(exports, '__esModule', {
|
|
142
|
+
value: true
|
|
143
|
+
});
|