@bleedingdev/modern-js-plugin-i18n 3.4.0-ultramodern.1 → 3.4.0-ultramodern.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. package/dist/cjs/cli/index.js +2 -1
  2. package/dist/cjs/runtime/context.js +8 -0
  3. package/dist/cjs/runtime/core.js +217 -0
  4. package/dist/cjs/runtime/i18n/backend/middleware.node.js +22 -4
  5. package/dist/cjs/runtime/index.js +44 -173
  6. package/dist/cjs/runtime/no-react-i18next.js +76 -0
  7. package/dist/esm/cli/index.mjs +2 -1
  8. package/dist/esm/runtime/context.mjs +8 -0
  9. package/dist/esm/runtime/core.mjs +152 -0
  10. package/dist/esm/runtime/i18n/backend/middleware.node.mjs +19 -4
  11. package/dist/esm/runtime/index.mjs +5 -140
  12. package/dist/esm/runtime/no-react-i18next.mjs +6 -0
  13. package/dist/esm-node/cli/index.mjs +2 -1
  14. package/dist/esm-node/runtime/context.mjs +8 -0
  15. package/dist/esm-node/runtime/core.mjs +153 -0
  16. package/dist/esm-node/runtime/i18n/backend/middleware.node.mjs +19 -4
  17. package/dist/esm-node/runtime/index.mjs +5 -140
  18. package/dist/esm-node/runtime/no-react-i18next.mjs +7 -0
  19. package/dist/types/runtime/context.d.ts +1 -0
  20. package/dist/types/runtime/core.d.ts +30 -0
  21. package/dist/types/runtime/i18n/backend/middleware.node.d.ts +4 -1
  22. package/dist/types/runtime/index.d.ts +2 -24
  23. package/dist/types/runtime/no-react-i18next.d.ts +3 -0
  24. package/package.json +21 -11
  25. package/rstest.config.mts +1 -0
  26. package/src/cli/index.ts +6 -1
  27. package/src/runtime/context.tsx +13 -0
  28. package/src/runtime/core.tsx +360 -0
  29. package/src/runtime/i18n/backend/middleware.node.ts +31 -1
  30. package/src/runtime/index.tsx +4 -316
  31. package/src/runtime/no-react-i18next.tsx +7 -0
  32. package/tests/i18nUtils.test.ts +34 -0
  33. package/tests/localisedUrls.test.ts +35 -0
  34. package/tests/reactI18nextRuntimeBoundary.test.ts +36 -0
  35. package/tests/routerAdapter.test.tsx +58 -1
