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