@bleedingdev/modern-js-plugin-i18n 3.2.0-ultramodern.11 → 3.2.0-ultramodern.110

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 (97) hide show
  1. package/dist/cjs/cli/index.js +32 -5
  2. package/dist/cjs/runtime/I18nLink.js +21 -22
  3. package/dist/cjs/runtime/context.js +41 -10
  4. package/dist/cjs/runtime/hooks.js +17 -10
  5. package/dist/cjs/runtime/i18n/backend/config.js +9 -5
  6. package/dist/cjs/runtime/i18n/backend/defaults.js +15 -10
  7. package/dist/cjs/runtime/i18n/backend/defaults.node.js +16 -11
  8. package/dist/cjs/runtime/i18n/backend/index.js +9 -5
  9. package/dist/cjs/runtime/i18n/backend/middleware.common.js +9 -5
  10. package/dist/cjs/runtime/i18n/backend/middleware.js +9 -5
  11. package/dist/cjs/runtime/i18n/backend/middleware.node.js +13 -9
  12. package/dist/cjs/runtime/i18n/backend/sdk-backend.js +9 -5
  13. package/dist/cjs/runtime/i18n/backend/sdk-event.js +16 -11
  14. package/dist/cjs/runtime/i18n/detection/config.js +9 -5
  15. package/dist/cjs/runtime/i18n/detection/index.js +9 -5
  16. package/dist/cjs/runtime/i18n/detection/middleware.js +9 -5
  17. package/dist/cjs/runtime/i18n/detection/middleware.node.js +9 -5
  18. package/dist/cjs/runtime/i18n/index.js +9 -5
  19. package/dist/cjs/runtime/i18n/instance.js +17 -37
  20. package/dist/cjs/runtime/i18n/react-i18next.js +53 -0
  21. package/dist/cjs/runtime/i18n/utils.js +9 -17
  22. package/dist/cjs/runtime/index.js +27 -15
  23. package/dist/cjs/runtime/routerAdapter.js +167 -0
  24. package/dist/cjs/runtime/utils.js +72 -99
  25. package/dist/cjs/server/index.js +69 -13
  26. package/dist/cjs/shared/deepMerge.js +12 -8
  27. package/dist/cjs/shared/detection.js +9 -5
  28. package/dist/cjs/shared/localisedUrls.js +241 -0
  29. package/dist/cjs/shared/utils.js +15 -11
  30. package/dist/esm/cli/index.mjs +23 -0
  31. package/dist/esm/runtime/I18nLink.mjs +12 -17
  32. package/dist/esm/runtime/context.mjs +34 -7
  33. package/dist/esm/runtime/hooks.mjs +9 -6
  34. package/dist/esm/runtime/i18n/backend/defaults.mjs +1 -1
  35. package/dist/esm/runtime/i18n/backend/defaults.node.mjs +2 -2
  36. package/dist/esm/runtime/i18n/backend/middleware.node.mjs +3 -3
  37. package/dist/esm/runtime/i18n/instance.mjs +1 -19
  38. package/dist/esm/runtime/i18n/react-i18next.mjs +15 -0
  39. package/dist/esm/runtime/i18n/utils.mjs +0 -12
  40. package/dist/esm/runtime/index.mjs +19 -11
  41. package/dist/esm/runtime/routerAdapter.mjs +129 -0
  42. package/dist/esm/runtime/utils.mjs +11 -30
  43. package/dist/esm/server/index.mjs +53 -7
  44. package/dist/esm/shared/localisedUrls.mjs +191 -0
  45. package/dist/esm-node/cli/index.mjs +23 -0
  46. package/dist/esm-node/runtime/I18nLink.mjs +12 -17
  47. package/dist/esm-node/runtime/context.mjs +34 -7
  48. package/dist/esm-node/runtime/hooks.mjs +9 -6
  49. package/dist/esm-node/runtime/i18n/backend/defaults.mjs +1 -1
  50. package/dist/esm-node/runtime/i18n/backend/defaults.node.mjs +2 -2
  51. package/dist/esm-node/runtime/i18n/backend/middleware.node.mjs +3 -3
  52. package/dist/esm-node/runtime/i18n/instance.mjs +1 -19
  53. package/dist/esm-node/runtime/i18n/react-i18next.mjs +16 -0
  54. package/dist/esm-node/runtime/i18n/utils.mjs +0 -12
  55. package/dist/esm-node/runtime/index.mjs +19 -11
  56. package/dist/esm-node/runtime/routerAdapter.mjs +130 -0
  57. package/dist/esm-node/runtime/utils.mjs +11 -30
  58. package/dist/esm-node/server/index.mjs +53 -7
  59. package/dist/esm-node/shared/localisedUrls.mjs +192 -0
  60. package/dist/types/cli/index.d.ts +1 -0
  61. package/dist/types/runtime/I18nLink.d.ts +15 -0
  62. package/dist/types/runtime/context.d.ts +3 -0
  63. package/dist/types/runtime/hooks.d.ts +4 -2
  64. package/dist/types/runtime/i18n/backend/middleware.node.d.ts +1 -1
  65. package/dist/types/runtime/i18n/instance.d.ts +4 -6
  66. package/dist/types/runtime/i18n/react-i18next.d.ts +7 -0
  67. package/dist/types/runtime/index.d.ts +1 -0
  68. package/dist/types/runtime/routerAdapter.d.ts +26 -0
  69. package/dist/types/runtime/types.d.ts +1 -1
  70. package/dist/types/runtime/utils.d.ts +2 -7
  71. package/dist/types/server/index.d.ts +6 -0
  72. package/dist/types/shared/localisedUrls.d.ts +13 -0
  73. package/dist/types/shared/type.d.ts +12 -0
  74. package/package.json +23 -27
  75. package/rstest.config.mts +39 -0
  76. package/src/cli/index.ts +44 -1
  77. package/src/runtime/I18nLink.tsx +13 -17
  78. package/src/runtime/context.tsx +45 -7
  79. package/src/runtime/hooks.ts +13 -4
  80. package/src/runtime/i18n/backend/defaults.node.ts +2 -2
  81. package/src/runtime/i18n/backend/defaults.ts +3 -1
  82. package/src/runtime/i18n/backend/middleware.node.ts +1 -1
  83. package/src/runtime/i18n/instance.ts +3 -30
  84. package/src/runtime/i18n/react-i18next.ts +25 -0
  85. package/src/runtime/i18n/utils.ts +4 -26
  86. package/src/runtime/index.tsx +23 -10
  87. package/src/runtime/routerAdapter.tsx +333 -0
  88. package/src/runtime/types.ts +1 -1
  89. package/src/runtime/utils.ts +22 -34
  90. package/src/server/index.ts +117 -10
  91. package/src/shared/localisedUrls.ts +393 -0
  92. package/src/shared/type.ts +12 -0
  93. package/tests/i18nUtils.test.ts +52 -0
  94. package/tests/localisedUrls.test.ts +312 -0
  95. package/tests/routerAdapter.test.tsx +452 -0
  96. package/dist/esm/rslib-runtime.mjs +0 -18
  97. package/dist/esm-node/rslib-runtime.mjs +0 -19