@@ -64,9 +64,10 @@ const i18nPlugin = (options = {})=>({
64
64
  backend: backendOptions,
65
65
  ...extendedConfig
66
66
  };
67
+ const runtimePluginPath = customPlugin?.runtime?.path || (false === config.reactI18next ? `@${metaName}/plugin-i18n/runtime/no-react-i18next` : `@${metaName}/plugin-i18n/runtime`);
67
68
  plugins.push({
68
69
  name: customPlugin?.runtime?.name || 'i18n',
69
- path: customPlugin?.runtime?.path || `@${metaName}/plugin-i18n/runtime`,
70
+ path: runtimePluginPath,
70
71
  config
71
72
  });
72
73
  return {
@@ -122,6 +122,13 @@ const useModernI18n = ()=>{
122
122
  navigate,
123
123
  location
124
124
  ]);
125
+ const t = (0, external_react_namespaceObject.useCallback)((key, ...args)=>{
126
+ if ('function' != typeof i18nInstance.t) throw new Error('i18nInstance.t is required');
127
+ return i18nInstance.t(key, ...args);
128
+ }, [
129
+ currentLanguage,
130
+ i18nInstance
131
+ ]);
125
132
  const isLanguageSupported = (0, external_react_namespaceObject.useCallback)((lang)=>languages?.includes(lang) || false, [
126
133
  languages
127
134
  ]);
@@ -155,6 +162,7 @@ const useModernI18n = ()=>{
155
162
  return {
156
163
  language: currentLanguage,
157
164
  changeLanguage,
165
+ t,
158
166
  i18nInstance,
159
167
  supportedLanguages: languages || [],
160
168
  localisedUrls,
@@ -0,0 +1,217 @@
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
+ I18nLink: ()=>external_I18nLink_js_namespaceObject.I18nLink,
32
+ Link: ()=>external_Link_js_namespaceObject.Link,
33
+ buildLocalizedUrl: ()=>external_utils_js_namespaceObject.buildLocalizedUrl,
34
+ canonicalPath: ()=>external_localizedPaths_js_namespaceObject.canonicalPath,
35
+ createI18nPlugin: ()=>createI18nPlugin,
36
+ localizePath: ()=>external_localizedPaths_js_namespaceObject.localizePath,
37
+ splitUrlTarget: ()=>external_utils_js_namespaceObject.splitUrlTarget,
38
+ useLocalizedLocation: ()=>external_localizedPaths_js_namespaceObject.useLocalizedLocation,
39
+ useLocalizedPaths: ()=>external_localizedPaths_js_namespaceObject.useLocalizedPaths,
40
+ useModernI18n: ()=>external_context_js_namespaceObject.useModernI18n
41
+ });
42
+ const jsx_runtime_namespaceObject = require("react/jsx-runtime");
43
+ const runtime_namespaceObject = require("@modern-js/runtime");
44
+ const head_namespaceObject = require("@modern-js/runtime/head");
45
+ const merge_namespaceObject = require("@modern-js/runtime-utils/merge");
46
+ const external_react_namespaceObject = require("react");
47
+ const external_context_js_namespaceObject = require("./context.js");
48
+ const external_hooks_js_namespaceObject = require("./hooks.js");
49
+ const index_js_namespaceObject = require("./i18n/index.js");
50
+ const backend_index_js_namespaceObject = require("./i18n/backend/index.js");
51
+ const detection_index_js_namespaceObject = require("./i18n/detection/index.js");
52
+ const middleware_js_namespaceObject = require("./i18n/detection/middleware.js");
53
+ const instance_js_namespaceObject = require("./i18n/instance.js");
54
+ const external_utils_js_namespaceObject = require("./utils.js");
55
+ require("./types.js");
56
+ const external_I18nLink_js_namespaceObject = require("./I18nLink.js");
57
+ const external_Link_js_namespaceObject = require("./Link.js");
58
+ const external_localizedPaths_js_namespaceObject = require("./localizedPaths.js");
59
+ let i18nLifecycleHelpersPromise;
60
+ function loadI18nLifecycleHelpers() {
61
+ i18nLifecycleHelpersPromise ??= Promise.all([
62
+ import("./i18n/backend/middleware.js"),
63
+ import("./i18n/utils.js")
64
+ ]).then(([backendMiddleware, utils])=>({
65
+ useI18nextBackend: backendMiddleware.useI18nextBackend,
66
+ changeI18nLanguage: utils.changeI18nLanguage,
67
+ ensureLanguageMatch: utils.ensureLanguageMatch,
68
+ initializeI18nInstance: utils.initializeI18nInstance,
69
+ setupClonedInstance: utils.setupClonedInstance
70
+ }));
71
+ return i18nLifecycleHelpersPromise;
72
+ }
73
+ const createI18nPlugin = (loadReactI18nextIntegration)=>(options)=>({
74
+ name: '@modern-js/plugin-i18n',
75
+ setup: (api)=>{
76
+ const { entryName, i18nInstance: userI18nInstance, initOptions, localeDetection, backend, htmlLangAttr = false, reactI18next = true } = options;
77
+ const { localePathRedirect = false, i18nextDetector = true, languages = [], fallbackLanguage = 'en', detection, ignoreRedirectRoutes, localisedUrls } = localeDetection || {};
78
+ const { enabled: backendEnabled = false } = backend || {};
79
+ let latestI18nInstance;
80
+ let I18nextProvider;
81
+ const resolveReactI18nextIntegration = async ()=>{
82
+ if (!reactI18next) return null;
83
+ return loadReactI18nextIntegration?.() ?? null;
84
+ };
85
+ api.onBeforeRender(async (context)=>{
86
+ const { useI18nextBackend, changeI18nLanguage, ensureLanguageMatch, initializeI18nInstance, setupClonedInstance } = await loadI18nLifecycleHelpers();
87
+ let i18nInstance = await (0, index_js_namespaceObject.getI18nInstance)(userI18nInstance);
88
+ const { i18n: otherConfig } = api.getRuntimeConfig();
89
+ const { initOptions: otherInitOptions } = otherConfig || {};
90
+ const userInitOptions = (0, merge_namespaceObject.merge)(otherInitOptions || {}, initOptions || {});
91
+ const reactI18nextIntegration = await resolveReactI18nextIntegration();
92
+ I18nextProvider = reactI18nextIntegration?.I18nextProvider ?? null;
93
+ if (reactI18nextIntegration?.initReactI18next) i18nInstance.use(reactI18nextIntegration.initReactI18next);
94
+ const pathname = (0, external_utils_js_namespaceObject.getPathname)(context);
95
+ if (i18nextDetector) (0, middleware_js_namespaceObject.useI18nextLanguageDetector)(i18nInstance);
96
+ const mergedDetection = (0, detection_index_js_namespaceObject.mergeDetectionOptions)(i18nextDetector, detection, localePathRedirect, userInitOptions);
97
+ const mergedBackend = (0, backend_index_js_namespaceObject.mergeBackendOptions)(backend, userInitOptions);
98
+ const hasSdkConfig = 'function' == typeof userInitOptions?.backend?.sdk || mergedBackend?.sdk && 'function' == typeof mergedBackend.sdk;
99
+ if (mergedBackend && (backendEnabled || hasSdkConfig)) useI18nextBackend(i18nInstance, mergedBackend);
100
+ const { finalLanguage } = await (0, detection_index_js_namespaceObject.detectLanguageWithPriority)(i18nInstance, {
101
+ languages,
102
+ fallbackLanguage,
103
+ localePathRedirect,
104
+ i18nextDetector,
105
+ detection,
106
+ userInitOptions,
107
+ mergedBackend,
108
+ pathname,
109
+ ssrContext: context.ssrContext
110
+ });
111
+ await initializeI18nInstance(i18nInstance, finalLanguage, fallbackLanguage, languages, mergedDetection, mergedBackend, userInitOptions);
112
+ if (!(0, runtime_namespaceObject.isBrowser)() && i18nInstance.cloneInstance) {
113
+ i18nInstance = i18nInstance.cloneInstance();
114
+ await setupClonedInstance(i18nInstance, finalLanguage, fallbackLanguage, languages, backendEnabled, backend, i18nextDetector, detection, localePathRedirect, userInitOptions);
115
+ }
116
+ if (localePathRedirect) await ensureLanguageMatch(i18nInstance, finalLanguage);
117
+ if (!(0, runtime_namespaceObject.isBrowser)()) (0, detection_index_js_namespaceObject.exportServerLngToWindow)(context, finalLanguage);
118
+ context.i18nInstance = i18nInstance;
119
+ latestI18nInstance = i18nInstance;
120
+ context.changeLanguage = async (newLang)=>{
121
+ await changeI18nLanguage(i18nInstance, newLang, {
122
+ detectionOptions: mergedDetection
123
+ });
124
+ };
125
+ });
126
+ api.wrapRoot((App)=>(props)=>{
127
+ const runtimeContext = (0, external_react_namespaceObject.useContext)(runtime_namespaceObject.RuntimeContext);
128
+ const i18nInstance = runtimeContext.i18nInstance || latestI18nInstance;
129
+ const initialLang = (0, external_react_namespaceObject.useMemo)(()=>i18nInstance?.language || (localeDetection?.fallbackLanguage ?? 'en'), [
130
+ i18nInstance?.language,
131
+ localeDetection?.fallbackLanguage
132
+ ]);
133
+ const [lang, setLang] = (0, external_react_namespaceObject.useState)(initialLang);
134
+ const [forceUpdate, setForceUpdate] = (0, external_react_namespaceObject.useState)(0);
135
+ const prevLangRef = (0, external_react_namespaceObject.useRef)(lang);
136
+ const runtimeContextRef = (0, external_react_namespaceObject.useRef)(runtimeContext);
137
+ runtimeContextRef.current = runtimeContext;
138
+ (0, external_react_namespaceObject.useEffect)(()=>{
139
+ if (i18nInstance?.language) {
140
+ const translator = i18nInstance.translator;
141
+ if (translator) translator.language = i18nInstance.language;
142
+ }
143
+ }, [
144
+ i18nInstance?.language
145
+ ]);
146
+ (0, external_react_namespaceObject.useEffect)(()=>{
147
+ prevLangRef.current = lang;
148
+ }, [
149
+ lang
150
+ ]);
151
+ (0, external_hooks_js_namespaceObject.useSdkResourcesLoader)(i18nInstance, setForceUpdate);
152
+ (0, external_hooks_js_namespaceObject.useLanguageSync)(i18nInstance, localePathRedirect, languages, runtimeContextRef, prevLangRef, setLang);
153
+ (0, external_hooks_js_namespaceObject.useClientSideRedirect)(i18nInstance, localePathRedirect, languages, fallbackLanguage, ignoreRedirectRoutes, localisedUrls);
154
+ const contextValue = (0, external_react_namespaceObject.useMemo)(()=>(0, external_hooks_js_namespaceObject.createContextValue)(lang, i18nInstance, entryName, languages, localePathRedirect, ignoreRedirectRoutes, localisedUrls, setLang), [
155
+ lang,
156
+ i18nInstance,
157
+ entryName,
158
+ languages,
159
+ localePathRedirect,
160
+ ignoreRedirectRoutes,
161
+ localisedUrls,
162
+ forceUpdate
163
+ ]);
164
+ const children = props.children;
165
+ const appContent = /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(jsx_runtime_namespaceObject.Fragment, {
166
+ children: [
167
+ Boolean(htmlLangAttr) && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(head_namespaceObject.Helmet, {
168
+ htmlAttributes: {
169
+ lang
170
+ }
171
+ }),
172
+ /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_context_js_namespaceObject.ModernI18nProvider, {
173
+ value: contextValue,
174
+ children: App ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(App, {
175
+ ...props,
176
+ children: children
177
+ }) : children
178
+ })
179
+ ]
180
+ });
181
+ if (!i18nInstance) return appContent;
182
+ if (I18nextProvider) {
183
+ const i18nextInstanceForProvider = (0, instance_js_namespaceObject.getI18nextInstanceForProvider)(i18nInstance);
184
+ return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(I18nextProvider, {
185
+ i18n: i18nextInstanceForProvider,
186
+ children: appContent
187
+ });
188
+ }
189
+ return appContent;
190
+ });
191
+ }
192
+ });
193
+ exports.I18nLink = __webpack_exports__.I18nLink;
194
+ exports.Link = __webpack_exports__.Link;
195
+ exports.buildLocalizedUrl = __webpack_exports__.buildLocalizedUrl;
196
+ exports.canonicalPath = __webpack_exports__.canonicalPath;
197
+ exports.createI18nPlugin = __webpack_exports__.createI18nPlugin;
198
+ exports.localizePath = __webpack_exports__.localizePath;
199
+ exports.splitUrlTarget = __webpack_exports__.splitUrlTarget;
200
+ exports.useLocalizedLocation = __webpack_exports__.useLocalizedLocation;
201
+ exports.useLocalizedPaths = __webpack_exports__.useLocalizedPaths;
202
+ exports.useModernI18n = __webpack_exports__.useModernI18n;
203
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
204
+ "I18nLink",
205
+ "Link",
206
+ "buildLocalizedUrl",
207
+ "canonicalPath",
208
+ "createI18nPlugin",
209
+ "localizePath",
210
+ "splitUrlTarget",
211
+ "useLocalizedLocation",
212
+ "useLocalizedPaths",
213
+ "useModernI18n"
214
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
215
+ Object.defineProperty(exports, '__esModule', {
216
+ value: true
217
+ });
@@ -39,22 +39,40 @@ __webpack_require__.r(__webpack_exports__);
39
39
  __webpack_require__.d(__webpack_exports__, {
40
40
  FsBackendWithSave: ()=>FsBackendWithSave,
41
41
  HttpBackendWithSave: ()=>HttpBackendWithSave,
42
+ resolveFsBackendConstructor: ()=>resolveFsBackendConstructor,
42
43
  useI18nextBackend: ()=>useI18nextBackend
43
44
  });
