@bleedingdev/modern-js-plugin-i18n 3.2.0-ultramodern.0

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.
Files changed (147) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +32 -0
  3. package/dist/cjs/cli/index.js +154 -0
  4. package/dist/cjs/runtime/I18nLink.js +68 -0
  5. package/dist/cjs/runtime/context.js +142 -0
  6. package/dist/cjs/runtime/hooks.js +194 -0
  7. package/dist/cjs/runtime/i18n/backend/config.js +39 -0
  8. package/dist/cjs/runtime/i18n/backend/defaults.js +56 -0
  9. package/dist/cjs/runtime/i18n/backend/defaults.node.js +56 -0
  10. package/dist/cjs/runtime/i18n/backend/index.js +108 -0
  11. package/dist/cjs/runtime/i18n/backend/middleware.common.js +105 -0
  12. package/dist/cjs/runtime/i18n/backend/middleware.js +54 -0
  13. package/dist/cjs/runtime/i18n/backend/middleware.node.js +58 -0
  14. package/dist/cjs/runtime/i18n/backend/sdk-backend.js +175 -0
  15. package/dist/cjs/runtime/i18n/backend/sdk-event.js +64 -0
  16. package/dist/cjs/runtime/i18n/detection/config.js +63 -0
  17. package/dist/cjs/runtime/i18n/detection/index.js +309 -0
  18. package/dist/cjs/runtime/i18n/detection/middleware.js +185 -0
  19. package/dist/cjs/runtime/i18n/detection/middleware.node.js +74 -0
  20. package/dist/cjs/runtime/i18n/index.js +43 -0
  21. package/dist/cjs/runtime/i18n/instance.js +132 -0
  22. package/dist/cjs/runtime/i18n/utils.js +189 -0
  23. package/dist/cjs/runtime/index.js +174 -0
  24. package/dist/cjs/runtime/types.js +18 -0
  25. package/dist/cjs/runtime/utils.js +136 -0
  26. package/dist/cjs/server/index.js +218 -0
  27. package/dist/cjs/shared/deepMerge.js +54 -0
  28. package/dist/cjs/shared/detection.js +105 -0
  29. package/dist/cjs/shared/type.js +18 -0
  30. package/dist/cjs/shared/utils.js +78 -0
  31. package/dist/esm/cli/index.mjs +107 -0
  32. package/dist/esm/rslib-runtime.mjs +18 -0
  33. package/dist/esm/runtime/I18nLink.mjs +32 -0
  34. package/dist/esm/runtime/context.mjs +105 -0
  35. package/dist/esm/runtime/hooks.mjs +151 -0
  36. package/dist/esm/runtime/i18n/backend/config.mjs +5 -0
  37. package/dist/esm/runtime/i18n/backend/defaults.mjs +19 -0
  38. package/dist/esm/runtime/i18n/backend/defaults.node.mjs +19 -0
  39. package/dist/esm/runtime/i18n/backend/index.mjs +74 -0
  40. package/dist/esm/runtime/i18n/backend/middleware.common.mjs +61 -0
  41. package/dist/esm/runtime/i18n/backend/middleware.mjs +7 -0
  42. package/dist/esm/runtime/i18n/backend/middleware.node.mjs +8 -0
  43. package/dist/esm/runtime/i18n/backend/sdk-backend.mjs +141 -0
  44. package/dist/esm/runtime/i18n/backend/sdk-event.mjs +21 -0
  45. package/dist/esm/runtime/i18n/detection/config.mjs +26 -0
  46. package/dist/esm/runtime/i18n/detection/index.mjs +260 -0
  47. package/dist/esm/runtime/i18n/detection/middleware.mjs +132 -0
  48. package/dist/esm/runtime/i18n/detection/middleware.node.mjs +31 -0
  49. package/dist/esm/runtime/i18n/index.mjs +2 -0
  50. package/dist/esm/runtime/i18n/instance.mjs +77 -0
  51. package/dist/esm/runtime/i18n/utils.mjs +140 -0
  52. package/dist/esm/runtime/index.mjs +132 -0
  53. package/dist/esm/runtime/types.mjs +0 -0
  54. package/dist/esm/runtime/utils.mjs +75 -0
  55. package/dist/esm/server/index.mjs +182 -0
  56. package/dist/esm/shared/deepMerge.mjs +20 -0
  57. package/dist/esm/shared/detection.mjs +71 -0
  58. package/dist/esm/shared/type.mjs +0 -0
  59. package/dist/esm/shared/utils.mjs +35 -0
  60. package/dist/esm-node/cli/index.mjs +108 -0
  61. package/dist/esm-node/rslib-runtime.mjs +19 -0
  62. package/dist/esm-node/runtime/I18nLink.mjs +33 -0
  63. package/dist/esm-node/runtime/context.mjs +106 -0
  64. package/dist/esm-node/runtime/hooks.mjs +152 -0
  65. package/dist/esm-node/runtime/i18n/backend/config.mjs +6 -0
  66. package/dist/esm-node/runtime/i18n/backend/defaults.mjs +20 -0
  67. package/dist/esm-node/runtime/i18n/backend/defaults.node.mjs +20 -0
  68. package/dist/esm-node/runtime/i18n/backend/index.mjs +75 -0
  69. package/dist/esm-node/runtime/i18n/backend/middleware.common.mjs +62 -0
  70. package/dist/esm-node/runtime/i18n/backend/middleware.mjs +8 -0
  71. package/dist/esm-node/runtime/i18n/backend/middleware.node.mjs +9 -0
  72. package/dist/esm-node/runtime/i18n/backend/sdk-backend.mjs +142 -0
  73. package/dist/esm-node/runtime/i18n/backend/sdk-event.mjs +22 -0
  74. package/dist/esm-node/runtime/i18n/detection/config.mjs +27 -0
  75. package/dist/esm-node/runtime/i18n/detection/index.mjs +261 -0
  76. package/dist/esm-node/runtime/i18n/detection/middleware.mjs +133 -0
  77. package/dist/esm-node/runtime/i18n/detection/middleware.node.mjs +32 -0
  78. package/dist/esm-node/runtime/i18n/index.mjs +3 -0
  79. package/dist/esm-node/runtime/i18n/instance.mjs +78 -0
  80. package/dist/esm-node/runtime/i18n/utils.mjs +141 -0
  81. package/dist/esm-node/runtime/index.mjs +133 -0
  82. package/dist/esm-node/runtime/types.mjs +1 -0
  83. package/dist/esm-node/runtime/utils.mjs +76 -0
  84. package/dist/esm-node/server/index.mjs +183 -0
  85. package/dist/esm-node/shared/deepMerge.mjs +21 -0
  86. package/dist/esm-node/shared/detection.mjs +72 -0
  87. package/dist/esm-node/shared/type.mjs +1 -0
  88. package/dist/esm-node/shared/utils.mjs +36 -0
  89. package/dist/types/cli/index.d.ts +21 -0
  90. package/dist/types/runtime/I18nLink.d.ts +8 -0
  91. package/dist/types/runtime/context.d.ts +38 -0
  92. package/dist/types/runtime/hooks.d.ts +28 -0
  93. package/dist/types/runtime/i18n/backend/config.d.ts +2 -0
  94. package/dist/types/runtime/i18n/backend/defaults.d.ts +13 -0
  95. package/dist/types/runtime/i18n/backend/defaults.node.d.ts +8 -0
  96. package/dist/types/runtime/i18n/backend/index.d.ts +3 -0
  97. package/dist/types/runtime/i18n/backend/middleware.common.d.ts +14 -0
  98. package/dist/types/runtime/i18n/backend/middleware.d.ts +12 -0
  99. package/dist/types/runtime/i18n/backend/middleware.node.d.ts +13 -0
  100. package/dist/types/runtime/i18n/backend/sdk-backend.d.ts +53 -0
  101. package/dist/types/runtime/i18n/backend/sdk-event.d.ts +9 -0
  102. package/dist/types/runtime/i18n/detection/config.d.ts +11 -0
  103. package/dist/types/runtime/i18n/detection/index.d.ts +50 -0
  104. package/dist/types/runtime/i18n/detection/middleware.d.ts +24 -0
  105. package/dist/types/runtime/i18n/detection/middleware.node.d.ts +17 -0
  106. package/dist/types/runtime/i18n/index.d.ts +3 -0
  107. package/dist/types/runtime/i18n/instance.d.ts +93 -0
  108. package/dist/types/runtime/i18n/utils.d.ts +29 -0
  109. package/dist/types/runtime/index.d.ts +20 -0
  110. package/dist/types/runtime/types.d.ts +15 -0
  111. package/dist/types/runtime/utils.d.ts +33 -0
  112. package/dist/types/server/index.d.ts +8 -0
  113. package/dist/types/shared/deepMerge.d.ts +1 -0
  114. package/dist/types/shared/detection.d.ts +11 -0
  115. package/dist/types/shared/type.d.ts +156 -0
  116. package/dist/types/shared/utils.d.ts +5 -0
  117. package/package.json +136 -0
  118. package/rslib.config.mts +4 -0
  119. package/src/cli/index.ts +245 -0
  120. package/src/runtime/I18nLink.tsx +76 -0
  121. package/src/runtime/context.tsx +281 -0
  122. package/src/runtime/hooks.ts +298 -0
  123. package/src/runtime/i18n/backend/config.ts +10 -0
  124. package/src/runtime/i18n/backend/defaults.node.ts +31 -0
  125. package/src/runtime/i18n/backend/defaults.ts +37 -0
  126. package/src/runtime/i18n/backend/index.ts +181 -0
  127. package/src/runtime/i18n/backend/middleware.common.ts +116 -0
  128. package/src/runtime/i18n/backend/middleware.node.ts +32 -0
  129. package/src/runtime/i18n/backend/middleware.ts +28 -0
  130. package/src/runtime/i18n/backend/sdk-backend.ts +306 -0
  131. package/src/runtime/i18n/backend/sdk-event.ts +39 -0
  132. package/src/runtime/i18n/detection/config.ts +32 -0
  133. package/src/runtime/i18n/detection/index.ts +641 -0
  134. package/src/runtime/i18n/detection/middleware.node.ts +84 -0
  135. package/src/runtime/i18n/detection/middleware.ts +251 -0
  136. package/src/runtime/i18n/index.ts +8 -0
  137. package/src/runtime/i18n/instance.ts +227 -0
  138. package/src/runtime/i18n/utils.ts +351 -0
  139. package/src/runtime/index.tsx +285 -0
  140. package/src/runtime/types.ts +17 -0
  141. package/src/runtime/utils.ts +163 -0
  142. package/src/server/index.ts +406 -0
  143. package/src/shared/deepMerge.ts +38 -0
  144. package/src/shared/detection.ts +131 -0
  145. package/src/shared/type.ts +170 -0
  146. package/src/shared/utils.ts +82 -0
  147. package/tsconfig.json +13 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021-present Modern.js
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,32 @@
1
+ <p align="center">
2
+ <a href="https://modernjs.dev" target="blank"><img src="https://lf3-static.bytednsdoc.com/obj/eden-cn/ylaelkeh7nuhfnuhf/modernjs-cover.png" width="300" alt="Modern.js Logo" /></a>
3
+ </p>
4
+
5
+ <h1 align="center">Modern.js</h1>
6
+
7
+ <p align="center">
8
+ A Progressive React Framework for modern web development.
9
+ </p>
10
+
11
+ ## Getting Started
12
+
13
+ Please follow [Quick Start](https://modernjs.dev/en/guides/get-started/quick-start) to get started with Modern.js.
14
+
15
+ ## Documentation
16
+
17
+ - [English Documentation](https://modernjs.dev/en/)
18
+ - [中文文档](https://modernjs.dev)
19
+
20
+ ## Contributing
21
+
22
+ Please read the [Contributing Guide](https://github.com/web-infra-dev/modern.js/blob/main/CONTRIBUTING.md).
23
+
24
+ ## License
25
+
26
+ Modern.js is [MIT licensed](https://github.com/web-infra-dev/modern.js/blob/main/LICENSE).
27
+
28
+ ## Credist
29
+
30
+ Thanks to:
31
+
32
+ - [@loadable/webpack-plugin](https://github.com/gregberge/loadable-components) to create a webpack plugin prepare for loadable usage in ssr.
@@ -0,0 +1,154 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.n = (module)=>{
5
+ var getter = module && module.__esModule ? ()=>module['default'] : ()=>module;
6
+ __webpack_require__.d(getter, {
7
+ a: getter
8
+ });
9
+ return getter;
10
+ };
11
+ })();
12
+ (()=>{
13
+ __webpack_require__.d = (exports1, definition)=>{
14
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
15
+ enumerable: true,
16
+ get: definition[key]
17
+ });
18
+ };
19
+ })();
20
+ (()=>{
21
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
22
+ })();
23
+ (()=>{
24
+ __webpack_require__.r = (exports1)=>{
25
+ if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
26
+ value: 'Module'
27
+ });
28
+ Object.defineProperty(exports1, '__esModule', {
29
+ value: true
30
+ });
31
+ };
32
+ })();
33
+ var __webpack_exports__ = {};
34
+ __webpack_require__.r(__webpack_exports__);
35
+ __webpack_require__.d(__webpack_exports__, {
36
+ default: ()=>cli,
37
+ i18nPlugin: ()=>i18nPlugin
38
+ });
39
+ const server_core_namespaceObject = require("@modern-js/server-core");
40
+ const external_fs_namespaceObject = require("fs");
41
+ var external_fs_default = /*#__PURE__*/ __webpack_require__.n(external_fs_namespaceObject);
42
+ const external_path_namespaceObject = require("path");
43
+ var external_path_default = /*#__PURE__*/ __webpack_require__.n(external_path_namespaceObject);
44
+ const utils_js_namespaceObject = require("../shared/utils.js");
45
+ function hasJsonFiles(dirPath) {
46
+ try {
47
+ if (!external_fs_default().existsSync(dirPath) || !external_fs_default().statSync(dirPath).isDirectory()) return false;
48
+ const entries = external_fs_default().readdirSync(dirPath);
49
+ for (const entry of entries){
50
+ const entryPath = external_path_default().join(dirPath, entry);
51
+ const stat = external_fs_default().statSync(entryPath);
52
+ if (stat.isFile() && entry.endsWith('.json')) return true;
53
+ if (stat.isDirectory()) {
54
+ if (hasJsonFiles(entryPath)) return true;
55
+ }
56
+ }
57
+ return false;
58
+ } catch {
59
+ return false;
60
+ }
61
+ }
62
+ function detectLocalesDirectory(appDirectory, normalizedConfig) {
63
+ const rootLocalesPath = external_path_default().join(appDirectory, 'locales');
64
+ if (hasJsonFiles(rootLocalesPath)) return true;
65
+ const configPublicPath = external_path_default().join(appDirectory, 'config', 'public', 'locales');
66
+ if (hasJsonFiles(configPublicPath)) return true;
67
+ const publicDir = normalizedConfig?.server?.publicDir;
68
+ if (publicDir) {
69
+ const publicDirPath = Array.isArray(publicDir) ? publicDir[0] : publicDir;
70
+ const localesPath = external_path_default().isAbsolute(publicDirPath) ? external_path_default().join(publicDirPath, 'locales') : external_path_default().join(appDirectory, publicDirPath, 'locales');
71
+ if (hasJsonFiles(localesPath)) return true;
72
+ }
73
+ return false;
74
+ }
75
+ const i18nPlugin = (options = {})=>({
76
+ name: '@modern-js/plugin-i18n',
77
+ setup: (api)=>{
78
+ const { localeDetection, backend, transformRuntimeConfig, customPlugin, ...restOptions } = options;
79
+ api._internalRuntimePlugins(({ entrypoint, plugins })=>{
80
+ const localeDetectionOptions = localeDetection ? (0, utils_js_namespaceObject.getLocaleDetectionOptions)(entrypoint.entryName, localeDetection) : void 0;
81
+ let backendOptions;
82
+ const { appDirectory } = api.getAppContext();
83
+ const normalizedConfig = api.getNormalizedConfig();
84
+ if (backend) {
85
+ const entryBackendOptions = (0, utils_js_namespaceObject.getBackendOptions)(entrypoint.entryName, backend);
86
+ if (entryBackendOptions?.enabled === false) backendOptions = entryBackendOptions;
87
+ else if (entryBackendOptions?.loadPath || entryBackendOptions?.addPath) backendOptions = {
88
+ ...entryBackendOptions,
89
+ enabled: true
90
+ };
91
+ else if (entryBackendOptions?.enabled !== true) {
92
+ const hasLocales = detectLocalesDirectory(appDirectory, normalizedConfig);
93
+ backendOptions = hasLocales ? {
94
+ ...entryBackendOptions,
95
+ enabled: true
96
+ } : entryBackendOptions;
97
+ } else backendOptions = entryBackendOptions;
98
+ } else {
99
+ const hasLocales = detectLocalesDirectory(appDirectory, normalizedConfig);
100
+ if (hasLocales) backendOptions = (0, utils_js_namespaceObject.getBackendOptions)(entrypoint.entryName, {
101
+ enabled: true
102
+ });
103
+ }
104
+ const { metaName } = api.getAppContext();
105
+ let extendedConfig = restOptions;
106
+ if (transformRuntimeConfig) extendedConfig = transformRuntimeConfig(restOptions, entrypoint);
107
+ const config = {
108
+ entryName: entrypoint.entryName,
109
+ localeDetection: localeDetectionOptions,
110
+ backend: backendOptions,
111
+ ...extendedConfig
112
+ };
113
+ plugins.push({
114
+ name: customPlugin?.runtime?.name || 'i18n',
115
+ path: customPlugin?.runtime?.path || `@${metaName}/plugin-i18n/runtime`,
116
+ config
117
+ });
118
+ return {
119
+ entrypoint,
120
+ plugins
121
+ };
122
+ });
123
+ api._internalServerPlugins(({ plugins })=>{
124
+ const { serverRoutes, metaName } = api.getAppContext();
125
+ const normalizedConfig = api.getNormalizedConfig();
126
+ let staticRoutePrefixes = [];
127
+ if (serverRoutes && Array.isArray(serverRoutes)) staticRoutePrefixes = serverRoutes.filter((route)=>!route.entryName && route.entryPath.startsWith('public')).map((route)=>route.urlPath).filter(Boolean);
128
+ const publicDirPrefixes = (0, server_core_namespaceObject.getPublicDirRoutePrefixes)(normalizedConfig?.server?.publicDir);
129
+ publicDirPrefixes.forEach((prefix)=>{
130
+ if (!staticRoutePrefixes.includes(prefix)) staticRoutePrefixes.push(prefix);
131
+ });
132
+ plugins.push({
133
+ name: customPlugin?.server?.name || `@${metaName}/plugin-i18n/server`,
134
+ options: {
135
+ localeDetection,
136
+ staticRoutePrefixes
137
+ }
138
+ });
139
+ return {
140
+ plugins
141
+ };
142
+ });
143
+ }
144
+ });
145
+ const cli = i18nPlugin;
146
+ exports["default"] = __webpack_exports__["default"];
147
+ exports.i18nPlugin = __webpack_exports__.i18nPlugin;
148
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
149
+ "default",
150
+ "i18nPlugin"
151
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
152
+ Object.defineProperty(exports, '__esModule', {
153
+ value: true
154
+ });
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.d = (exports1, definition)=>{
5
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
6
+ enumerable: true,
7
+ get: definition[key]
8
+ });
9
+ };
10
+ })();
11
+ (()=>{
12
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
13
+ })();
14
+ (()=>{
15
+ __webpack_require__.r = (exports1)=>{
16
+ if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
17
+ value: 'Module'
18
+ });
19
+ Object.defineProperty(exports1, '__esModule', {
20
+ value: true
21
+ });
22
+ };
23
+ })();
24
+ var __webpack_exports__ = {};
25
+ __webpack_require__.r(__webpack_exports__);
26
+ __webpack_require__.d(__webpack_exports__, {
27
+ I18nLink: ()=>I18nLink,
28
+ default: ()=>runtime_I18nLink
29
+ });
30
+ const jsx_runtime_namespaceObject = require("react/jsx-runtime");
31
+ const router_namespaceObject = require("@modern-js/runtime/router");
32
+ const external_context_js_namespaceObject = require("./context.js");
33
+ const external_utils_js_namespaceObject = require("./utils.js");
34
+ const useRouterHooks = ()=>{
35
+ const inRouter = (0, router_namespaceObject.useInRouterContext)();
36
+ return {
37
+ Link: inRouter ? router_namespaceObject.Link : null,
38
+ params: inRouter ? (0, router_namespaceObject.useParams)() : {},
39
+ hasRouter: inRouter
40
+ };
41
+ };
42
+ const I18nLink = ({ to, children, ...props })=>{
43
+ const { Link, params, hasRouter } = useRouterHooks();
44
+ const { language, supportedLanguages } = (0, external_context_js_namespaceObject.useModernI18n)();
45
+ const currentLang = language;
46
+ const localizedTo = (0, external_utils_js_namespaceObject.buildLocalizedUrl)(to, currentLang, supportedLanguages);
47
+ if ('development' === process.env.NODE_ENV && hasRouter && !params.lang) console.warn("I18nLink is being used outside of a :lang dynamic route context. This may cause unexpected behavior. Please ensure I18nLink is used within a route that has a :lang parameter.");
48
+ if (!hasRouter || !Link) return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("a", {
49
+ href: localizedTo,
50
+ ...props,
51
+ children: children
52
+ });
53
+ return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(Link, {
54
+ to: localizedTo,
55
+ ...props,
56
+ children: children
57
+ });
58
+ };
59
+ const runtime_I18nLink = I18nLink;
60
+ exports.I18nLink = __webpack_exports__.I18nLink;
61
+ exports["default"] = __webpack_exports__["default"];
62
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
63
+ "I18nLink",
64
+ "default"
65
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
66
+ Object.defineProperty(exports, '__esModule', {
67
+ value: true
68
+ });
@@ -0,0 +1,142 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.d = (exports1, definition)=>{
5
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
6
+ enumerable: true,
7
+ get: definition[key]
8
+ });
9
+ };
10
+ })();
11
+ (()=>{
12
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
13
+ })();
14
+ (()=>{
15
+ __webpack_require__.r = (exports1)=>{
16
+ if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
17
+ value: 'Module'
18
+ });
19
+ Object.defineProperty(exports1, '__esModule', {
20
+ value: true
21
+ });
22
+ };
23
+ })();
24
+ var __webpack_exports__ = {};
25
+ __webpack_require__.r(__webpack_exports__);
26
+ __webpack_require__.d(__webpack_exports__, {
27
+ ModernI18nProvider: ()=>ModernI18nProvider,
28
+ useModernI18n: ()=>useModernI18n
29
+ });
30
+ const jsx_runtime_namespaceObject = require("react/jsx-runtime");
31
+ const runtime_namespaceObject = require("@modern-js/runtime");
32
+ const external_react_namespaceObject = require("react");
33
+ const index_js_namespaceObject = require("./i18n/detection/index.js");
34
+ const external_utils_js_namespaceObject = require("./utils.js");
35
+ const ModernI18nContext = /*#__PURE__*/ (0, external_react_namespaceObject.createContext)(null);
36
+ const ModernI18nProvider = ({ children, value })=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(ModernI18nContext.Provider, {
37
+ value: value,
38
+ children: children
39
+ });
40
+ const useModernI18n = ()=>{
41
+ const context = (0, external_react_namespaceObject.useContext)(ModernI18nContext);
42
+ if (!context) throw new Error('useModernI18n must be used within a ModernI18nProvider');
43
+ const { language: contextLanguage, i18nInstance, languages, localePathRedirect, ignoreRedirectRoutes, updateLanguage } = context;
44
+ const { navigate, location, hasRouter } = (0, external_utils_js_namespaceObject.useRouterHooks)();
45
+ const currentLanguage = contextLanguage;
46
+ const changeLanguage = (0, external_react_namespaceObject.useCallback)(async (newLang)=>{
47
+ try {
48
+ if (!newLang || 'string' != typeof newLang) throw new Error('Language must be a non-empty string');
49
+ await i18nInstance?.setLang?.(newLang);
50
+ await i18nInstance?.changeLanguage?.(newLang);
51
+ if ((0, runtime_namespaceObject.isBrowser)()) {
52
+ const detectionOptions = i18nInstance.options?.detection;
53
+ (0, index_js_namespaceObject.cacheUserLanguage)(i18nInstance, newLang, detectionOptions);
54
+ }
55
+ if (localePathRedirect && (0, runtime_namespaceObject.isBrowser)() && hasRouter && navigate && location) {
56
+ const currentPath = location.pathname;
57
+ const entryPath = (0, external_utils_js_namespaceObject.getEntryPath)();
58
+ const relativePath = currentPath.replace(entryPath, '');
59
+ const pathLanguage = (0, external_utils_js_namespaceObject.detectLanguageFromPath)(currentPath, languages || [], localePathRedirect);
60
+ if (pathLanguage.detected && pathLanguage.language === newLang) return;
61
+ if (!(0, external_utils_js_namespaceObject.shouldIgnoreRedirect)(relativePath, languages || [], ignoreRedirectRoutes)) {
62
+ const newPath = (0, external_utils_js_namespaceObject.buildLocalizedUrl)(relativePath, newLang, languages || []);
63
+ const newUrl = entryPath + newPath + location.search + location.hash;
64
+ await navigate(newUrl, {
65
+ replace: true
66
+ });
67
+ }
68
+ } else if (localePathRedirect && (0, runtime_namespaceObject.isBrowser)() && !hasRouter) {
69
+ const currentPath = window.location.pathname;
70
+ const entryPath = (0, external_utils_js_namespaceObject.getEntryPath)();
71
+ const relativePath = currentPath.replace(entryPath, '');
72
+ const pathLanguage = (0, external_utils_js_namespaceObject.detectLanguageFromPath)(currentPath, languages || [], localePathRedirect);
73
+ if (pathLanguage.detected && pathLanguage.language === newLang) return;
74
+ if (!(0, external_utils_js_namespaceObject.shouldIgnoreRedirect)(relativePath, languages || [], ignoreRedirectRoutes)) {
75
+ const newPath = (0, external_utils_js_namespaceObject.buildLocalizedUrl)(relativePath, newLang, languages || []);
76
+ const newUrl = entryPath + newPath + window.location.search + window.location.hash;
77
+ window.history.pushState(null, '', newUrl);
78
+ }
79
+ }
80
+ if (updateLanguage) updateLanguage(newLang);
81
+ } catch (error) {
82
+ console.error('Failed to change language:', error);
83
+ throw error;
84
+ }
85
+ }, [
86
+ i18nInstance,
87
+ updateLanguage,
88
+ localePathRedirect,
89
+ ignoreRedirectRoutes,
90
+ languages,
91
+ hasRouter,
92
+ navigate,
93
+ location
94
+ ]);
95
+ const isLanguageSupported = (0, external_react_namespaceObject.useCallback)((lang)=>languages?.includes(lang) || false, [
96
+ languages
97
+ ]);
98
+ const isResourcesReady = (0, external_react_namespaceObject.useMemo)(()=>{
99
+ if (!i18nInstance?.isInitialized) return false;
100
+ const backend = i18nInstance?.services?.backend;
101
+ if (backend && 'function' == typeof backend.isLoading) {
102
+ const loadingResources = backend.getLoadingResources();
103
+ const isCurrentLanguageLoading = loadingResources.some(({ language })=>language === currentLanguage);
104
+ if (isCurrentLanguageLoading) return false;
105
+ }
106
+ const store = i18nInstance.store;
107
+ if (!store?.data) return false;
108
+ const langData = store.data[currentLanguage];
109
+ if (!langData || 'object' != typeof langData) return false;
110
+ const options = i18nInstance.options;
111
+ const namespaces = options?.ns || options?.defaultNS || [
112
+ 'translation'
113
+ ];
114
+ const requiredNamespaces = Array.isArray(namespaces) ? namespaces : [
115
+ namespaces
116
+ ];
117
+ return requiredNamespaces.every((ns)=>{
118
+ const nsData = langData[ns];
119
+ return nsData && 'object' == typeof nsData && Object.keys(nsData).length > 0;
120
+ });
121
+ }, [
122
+ currentLanguage,
123
+ i18nInstance
124
+ ]);
125
+ return {
126
+ language: currentLanguage,
127
+ changeLanguage,
128
+ i18nInstance,
129
+ supportedLanguages: languages || [],
130
+ isLanguageSupported,
131
+ isResourcesReady
132
+ };
133
+ };
134
+ exports.ModernI18nProvider = __webpack_exports__.ModernI18nProvider;
135
+ exports.useModernI18n = __webpack_exports__.useModernI18n;
136
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
137
+ "ModernI18nProvider",
138
+ "useModernI18n"
139
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
140
+ Object.defineProperty(exports, '__esModule', {
141
+ value: true
142
+ });
@@ -0,0 +1,194 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.d = (exports1, definition)=>{
5
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
6
+ enumerable: true,
7
+ get: definition[key]
8
+ });
9
+ };
10
+ })();
11
+ (()=>{
12
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
13
+ })();
14
+ (()=>{
15
+ __webpack_require__.r = (exports1)=>{
16
+ if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
17
+ value: 'Module'
18
+ });
19
+ Object.defineProperty(exports1, '__esModule', {
20
+ value: true
21
+ });
22
+ };
23
+ })();
24
+ var __webpack_exports__ = {};
25
+ __webpack_require__.r(__webpack_exports__);
26
+ __webpack_require__.d(__webpack_exports__, {
27
+ createContextValue: ()=>createContextValue,
28
+ useClientSideRedirect: ()=>useClientSideRedirect,
29
+ useLanguageSync: ()=>useLanguageSync,
30
+ useSdkResourcesLoader: ()=>useSdkResourcesLoader
31
+ });
32
+ const runtime_namespaceObject = require("@modern-js/runtime");
33
+ const external_react_namespaceObject = require("react");
34
+ const sdk_event_js_namespaceObject = require("./i18n/backend/sdk-event.js");
35
+ const index_js_namespaceObject = require("./i18n/detection/index.js");
36
+ const external_utils_js_namespaceObject = require("./utils.js");
37
+ function createMinimalI18nInstance(language) {
38
+ const minimalInstance = {
39
+ language,
40
+ isInitialized: false,
41
+ init: ()=>Promise.resolve(void 0),
42
+ use: ()=>{},
43
+ createInstance: ()=>minimalInstance,
44
+ services: {}
45
+ };
46
+ return minimalInstance;
47
+ }
48
+ function createContextValue(lang, i18nInstance, entryName, languages, localePathRedirect, ignoreRedirectRoutes, setLang) {
49
+ const instance = i18nInstance || createMinimalI18nInstance(lang);
50
+ return {
51
+ language: lang,
52
+ i18nInstance: instance,
53
+ entryName,
54
+ languages,
55
+ localePathRedirect,
56
+ ignoreRedirectRoutes,
57
+ updateLanguage: setLang
58
+ };
59
+ }
60
+ function useSdkResourcesLoader(i18nInstance, setForceUpdate) {
61
+ (0, external_react_namespaceObject.useEffect)(()=>{
62
+ if (!i18nInstance || !(0, runtime_namespaceObject.isBrowser)()) return;
63
+ const backendId = (0, sdk_event_js_namespaceObject.getI18nSdkBackendId)(i18nInstance.services?.resourceStore) || (0, sdk_event_js_namespaceObject.getI18nSdkBackendId)(i18nInstance.services?.store) || (0, sdk_event_js_namespaceObject.getI18nSdkBackendId)(i18nInstance.store);
64
+ if (!backendId) return;
65
+ const handleSdkResourcesLoaded = (event)=>{
66
+ const customEvent = event;
67
+ const { language, namespace, backendId: eventBackendId } = customEvent.detail || {};
68
+ if (!language || !namespace) return;
69
+ if (eventBackendId && eventBackendId !== backendId) return;
70
+ const triggerUpdate = (retryCount = 0)=>{
71
+ const store = i18nInstance.store;
72
+ const hasResource = store?.data?.[language]?.[namespace];
73
+ if (hasResource || retryCount >= 10) {
74
+ if (store?.data?.[language]?.[namespace]) {
75
+ if ('function' == typeof store.emit) store.emit('added', language, namespace);
76
+ }
77
+ if ('function' == typeof i18nInstance.emit) {
78
+ i18nInstance.emit('loaded', {
79
+ language,
80
+ namespace
81
+ });
82
+ i18nInstance.emit('loaded', language, namespace);
83
+ }
84
+ if ('function' == typeof i18nInstance.reloadResources) i18nInstance.reloadResources(language, namespace).then(()=>{
85
+ if ('function' == typeof i18nInstance.emit) i18nInstance.emit('loaded', {
86
+ language,
87
+ namespace
88
+ });
89
+ setForceUpdate((prev)=>prev + 1);
90
+ }).catch(()=>{});
91
+ if ('function' == typeof i18nInstance.emit) i18nInstance.emit('languageChanged', language);
92
+ setForceUpdate((prev)=>prev + 1);
93
+ } else setTimeout(()=>triggerUpdate(retryCount + 1), 10);
94
+ };
95
+ triggerUpdate();
96
+ };
97
+ window.addEventListener(sdk_event_js_namespaceObject.I18N_SDK_RESOURCES_LOADED_EVENT, handleSdkResourcesLoaded);
98
+ return ()=>{
99
+ window.removeEventListener(sdk_event_js_namespaceObject.I18N_SDK_RESOURCES_LOADED_EVENT, handleSdkResourcesLoaded);
100
+ };
101
+ }, [
102
+ i18nInstance,
103
+ setForceUpdate
104
+ ]);
105
+ }
106
+ function useClientSideRedirect(i18nInstance, localePathRedirect, languages, fallbackLanguage, ignoreRedirectRoutes) {
107
+ const hasRedirectedRef = (0, external_react_namespaceObject.useRef)(false);
108
+ const { navigate, location, hasRouter } = (0, external_utils_js_namespaceObject.useRouterHooks)();
109
+ (0, external_react_namespaceObject.useEffect)(()=>{
110
+ if ('browser' !== process.env.MODERN_TARGET) return;
111
+ if (!localePathRedirect || !i18nInstance) return;
112
+ try {
113
+ const ssrData = window._SSR_DATA;
114
+ if (ssrData) return;
115
+ } catch {}
116
+ if (hasRedirectedRef.current) return;
117
+ if (!i18nInstance.isInitialized) return;
118
+ const currentPathname = hasRouter && location ? location.pathname : window.location.pathname;
119
+ const currentSearch = hasRouter && location ? location.search : window.location.search;
120
+ const currentHash = hasRouter && location ? location.hash : window.location.hash;
121
+ const entryPath = (0, external_utils_js_namespaceObject.getEntryPath)();
122
+ const relativePath = currentPathname.replace(entryPath, '');
123
+ if ((0, external_utils_js_namespaceObject.shouldIgnoreRedirect)(relativePath, languages, ignoreRedirectRoutes)) return;
124
+ const pathDetection = (0, external_utils_js_namespaceObject.detectLanguageFromPath)(currentPathname, languages, localePathRedirect);
125
+ if (pathDetection.detected) return;
126
+ const targetLanguage = i18nInstance.language || fallbackLanguage || languages[0] || 'en';
127
+ const newPath = (0, external_utils_js_namespaceObject.buildLocalizedUrl)(relativePath, targetLanguage, languages);
128
+ const newUrl = entryPath + newPath + currentSearch + currentHash;
129
+ if (newUrl !== currentPathname + currentSearch + currentHash) {
130
+ hasRedirectedRef.current = true;
131
+ if (hasRouter && navigate && location) navigate(newUrl, {
132
+ replace: true
133
+ });
134
+ else window.location.replace(newUrl);
135
+ }
136
+ }, [
137
+ navigate,
138
+ location,
139
+ hasRouter,
140
+ localePathRedirect,
141
+ i18nInstance,
142
+ languages,
143
+ fallbackLanguage,
144
+ ignoreRedirectRoutes
145
+ ]);
146
+ }
147
+ function useLanguageSync(i18nInstance, localePathRedirect, languages, runtimeContextRef, prevLangRef, setLang) {
148
+ (0, external_react_namespaceObject.useEffect)(()=>{
149
+ if (!i18nInstance) return;
150
+ if (localePathRedirect) {
151
+ const currentPathname = (0, external_utils_js_namespaceObject.getPathname)(runtimeContextRef.current);
152
+ const pathDetection = (0, external_utils_js_namespaceObject.detectLanguageFromPath)(currentPathname, languages, localePathRedirect);
153
+ if (pathDetection.detected && pathDetection.language) {
154
+ const currentLang = pathDetection.language;
155
+ if (currentLang !== prevLangRef.current) {
156
+ prevLangRef.current = currentLang;
157
+ setLang(currentLang);
158
+ i18nInstance.setLang?.(currentLang);
159
+ i18nInstance.changeLanguage?.(currentLang);
160
+ if ((0, runtime_namespaceObject.isBrowser)()) {
161
+ const detectionOptions = i18nInstance.options?.detection;
162
+ (0, index_js_namespaceObject.cacheUserLanguage)(i18nInstance, currentLang, detectionOptions);
163
+ }
164
+ }
165
+ }
166
+ } else {
167
+ const instanceLang = i18nInstance.language;
168
+ if (instanceLang && instanceLang !== prevLangRef.current) {
169
+ prevLangRef.current = instanceLang;
170
+ setLang(instanceLang);
171
+ }
172
+ }
173
+ }, [
174
+ i18nInstance,
175
+ localePathRedirect,
176
+ languages,
177
+ runtimeContextRef,
178
+ prevLangRef,
179
+ setLang
180
+ ]);
181
+ }
182
+ exports.createContextValue = __webpack_exports__.createContextValue;
183
+ exports.useClientSideRedirect = __webpack_exports__.useClientSideRedirect;
184
+ exports.useLanguageSync = __webpack_exports__.useLanguageSync;
185
+ exports.useSdkResourcesLoader = __webpack_exports__.useSdkResourcesLoader;
186
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
187
+ "createContextValue",
188
+ "useClientSideRedirect",
189
+ "useLanguageSync",
190
+ "useSdkResourcesLoader"
191
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
192
+ Object.defineProperty(exports, '__esModule', {
193
+ value: true
194
+ });
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ var __webpack_require__ = {};
3
+ (()=>{
4
+ __webpack_require__.d = (exports1, definition)=>{
5
+ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
6
+ enumerable: true,
7
+ get: definition[key]
8
+ });
9
+ };
10
+ })();
11
+ (()=>{
12
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
13
+ })();
14
+ (()=>{
15
+ __webpack_require__.r = (exports1)=>{
16
+ if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
17
+ value: 'Module'
18
+ });
19
+ Object.defineProperty(exports1, '__esModule', {
20
+ value: true
21
+ });
22
+ };
23
+ })();
24
+ var __webpack_exports__ = {};
25
+ __webpack_require__.r(__webpack_exports__);
26
+ __webpack_require__.d(__webpack_exports__, {
27
+ mergeBackendOptions: ()=>mergeBackendOptions
28
+ });
29
+ const deepMerge_js_namespaceObject = require("../../../shared/deepMerge.js");
30
+ function mergeBackendOptions(defaultOptions, cliOptions = {}, userOptions) {
31
+ return (0, deepMerge_js_namespaceObject.deepMerge)((0, deepMerge_js_namespaceObject.deepMerge)(defaultOptions, cliOptions), userOptions ?? {});
32
+ }
33
+ exports.mergeBackendOptions = __webpack_exports__.mergeBackendOptions;
34
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
35
+ "mergeBackendOptions"
36
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
37
+ Object.defineProperty(exports, '__esModule', {
38
+ value: true
39
+ });