@bleedingdev/modern-js-plugin-i18n 3.2.0-ultramodern.17 → 3.2.0-ultramodern.22

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 (63) 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/middleware.node.js +4 -4
  7. package/dist/cjs/runtime/i18n/instance.js +95 -80
  8. package/dist/cjs/runtime/index.js +7 -6
  9. package/dist/cjs/runtime/routerAdapter.js +164 -0
  10. package/dist/cjs/runtime/utils.js +63 -94
  11. package/dist/cjs/server/index.js +64 -8
  12. package/dist/cjs/shared/localisedUrls.js +237 -0
  13. package/dist/esm/cli/index.mjs +22 -0
  14. package/dist/esm/runtime/I18nLink.mjs +4 -12
  15. package/dist/esm/runtime/context.mjs +34 -7
  16. package/dist/esm/runtime/hooks.mjs +9 -6
  17. package/dist/esm/runtime/i18n/backend/defaults.mjs +1 -1
  18. package/dist/esm/runtime/i18n/backend/middleware.node.mjs +3 -3
  19. package/dist/esm/runtime/i18n/instance.mjs +8 -2
  20. package/dist/esm/runtime/index.mjs +7 -6
  21. package/dist/esm/runtime/routerAdapter.mjs +130 -0
  22. package/dist/esm/runtime/utils.mjs +11 -30
  23. package/dist/esm/server/index.mjs +57 -7
  24. package/dist/esm/shared/localisedUrls.mjs +191 -0
  25. package/dist/esm-node/cli/index.mjs +22 -0
  26. package/dist/esm-node/runtime/I18nLink.mjs +4 -12
  27. package/dist/esm-node/runtime/context.mjs +34 -7
  28. package/dist/esm-node/runtime/hooks.mjs +9 -6
  29. package/dist/esm-node/runtime/i18n/backend/defaults.mjs +1 -1
  30. package/dist/esm-node/runtime/i18n/backend/middleware.node.mjs +3 -3
  31. package/dist/esm-node/runtime/i18n/instance.mjs +8 -2
  32. package/dist/esm-node/runtime/index.mjs +7 -6
  33. package/dist/esm-node/runtime/routerAdapter.mjs +131 -0
  34. package/dist/esm-node/runtime/utils.mjs +11 -30
  35. package/dist/esm-node/server/index.mjs +57 -7
  36. package/dist/esm-node/shared/localisedUrls.mjs +192 -0
  37. package/dist/types/runtime/I18nLink.d.ts +15 -0
  38. package/dist/types/runtime/context.d.ts +3 -0
  39. package/dist/types/runtime/hooks.d.ts +4 -2
  40. package/dist/types/runtime/i18n/backend/middleware.node.d.ts +1 -1
  41. package/dist/types/runtime/index.d.ts +1 -0
  42. package/dist/types/runtime/routerAdapter.d.ts +26 -0
  43. package/dist/types/runtime/utils.d.ts +2 -7
  44. package/dist/types/server/index.d.ts +6 -0
  45. package/dist/types/shared/localisedUrls.d.ts +13 -0
  46. package/dist/types/shared/type.d.ts +12 -0
  47. package/package.json +15 -15
  48. package/rstest.config.mts +39 -0
  49. package/src/cli/index.ts +43 -1
  50. package/src/runtime/I18nLink.tsx +10 -16
  51. package/src/runtime/context.tsx +45 -7
  52. package/src/runtime/hooks.ts +13 -4
  53. package/src/runtime/i18n/backend/defaults.ts +3 -1
  54. package/src/runtime/i18n/backend/middleware.node.ts +1 -1
  55. package/src/runtime/i18n/instance.ts +1 -2
  56. package/src/runtime/index.tsx +10 -2
  57. package/src/runtime/routerAdapter.tsx +333 -0
  58. package/src/runtime/utils.ts +22 -34
  59. package/src/server/index.ts +135 -10
  60. package/src/shared/localisedUrls.ts +393 -0
  61. package/src/shared/type.ts +12 -0
  62. package/tests/localisedUrls.test.ts +278 -0
  63. package/tests/routerAdapter.test.tsx +278 -0