44
- const cjs_namespaceObject = require("i18next-fs-backend/cjs");
45
- var cjs_default = /*#__PURE__*/ __webpack_require__.n(cjs_namespaceObject);
45
+ const external_i18next_fs_backend_namespaceObject = require("i18next-fs-backend");
46
+ var external_i18next_fs_backend_default = /*#__PURE__*/ __webpack_require__.n(external_i18next_fs_backend_namespaceObject);
46
47
  const external_middleware_common_js_namespaceObject = require("./middleware.common.js");
47
- class FsBackendWithSave extends cjs_default() {
48
+ const resolveFsBackendConstructor = (backendModule)=>{
49
+ const nestedDefault = backendModule?.default?.default;
50
+ const nestedModuleExports = backendModule?.default?.['module.exports'];
51
+ const candidates = [
52
+ backendModule,
53
+ backendModule?.default,
54
+ backendModule?.['module.exports'],
55
+ nestedDefault,
56
+ nestedModuleExports
57
+ ];
58
+ const Backend = candidates.find((candidate)=>'function' == typeof candidate);
59
+ if (!Backend) throw new Error('Failed to resolve i18next-fs-backend constructor for the i18n Node backend.');
60
+ return Backend;
61
+ };
62
+ const middleware_node_Backend = resolveFsBackendConstructor(external_i18next_fs_backend_default());
63
+ class FsBackendWithSave extends middleware_node_Backend {
48
64
  save(_language, _namespace, _data) {}
49
65
  }
50
66
  const HttpBackendWithSave = FsBackendWithSave;
51
- const useI18nextBackend = (i18nInstance, backend)=>(0, external_middleware_common_js_namespaceObject.useI18nextBackendCommon)(i18nInstance, FsBackendWithSave, cjs_default(), backend);
67
+ const useI18nextBackend = (i18nInstance, backend)=>(0, external_middleware_common_js_namespaceObject.useI18nextBackendCommon)(i18nInstance, FsBackendWithSave, middleware_node_Backend, backend);
52
68
  exports.FsBackendWithSave = __webpack_exports__.FsBackendWithSave;
53
69
  exports.HttpBackendWithSave = __webpack_exports__.HttpBackendWithSave;
70
+ exports.resolveFsBackendConstructor = __webpack_exports__.resolveFsBackendConstructor;
54
71
  exports.useI18nextBackend = __webpack_exports__.useI18nextBackend;
55
72
  for(var __rspack_i in __webpack_exports__)if (-1 === [
56
73
  "FsBackendWithSave",
57
74
  "HttpBackendWithSave",
75
+ "resolveFsBackendConstructor",
58
76
  "useI18nextBackend"
59
77
  ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
60
78
  Object.defineProperty(exports, '__esModule', {
@@ -1,5 +1,31 @@
1
1
  "use strict";
2
- var __webpack_require__ = {};
2
+ var __webpack_modules__ = {
3
+ "./core" (module) {
4
+ module.exports = require("./core.js");
5
+ },
6
+ "./i18n/react-i18next" (module) {
7
+ module.exports = require("./i18n/react-i18next.js");
8
+ }
9
+ };
10
+ var __webpack_module_cache__ = {};
11
+ function __webpack_require__(moduleId) {
12
+ var cachedModule = __webpack_module_cache__[moduleId];
13
+ if (void 0 !== cachedModule) return cachedModule.exports;
14
+ var module = __webpack_module_cache__[moduleId] = {
15
+ exports: {}
16
+ };
17
+ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
18
+ return module.exports;
19
+ }
20
+ (()=>{
21
+ __webpack_require__.n = (module)=>{
22
+ var getter = module && module.__esModule ? ()=>module['default'] : ()=>module;
23
+ __webpack_require__.d(getter, {
24
+ a: getter
25
+ });
26
+ return getter;
27
+ };
28
+ })();
3
29
  (()=>{
4
30
  __webpack_require__.d = (exports1, getters, values)=>{
5
31
  var define = (defs, kind)=>{
@@ -26,183 +52,28 @@ var __webpack_require__ = {};
26
52
  };
27
53
  })();
28
54
  var __webpack_exports__ = {};
29
- __webpack_require__.r(__webpack_exports__);
30
- __webpack_require__.d(__webpack_exports__, {
31
- I18nLink: ()=>external_I18nLink_js_namespaceObject.I18nLink,
32
- Link: ()=>external_Link_js_namespaceObject.Link,
33
- buildLocalizedUrl: ()=>external_utils_js_namespaceObject.buildLocalizedUrl,
34
- canonicalPath: ()=>external_localizedPaths_js_namespaceObject.canonicalPath,
35
- default: ()=>runtime,
36
- i18nPlugin: ()=>i18nPlugin,
37
- localizePath: ()=>external_localizedPaths_js_namespaceObject.localizePath,
38
- splitUrlTarget: ()=>external_utils_js_namespaceObject.splitUrlTarget,
39
- useLocalizedLocation: ()=>external_localizedPaths_js_namespaceObject.useLocalizedLocation,
40
- useLocalizedPaths: ()=>external_localizedPaths_js_namespaceObject.useLocalizedPaths,
41
- useModernI18n: ()=>external_context_js_namespaceObject.useModernI18n
42
- });
43
- const jsx_runtime_namespaceObject = require("react/jsx-runtime");
44
- const runtime_namespaceObject = require("@modern-js/runtime");
45
- const head_namespaceObject = require("@modern-js/runtime/head");
46
- const merge_namespaceObject = require("@modern-js/runtime-utils/merge");
47
- const external_react_namespaceObject = require("react");
48
- const external_context_js_namespaceObject = require("./context.js");
49
- const external_hooks_js_namespaceObject = require("./hooks.js");
50
- const index_js_namespaceObject = require("./i18n/index.js");
51
- const backend_index_js_namespaceObject = require("./i18n/backend/index.js");
52
- const middleware_js_namespaceObject = require("./i18n/backend/middleware.js");
53
- const detection_index_js_namespaceObject = require("./i18n/detection/index.js");
54
- const detection_middleware_js_namespaceObject = require("./i18n/detection/middleware.js");
55
- const instance_js_namespaceObject = require("./i18n/instance.js");
56
- const utils_js_namespaceObject = require("./i18n/utils.js");
57
- const external_utils_js_namespaceObject = require("./utils.js");
58
- require("./types.js");
59
- const external_I18nLink_js_namespaceObject = require("./I18nLink.js");
60
- const external_Link_js_namespaceObject = require("./Link.js");
61
- const external_localizedPaths_js_namespaceObject = require("./localizedPaths.js");
62
- const i18nPlugin = (options)=>({
63
- name: '@modern-js/plugin-i18n',
64
- setup: (api)=>{
65
- const { entryName, i18nInstance: userI18nInstance, initOptions, localeDetection, backend, htmlLangAttr = false, reactI18next = true } = options;
66
- const { localePathRedirect = false, i18nextDetector = true, languages = [], fallbackLanguage = 'en', detection, ignoreRedirectRoutes, localisedUrls } = localeDetection || {};
67
- const { enabled: backendEnabled = false } = backend || {};
68
- let latestI18nInstance;
69
- let I18nextProvider;
70
- const loadReactI18nextIntegration = async ()=>{
71
- if (!reactI18next) return null;
72
- const { getReactI18nextIntegration } = await import("./i18n/react-i18next.js");
73
- return getReactI18nextIntegration();
74
- };
75
- api.onBeforeRender(async (context)=>{
76
- let i18nInstance = await (0, index_js_namespaceObject.getI18nInstance)(userI18nInstance);
77
- const { i18n: otherConfig } = api.getRuntimeConfig();
78
- const { initOptions: otherInitOptions } = otherConfig || {};
79
- const userInitOptions = (0, merge_namespaceObject.merge)(otherInitOptions || {}, initOptions || {});
80
- const reactI18nextIntegration = await loadReactI18nextIntegration();
81
- I18nextProvider = reactI18nextIntegration?.I18nextProvider ?? null;
82
- if (reactI18nextIntegration?.initReactI18next) i18nInstance.use(reactI18nextIntegration.initReactI18next);
83
- const pathname = (0, external_utils_js_namespaceObject.getPathname)(context);
84
- if (i18nextDetector) (0, detection_middleware_js_namespaceObject.useI18nextLanguageDetector)(i18nInstance);
85
- const mergedDetection = (0, detection_index_js_namespaceObject.mergeDetectionOptions)(i18nextDetector, detection, localePathRedirect, userInitOptions);
86
- const mergedBackend = (0, backend_index_js_namespaceObject.mergeBackendOptions)(backend, userInitOptions);
87
- const hasSdkConfig = 'function' == typeof userInitOptions?.backend?.sdk || mergedBackend?.sdk && 'function' == typeof mergedBackend.sdk;
88
- if (mergedBackend && (backendEnabled || hasSdkConfig)) (0, middleware_js_namespaceObject.useI18nextBackend)(i18nInstance, mergedBackend);
89
- const { finalLanguage } = await (0, detection_index_js_namespaceObject.detectLanguageWithPriority)(i18nInstance, {
90
- languages,
91
- fallbackLanguage,
92
- localePathRedirect,
93
- i18nextDetector,
94
- detection,
95
- userInitOptions,
96
- mergedBackend,
97
- pathname,
98
- ssrContext: context.ssrContext
99
- });
100
- await (0, utils_js_namespaceObject.initializeI18nInstance)(i18nInstance, finalLanguage, fallbackLanguage, languages, mergedDetection, mergedBackend, userInitOptions);
101
- if (!(0, runtime_namespaceObject.isBrowser)() && i18nInstance.cloneInstance) {
102
- i18nInstance = i18nInstance.cloneInstance();
103
- await (0, utils_js_namespaceObject.setupClonedInstance)(i18nInstance, finalLanguage, fallbackLanguage, languages, backendEnabled, backend, i18nextDetector, detection, localePathRedirect, userInitOptions);
104
- }
105
- if (localePathRedirect) await (0, utils_js_namespaceObject.ensureLanguageMatch)(i18nInstance, finalLanguage);
106
- if (!(0, runtime_namespaceObject.isBrowser)()) (0, detection_index_js_namespaceObject.exportServerLngToWindow)(context, finalLanguage);
107
- context.i18nInstance = i18nInstance;
108
- latestI18nInstance = i18nInstance;
109
- context.changeLanguage = async (newLang)=>{
110
- await (0, utils_js_namespaceObject.changeI18nLanguage)(i18nInstance, newLang, {
111
- detectionOptions: mergedDetection
112
- });
113
- };
114
- });
115
- api.wrapRoot((App)=>(props)=>{
116
- const runtimeContext = (0, external_react_namespaceObject.useContext)(runtime_namespaceObject.RuntimeContext);
117
- const i18nInstance = runtimeContext.i18nInstance || latestI18nInstance;
118
- const initialLang = (0, external_react_namespaceObject.useMemo)(()=>i18nInstance?.language || (localeDetection?.fallbackLanguage ?? 'en'), [
119
- i18nInstance?.language,
120
- localeDetection?.fallbackLanguage
121
- ]);
122
- const [lang, setLang] = (0, external_react_namespaceObject.useState)(initialLang);
123
- const [forceUpdate, setForceUpdate] = (0, external_react_namespaceObject.useState)(0);
124
- const prevLangRef = (0, external_react_namespaceObject.useRef)(lang);
125
- const runtimeContextRef = (0, external_react_namespaceObject.useRef)(runtimeContext);
126
- runtimeContextRef.current = runtimeContext;
127
- (0, external_react_namespaceObject.useEffect)(()=>{
128
- if (i18nInstance?.language) {
129
- const translator = i18nInstance.translator;
130
- if (translator) translator.language = i18nInstance.language;
131
- }
132
- }, [
133
- i18nInstance?.language
134
- ]);
135
- (0, external_react_namespaceObject.useEffect)(()=>{
136
- prevLangRef.current = lang;
137
- }, [
138
- lang
139
- ]);
140
- (0, external_hooks_js_namespaceObject.useSdkResourcesLoader)(i18nInstance, setForceUpdate);
141
- (0, external_hooks_js_namespaceObject.useLanguageSync)(i18nInstance, localePathRedirect, languages, runtimeContextRef, prevLangRef, setLang);
142
- (0, external_hooks_js_namespaceObject.useClientSideRedirect)(i18nInstance, localePathRedirect, languages, fallbackLanguage, ignoreRedirectRoutes, localisedUrls);
143
- const contextValue = (0, external_react_namespaceObject.useMemo)(()=>(0, external_hooks_js_namespaceObject.createContextValue)(lang, i18nInstance, entryName, languages, localePathRedirect, ignoreRedirectRoutes, localisedUrls, setLang), [
144
- lang,
145
- i18nInstance,
146
- entryName,
147
- languages,
148
- localePathRedirect,
149
- ignoreRedirectRoutes,
150
- localisedUrls,
151
- forceUpdate
152
- ]);
153
- const children = props.children;
154
- const appContent = /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(jsx_runtime_namespaceObject.Fragment, {
155
- children: [
156
- Boolean(htmlLangAttr) && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(head_namespaceObject.Helmet, {
157
- htmlAttributes: {
158
- lang
159
- }
160
- }),
161
- /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_context_js_namespaceObject.ModernI18nProvider, {
162
- value: contextValue,
163
- children: App ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(App, {
164
- ...props,
165
- children: children
166
- }) : children
167
- })
168
- ]
169
- });
170
- if (!i18nInstance) return appContent;
171
- if (I18nextProvider) {
172
- const i18nextInstanceForProvider = (0, instance_js_namespaceObject.getI18nextInstanceForProvider)(i18nInstance);
173
- return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(I18nextProvider, {
174
- i18n: i18nextInstanceForProvider,
175
- children: appContent
176
- });
177
- }
178
- return appContent;
179
- });
180
- }
55
+ (()=>{
56
+ __webpack_require__.r(__webpack_exports__);
57
+ var _core__rspack_import_0 = __webpack_require__("./core");
58
+ var _i18n_react_i18next__rspack_import_1 = __webpack_require__("./i18n/react-i18next");
59
+ var __rspack_reexport = {};
60
+ for(const __rspack_import_key in _core__rspack_import_0)if ([
61
+ "i18nPlugin",
62
+ "default"
63
+ ].indexOf(__rspack_import_key) < 0) __rspack_reexport[__rspack_import_key] = ()=>_core__rspack_import_0[__rspack_import_key];
64
+ __webpack_require__.d(__webpack_exports__, __rspack_reexport);
65
+ const i18nPlugin = (0, _core__rspack_import_0.createI18nPlugin)(_i18n_react_i18next__rspack_import_1.getReactI18nextIntegration);
66
+ const __rspack_default_export = i18nPlugin;
67
+ __webpack_require__.d(__webpack_exports__, {}, {
68
+ default: __rspack_default_export,
69
+ i18nPlugin: i18nPlugin
181
70
  });
182
- const runtime = i18nPlugin;
183
- exports.I18nLink = __webpack_exports__.I18nLink;
184
- exports.Link = __webpack_exports__.Link;
185
- exports.buildLocalizedUrl = __webpack_exports__.buildLocalizedUrl;
186
- exports.canonicalPath = __webpack_exports__.canonicalPath;
71
+ })();
187
72
  exports["default"] = __webpack_exports__["default"];
188
73
  exports.i18nPlugin = __webpack_exports__.i18nPlugin;
189
- exports.localizePath = __webpack_exports__.localizePath;
190
- exports.splitUrlTarget = __webpack_exports__.splitUrlTarget;
191
- exports.useLocalizedLocation = __webpack_exports__.useLocalizedLocation;
192
- exports.useLocalizedPaths = __webpack_exports__.useLocalizedPaths;
193
- exports.useModernI18n = __webpack_exports__.useModernI18n;
194
74
  for(var __rspack_i in __webpack_exports__)if (-1 === [
195
- "I18nLink",
196
- "Link",
197
- "buildLocalizedUrl",
198
- "canonicalPath",
199
75
  "default",
200
- "i18nPlugin",
201
- "localizePath",
202
- "splitUrlTarget",
203
- "useLocalizedLocation",
204
- "useLocalizedPaths",
205
- "useModernI18n"
76
+ "i18nPlugin"
206
77
  ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
207
78
  Object.defineProperty(exports, '__esModule', {
208
79
  value: true
@@ -0,0 +1,76 @@
1
+ "use strict";
2
+ var __webpack_modules__ = {
3
+ "./core" (module) {
4
+ module.exports = require("./core.js");
5
+ }
6
+ };
7
+ var __webpack_module_cache__ = {};
8
+ function __webpack_require__(moduleId) {
9
+ var cachedModule = __webpack_module_cache__[moduleId];
10
+ if (void 0 !== cachedModule) return cachedModule.exports;
11
+ var module = __webpack_module_cache__[moduleId] = {
12
+ exports: {}
13
+ };
14
+ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
15
+ return module.exports;
16
+ }
17
+ (()=>{
18
+ __webpack_require__.n = (module)=>{
19
+ var getter = module && module.__esModule ? ()=>module['default'] : ()=>module;
20
+ __webpack_require__.d(getter, {
21
+ a: getter
22
+ });
23
+ return getter;
24
+ };
25
+ })();
26
+ (()=>{
27
+ __webpack_require__.d = (exports1, getters, values)=>{
28
+ var define = (defs, kind)=>{
29
+ for(var key in defs)if (__webpack_require__.o(defs, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
30
+ enumerable: true,
31
+ [kind]: defs[key]
32
+ });
33
+ };
34
+ define(getters, "get");
35
+ define(values, "value");
36
+ };
37
+ })();
38
+ (()=>{
39
+ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
40
+ })();
41
+ (()=>{
42
+ __webpack_require__.r = (exports1)=>{
43
+ if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
44
+ value: 'Module'
45
+ });
46
+ Object.defineProperty(exports1, '__esModule', {
47
+ value: true
48
+ });
49
+ };
50
+ })();
51
+ var __webpack_exports__ = {};
52
+ (()=>{
53
+ __webpack_require__.r(__webpack_exports__);
54
+ var _core__rspack_import_0 = __webpack_require__("./core");
55
+ var __rspack_reexport = {};
56
+ for(const __rspack_import_key in _core__rspack_import_0)if ([
57
+ "i18nPlugin",
58
+ "default"
59
+ ].indexOf(__rspack_import_key) < 0) __rspack_reexport[__rspack_import_key] = ()=>_core__rspack_import_0[__rspack_import_key];
60
+ __webpack_require__.d(__webpack_exports__, __rspack_reexport);
61
+ const i18nPlugin = (0, _core__rspack_import_0.createI18nPlugin)();
62
+ const __rspack_default_export = i18nPlugin;
63
+ __webpack_require__.d(__webpack_exports__, {}, {
64
+ default: __rspack_default_export,
65
+ i18nPlugin: i18nPlugin
66
+ });
67
+ })();
68
+ exports["default"] = __webpack_exports__["default"];
69
+ exports.i18nPlugin = __webpack_exports__.i18nPlugin;
70
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
71
+ "default",
72
+ "i18nPlugin"
73
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
74
+ Object.defineProperty(exports, '__esModule', {
75
+ value: true
76
+ });
@@ -31,9 +31,10 @@ const i18nPlugin = (options = {})=>({
31
31
  backend: backendOptions,
32
32
  ...extendedConfig
33
33
  };
34
+ const runtimePluginPath = customPlugin?.runtime?.path || (false === config.reactI18next ? `@${metaName}/plugin-i18n/runtime/no-react-i18next` : `@${metaName}/plugin-i18n/runtime`);
34
35
  plugins.push({
35
36
  name: customPlugin?.runtime?.name || 'i18n',
36
- path: customPlugin?.runtime?.path || `@${metaName}/plugin-i18n/runtime`,
37
+ path: runtimePluginPath,
37
38
  config
38
39
  });
39
40
  return {
@@ -89,6 +89,13 @@ const useModernI18n = ()=>{
89
89
  navigate,
90
90
  location
91
91
  ]);
92
+ const t = useCallback((key, ...args)=>{
93
+ if ('function' != typeof i18nInstance.t) throw new Error('i18nInstance.t is required');
94
+ return i18nInstance.t(key, ...args);
95
+ }, [
96
+ currentLanguage,
97
+ i18nInstance
98
+ ]);
92
99
  const isLanguageSupported = useCallback((lang)=>languages?.includes(lang) || false, [
93
100
  languages
94
101
  ]);
@@ -122,6 +129,7 @@ const useModernI18n = ()=>{
122
129
  return {
123
130
  language: currentLanguage,
124
131
  changeLanguage,
132
+ t,
125
133
  i18nInstance,
126
134
  supportedLanguages: languages || [],
127
135
  localisedUrls,