@bleedingdev/modern-js-plugin-i18n 3.2.0-ultramodern.9 → 3.2.0-ultramodern.90

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 (80) hide show
  1. package/dist/cjs/cli/index.js +22 -0
  2. package/dist/cjs/runtime/I18nLink.js +4 -12
  3. package/dist/cjs/runtime/context.js +32 -5
  4. package/dist/cjs/runtime/hooks.js +8 -5
  5. package/dist/cjs/runtime/i18n/backend/defaults.js +1 -1
  6. package/dist/cjs/runtime/i18n/backend/defaults.node.js +2 -2
  7. package/dist/cjs/runtime/i18n/backend/middleware.node.js +4 -4
  8. package/dist/cjs/runtime/i18n/instance.js +0 -24
  9. package/dist/cjs/runtime/i18n/react-i18next.js +49 -0
  10. package/dist/cjs/runtime/i18n/utils.js +0 -12
  11. package/dist/cjs/runtime/index.js +18 -10
  12. package/dist/cjs/runtime/routerAdapter.js +163 -0
  13. package/dist/cjs/runtime/utils.js +63 -94
  14. package/dist/cjs/server/index.js +60 -8
  15. package/dist/cjs/shared/localisedUrls.js +237 -0
  16. package/dist/esm/cli/index.mjs +22 -0
  17. package/dist/esm/runtime/I18nLink.mjs +4 -12
  18. package/dist/esm/runtime/context.mjs +34 -7
  19. package/dist/esm/runtime/hooks.mjs +9 -6
  20. package/dist/esm/runtime/i18n/backend/defaults.mjs +1 -1
  21. package/dist/esm/runtime/i18n/backend/defaults.node.mjs +2 -2
  22. package/dist/esm/runtime/i18n/backend/middleware.node.mjs +3 -3
  23. package/dist/esm/runtime/i18n/instance.mjs +1 -19
  24. package/dist/esm/runtime/i18n/react-i18next.mjs +15 -0
  25. package/dist/esm/runtime/i18n/utils.mjs +0 -12
  26. package/dist/esm/runtime/index.mjs +19 -11
  27. package/dist/esm/runtime/routerAdapter.mjs +129 -0
  28. package/dist/esm/runtime/utils.mjs +11 -30
  29. package/dist/esm/server/index.mjs +53 -7
  30. package/dist/esm/shared/localisedUrls.mjs +191 -0
  31. package/dist/esm-node/cli/index.mjs +22 -0
  32. package/dist/esm-node/runtime/I18nLink.mjs +4 -12
  33. package/dist/esm-node/runtime/context.mjs +34 -7
  34. package/dist/esm-node/runtime/hooks.mjs +9 -6
  35. package/dist/esm-node/runtime/i18n/backend/defaults.mjs +1 -1
  36. package/dist/esm-node/runtime/i18n/backend/defaults.node.mjs +2 -2
  37. package/dist/esm-node/runtime/i18n/backend/middleware.node.mjs +3 -3
  38. package/dist/esm-node/runtime/i18n/instance.mjs +1 -19
  39. package/dist/esm-node/runtime/i18n/react-i18next.mjs +16 -0
  40. package/dist/esm-node/runtime/i18n/utils.mjs +0 -12
  41. package/dist/esm-node/runtime/index.mjs +19 -11
  42. package/dist/esm-node/runtime/routerAdapter.mjs +130 -0
  43. package/dist/esm-node/runtime/utils.mjs +11 -30
  44. package/dist/esm-node/server/index.mjs +53 -7
  45. package/dist/esm-node/shared/localisedUrls.mjs +192 -0
  46. package/dist/types/runtime/I18nLink.d.ts +15 -0
  47. package/dist/types/runtime/context.d.ts +3 -0
  48. package/dist/types/runtime/hooks.d.ts +4 -2
  49. package/dist/types/runtime/i18n/backend/middleware.node.d.ts +1 -1
  50. package/dist/types/runtime/i18n/instance.d.ts +0 -5
  51. package/dist/types/runtime/i18n/react-i18next.d.ts +7 -0
  52. package/dist/types/runtime/index.d.ts +1 -0
  53. package/dist/types/runtime/routerAdapter.d.ts +26 -0
  54. package/dist/types/runtime/utils.d.ts +2 -7
  55. package/dist/types/server/index.d.ts +6 -0
  56. package/dist/types/shared/localisedUrls.d.ts +13 -0
  57. package/dist/types/shared/type.d.ts +12 -0
  58. package/package.json +18 -22
  59. package/rstest.config.mts +39 -0
  60. package/src/cli/index.ts +43 -1
  61. package/src/runtime/I18nLink.tsx +10 -16
  62. package/src/runtime/context.tsx +45 -7
  63. package/src/runtime/hooks.ts +13 -4
  64. package/src/runtime/i18n/backend/defaults.node.ts +2 -2
  65. package/src/runtime/i18n/backend/defaults.ts +3 -1
  66. package/src/runtime/i18n/backend/middleware.node.ts +1 -1
  67. package/src/runtime/i18n/instance.ts +0 -29
  68. package/src/runtime/i18n/react-i18next.ts +25 -0
  69. package/src/runtime/i18n/utils.ts +4 -26
  70. package/src/runtime/index.tsx +23 -10
  71. package/src/runtime/routerAdapter.tsx +333 -0
  72. package/src/runtime/utils.ts +22 -34
  73. package/src/server/index.ts +117 -10
  74. package/src/shared/localisedUrls.ts +393 -0
  75. package/src/shared/type.ts +12 -0
  76. package/tests/i18nUtils.test.ts +52 -0
  77. package/tests/localisedUrls.test.ts +312 -0
  78. package/tests/routerAdapter.test.tsx +382 -0
  79. package/dist/esm/rslib-runtime.mjs +0 -18
  80. package/dist/esm-node/rslib-runtime.mjs +0 -19