@@ -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) {
@@ -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;
@@ -1,5 +1,19 @@
1
1
  "use strict";
2
- var __webpack_require__ = {};
2
+ var __webpack_modules__ = {
3
+ "react-i18next" (module) {
4
+ module.exports = require("react-i18next");
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
+ }
3
17
  (()=>{
4
18
  __webpack_require__.d = (exports1, definition)=>{
5
19
  for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
@@ -22,93 +36,94 @@ var __webpack_require__ = {};
22
36
  };
23
37
  })();
24
38
  var __webpack_exports__ = {};
25
- __webpack_require__.r(__webpack_exports__);
26
- __webpack_require__.d(__webpack_exports__, {
27
- getActualI18nextInstance: ()=>getActualI18nextInstance,
28
- getI18nInstance: ()=>getI18nInstance,
29
- getI18nWrapperI18nextInstance: ()=>getI18nWrapperI18nextInstance,
30
- getI18nextInstanceForProvider: ()=>getI18nextInstanceForProvider,
31
- getI18nextProvider: ()=>getI18nextProvider,
32
- getInitReactI18next: ()=>getInitReactI18next,
33
- isI18nInstance: ()=>isI18nInstance,
34
- isI18nWrapperInstance: ()=>isI18nWrapperInstance
35
- });
36
- function isI18nWrapperInstance(obj) {
37
- if (!obj || 'object' != typeof obj) return false;
38
- if (!obj.i18nInstance || 'object' != typeof obj.i18nInstance) return false;
39
- if (!obj.i18nInstance.instance) return false;
40
- if ('function' != typeof obj.init || 'function' != typeof obj.use) return false;
41
- return true;
42
- }
43
- function getI18nWrapperI18nextInstance(wrapperInstance) {
44
- if (isI18nWrapperInstance(wrapperInstance)) return wrapperInstance.i18nInstance?.instance;
45
- return null;
46
- }
47
- function getActualI18nextInstance(instance) {
48
- if (isI18nWrapperInstance(instance)) {
49
- const i18nextInstance = getI18nWrapperI18nextInstance(instance);
50
- return i18nextInstance || instance;
39
+ (()=>{
40
+ __webpack_require__.r(__webpack_exports__);
41
+ __webpack_require__.d(__webpack_exports__, {
42
+ getActualI18nextInstance: ()=>getActualI18nextInstance,
43
+ getI18nInstance: ()=>getI18nInstance,
44
+ getI18nWrapperI18nextInstance: ()=>getI18nWrapperI18nextInstance,
45
+ getI18nextInstanceForProvider: ()=>getI18nextInstanceForProvider,
46
+ getI18nextProvider: ()=>getI18nextProvider,
47
+ getInitReactI18next: ()=>getInitReactI18next,
48
+ isI18nInstance: ()=>isI18nInstance,
49
+ isI18nWrapperInstance: ()=>isI18nWrapperInstance
50
+ });
51
+ function isI18nWrapperInstance(obj) {
52
+ if (!obj || 'object' != typeof obj) return false;
53
+ if (!obj.i18nInstance || 'object' != typeof obj.i18nInstance) return false;
54
+ if (!obj.i18nInstance.instance) return false;
55
+ if ('function' != typeof obj.init || 'function' != typeof obj.use) return false;
56
+ return true;
51
57
  }
52
- return instance;
53
- }
54
- function isI18nInstance(obj) {
55
- if (!obj || 'object' != typeof obj) return false;
56
- if (isI18nWrapperInstance(obj)) return true;
57
- return 'function' == typeof obj.init && 'function' == typeof obj.use;
58
- }
59
- async function tryImportI18next() {
60
- try {
61
- const i18next = await import("i18next");
62
- return i18next.default;
63
- } catch (error) {
58
+ function getI18nWrapperI18nextInstance(wrapperInstance) {
59
+ if (isI18nWrapperInstance(wrapperInstance)) return wrapperInstance.i18nInstance?.instance;
64
60
  return null;
65
61
  }
66
- }
67
- async function createI18nextInstance() {
68
- try {
69
- const i18next = await tryImportI18next();
70
- if (!i18next) return null;
71
- return i18next.createInstance?.({
72
- initImmediate: false
73
- });
74
- } catch (error) {
75
- return null;
62
+ function getActualI18nextInstance(instance) {
63
+ if (isI18nWrapperInstance(instance)) {
64
+ const i18nextInstance = getI18nWrapperI18nextInstance(instance);
65
+ return i18nextInstance || instance;
66
+ }
67
+ return instance;
76
68
  }
77
- }
78
- async function tryImportReactI18next() {
79
- try {
80
- const reactI18next = await import("react-i18next");
81
- return reactI18next;
82
- } catch (error) {
83
- return null;
69
+ function isI18nInstance(obj) {
70
+ if (!obj || 'object' != typeof obj) return false;
71
+ if (isI18nWrapperInstance(obj)) return true;
72
+ return 'function' == typeof obj.init && 'function' == typeof obj.use;
84
73
  }
85
- }
86
- function getI18nextInstanceForProvider(instance) {
87
- if (isI18nWrapperInstance(instance)) {
88
- const i18nextInstance = getI18nWrapperI18nextInstance(instance);
74
+ async function tryImportI18next() {
75
+ try {
76
+ const i18next = await import("i18next");
77
+ return i18next.default;
78
+ } catch (error) {
79
+ return null;
80
+ }
81
+ }
82
+ async function createI18nextInstance() {
83
+ try {
84
+ const i18next = await tryImportI18next();
85
+ if (!i18next) return null;
86
+ return i18next.createInstance?.({
87
+ initImmediate: false
88
+ });
89
+ } catch (error) {
90
+ return null;
91
+ }
92
+ }
93
+ async function tryImportReactI18next() {
94
+ try {
95
+ return __webpack_require__("react-i18next");
96
+ } catch (error) {
97
+ return null;
98
+ }
99
+ }
100
+ function getI18nextInstanceForProvider(instance) {
101
+ if (isI18nWrapperInstance(instance)) {
102
+ const i18nextInstance = getI18nWrapperI18nextInstance(instance);
103
+ if (i18nextInstance) return i18nextInstance;
104
+ }
105
+ return instance;
106
+ }
107
+ async function getI18nInstance(userInstance) {
108
+ if (userInstance) {
109
+ if (isI18nWrapperInstance(userInstance)) return userInstance;
110
+ if (isI18nInstance(userInstance)) return userInstance;
111
+ }
112
+ const i18nextInstance = await createI18nextInstance();
89
113
  if (i18nextInstance) return i18nextInstance;
114
+ throw new Error('No i18n instance found');
90
115
  }
91
- return instance;
92
- }
93
- async function getI18nInstance(userInstance) {
94
- if (userInstance) {
95
- if (isI18nWrapperInstance(userInstance)) return userInstance;
96
- if (isI18nInstance(userInstance)) return userInstance;
116
+ async function getInitReactI18next() {
117
+ const reactI18nextModule = await tryImportReactI18next();
118
+ if (reactI18nextModule) return reactI18nextModule.initReactI18next;
119
+ return null;
97
120
  }
98
- const i18nextInstance = await createI18nextInstance();
99
- if (i18nextInstance) return i18nextInstance;
100
- throw new Error('No i18n instance found');
101
- }
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
- }
121
+ async function getI18nextProvider() {
122
+ const reactI18nextModule = await tryImportReactI18next();
123
+ if (reactI18nextModule) return reactI18nextModule.I18nextProvider;
124
+ return null;
125
+ }
126
+ })();
112
127
  exports.getActualI18nextInstance = __webpack_exports__.getActualI18nextInstance;
113
128
  exports.getI18nInstance = __webpack_exports__.getI18nInstance;
114
129
  exports.getI18nWrapperI18nextInstance = __webpack_exports__.getI18nWrapperI18nextInstance;
@@ -49,8 +49,8 @@ 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;
@@ -59,8 +59,8 @@ const i18nPlugin = (options)=>({
59
59
  const { i18n: otherConfig } = api.getRuntimeConfig();
60
60
  const { initOptions: otherInitOptions } = otherConfig || {};
61
61
  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)();
62
+ const initReactI18next = reactI18next ? await (0, instance_js_namespaceObject.getInitReactI18next)() : null;
63
+ I18nextProvider = reactI18next ? await (0, instance_js_namespaceObject.getI18nextProvider)() : null;
64
64
  if (initReactI18next) i18nInstance.use(initReactI18next);
65
65
  const pathname = (0, external_utils_js_namespaceObject.getPathname)(context);
66
66
  if (i18nextDetector) (0, detection_middleware_js_namespaceObject.useI18nextLanguageDetector)(i18nInstance);
@@ -121,14 +121,15 @@ const i18nPlugin = (options)=>({
121
121
  ]);
122
122
  (0, external_hooks_js_namespaceObject.useSdkResourcesLoader)(i18nInstance, setForceUpdate);
123
123
  (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), [
124
+ (0, external_hooks_js_namespaceObject.useClientSideRedirect)(i18nInstance, localePathRedirect, languages, fallbackLanguage, ignoreRedirectRoutes, localisedUrls);
125
+ const contextValue = (0, external_react_namespaceObject.useMemo)(()=>(0, external_hooks_js_namespaceObject.createContextValue)(lang, i18nInstance, entryName, languages, localePathRedirect, ignoreRedirectRoutes, localisedUrls, setLang), [
126
126
  lang,
127
127
  i18nInstance,
128
128
  entryName,
129
129
  languages,
130
130
  localePathRedirect,
131
131
  ignoreRedirectRoutes,
132
+ localisedUrls,
132
133
  forceUpdate
133
134
  ]);
134
135
  const appContent = /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(jsx_runtime_namespaceObject.Fragment, {
@@ -0,0 +1,164 @@
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 runtime_utils_router_namespaceObject = require("@modern-js/runtime-utils/router");
33
+ const external_react_namespaceObject = require("react");
34
+ const normalizeUrlPart = (value, prefix)=>{
35
+ if ('string' != typeof value || !value) return '';
36
+ return value.startsWith(prefix) ? value : `${prefix}${value}`;
37
+ };
38
+ const normalizeLocation = (location)=>{
39
+ if (!location || 'object' != typeof location) return null;
40
+ const locationValue = location;
41
+ if ('string' != typeof locationValue.pathname) return null;
42
+ return {
43
+ pathname: locationValue.pathname,
44
+ search: normalizeUrlPart('string' == typeof locationValue.search ? locationValue.search : locationValue.searchStr, '?'),
45
+ hash: normalizeUrlPart(locationValue.hash, '#')
46
+ };
47
+ };
48
+ const getWindowLocation = ()=>{
49
+ if (!(0, runtime_namespaceObject.isBrowser)()) return null;
50
+ return {
51
+ pathname: window.location.pathname,
52
+ search: window.location.search,
53
+ hash: window.location.hash
54
+ };
55
+ };
56
+ const getRouterFramework = (runtimeContext, internalContext, inReactRouter)=>{
57
+ const framework = internalContext.routerFramework || internalContext.routerRuntime?.framework || runtimeContext.routerFramework;
58
+ if (framework) return framework;
59
+ if (internalContext.router?.useRouter || runtimeContext.router?.useRouter) return 'tanstack';
60
+ if (internalContext.router?.useLocation || internalContext.router?.useHref || runtimeContext.router?.useLocation || runtimeContext.router?.useHref) return 'react-router';
61
+ if (inReactRouter) return 'react-router';
62
+ };
63
+ const getRouterInstance = (internalContext, contextRouter)=>{
64
+ if (contextRouter) return contextRouter;
65
+ const router = internalContext.routerInstance || internalContext.routerRuntime?.instance;
66
+ if (!router || 'object' != typeof router) return null;
67
+ return router;
68
+ };
69
+ const getRouterStateLocation = (internalContext, contextRouter)=>{
70
+ const router = getRouterInstance(internalContext, contextRouter);
71
+ return normalizeLocation(router?.stores?.location?.get?.()) || normalizeLocation(router?.state?.location);
72
+ };
73
+ const getRouterParams = (internalContext, contextRouter)=>{
74
+ const router = getRouterInstance(internalContext, contextRouter);
75
+ const matches = router?.stores?.matches?.get?.() || router?.state?.matches;
76
+ if (!Array.isArray(matches)) return {};
77
+ return matches.reduce((params, match)=>{
78
+ if (match?.params) Object.assign(params, match.params);
79
+ return params;
80
+ }, {});
81
+ };
82
+ const useI18nRouterAdapter = ()=>{
83
+ const runtimeContext = (0, external_react_namespaceObject.useContext)(runtime_namespaceObject.RuntimeContext);
84
+ const internalContext = (0, external_react_namespaceObject.useContext)(context_namespaceObject.InternalRuntimeContext);
85
+ const inReactRouter = (0, router_namespaceObject.useInRouterContext)();
86
+ const reactRouterNavigate = inReactRouter ? (0, router_namespaceObject.useNavigate)() : null;
87
+ const reactRouterLocation = inReactRouter ? (0, router_namespaceObject.useLocation)() : null;
88
+ const reactRouterParams = inReactRouter ? (0, router_namespaceObject.useParams)() : {};
89
+ const framework = getRouterFramework(runtimeContext, internalContext, inReactRouter);
90
+ const contextUseRouter = inReactRouter || 'tanstack' !== framework ? void 0 : internalContext.router?.useRouter || runtimeContext.router?.useRouter;
91
+ const contextRouter = contextUseRouter ? contextUseRouter({
92
+ warn: false
93
+ }) : null;
94
+ const [, setRouterVersion] = (0, external_react_namespaceObject.useState)(0);
95
+ const hasRouter = 'tanstack' === framework || 'react-router' === framework || Boolean(reactRouterNavigate);
96
+ (0, external_react_namespaceObject.useEffect)(()=>{
97
+ if ('tanstack' !== framework) return;
98
+ const router = getRouterInstance(internalContext, contextRouter);
99
+ if (!router) return;
100
+ const update = ()=>setRouterVersion((version)=>version + 1);
101
+ const unsubscribers = [];
102
+ if ('function' == typeof router.stores?.location?.subscribe) {
103
+ const unsubscribe = router.stores.location.subscribe(update);
104
+ if ('function' == typeof unsubscribe) unsubscribers.push(unsubscribe);
105
+ }
106
+ if ('function' == typeof router.subscribe) for (const eventType of [
107
+ 'onBeforeNavigate',
108
+ 'onBeforeLoad'
109
+ ]){
110
+ const unsubscribe = router.subscribe(eventType, update);
111
+ if ('function' == typeof unsubscribe) unsubscribers.push(unsubscribe);
112
+ }
113
+ return ()=>{
114
+ for (const unsubscribe of unsubscribers)unsubscribe();
115
+ };
116
+ }, [
117
+ contextRouter,
118
+ framework,
119
+ internalContext
120
+ ]);
121
+ const navigate = (0, external_react_namespaceObject.useCallback)((href, options)=>{
122
+ const router = getRouterInstance(internalContext, contextRouter);
123
+ const activeFramework = getRouterFramework(runtimeContext, internalContext, inReactRouter);
124
+ if ('tanstack' === activeFramework) {
125
+ if ('function' == typeof router?.navigate) return router.navigate({
126
+ to: href,
127
+ replace: options?.replace,
128
+ ...options?.state === void 0 ? {} : {
129
+ state: options.state
130
+ }
131
+ });
132
+ throw new Error('TanStack router instance is not available.');
133
+ }
134
+ if (reactRouterNavigate) return reactRouterNavigate(href, options);
135
+ if ('react-router' === activeFramework) {
136
+ if ('function' == typeof router?.navigate) return router.navigate(href, options);
137
+ throw new Error('React Router instance is not available.');
138
+ }
139
+ }, [
140
+ contextRouter,
141
+ internalContext,
142
+ inReactRouter,
143
+ reactRouterNavigate,
144
+ runtimeContext
145
+ ]);
146
+ const location = (reactRouterLocation ? normalizeLocation(reactRouterLocation) : getRouterStateLocation(internalContext, contextRouter)) || getWindowLocation();
147
+ const params = inReactRouter ? reactRouterParams : getRouterParams(internalContext, contextRouter);
148
+ const Link = 'tanstack' === framework ? internalContext.router?.Link || runtimeContext.router?.Link || null : 'react-router' === framework || inReactRouter ? runtime_utils_router_namespaceObject.Link : null;
149
+ return {
150
+ framework,
151
+ hasRouter,
152
+ location,
153
+ navigate: hasRouter ? navigate : null,
154
+ Link,
155
+ params
156
+ };
157
+ };
158
+ exports.useI18nRouterAdapter = __webpack_exports__.useI18nRouterAdapter;
159
+ for(var __rspack_i in __webpack_exports__)if (-1 === [
160
+ "useI18nRouterAdapter"
161
+ ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
162
+ Object.defineProperty(exports, '__esModule', {
163
+ value: true
164
+ });