@@ -1,9 +1,10 @@
1
1
  import "node:module";
2
2
  import { jsx } from "react/jsx-runtime";
3
3
  import { isBrowser } from "@modern-js/runtime";
4
- import { createContext, useCallback, useContext, useMemo } from "react";
4
+ import { createContext, useCallback, useContext, useEffect, useMemo } from "react";
5
5
  import { cacheUserLanguage } from "./i18n/detection/index.mjs";
6
- import { buildLocalizedUrl, detectLanguageFromPath, getEntryPath, shouldIgnoreRedirect, useRouterHooks } from "./utils.mjs";
6
+ import { useI18nRouterAdapter } from "./routerAdapter.mjs";
7
+ import { buildLocalizedUrl, detectLanguageFromPath, getEntryPath, shouldIgnoreRedirect } from "./utils.mjs";
7
8
  const ModernI18nContext = /*#__PURE__*/ createContext(null);
8
9
  const ModernI18nProvider = ({ children, value })=>/*#__PURE__*/ jsx(ModernI18nContext.Provider, {
9
10
  value: value,
@@ -12,9 +13,33 @@ const ModernI18nProvider = ({ children, value })=>/*#__PURE__*/ jsx(ModernI18nCo
12
13
  const useModernI18n = ()=>{
13
14
  const context = useContext(ModernI18nContext);
14
15
  if (!context) throw new Error('useModernI18n must be used within a ModernI18nProvider');
15
- const { language: contextLanguage, i18nInstance, languages, localePathRedirect, ignoreRedirectRoutes, updateLanguage } = context;
16
- const { navigate, location, hasRouter } = useRouterHooks();
17
- const currentLanguage = contextLanguage;
16
+ const { language: contextLanguage, i18nInstance, languages, localePathRedirect, ignoreRedirectRoutes, localisedUrls, updateLanguage } = context;
17
+ const { navigate, location, hasRouter } = useI18nRouterAdapter();
18
+ const pathLanguage = useMemo(()=>{
19
+ if (!localePathRedirect || !location?.pathname) return;
20
+ const detected = detectLanguageFromPath(location.pathname, languages || [], localePathRedirect);
21
+ return detected.detected ? detected.language : void 0;
22
+ }, [
23
+ languages,
24
+ localePathRedirect,
25
+ location?.pathname
26
+ ]);
27
+ const currentLanguage = pathLanguage || contextLanguage;
28
+ useEffect(()=>{
29
+ if (!pathLanguage || pathLanguage === contextLanguage) return;
30
+ updateLanguage?.(pathLanguage);
31
+ i18nInstance?.setLang?.(pathLanguage);
32
+ i18nInstance?.changeLanguage?.(pathLanguage);
33
+ if (isBrowser()) {
34
+ const detectionOptions = i18nInstance.options?.detection;
35
+ cacheUserLanguage(i18nInstance, pathLanguage, detectionOptions);
36
+ }
37
+ }, [
38
+ contextLanguage,
39
+ i18nInstance,
40
+ pathLanguage,
41
+ updateLanguage
42
+ ]);
18
43
  const changeLanguage = useCallback(async (newLang)=>{
19
44
  try {
20
45
  if (!newLang || 'string' != typeof newLang) throw new Error('Language must be a non-empty string');
@@ -31,7 +56,7 @@ const useModernI18n = ()=>{
31
56
  const pathLanguage = detectLanguageFromPath(currentPath, languages || [], localePathRedirect);
32
57
  if (pathLanguage.detected && pathLanguage.language === newLang) return;
33
58
  if (!shouldIgnoreRedirect(relativePath, languages || [], ignoreRedirectRoutes)) {
34
- const newPath = buildLocalizedUrl(relativePath, newLang, languages || []);
59
+ const newPath = buildLocalizedUrl(relativePath, newLang, languages || [], localisedUrls);
35
60
  const newUrl = entryPath + newPath + location.search + location.hash;
36
61
  await navigate(newUrl, {
37
62
  replace: true
@@ -44,7 +69,7 @@ const useModernI18n = ()=>{
44
69
  const pathLanguage = detectLanguageFromPath(currentPath, languages || [], localePathRedirect);
45
70
  if (pathLanguage.detected && pathLanguage.language === newLang) return;
46
71
  if (!shouldIgnoreRedirect(relativePath, languages || [], ignoreRedirectRoutes)) {
47
- const newPath = buildLocalizedUrl(relativePath, newLang, languages || []);
72
+ const newPath = buildLocalizedUrl(relativePath, newLang, languages || [], localisedUrls);
48
73
  const newUrl = entryPath + newPath + window.location.search + window.location.hash;
49
74
  window.history.pushState(null, '', newUrl);
50
75
  }
@@ -59,6 +84,7 @@ const useModernI18n = ()=>{
59
84
  updateLanguage,
60
85
  localePathRedirect,
61
86
  ignoreRedirectRoutes,
87
+ localisedUrls,
62
88
  languages,
63
89
  hasRouter,
64
90
  navigate,
@@ -99,6 +125,7 @@ const useModernI18n = ()=>{
99
125
  changeLanguage,
100
126
  i18nInstance,
101
127
  supportedLanguages: languages || [],
128
+ localisedUrls,
102
129
  isLanguageSupported,
103
130
  isResourcesReady
104
131
  };
@@ -3,7 +3,8 @@ import { isBrowser } from "@modern-js/runtime";
3
3
  import { useEffect, useRef } from "react";
4
4
  import { I18N_SDK_RESOURCES_LOADED_EVENT, getI18nSdkBackendId } from "./i18n/backend/sdk-event.mjs";
5
5
  import { cacheUserLanguage } from "./i18n/detection/index.mjs";
6
- import { buildLocalizedUrl, detectLanguageFromPath, getEntryPath, getPathname, shouldIgnoreRedirect, useRouterHooks } from "./utils.mjs";
6
+ import { useI18nRouterAdapter } from "./routerAdapter.mjs";
7
+ import { buildLocalizedUrl, detectLanguageFromPath, getEntryPath, getPathname, shouldIgnoreRedirect } from "./utils.mjs";
7
8
  function createMinimalI18nInstance(language) {
8
9
  const minimalInstance = {
9
10
  language,
@@ -15,7 +16,7 @@ function createMinimalI18nInstance(language) {
15
16
  };
16
17
  return minimalInstance;
17
18
  }
18
- function createContextValue(lang, i18nInstance, entryName, languages, localePathRedirect, ignoreRedirectRoutes, setLang) {
19
+ function createContextValue(lang, i18nInstance, entryName, languages, localePathRedirect, ignoreRedirectRoutes, localisedUrls, setLang) {
19
20
  const instance = i18nInstance || createMinimalI18nInstance(lang);
20
21
  return {
21
22
  language: lang,
@@ -24,6 +25,7 @@ function createContextValue(lang, i18nInstance, entryName, languages, localePath
24
25
  languages,
25
26
  localePathRedirect,
26
27
  ignoreRedirectRoutes,
28
+ localisedUrls,
27
29
  updateLanguage: setLang
28
30
  };
29
31
  }
@@ -73,9 +75,9 @@ function useSdkResourcesLoader(i18nInstance, setForceUpdate) {
73
75
  setForceUpdate
74
76
  ]);
75
77
  }
76
- function useClientSideRedirect(i18nInstance, localePathRedirect, languages, fallbackLanguage, ignoreRedirectRoutes) {
78
+ function useClientSideRedirect(i18nInstance, localePathRedirect, languages, fallbackLanguage, ignoreRedirectRoutes, localisedUrls) {
77
79
  const hasRedirectedRef = useRef(false);
78
- const { navigate, location, hasRouter } = useRouterHooks();
80
+ const { navigate, location, hasRouter } = useI18nRouterAdapter();
79
81
  useEffect(()=>{
80
82
  if ('browser' !== process.env.MODERN_TARGET) return;
81
83
  if (!localePathRedirect || !i18nInstance) return;
@@ -94,7 +96,7 @@ function useClientSideRedirect(i18nInstance, localePathRedirect, languages, fall
94
96
  const pathDetection = detectLanguageFromPath(currentPathname, languages, localePathRedirect);
95
97
  if (pathDetection.detected) return;
96
98
  const targetLanguage = i18nInstance.language || fallbackLanguage || languages[0] || 'en';
97
- const newPath = buildLocalizedUrl(relativePath, targetLanguage, languages);
99
+ const newPath = buildLocalizedUrl(relativePath, targetLanguage, languages, localisedUrls);
98
100
  const newUrl = entryPath + newPath + currentSearch + currentHash;
99
101
  if (newUrl !== currentPathname + currentSearch + currentHash) {
100
102
  hasRedirectedRef.current = true;
@@ -111,7 +113,8 @@ function useClientSideRedirect(i18nInstance, localePathRedirect, languages, fall
111
113
  i18nInstance,
112
114
  languages,
113
115
  fallbackLanguage,
114
- ignoreRedirectRoutes
116
+ ignoreRedirectRoutes,
117
+ localisedUrls
115
118
  ]);
116
119
  }
117
120
  function useLanguageSync(i18nInstance, localePathRedirect, languages, runtimeContextRef, prevLangRef, setLang) {
@@ -5,7 +5,7 @@ const DEFAULT_I18NEXT_BACKEND_OPTIONS = {
5
5
  };
6
6
  function convertPath(path) {
7
7
  if (!path) return path;
8
- if (path.startsWith('/')) return `${window.__assetPrefix__ || ''}${path}`;
8
+ if (path.startsWith('/')) return "u" < typeof window ? path : `${window.__assetPrefix__ || ''}${path}`;
9
9
  return path;
10
10
  }
11
11
  function convertBackendOptions(options) {
@@ -1,7 +1,7 @@
1
1
  import "node:module";
2
2
  const DEFAULT_I18NEXT_BACKEND_OPTIONS = {
3
- loadPath: './locales/{{lng}}/{{ns}}.json',
4
- addPath: './locales/{{lng}}/{{ns}}.json'
3
+ loadPath: './config/public/locales/{{lng}}/{{ns}}.json',
4
+ addPath: './config/public/locales/{{lng}}/{{ns}}.json'
5
5
  };
6
6
  function convertPath(path) {
7
7
  if (!path) return path;
@@ -1,9 +1,9 @@
1
1
  import "node:module";
2
- import i18next_fs_backend from "i18next-fs-backend";
2
+ import cjs from "i18next-fs-backend/cjs";
3
3
  import { useI18nextBackendCommon } from "./middleware.common.mjs";
4
- class FsBackendWithSave extends i18next_fs_backend {
4
+ class FsBackendWithSave extends cjs {
5
5
  save(_language, _namespace, _data) {}
6
6
  }
7
7
  const HttpBackendWithSave = FsBackendWithSave;
8
- const useI18nextBackend = (i18nInstance, backend)=>useI18nextBackendCommon(i18nInstance, FsBackendWithSave, i18next_fs_backend, backend);
8
+ const useI18nextBackend = (i18nInstance, backend)=>useI18nextBackendCommon(i18nInstance, FsBackendWithSave, cjs, backend);
9
9
  export { FsBackendWithSave, HttpBackendWithSave, useI18nextBackend };
@@ -41,14 +41,6 @@ async function createI18nextInstance() {
41
41
  return null;
42
42
  }
43
43
  }
44
- async function tryImportReactI18next() {
45
- try {
46
- const reactI18next = await import("react-i18next");
47
- return reactI18next;
48
- } catch (error) {
49
- return null;
50
- }
51
- }
52
44
  function getI18nextInstanceForProvider(instance) {
53
45
  if (isI18nWrapperInstance(instance)) {
54
46
  const i18nextInstance = getI18nWrapperI18nextInstance(instance);
@@ -65,14 +57,4 @@ async function getI18nInstance(userInstance) {
65
57
  if (i18nextInstance) return i18nextInstance;
66
58
  throw new Error('No i18n instance found');
67
59
  }
68
- async function getInitReactI18next() {
69
- const reactI18nextModule = await tryImportReactI18next();
70
- if (reactI18nextModule) return reactI18nextModule.initReactI18next;
71
- return null;
72
- }
73
- async function getI18nextProvider() {
74
- const reactI18nextModule = await tryImportReactI18next();
75
- if (reactI18nextModule) return reactI18nextModule.I18nextProvider;
76
- return null;
77
- }
78
- export { getActualI18nextInstance, getI18nInstance, getI18nWrapperI18nextInstance, getI18nextInstanceForProvider, getI18nextProvider, getInitReactI18next, isI18nInstance, isI18nWrapperInstance };
60
+ export { getActualI18nextInstance, getI18nInstance, getI18nWrapperI18nextInstance, getI18nextInstanceForProvider, isI18nInstance, isI18nWrapperInstance };
@@ -0,0 +1,16 @@
1
+ import "node:module";
2
+ async function tryImportReactI18next() {
3
+ try {
4
+ return await import("react-i18next");
5
+ } catch (error) {
6
+ return null;
7
+ }
8
+ }
9
+ async function getReactI18nextIntegration() {
10
+ const reactI18nextModule = await tryImportReactI18next();
11
+ return {
12
+ I18nextProvider: reactI18nextModule?.I18nextProvider ?? null,
13
+ initReactI18next: reactI18nextModule?.initReactI18next ?? null
14
+ };
15
+ }
16
+ export { getReactI18nextIntegration };
@@ -101,18 +101,6 @@ const initializeI18nInstance = async (i18nInstance, finalLanguage, fallbackLangu
101
101
  };
102
102
  }
103
103
  }
104
- if (mergedBackend && hasOptions(i18nInstance)) {
105
- const defaultNS = initOptions.defaultNS || initOptions.ns || 'translation';
106
- const ns = Array.isArray(defaultNS) ? defaultNS[0] : defaultNS;
107
- let retries = 20;
108
- while(retries > 0){
109
- const actualInstance = getActualI18nextInstance(i18nInstance);
110
- const store = actualInstance.store;
111
- if (store?.data?.[finalLanguage]?.[ns]) break;
112
- await new Promise((resolve)=>setTimeout(resolve, 100));
113
- retries--;
114
- }
115
- }
116
104
  }
117
105
  };
118
106
  function hasOptions(instance) {
@@ -11,26 +11,31 @@ import { mergeBackendOptions } from "./i18n/backend/index.mjs";
11
11
  import { useI18nextBackend } from "./i18n/backend/middleware.mjs";
12
12
  import { detectLanguageWithPriority, exportServerLngToWindow, mergeDetectionOptions } from "./i18n/detection/index.mjs";
13
13
  import { useI18nextLanguageDetector } from "./i18n/detection/middleware.mjs";
14
- import { getI18nextInstanceForProvider, getI18nextProvider, getInitReactI18next } from "./i18n/instance.mjs";
14
+ import { getI18nextInstanceForProvider } from "./i18n/instance.mjs";
15
15
  import { changeI18nLanguage, ensureLanguageMatch, initializeI18nInstance, setupClonedInstance } from "./i18n/utils.mjs";
16
16
  import { getPathname } from "./utils.mjs";
17
17
  import "./types.mjs";
18
18
  const i18nPlugin = (options)=>({
19
19
  name: '@modern-js/plugin-i18n',
20
20
  setup: (api)=>{
21
- const { entryName, i18nInstance: userI18nInstance, initOptions, localeDetection, backend, htmlLangAttr = false } = options;
22
- const { localePathRedirect = false, i18nextDetector = true, languages = [], fallbackLanguage = 'en', detection, ignoreRedirectRoutes } = localeDetection || {};
21
+ const { entryName, i18nInstance: userI18nInstance, initOptions, localeDetection, backend, htmlLangAttr = false, reactI18next = true } = options;
22
+ const { localePathRedirect = false, i18nextDetector = true, languages = [], fallbackLanguage = 'en', detection, ignoreRedirectRoutes, localisedUrls } = localeDetection || {};
23
23
  const { enabled: backendEnabled = false } = backend || {};
24
24
  let latestI18nInstance;
25
25
  let I18nextProvider;
26
+ const loadReactI18nextIntegration = async ()=>{
27
+ if (!reactI18next) return null;
28
+ const { getReactI18nextIntegration } = await import("./i18n/react-i18next.mjs");
29
+ return getReactI18nextIntegration();
30
+ };
26
31
  api.onBeforeRender(async (context)=>{
27
32
  let i18nInstance = await getI18nInstance(userI18nInstance);
28
33
  const { i18n: otherConfig } = api.getRuntimeConfig();
29
34
  const { initOptions: otherInitOptions } = otherConfig || {};
30
35
  const userInitOptions = merge(otherInitOptions || {}, initOptions || {});
31
- const initReactI18next = await getInitReactI18next();
32
- I18nextProvider = await getI18nextProvider();
33
- if (initReactI18next) i18nInstance.use(initReactI18next);
36
+ const reactI18nextIntegration = await loadReactI18nextIntegration();
37
+ I18nextProvider = reactI18nextIntegration?.I18nextProvider ?? null;
38
+ if (reactI18nextIntegration?.initReactI18next) i18nInstance.use(reactI18nextIntegration.initReactI18next);
34
39
  const pathname = getPathname(context);
35
40
  if (i18nextDetector) useI18nextLanguageDetector(i18nInstance);
36
41
  const mergedDetection = mergeDetectionOptions(i18nextDetector, detection, localePathRedirect, userInitOptions);
@@ -90,16 +95,18 @@ const i18nPlugin = (options)=>({
90
95
  ]);
91
96
  useSdkResourcesLoader(i18nInstance, setForceUpdate);
92
97
  useLanguageSync(i18nInstance, localePathRedirect, languages, runtimeContextRef, prevLangRef, setLang);
93
- useClientSideRedirect(i18nInstance, localePathRedirect, languages, fallbackLanguage, ignoreRedirectRoutes);
94
- const contextValue = useMemo(()=>createContextValue(lang, i18nInstance, entryName, languages, localePathRedirect, ignoreRedirectRoutes, setLang), [
98
+ useClientSideRedirect(i18nInstance, localePathRedirect, languages, fallbackLanguage, ignoreRedirectRoutes, localisedUrls);
99
+ const contextValue = useMemo(()=>createContextValue(lang, i18nInstance, entryName, languages, localePathRedirect, ignoreRedirectRoutes, localisedUrls, setLang), [
95
100
  lang,
96
101
  i18nInstance,
97
102
  entryName,
98
103
  languages,
99
104
  localePathRedirect,
100
105
  ignoreRedirectRoutes,
106
+ localisedUrls,
101
107
  forceUpdate
102
108
  ]);
109
+ const children = props.children;
103
110
  const appContent = /*#__PURE__*/ jsxs(Fragment, {
104
111
  children: [
105
112
  Boolean(htmlLangAttr) && /*#__PURE__*/ jsx(Helmet, {
@@ -109,9 +116,10 @@ const i18nPlugin = (options)=>({
109
116
  }),
110
117
  /*#__PURE__*/ jsx(ModernI18nProvider, {
111
118
  value: contextValue,
112
- children: /*#__PURE__*/ jsx(App, {
113
- ...props
114
- })
119
+ children: App ? /*#__PURE__*/ jsx(App, {
120
+ ...props,
121
+ children: children
122
+ }) : children
115
123
  })
116
124
  ]
117
125
  });
@@ -0,0 +1,130 @@
1
+ import "node:module";
2
+ import { RuntimeContext, isBrowser } from "@modern-js/runtime";
3
+ import { InternalRuntimeContext } from "@modern-js/runtime/context";
4
+ import { Link as router_Link, useInRouterContext, useLocation, useNavigate, useParams } from "@modern-js/runtime/router";
5
+ import { useCallback, useContext, useEffect, useState } from "react";
6
+ const normalizeUrlPart = (value, prefix)=>{
7
+ if ('string' != typeof value || !value) return '';
8
+ return value.startsWith(prefix) ? value : `${prefix}${value}`;
9
+ };
10
+ const normalizeLocation = (location)=>{
11
+ if (!location || 'object' != typeof location) return null;
12
+ const locationValue = location;
13
+ if ('string' != typeof locationValue.pathname) return null;
14
+ return {
15
+ pathname: locationValue.pathname,
16
+ search: normalizeUrlPart('string' == typeof locationValue.search ? locationValue.search : locationValue.searchStr, '?'),
17
+ hash: normalizeUrlPart(locationValue.hash, '#')
18
+ };
19
+ };
20
+ const getWindowLocation = ()=>{
21
+ if (!isBrowser()) return null;
22
+ return {
23
+ pathname: window.location.pathname,
24
+ search: window.location.search,
25
+ hash: window.location.hash
26
+ };
27
+ };
28
+ const getRouterFramework = (runtimeContext, internalContext, inReactRouter)=>{
29
+ const framework = internalContext.routerFramework || internalContext.routerRuntime?.framework || runtimeContext.routerFramework;
30
+ if (framework) return framework;
31
+ if (internalContext.router?.useRouter || runtimeContext.router?.useRouter) return 'tanstack';
32
+ if (internalContext.router?.useLocation || internalContext.router?.useHref || runtimeContext.router?.useLocation || runtimeContext.router?.useHref) return 'react-router';
33
+ if (inReactRouter) return 'react-router';
34
+ };
35
+ const getRouterInstance = (internalContext, contextRouter)=>{
36
+ if (contextRouter) return contextRouter;
37
+ const router = internalContext.routerInstance || internalContext.routerRuntime?.instance;
38
+ if (!router || 'object' != typeof router) return null;
39
+ return router;
40
+ };
41
+ const getRouterStateLocation = (internalContext, contextRouter)=>{
42
+ const router = getRouterInstance(internalContext, contextRouter);
43
+ return normalizeLocation(router?.stores?.location?.get?.()) || normalizeLocation(router?.state?.location);
44
+ };
45
+ const getRouterParams = (internalContext, contextRouter)=>{
46
+ const router = getRouterInstance(internalContext, contextRouter);
47
+ const matches = router?.stores?.matches?.get?.() || router?.state?.matches;
48
+ if (!Array.isArray(matches)) return {};
49
+ return matches.reduce((params, match)=>{
50
+ if (match?.params) Object.assign(params, match.params);
51
+ return params;
52
+ }, {});
53
+ };
54
+ const useI18nRouterAdapter = ()=>{
55
+ const runtimeContext = useContext(RuntimeContext);
56
+ const internalContext = useContext(InternalRuntimeContext);
57
+ const inReactRouter = useInRouterContext();
58
+ const reactRouterNavigate = inReactRouter ? useNavigate() : null;
59
+ const reactRouterLocation = inReactRouter ? useLocation() : null;
60
+ const reactRouterParams = inReactRouter ? useParams() : {};
61
+ const framework = getRouterFramework(runtimeContext, internalContext, inReactRouter);
62
+ const contextUseRouter = inReactRouter || 'tanstack' !== framework ? void 0 : internalContext.router?.useRouter || runtimeContext.router?.useRouter;
63
+ const contextRouter = contextUseRouter ? contextUseRouter({
64
+ warn: false
65
+ }) : null;
66
+ const [, setRouterVersion] = useState(0);
67
+ const hasRouter = 'tanstack' === framework || 'react-router' === framework || Boolean(reactRouterNavigate);
68
+ useEffect(()=>{
69
+ if ('tanstack' !== framework) return;
70
+ const router = getRouterInstance(internalContext, contextRouter);
71
+ if (!router) return;
72
+ const update = ()=>setRouterVersion((version)=>version + 1);
73
+ const unsubscribers = [];
74
+ if ('function' == typeof router.stores?.location?.subscribe) {
75
+ const unsubscribe = router.stores.location.subscribe(update);
76
+ if ('function' == typeof unsubscribe) unsubscribers.push(unsubscribe);
77
+ }
78
+ if ('function' == typeof router.subscribe) for (const eventType of [
79
+ 'onBeforeNavigate',
80
+ 'onBeforeLoad'
81
+ ]){
82
+ const unsubscribe = router.subscribe(eventType, update);
83
+ if ('function' == typeof unsubscribe) unsubscribers.push(unsubscribe);
84
+ }
85
+ return ()=>{
86
+ for (const unsubscribe of unsubscribers)unsubscribe();
87
+ };
88
+ }, [
89
+ contextRouter,
90
+ framework,
91
+ internalContext
92
+ ]);
93
+ const navigate = useCallback((href, options)=>{
94
+ const router = getRouterInstance(internalContext, contextRouter);
95
+ const activeFramework = getRouterFramework(runtimeContext, internalContext, inReactRouter);
96
+ if ('tanstack' === activeFramework) {
97
+ if ('function' == typeof router?.navigate) return router.navigate({
98
+ to: href,
99
+ replace: options?.replace,
100
+ ...options?.state === void 0 ? {} : {
101
+ state: options.state
102
+ }
103
+ });
104
+ throw new Error('TanStack router instance is not available.');
105
+ }
106
+ if (reactRouterNavigate) return reactRouterNavigate(href, options);
107
+ if ('react-router' === activeFramework) {
108
+ if ('function' == typeof router?.navigate) return router.navigate(href, options);
109
+ throw new Error('React Router instance is not available.');
110
+ }
111
+ }, [
112
+ contextRouter,
113
+ internalContext,
114
+ inReactRouter,
115
+ reactRouterNavigate,
116
+ runtimeContext
117
+ ]);
118
+ const location = (reactRouterLocation ? normalizeLocation(reactRouterLocation) : getRouterStateLocation(internalContext, contextRouter)) || getWindowLocation();
119
+ const params = inReactRouter ? reactRouterParams : getRouterParams(internalContext, contextRouter);
120
+ const Link = 'tanstack' === framework ? internalContext.router?.Link || runtimeContext.router?.Link || null : 'react-router' === framework || inReactRouter ? router_Link : null;
121
+ return {
122
+ framework,
123
+ hasRouter,
124
+ location,
125
+ navigate: hasRouter ? navigate : null,
126
+ Link,
127
+ params
128
+ };
129
+ };
130
+ export { useI18nRouterAdapter };
@@ -1,13 +1,7 @@
1
1
  import "node:module";
2
2
  import { isBrowser } from "@modern-js/runtime";
3
3
  import { getGlobalBasename } from "@modern-js/runtime/context";
4
- import { __webpack_require__ } from "../rslib-runtime.mjs";
5
- import * as __rspack_external__modern_js_runtime_router_2dfd0c78 from "@modern-js/runtime/router";
6
- __webpack_require__.add({
7
- "@modern-js/runtime/router?f1fa" (module) {
8
- module.exports = __rspack_external__modern_js_runtime_router_2dfd0c78;
9
- }
10
- });
4
+ import { resolveLocalisedPath, resolveLocalisedUrlsConfig } from "../shared/localisedUrls.mjs";
11
5
  const getPathname = (context)=>{
12
6
  if (isBrowser()) return window.location.pathname;
13
7
  return context.ssrContext?.request?.pathname || '/';
@@ -23,11 +17,16 @@ const getLanguageFromPath = (pathname, languages, fallbackLanguage)=>{
23
17
  if (languages.includes(firstSegment)) return firstSegment;
24
18
  return fallbackLanguage;
25
19
  };
26
- const buildLocalizedUrl = (pathname, language, languages)=>{
20
+ const buildLocalizedUrl = (pathname, language, languages, localisedUrls)=>{
27
21
  const segments = pathname.split('/').filter(Boolean);
28
- if (segments.length > 0 && languages.includes(segments[0])) segments[0] = language;
29
- else segments.unshift(language);
30
- return `/${segments.join('/')}`;
22
+ const localisedUrlsConfig = resolveLocalisedUrlsConfig(localisedUrls);
23
+ const pathWithoutLanguage = segments.length > 0 && languages.includes(segments[0]) ? `/${segments.slice(1).join('/')}` : pathname;
24
+ const resolvedPath = localisedUrlsConfig.enabled ? resolveLocalisedPath(pathWithoutLanguage, language, languages, localisedUrlsConfig.map) : pathWithoutLanguage;
25
+ const resolvedSegments = resolvedPath.split('/').filter(Boolean);
26
+ return `/${[
27
+ language,
28
+ ...resolvedSegments
29
+ ].join('/')}`;
31
30
  };
32
31
  const detectLanguageFromPath = (pathname, languages, localePathRedirect)=>{
33
32
  if (!localePathRedirect) return {
@@ -55,22 +54,4 @@ const shouldIgnoreRedirect = (pathname, languages, ignoreRedirectRoutes)=>{
55
54
  if ('function' == typeof ignoreRedirectRoutes) return ignoreRedirectRoutes(normalizedPath);
56
55
  return ignoreRedirectRoutes.some((pattern)=>normalizedPath === pattern || normalizedPath.startsWith(`${pattern}/`));
57
56
  };
58
- const useRouterHooks = ()=>{
59
- try {
60
- const { useLocation, useNavigate, useParams } = __webpack_require__("@modern-js/runtime/router?f1fa");
61
- return {
62
- navigate: useNavigate(),
63
- location: useLocation(),
64
- params: useParams(),
65
- hasRouter: true
66
- };
67
- } catch (error) {
68
- return {
69
- navigate: null,
70
- location: null,
71
- params: {},
72
- hasRouter: false
73
- };
74
- }
75
- };
76
- export { buildLocalizedUrl, detectLanguageFromPath, getEntryPath, getLanguageFromPath, getPathname, shouldIgnoreRedirect, useRouterHooks };
57
+ export { buildLocalizedUrl, detectLanguageFromPath, getEntryPath, getLanguageFromPath, getPathname, shouldIgnoreRedirect };
@@ -1,8 +1,39 @@
1
1
  import "node:module";
2
2
  import { DEFAULT_I18NEXT_DETECTION_OPTIONS, mergeDetectionOptions } from "../runtime/i18n/detection/config.mjs";
3
+ import { resolveLocalisedPath, resolveLocalisedUrlsConfig } from "../shared/localisedUrls.mjs";
3
4
  import { getLocaleDetectionOptions } from "../shared/utils.mjs";
4
5
  import * as __rspack_external__modern_js_server_core_hono_a76ca254 from "@modern-js/server-core/hono";
5
6
  const { languageDetector: languageDetector } = __rspack_external__modern_js_server_core_hono_a76ca254;
7
+ const normalizeApiPrefix = (prefix)=>{
8
+ const trimmedPrefix = prefix.trim();
9
+ if (!trimmedPrefix) return null;
10
+ const prefixedPath = trimmedPrefix.startsWith('/') ? trimmedPrefix : `/${trimmedPrefix}`;
11
+ const withoutWildcard = prefixedPath.replace(/\/\*$/, '');
12
+ const normalizedPrefix = withoutWildcard.length > 1 ? withoutWildcard.replace(/\/+$/, '') : withoutWildcard;
13
+ return '/' === normalizedPrefix ? null : normalizedPrefix;
14
+ };
15
+ const collectApiPrefixes = (routes, bffPrefix)=>{
16
+ const prefixes = new Set();
17
+ for (const route of routes){
18
+ if (!route.isApi || !route.urlPath) continue;
19
+ const normalizedPrefix = normalizeApiPrefix(route.urlPath);
20
+ if (normalizedPrefix) prefixes.add(normalizedPrefix);
21
+ }
22
+ const bffPrefixes = Array.isArray(bffPrefix) ? bffPrefix : bffPrefix ? [
23
+ bffPrefix
24
+ ] : [];
25
+ for (const prefix of bffPrefixes){
26
+ const normalizedPrefix = normalizeApiPrefix(prefix);
27
+ if (normalizedPrefix) prefixes.add(normalizedPrefix);
28
+ }
29
+ return [
30
+ ...prefixes
31
+ ];
32
+ };
33
+ const matchesApiPrefix = (pathname, apiPrefixes)=>{
34
+ const normalizedPathname = pathname.startsWith('/') ? pathname : `/${pathname}`;
35
+ return apiPrefixes.some((prefix)=>normalizedPathname === prefix || normalizedPathname.startsWith(`${prefix}/`));
36
+ };
6
37
  const convertToHonoLanguageDetectorOptions = (languages, fallbackLanguage, detectionOptions)=>{
7
38
  const mergedDetection = detectionOptions ? mergeDetectionOptions(detectionOptions) : DEFAULT_I18NEXT_DETECTION_OPTIONS;
8
39
  const order = (mergedDetection.order || []).filter((item)=>![
@@ -92,15 +123,20 @@ const getLanguageFromPath = (req, urlPath, languages)=>{
92
123
  if (languages.includes(firstSegment)) return firstSegment;
93
124
  return null;
94
125
  };
95
- const buildLocalizedUrl = (req, urlPath, language, languages)=>{
126
+ const buildLocalizedUrl = (req, urlPath, language, languages, localisedUrls)=>{
96
127
  const url = new URL(req.url);
97
128
  const pathname = url.pathname;
98
129
  const basePath = urlPath.replace('/*', '');
99
130
  const remainingPath = pathname.startsWith(basePath) ? pathname.slice(basePath.length) : pathname;
100
131
  const segments = remainingPath.split('/').filter(Boolean);
101
- if (segments.length > 0 && languages.includes(segments[0])) segments[0] = language;
102
- else segments.unshift(language);
103
- const newPathname = `/${segments.join('/')}`;
132
+ const localisedUrlsConfig = resolveLocalisedUrlsConfig(localisedUrls);
133
+ const pathWithoutLanguage = segments.length > 0 && languages.includes(segments[0]) ? `/${segments.slice(1).join('/')}` : remainingPath;
134
+ const resolvedPath = localisedUrlsConfig.enabled ? resolveLocalisedPath(pathWithoutLanguage, language, languages, localisedUrlsConfig.map) : pathWithoutLanguage;
135
+ const resolvedSegments = resolvedPath.split('/').filter(Boolean);
136
+ const newPathname = `/${[
137
+ language,
138
+ ...resolvedSegments
139
+ ].join('/')}`;
104
140
  const suffix = `${url.search}${url.hash}`;
105
141
  const localizedUrl = '/' === basePath ? newPathname + suffix : basePath + newPathname + suffix;
106
142
  return localizedUrl;
@@ -110,6 +146,9 @@ const i18nServerPlugin = (options)=>({
110
146
  setup: (api)=>{
111
147
  api.onPrepare(()=>{
112
148
  const { middlewares, routes } = api.getServerContext();
149
+ const serverConfig = api.getServerConfig();
150
+ const bffPrefix = serverConfig?.bff ? serverConfig.bff.prefix ?? '/api' : void 0;
151
+ const apiPrefixes = collectApiPrefixes(routes, bffPrefix);
113
152
  const entryPaths = new Set();
114
153
  routes.forEach((route)=>{
115
154
  if (route.entryName && route.urlPath && '/' !== route.urlPath) {
@@ -121,7 +160,7 @@ const i18nServerPlugin = (options)=>({
121
160
  const { entryName } = route;
122
161
  if (!entryName) return;
123
162
  if (!options.localeDetection) return;
124
- const { localePathRedirect, i18nextDetector = true, languages = [], fallbackLanguage = 'en', detection, ignoreRedirectRoutes } = getLocaleDetectionOptions(entryName, options.localeDetection);
163
+ const { localePathRedirect, i18nextDetector = true, languages = [], fallbackLanguage = 'en', detection, ignoreRedirectRoutes, localisedUrls } = getLocaleDetectionOptions(entryName, options.localeDetection);
125
164
  const staticRoutePrefixes = options.staticRoutePrefixes;
126
165
  const originUrlPath = route.urlPath;
127
166
  const urlPath = originUrlPath.endsWith('/') ? `${originUrlPath}*` : `${originUrlPath}/*`;
@@ -135,6 +174,7 @@ const i18nServerPlugin = (options)=>({
135
174
  handler: async (c, next)=>{
136
175
  const url = new URL(c.req.url);
137
176
  const pathname = url.pathname;
177
+ if (matchesApiPrefix(pathname, apiPrefixes)) return await next();
138
178
  if (isStaticResourceRequest(pathname, staticRoutePrefixes, languages)) return await next();
139
179
  if ('/' === originUrlPath) {
140
180
  const pathSegments = pathname.split('/').filter(Boolean);
@@ -153,6 +193,7 @@ const i18nServerPlugin = (options)=>({
153
193
  handler: async (c, next)=>{
154
194
  const url = new URL(c.req.url);
155
195
  const pathname = url.pathname;
196
+ if (matchesApiPrefix(pathname, apiPrefixes)) return await next();
156
197
  if (isStaticResourceRequest(pathname, staticRoutePrefixes, languages)) return await next();
157
198
  if (shouldIgnoreRedirect(pathname, urlPath, ignoreRedirectRoutes)) return await next();
158
199
  if ('/' === originUrlPath) {
@@ -167,9 +208,14 @@ const i18nServerPlugin = (options)=>({
167
208
  let detectedLanguage = null;
168
209
  if (i18nextDetector) detectedLanguage = c.get('language') || null;
169
210
  const targetLanguage = detectedLanguage || fallbackLanguage;
170
- const localizedUrl = buildLocalizedUrl(c.req, originUrlPath, targetLanguage, languages);
211
+ const localizedUrl = buildLocalizedUrl(c.req, originUrlPath, targetLanguage, languages, localisedUrls);
171
212
  return c.redirect(localizedUrl);
172
213
  }
214
+ const localisedUrlsConfig = resolveLocalisedUrlsConfig(localisedUrls);
215
+ if (localisedUrlsConfig.enabled) {
216
+ const expectedUrl = buildLocalizedUrl(c.req, originUrlPath, language, languages, localisedUrls);
217
+ if (expectedUrl !== `${pathname}${url.search}${url.hash}`) return c.redirect(expectedUrl);
218
+ }
173
219
  await next();
174
220
  }
175
221
  });
@@ -180,4 +226,4 @@ const i18nServerPlugin = (options)=>({
180
226
  });
181
227
  const server = i18nServerPlugin;
182
228
  export default server;
183
- export { i18nServerPlugin };
229
+ export { collectApiPrefixes, i18nServerPlugin, matchesApiPrefix };