@@ -41,6 +41,7 @@ const external_fs_namespaceObject = require("fs");
41
41
  var external_fs_default = /*#__PURE__*/ __webpack_require__.n(external_fs_namespaceObject);
42
42
  const external_path_namespaceObject = require("path");
43
43
  var external_path_default = /*#__PURE__*/ __webpack_require__.n(external_path_namespaceObject);
44
+ const localisedUrls_js_namespaceObject = require("../shared/localisedUrls.js");
44
45
  const utils_js_namespaceObject = require("../shared/utils.js");
45
46
  function hasJsonFiles(dirPath) {
46
47
  try {
@@ -120,6 +121,27 @@ const i18nPlugin = (options = {})=>({
120
121
  plugins
121
122
  };
122
123
  });
124
+ api.modifyFileSystemRoutes(({ entrypoint, routes })=>{
125
+ if (!localeDetection) return {
126
+ entrypoint,
127
+ routes
128
+ };
129
+ const localeDetectionOptions = (0, utils_js_namespaceObject.getLocaleDetectionOptions)(entrypoint.entryName, localeDetection);
130
+ const { localePathRedirect, languages = [], localisedUrls } = localeDetectionOptions;
131
+ if (!localePathRedirect || 0 === languages.length) return {
132
+ entrypoint,
133
+ routes
134
+ };
135
+ const localisedUrlsConfig = (0, localisedUrls_js_namespaceObject.resolveLocalisedUrlsConfig)(localisedUrls);
136
+ if (!localisedUrlsConfig.enabled) return {
137
+ entrypoint,
138
+ routes
139
+ };
140
+ return {
141
+ entrypoint,
142
+ routes: (0, localisedUrls_js_namespaceObject.applyLocalisedUrlsToRoutes)(routes, languages, localisedUrlsConfig.map)
143
+ };
144
+ });
123
145
  api._internalServerPlugins(({ plugins })=>{
124
146
  const { serverRoutes, metaName } = api.getAppContext();
125
147
  const normalizedConfig = api.getNormalizedConfig();
@@ -28,22 +28,14 @@ __webpack_require__.d(__webpack_exports__, {
28
28
  default: ()=>runtime_I18nLink
29
29
  });
30
30
  const jsx_runtime_namespaceObject = require("react/jsx-runtime");
31
- const router_namespaceObject = require("@modern-js/runtime/router");
32
31
  const external_context_js_namespaceObject = require("./context.js");
32
+ const external_routerAdapter_js_namespaceObject = require("./routerAdapter.js");
33
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
34
  const I18nLink = ({ to, children, ...props })=>{
43
- const { Link, params, hasRouter } = useRouterHooks();
44
- const { language, supportedLanguages } = (0, external_context_js_namespaceObject.useModernI18n)();
35
+ const { Link, params, hasRouter } = (0, external_routerAdapter_js_namespaceObject.useI18nRouterAdapter)();
36
+ const { language, supportedLanguages, localisedUrls } = (0, external_context_js_namespaceObject.useModernI18n)();
45
37
  const currentLang = language;
46
- const localizedTo = (0, external_utils_js_namespaceObject.buildLocalizedUrl)(to, currentLang, supportedLanguages);
38
+ const localizedTo = (0, external_utils_js_namespaceObject.buildLocalizedUrl)(to, currentLang, supportedLanguages, localisedUrls);
47
39
  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
40
  if (!hasRouter || !Link) return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("a", {
49
41
  href: localizedTo,
@@ -31,6 +31,7 @@ const jsx_runtime_namespaceObject = require("react/jsx-runtime");
31
31
  const runtime_namespaceObject = require("@modern-js/runtime");
32
32
  const external_react_namespaceObject = require("react");
33
33
  const index_js_namespaceObject = require("./i18n/detection/index.js");
34
+ const external_routerAdapter_js_namespaceObject = require("./routerAdapter.js");
34
35
  const external_utils_js_namespaceObject = require("./utils.js");
35
36
  const ModernI18nContext = /*#__PURE__*/ (0, external_react_namespaceObject.createContext)(null);
36
37
  const ModernI18nProvider = ({ children, value })=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(ModernI18nContext.Provider, {
@@ -40,9 +41,33 @@ const ModernI18nProvider = ({ children, value })=>/*#__PURE__*/ (0, jsx_runtime_
40
41
  const useModernI18n = ()=>{
41
42
  const context = (0, external_react_namespaceObject.useContext)(ModernI18nContext);
42
43
  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;
44
+ const { language: contextLanguage, i18nInstance, languages, localePathRedirect, ignoreRedirectRoutes, localisedUrls, updateLanguage } = context;
45
+ const { navigate, location, hasRouter } = (0, external_routerAdapter_js_namespaceObject.useI18nRouterAdapter)();
46
+ const pathLanguage = (0, external_react_namespaceObject.useMemo)(()=>{
47
+ if (!localePathRedirect || !location?.pathname) return;
48
+ const detected = (0, external_utils_js_namespaceObject.detectLanguageFromPath)(location.pathname, languages || [], localePathRedirect);
49
+ return detected.detected ? detected.language : void 0;
50
+ }, [
51
+ languages,
52
+ localePathRedirect,
53
+ location?.pathname
54
+ ]);
55
+ const currentLanguage = pathLanguage || contextLanguage;
56
+ (0, external_react_namespaceObject.useEffect)(()=>{
57
+ if (!pathLanguage || pathLanguage === contextLanguage) return;
58
+ updateLanguage?.(pathLanguage);
59
+ i18nInstance?.setLang?.(pathLanguage);
60
+ i18nInstance?.changeLanguage?.(pathLanguage);
61
+ if ((0, runtime_namespaceObject.isBrowser)()) {
62
+ const detectionOptions = i18nInstance.options?.detection;
63
+ (0, index_js_namespaceObject.cacheUserLanguage)(i18nInstance, pathLanguage, detectionOptions);
64
+ }
65
+ }, [
66
+ contextLanguage,
67
+ i18nInstance,
68
+ pathLanguage,
69
+ updateLanguage
70
+ ]);
46
71
  const changeLanguage = (0, external_react_namespaceObject.useCallback)(async (newLang)=>{
47
72
  try {
48
73
  if (!newLang || 'string' != typeof newLang) throw new Error('Language must be a non-empty string');
@@ -59,7 +84,7 @@ const useModernI18n = ()=>{
59
84
  const pathLanguage = (0, external_utils_js_namespaceObject.detectLanguageFromPath)(currentPath, languages || [], localePathRedirect);
60
85
  if (pathLanguage.detected && pathLanguage.language === newLang) return;
61
86
  if (!(0, external_utils_js_namespaceObject.shouldIgnoreRedirect)(relativePath, languages || [], ignoreRedirectRoutes)) {
62
- const newPath = (0, external_utils_js_namespaceObject.buildLocalizedUrl)(relativePath, newLang, languages || []);
87
+ const newPath = (0, external_utils_js_namespaceObject.buildLocalizedUrl)(relativePath, newLang, languages || [], localisedUrls);
63
88
  const newUrl = entryPath + newPath + location.search + location.hash;
64
89
  await navigate(newUrl, {
65
90
  replace: true
@@ -72,7 +97,7 @@ const useModernI18n = ()=>{
72
97
  const pathLanguage = (0, external_utils_js_namespaceObject.detectLanguageFromPath)(currentPath, languages || [], localePathRedirect);
73
98
  if (pathLanguage.detected && pathLanguage.language === newLang) return;
74
99
  if (!(0, external_utils_js_namespaceObject.shouldIgnoreRedirect)(relativePath, languages || [], ignoreRedirectRoutes)) {
75
- const newPath = (0, external_utils_js_namespaceObject.buildLocalizedUrl)(relativePath, newLang, languages || []);
100
+ const newPath = (0, external_utils_js_namespaceObject.buildLocalizedUrl)(relativePath, newLang, languages || [], localisedUrls);
76
101
  const newUrl = entryPath + newPath + window.location.search + window.location.hash;
77
102
  window.history.pushState(null, '', newUrl);
78
103
  }
@@ -87,6 +112,7 @@ const useModernI18n = ()=>{
87
112
  updateLanguage,
88
113
  localePathRedirect,
89
114
  ignoreRedirectRoutes,
115
+ localisedUrls,
90
116
  languages,
91
117
  hasRouter,
92
118
  navigate,
@@ -127,6 +153,7 @@ const useModernI18n = ()=>{
127
153
  changeLanguage,
128
154
  i18nInstance,
129
155
  supportedLanguages: languages || [],
156
+ localisedUrls,
130
157
  isLanguageSupported,
131
158
  isResourcesReady
132
159
  };
@@ -33,6 +33,7 @@ const runtime_namespaceObject = require("@modern-js/runtime");
33
33
  const external_react_namespaceObject = require("react");
34
34
  const sdk_event_js_namespaceObject = require("./i18n/backend/sdk-event.js");
35
35
  const index_js_namespaceObject = require("./i18n/detection/index.js");
36
+ const external_routerAdapter_js_namespaceObject = require("./routerAdapter.js");
36
37
  const external_utils_js_namespaceObject = require("./utils.js");
37
38
  function createMinimalI18nInstance(language) {
38
39
  const minimalInstance = {
@@ -45,7 +46,7 @@ function createMinimalI18nInstance(language) {
45
46
  };
46
47
  return minimalInstance;
47
48
  }
48
- function createContextValue(lang, i18nInstance, entryName, languages, localePathRedirect, ignoreRedirectRoutes, setLang) {
49
+ function createContextValue(lang, i18nInstance, entryName, languages, localePathRedirect, ignoreRedirectRoutes, localisedUrls, setLang) {
49
50
  const instance = i18nInstance || createMinimalI18nInstance(lang);
50
51
  return {
51
52
  language: lang,
@@ -54,6 +55,7 @@ function createContextValue(lang, i18nInstance, entryName, languages, localePath
54
55
  languages,
55
56
  localePathRedirect,
56
57
  ignoreRedirectRoutes,
58
+ localisedUrls,
57
59
  updateLanguage: setLang
58
60
  };
59
61
  }
@@ -103,9 +105,9 @@ function useSdkResourcesLoader(i18nInstance, setForceUpdate) {
103
105
  setForceUpdate
104
106
  ]);
105
107
  }
106
- function useClientSideRedirect(i18nInstance, localePathRedirect, languages, fallbackLanguage, ignoreRedirectRoutes) {
108
+ function useClientSideRedirect(i18nInstance, localePathRedirect, languages, fallbackLanguage, ignoreRedirectRoutes, localisedUrls) {
107
109
  const hasRedirectedRef = (0, external_react_namespaceObject.useRef)(false);
108
- const { navigate, location, hasRouter } = (0, external_utils_js_namespaceObject.useRouterHooks)();
110
+ const { navigate, location, hasRouter } = (0, external_routerAdapter_js_namespaceObject.useI18nRouterAdapter)();
109
111
  (0, external_react_namespaceObject.useEffect)(()=>{
110
112
  if ('browser' !== process.env.MODERN_TARGET) return;
111
113
  if (!localePathRedirect || !i18nInstance) return;
@@ -124,7 +126,7 @@ function useClientSideRedirect(i18nInstance, localePathRedirect, languages, fall
124
126
  const pathDetection = (0, external_utils_js_namespaceObject.detectLanguageFromPath)(currentPathname, languages, localePathRedirect);
125
127
  if (pathDetection.detected) return;
126
128
  const targetLanguage = i18nInstance.language || fallbackLanguage || languages[0] || 'en';
127
- const newPath = (0, external_utils_js_namespaceObject.buildLocalizedUrl)(relativePath, targetLanguage, languages);
129
+ const newPath = (0, external_utils_js_namespaceObject.buildLocalizedUrl)(relativePath, targetLanguage, languages, localisedUrls);
128
130
  const newUrl = entryPath + newPath + currentSearch + currentHash;
129
131
  if (newUrl !== currentPathname + currentSearch + currentHash) {
130
132
  hasRedirectedRef.current = true;
@@ -141,7 +143,8 @@ function useClientSideRedirect(i18nInstance, localePathRedirect, languages, fall
141
143
  i18nInstance,
142
144
  languages,
143
145
  fallbackLanguage,
144
- ignoreRedirectRoutes
146
+ ignoreRedirectRoutes,
147
+ localisedUrls
145
148
  ]);
146
149
  }
147
150
  function useLanguageSync(i18nInstance, localePathRedirect, languages, runtimeContextRef, prevLangRef, setLang) {
@@ -33,7 +33,7 @@ const DEFAULT_I18NEXT_BACKEND_OPTIONS = {
33
33
  };
34
34
  function convertPath(path) {
35
35
  if (!path) return path;
36
- if (path.startsWith('/')) return `${window.__assetPrefix__ || ''}${path}`;
36
+ if (path.startsWith('/')) return "u" < typeof window ? path : `${window.__assetPrefix__ || ''}${path}`;
37
37
  return path;
38
38
  }
39
39
  function convertBackendOptions(options) {
@@ -28,8 +28,8 @@ __webpack_require__.d(__webpack_exports__, {
28
28
  convertBackendOptions: ()=>convertBackendOptions
29
29
  });
30
30
  const DEFAULT_I18NEXT_BACKEND_OPTIONS = {
31
- loadPath: './locales/{{lng}}/{{ns}}.json',
32
- addPath: './locales/{{lng}}/{{ns}}.json'
31
+ loadPath: './config/public/locales/{{lng}}/{{ns}}.json',
32
+ addPath: './config/public/locales/{{lng}}/{{ns}}.json'
33
33
  };
34
34
  function convertPath(path) {
35
35
  if (!path) return path;
@@ -37,14 +37,14 @@ __webpack_require__.d(__webpack_exports__, {
37
37
  HttpBackendWithSave: ()=>HttpBackendWithSave,
38
38
  useI18nextBackend: ()=>useI18nextBackend
39
39
  });
40
- const external_i18next_fs_backend_namespaceObject = require("i18next-fs-backend");
41
- var external_i18next_fs_backend_default = /*#__PURE__*/ __webpack_require__.n(external_i18next_fs_backend_namespaceObject);
40
+ const cjs_namespaceObject = require("i18next-fs-backend/cjs");
41
+ var cjs_default = /*#__PURE__*/ __webpack_require__.n(cjs_namespaceObject);
42
42
  const external_middleware_common_js_namespaceObject = require("./middleware.common.js");
43
- class FsBackendWithSave extends external_i18next_fs_backend_default() {
43
+ class FsBackendWithSave extends cjs_default() {
44
44
  save(_language, _namespace, _data) {}
45
45
  }
46
46
  const HttpBackendWithSave = FsBackendWithSave;
47
- const useI18nextBackend = (i18nInstance, backend)=>(0, external_middleware_common_js_namespaceObject.useI18nextBackendCommon)(i18nInstance, FsBackendWithSave, external_i18next_fs_backend_default(), backend);
47
+ const useI18nextBackend = (i18nInstance, backend)=>(0, external_middleware_common_js_namespaceObject.useI18nextBackendCommon)(i18nInstance, FsBackendWithSave, cjs_default(), backend);
48
48
  exports.FsBackendWithSave = __webpack_exports__.FsBackendWithSave;
49
49
  exports.HttpBackendWithSave = __webpack_exports__.HttpBackendWithSave;
50
50
  exports.useI18nextBackend = __webpack_exports__.useI18nextBackend;
@@ -28,8 +28,6 @@ __webpack_require__.d(__webpack_exports__, {
28
28
  getI18nInstance: ()=>getI18nInstance,
29
29
  getI18nWrapperI18nextInstance: ()=>getI18nWrapperI18nextInstance,
30
30
  getI18nextInstanceForProvider: ()=>getI18nextInstanceForProvider,
31
- getI18nextProvider: ()=>getI18nextProvider,
32
- getInitReactI18next: ()=>getInitReactI18next,
33
31
  isI18nInstance: ()=>isI18nInstance,
34
32
  isI18nWrapperInstance: ()=>isI18nWrapperInstance
35
33
  });
@@ -75,14 +73,6 @@ async function createI18nextInstance() {
75
73
  return null;
76
74
  }
77
75
  }
78
- async function tryImportReactI18next() {
79
- try {
80
- const reactI18next = await import("react-i18next");
81
- return reactI18next;
82
- } catch (error) {
83
- return null;
84
- }
85
- }
86
76
  function getI18nextInstanceForProvider(instance) {
87
77
  if (isI18nWrapperInstance(instance)) {
88
78
  const i18nextInstance = getI18nWrapperI18nextInstance(instance);
@@ -99,22 +89,10 @@ async function getI18nInstance(userInstance) {
99
89
  if (i18nextInstance) return i18nextInstance;
100
90
  throw new Error('No i18n instance found');
101
91
  }
102
- async function getInitReactI18next() {
103
- const reactI18nextModule = await tryImportReactI18next();
104
- if (reactI18nextModule) return reactI18nextModule.initReactI18next;
105
- return null;
106
- }
107
- async function getI18nextProvider() {
108
- const reactI18nextModule = await tryImportReactI18next();
109
- if (reactI18nextModule) return reactI18nextModule.I18nextProvider;
110
- return null;
111
- }
112
92
  exports.getActualI18nextInstance = __webpack_exports__.getActualI18nextInstance;
113
93
  exports.getI18nInstance = __webpack_exports__.getI18nInstance;
114
94
  exports.getI18nWrapperI18nextInstance = __webpack_exports__.getI18nWrapperI18nextInstance;
115
95
  exports.getI18nextInstanceForProvider = __webpack_exports__.getI18nextInstanceForProvider;
116
- exports.getI18nextProvider = __webpack_exports__.getI18nextProvider;
117
- exports.getInitReactI18next = __webpack_exports__.getInitReactI18next;
118
96
  exports.isI18nInstance = __webpack_exports__.isI18nInstance;
119
97
  exports.isI18nWrapperInstance = __webpack_exports__.isI18nWrapperInstance;
120
98
  for(var __rspack_i in __webpack_exports__)if (-1 === [
@@ -122,8 +100,6 @@ for(var __rspack_i in __webpack_exports__)if (-1 === [
122
100
  "getI18nInstance",
123
101
  "getI18nWrapperI18nextInstance",
124
102
  "getI18nextInstanceForProvider",
125
- "getI18nextProvider",
126
- "getInitReactI18next",
127
103
  "isI18nInstance",
128
104
  "isI18nWrapperInstance"
129
105
  ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
@@ -0,0 +1,49 @@
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
+ getReactI18nextIntegration: ()=>getReactI18nextIntegration
28
+ });
29
+ async function tryImportReactI18next() {
30
+ try {
31
+ return await import("react-i18next");
32
+ } catch (error) {
33
+ return null;
34
+ }
35
+ }
36
+ async function getReactI18nextIntegration() {
37
+ const reactI18nextModule = await tryImportReactI18next();
38
+ return {
39
+ I18nextProvider: reactI18nextModule?.I18nextProvider ?? null,
40
+ initReactI18next: reactI18nextModule?.initReactI18next ?? null
41
+ };
42
+ }
43
+ exports.getReactI18nextIntegration = __webpack_exports__.getReactI18nextIntegration;
44
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
45
+ "getReactI18nextIntegration"
46
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
47
+ Object.defineProperty(exports, '__esModule', {
48
+ value: true
49
+ });
@@ -133,18 +133,6 @@ const initializeI18nInstance = async (i18nInstance, finalLanguage, fallbackLangu
133
133
  };
134
134
  }
135
135
  }
136
- if (mergedBackend && hasOptions(i18nInstance)) {
137
- const defaultNS = initOptions.defaultNS || initOptions.ns || 'translation';
138
- const ns = Array.isArray(defaultNS) ? defaultNS[0] : defaultNS;
139
- let retries = 20;
140
- while(retries > 0){
141
- const actualInstance = (0, external_instance_js_namespaceObject.getActualI18nextInstance)(i18nInstance);
142
- const store = actualInstance.store;
143
- if (store?.data?.[finalLanguage]?.[ns]) break;
144
- await new Promise((resolve)=>setTimeout(resolve, 100));
145
- retries--;
146
- }
147
- }
148
136
  }
149
137
  };
150
138
  function hasOptions(instance) {
@@ -49,19 +49,24 @@ const external_I18nLink_js_namespaceObject = require("./I18nLink.js");
49
49
  const i18nPlugin = (options)=>({
50
50
  name: '@modern-js/plugin-i18n',
51
51
  setup: (api)=>{
52
- const { entryName, i18nInstance: userI18nInstance, initOptions, localeDetection, backend, htmlLangAttr = false } = options;
53
- const { localePathRedirect = false, i18nextDetector = true, languages = [], fallbackLanguage = 'en', detection, ignoreRedirectRoutes } = localeDetection || {};
52
+ const { entryName, i18nInstance: userI18nInstance, initOptions, localeDetection, backend, htmlLangAttr = false, reactI18next = true } = options;
53
+ const { localePathRedirect = false, i18nextDetector = true, languages = [], fallbackLanguage = 'en', detection, ignoreRedirectRoutes, localisedUrls } = localeDetection || {};
54
54
  const { enabled: backendEnabled = false } = backend || {};
55
55
  let latestI18nInstance;
56
56
  let I18nextProvider;
57
+ const loadReactI18nextIntegration = async ()=>{
58
+ if (!reactI18next) return null;
59
+ const { getReactI18nextIntegration } = await import("./i18n/react-i18next.js");
60
+ return getReactI18nextIntegration();
61
+ };
57
62
  api.onBeforeRender(async (context)=>{
58
63
  let i18nInstance = await (0, index_js_namespaceObject.getI18nInstance)(userI18nInstance);
59
64
  const { i18n: otherConfig } = api.getRuntimeConfig();
60
65
  const { initOptions: otherInitOptions } = otherConfig || {};
61
66
  const userInitOptions = (0, merge_namespaceObject.merge)(otherInitOptions || {}, initOptions || {});
62
- const initReactI18next = await (0, instance_js_namespaceObject.getInitReactI18next)();
63
- I18nextProvider = await (0, instance_js_namespaceObject.getI18nextProvider)();
64
- if (initReactI18next) i18nInstance.use(initReactI18next);
67
+ const reactI18nextIntegration = await loadReactI18nextIntegration();
68
+ I18nextProvider = reactI18nextIntegration?.I18nextProvider ?? null;
69
+ if (reactI18nextIntegration?.initReactI18next) i18nInstance.use(reactI18nextIntegration.initReactI18next);
65
70
  const pathname = (0, external_utils_js_namespaceObject.getPathname)(context);
66
71
  if (i18nextDetector) (0, detection_middleware_js_namespaceObject.useI18nextLanguageDetector)(i18nInstance);
67
72
  const mergedDetection = (0, detection_index_js_namespaceObject.mergeDetectionOptions)(i18nextDetector, detection, localePathRedirect, userInitOptions);
@@ -121,16 +126,18 @@ const i18nPlugin = (options)=>({
121
126
  ]);
122
127
  (0, external_hooks_js_namespaceObject.useSdkResourcesLoader)(i18nInstance, setForceUpdate);
123
128
  (0, external_hooks_js_namespaceObject.useLanguageSync)(i18nInstance, localePathRedirect, languages, runtimeContextRef, prevLangRef, setLang);
124
- (0, external_hooks_js_namespaceObject.useClientSideRedirect)(i18nInstance, localePathRedirect, languages, fallbackLanguage, ignoreRedirectRoutes);
125
- const contextValue = (0, external_react_namespaceObject.useMemo)(()=>(0, external_hooks_js_namespaceObject.createContextValue)(lang, i18nInstance, entryName, languages, localePathRedirect, ignoreRedirectRoutes, setLang), [
129
+ (0, external_hooks_js_namespaceObject.useClientSideRedirect)(i18nInstance, localePathRedirect, languages, fallbackLanguage, ignoreRedirectRoutes, localisedUrls);
130
+ const contextValue = (0, external_react_namespaceObject.useMemo)(()=>(0, external_hooks_js_namespaceObject.createContextValue)(lang, i18nInstance, entryName, languages, localePathRedirect, ignoreRedirectRoutes, localisedUrls, setLang), [
126
131
  lang,
127
132
  i18nInstance,
128
133
  entryName,
129
134
  languages,
130
135
  localePathRedirect,
131
136
  ignoreRedirectRoutes,
137
+ localisedUrls,
132
138
  forceUpdate
133
139
  ]);
140
+ const children = props.children;
134
141
  const appContent = /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(jsx_runtime_namespaceObject.Fragment, {
135
142
  children: [
136
143
  Boolean(htmlLangAttr) && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(head_namespaceObject.Helmet, {
@@ -140,9 +147,10 @@ const i18nPlugin = (options)=>({
140
147
  }),
141
148
  /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_context_js_namespaceObject.ModernI18nProvider, {
142
149
  value: contextValue,
143
- children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(App, {
144
- ...props
145
- })
150
+ children: App ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(App, {
151
+ ...props,
152
+ children: children
153
+ }) : children
146
154
  })
147
155
  ]
148
156
  });
@@ -0,0 +1,163 @@
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
+ useI18nRouterAdapter: ()=>useI18nRouterAdapter
28
+ });
29
+ const runtime_namespaceObject = require("@modern-js/runtime");
30
+ const context_namespaceObject = require("@modern-js/runtime/context");
31
+ const router_namespaceObject = require("@modern-js/runtime/router");
32
+ const external_react_namespaceObject = require("react");
33
+ const normalizeUrlPart = (value, prefix)=>{
34
+ if ('string' != typeof value || !value) return '';
35
+ return value.startsWith(prefix) ? value : `${prefix}${value}`;
36
+ };
37
+ const normalizeLocation = (location)=>{
38
+ if (!location || 'object' != typeof location) return null;
39
+ const locationValue = location;
40
+ if ('string' != typeof locationValue.pathname) return null;
41
+ return {
42
+ pathname: locationValue.pathname,
43
+ search: normalizeUrlPart('string' == typeof locationValue.search ? locationValue.search : locationValue.searchStr, '?'),
44
+ hash: normalizeUrlPart(locationValue.hash, '#')
45
+ };
46
+ };
47
+ const getWindowLocation = ()=>{
48
+ if (!(0, runtime_namespaceObject.isBrowser)()) return null;
49
+ return {
50
+ pathname: window.location.pathname,
51
+ search: window.location.search,
52
+ hash: window.location.hash
53
+ };
54
+ };
55
+ const getRouterFramework = (runtimeContext, internalContext, inReactRouter)=>{
56
+ const framework = internalContext.routerFramework || internalContext.routerRuntime?.framework || runtimeContext.routerFramework;
57
+ if (framework) return framework;
58
+ if (internalContext.router?.useRouter || runtimeContext.router?.useRouter) return 'tanstack';
59
+ if (internalContext.router?.useLocation || internalContext.router?.useHref || runtimeContext.router?.useLocation || runtimeContext.router?.useHref) return 'react-router';
60
+ if (inReactRouter) return 'react-router';
61
+ };
62
+ const getRouterInstance = (internalContext, contextRouter)=>{
63
+ if (contextRouter) return contextRouter;
64
+ const router = internalContext.routerInstance || internalContext.routerRuntime?.instance;
65
+ if (!router || 'object' != typeof router) return null;
66
+ return router;
67
+ };
68
+ const getRouterStateLocation = (internalContext, contextRouter)=>{
69
+ const router = getRouterInstance(internalContext, contextRouter);
70
+ return normalizeLocation(router?.stores?.location?.get?.()) || normalizeLocation(router?.state?.location);
71
+ };
72
+ const getRouterParams = (internalContext, contextRouter)=>{
73
+ const router = getRouterInstance(internalContext, contextRouter);
74
+ const matches = router?.stores?.matches?.get?.() || router?.state?.matches;
75
+ if (!Array.isArray(matches)) return {};
76
+ return matches.reduce((params, match)=>{
77
+ if (match?.params) Object.assign(params, match.params);
78
+ return params;
79
+ }, {});
80
+ };
81
+ const useI18nRouterAdapter = ()=>{
82
+ const runtimeContext = (0, external_react_namespaceObject.useContext)(runtime_namespaceObject.RuntimeContext);
83
+ const internalContext = (0, external_react_namespaceObject.useContext)(context_namespaceObject.InternalRuntimeContext);
84
+ const inReactRouter = (0, router_namespaceObject.useInRouterContext)();
85
+ const reactRouterNavigate = inReactRouter ? (0, router_namespaceObject.useNavigate)() : null;
86
+ const reactRouterLocation = inReactRouter ? (0, router_namespaceObject.useLocation)() : null;
87
+ const reactRouterParams = inReactRouter ? (0, router_namespaceObject.useParams)() : {};
88
+ const framework = getRouterFramework(runtimeContext, internalContext, inReactRouter);
89
+ const contextUseRouter = inReactRouter || 'tanstack' !== framework ? void 0 : internalContext.router?.useRouter || runtimeContext.router?.useRouter;
90
+ const contextRouter = contextUseRouter ? contextUseRouter({
91
+ warn: false
92
+ }) : null;
93
+ const [, setRouterVersion] = (0, external_react_namespaceObject.useState)(0);
94
+ const hasRouter = 'tanstack' === framework || 'react-router' === framework || Boolean(reactRouterNavigate);
95
+ (0, external_react_namespaceObject.useEffect)(()=>{
96
+ if ('tanstack' !== framework) return;
97
+ const router = getRouterInstance(internalContext, contextRouter);
98
+ if (!router) return;
99
+ const update = ()=>setRouterVersion((version)=>version + 1);
100
+ const unsubscribers = [];
101
+ if ('function' == typeof router.stores?.location?.subscribe) {
102
+ const unsubscribe = router.stores.location.subscribe(update);
103
+ if ('function' == typeof unsubscribe) unsubscribers.push(unsubscribe);
104
+ }
105
+ if ('function' == typeof router.subscribe) for (const eventType of [
106
+ 'onBeforeNavigate',
107
+ 'onBeforeLoad'
108
+ ]){
109
+ const unsubscribe = router.subscribe(eventType, update);
110
+ if ('function' == typeof unsubscribe) unsubscribers.push(unsubscribe);
111
+ }
112
+ return ()=>{
113
+ for (const unsubscribe of unsubscribers)unsubscribe();
114
+ };
115
+ }, [
116
+ contextRouter,
117
+ framework,
118
+ internalContext
119
+ ]);
120
+ const navigate = (0, external_react_namespaceObject.useCallback)((href, options)=>{
121
+ const router = getRouterInstance(internalContext, contextRouter);
122
+ const activeFramework = getRouterFramework(runtimeContext, internalContext, inReactRouter);
123
+ if ('tanstack' === activeFramework) {
124
+ if ('function' == typeof router?.navigate) return router.navigate({
125
+ to: href,
126
+ replace: options?.replace,
127
+ ...options?.state === void 0 ? {} : {
128
+ state: options.state
129
+ }
130
+ });
131
+ throw new Error('TanStack router instance is not available.');
132
+ }
133
+ if (reactRouterNavigate) return reactRouterNavigate(href, options);
134
+ if ('react-router' === activeFramework) {
135
+ if ('function' == typeof router?.navigate) return router.navigate(href, options);
136
+ throw new Error('React Router instance is not available.');
137
+ }
138
+ }, [
139
+ contextRouter,
140
+ internalContext,
141
+ inReactRouter,
142
+ reactRouterNavigate,
143
+ runtimeContext
144
+ ]);
145
+ const location = (reactRouterLocation ? normalizeLocation(reactRouterLocation) : getRouterStateLocation(internalContext, contextRouter)) || getWindowLocation();
146
+ const params = inReactRouter ? reactRouterParams : getRouterParams(internalContext, contextRouter);
147
+ const Link = 'tanstack' === framework ? internalContext.router?.Link || runtimeContext.router?.Link || null : 'react-router' === framework || inReactRouter ? router_namespaceObject.Link : null;
148
+ return {
149
+ framework,
150
+ hasRouter,
151
+ location,
152
+ navigate: hasRouter ? navigate : null,
153
+ Link,
154
+ params
155
+ };
156
+ };
157
+ exports.useI18nRouterAdapter = __webpack_exports__.useI18nRouterAdapter;
158
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
159
+ "useI18nRouterAdapter"
160
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
161
+ Object.defineProperty(exports, '__esModule', {
162
+ value: true
163
+ });