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

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
@@ -0,0 +1,192 @@
1
+ import "node:module";
2
+ const LOCALE_PARAM_NAMES = new Set([
3
+ 'lang',
4
+ 'locale',
5
+ 'language'
6
+ ]);
7
+ const normalisePathPattern = (path)=>{
8
+ const withoutDuplicateSlashes = path.replace(/\/+/g, '/');
9
+ const withLeadingSlash = withoutDuplicateSlashes.startsWith('/') ? withoutDuplicateSlashes : `/${withoutDuplicateSlashes}`;
10
+ const withoutTrailingSlash = withLeadingSlash.length > 1 ? withLeadingSlash.replace(/\/+$/, '') : withLeadingSlash;
11
+ return withoutTrailingSlash.replace(/\[(.+?)\]/g, ':$1');
12
+ };
13
+ const normaliseRoutePath = (path)=>{
14
+ const normalized = normalisePathPattern(path);
15
+ return '/' === normalized ? '' : normalized.slice(1);
16
+ };
17
+ const getLocaleParamSegment = (segment)=>{
18
+ if (!segment.startsWith(':')) return null;
19
+ const paramName = segment.slice(1).replace(/\?$/, '');
20
+ return LOCALE_PARAM_NAMES.has(paramName) ? segment : null;
21
+ };
22
+ const splitPathSegments = (path)=>{
23
+ if (!path) return [];
24
+ return normalisePathPattern(path).split('/').filter(Boolean);
25
+ };
26
+ const stripLeadingLocaleParam = (path)=>{
27
+ const segments = splitPathSegments(path);
28
+ const leadingLocaleParam = getLocaleParamSegment(segments[0] || '');
29
+ if (!leadingLocaleParam) return path;
30
+ const remainingPath = segments.slice(1).join('/');
31
+ return remainingPath ? `/${remainingPath}` : void 0;
32
+ };
33
+ const getLeadingLocaleParam = (path)=>{
34
+ const segments = splitPathSegments(path);
35
+ return getLocaleParamSegment(segments[0] || '');
36
+ };
37
+ const resolveLocalisedUrlsConfig = (option)=>{
38
+ if (false === option) return {
39
+ enabled: false,
40
+ map: {}
41
+ };
42
+ if (option && 'object' == typeof option) return {
43
+ enabled: true,
44
+ map: option
45
+ };
46
+ return {
47
+ enabled: true,
48
+ map: {}
49
+ };
50
+ };
51
+ const isLocalisableRoutePath = (path)=>{
52
+ const pathWithoutLocale = stripLeadingLocaleParam(path);
53
+ if (!pathWithoutLocale || '/' === pathWithoutLocale || '*' === pathWithoutLocale) return false;
54
+ return true;
55
+ };
56
+ const joinPath = (parentPath, routePath)=>{
57
+ if (!isLocalisableRoutePath(routePath)) return parentPath;
58
+ const segment = normaliseRoutePath(stripLeadingLocaleParam(routePath) || '');
59
+ return normalisePathPattern(`${parentPath}/${segment}`);
60
+ };
61
+ const ensureLocalisedUrlsForPath = (canonicalPath, languages, localisedUrls)=>{
62
+ const entry = localisedUrls[canonicalPath];
63
+ if (!entry) throw new Error(`localisedUrls is enabled, but route "${canonicalPath}" does not define localised URLs for languages: ${languages.join(', ')}. Add localisedUrls["${canonicalPath}"] or set localeDetection.localisedUrls to false.`);
64
+ const missingLanguages = languages.filter((language)=>!entry[language]);
65
+ if (missingLanguages.length > 0) throw new Error(`localisedUrls["${canonicalPath}"] is missing languages: ${missingLanguages.join(', ')}. Every configured language must have a localised URL.`);
66
+ return entry;
67
+ };
68
+ const validateLocalisedUrls = (routes, languages, localisedUrls)=>{
69
+ const visit = (route, parentPath)=>{
70
+ const canonicalPath = joinPath(parentPath, route.path);
71
+ if (isLocalisableRoutePath(route.path)) ensureLocalisedUrlsForPath(canonicalPath, languages, localisedUrls);
72
+ if ('children' in route && route.children) route.children.forEach((child)=>visit(child, canonicalPath));
73
+ };
74
+ routes.forEach((route)=>visit(route, ''));
75
+ };
76
+ const getLocalisedRoutePaths = (canonicalPath, parentLocalisedPaths, languages, entry)=>{
77
+ const paths = languages.map((language)=>{
78
+ const fullPath = normalisePathPattern(entry[language]);
79
+ const parentPath = normalisePathPattern(parentLocalisedPaths[language] || '/');
80
+ if ('/' === parentPath) return normaliseRoutePath(fullPath) || void 0;
81
+ const parentPrefix = `${parentPath}/`;
82
+ if (!fullPath.startsWith(parentPrefix)) throw new Error(`localisedUrls["${canonicalPath}"].${language} must be nested under "${parentPath}" because its parent route is localised there.`);
83
+ return normaliseRoutePath(fullPath.slice(parentPath.length));
84
+ });
85
+ return Array.from(new Set(paths.filter(Boolean)));
86
+ };
87
+ const transformLocalisedRoute = (route, parentCanonicalPath, parentLocalisedPaths, languages, localisedUrls)=>{
88
+ const canonicalPath = joinPath(parentCanonicalPath, route.path);
89
+ const localisedUrlEntry = isLocalisableRoutePath(route.path) ? ensureLocalisedUrlsForPath(canonicalPath, languages, localisedUrls) : void 0;
90
+ const routeLocalisedPaths = localisedUrlEntry ? languages.reduce((acc, language)=>{
91
+ acc[language] = normalisePathPattern(localisedUrlEntry[language]);
92
+ return acc;
93
+ }, {}) : parentLocalisedPaths;
94
+ const children = 'children' in route && route.children ? route.children.flatMap((child)=>transformLocalisedRoute(child, canonicalPath, routeLocalisedPaths, languages, localisedUrls)) : void 0;
95
+ const baseRoute = {
96
+ ...route,
97
+ ...children ? {
98
+ children
99
+ } : {}
100
+ };
101
+ if (!localisedUrlEntry) return [
102
+ baseRoute
103
+ ];
104
+ return getLocalisedRoutePaths(canonicalPath, parentLocalisedPaths, languages, localisedUrlEntry).map((localisedPath, index)=>cloneRouteWithLocalisedPath(baseRoute, localisedPath, index));
105
+ };
106
+ const legalRouteIdPart = (value)=>value.replace(/[^a-zA-Z0-9_$-]+/g, '_').replace(/^_+|_+$/g, '') || 'index';
107
+ const suffixRouteIds = (route, suffix)=>{
108
+ const children = 'children' in route && route.children ? route.children.map((child)=>suffixRouteIds(child, suffix)) : void 0;
109
+ return {
110
+ ...route,
111
+ ...route.id ? {
112
+ id: `${route.id}__localised_${suffix}`
113
+ } : {},
114
+ ...children ? {
115
+ children
116
+ } : {}
117
+ };
118
+ };
119
+ const cloneRouteWithLocalisedPath = (route, path, index)=>{
120
+ const leadingLocaleParam = getLeadingLocaleParam(route.path);
121
+ const localisedPath = leadingLocaleParam ? normaliseRoutePath(`${leadingLocaleParam}/${path}`) : path;
122
+ const routeWithPath = {
123
+ ...route,
124
+ path: localisedPath
125
+ };
126
+ return 0 === index ? routeWithPath : suffixRouteIds(routeWithPath, legalRouteIdPart(localisedPath));
127
+ };
128
+ const applyLocalisedUrlsToRoutes = (routes, languages, localisedUrls)=>{
129
+ const rootLocalisedPaths = languages.reduce((acc, language)=>{
130
+ acc[language] = '/';
131
+ return acc;
132
+ }, {});
133
+ validateLocalisedUrls(routes, languages, localisedUrls);
134
+ return routes.flatMap((route)=>transformLocalisedRoute(route, '', rootLocalisedPaths, languages, localisedUrls));
135
+ };
136
+ const escapeRegExp = (value)=>value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
137
+ const getParamName = (segment)=>segment.slice(1).replace(/\?$/, '');
138
+ const compilePathPattern = (pattern)=>{
139
+ const names = [];
140
+ const segments = normalisePathPattern(pattern).split('/').filter(Boolean);
141
+ const source = segments.map((segment)=>{
142
+ if (segment.startsWith(':')) {
143
+ names.push(getParamName(segment));
144
+ const paramPattern = '([^/]+)';
145
+ return segment.endsWith('?') ? `(?:/${paramPattern})?` : `/${paramPattern}`;
146
+ }
147
+ if ('*' === segment) {
148
+ names.push('*');
149
+ return '/(.*)';
150
+ }
151
+ return `/${escapeRegExp(segment)}`;
152
+ }).join('');
153
+ return {
154
+ names,
155
+ regexp: new RegExp(`^${source || '/'}$`)
156
+ };
157
+ };
158
+ const matchPathPattern = (pathname, pattern)=>{
159
+ const { names, regexp } = compilePathPattern(pattern);
160
+ const match = regexp.exec(normalisePathPattern(pathname));
161
+ if (!match) return null;
162
+ return names.reduce((params, name, index)=>{
163
+ params[name] = decodeURIComponent(match[index + 1] || '');
164
+ return params;
165
+ }, {});
166
+ };
167
+ const buildPathFromPattern = (pattern, params)=>{
168
+ const segments = normalisePathPattern(pattern).split('/').filter(Boolean);
169
+ const path = segments.map((segment)=>{
170
+ if (segment.startsWith(':')) {
171
+ const param = params[getParamName(segment)];
172
+ return param ? encodeURIComponent(param) : '';
173
+ }
174
+ if ('*' === segment) return params['*'] || '';
175
+ return segment;
176
+ }).filter(Boolean).join('/');
177
+ return `/${path}`;
178
+ };
179
+ const resolveLocalisedPath = (pathname, targetLanguage, languages, localisedUrls)=>{
180
+ const normalizedPathname = normalisePathPattern(pathname);
181
+ for (const localisedUrlEntry of Object.values(localisedUrls)){
182
+ const targetPattern = localisedUrlEntry[targetLanguage];
183
+ if (targetPattern) for (const language of languages){
184
+ const sourcePattern = localisedUrlEntry[language];
185
+ if (!sourcePattern) continue;
186
+ const params = matchPathPattern(normalizedPathname, sourcePattern);
187
+ if (params) return buildPathFromPattern(targetPattern, params);
188
+ }
189
+ }
190
+ return normalizedPathname;
191
+ };
192
+ export { applyLocalisedUrlsToRoutes, normalisePathPattern, resolveLocalisedPath, resolveLocalisedUrlsConfig, validateLocalisedUrls };
@@ -1,6 +1,7 @@
1
1
  import type { AppTools, CliPlugin } from '@modern-js/app-tools';
2
2
  import type { Entrypoint } from '@modern-js/types';
3
3
  import type { BackendOptions, LocaleDetectionOptions } from '../shared/type';
4
+ import '../runtime/types';
4
5
  export type TransformRuntimeConfigFn = (extendedConfig: Record<string, any>, entrypoint: Entrypoint) => Record<string, any>;
5
6
  export interface I18nPluginOptions {
6
7
  localeDetection?: LocaleDetectionOptions;
@@ -4,5 +4,20 @@ export interface I18nLinkProps {
4
4
  children: React.ReactNode;
5
5
  [key: string]: any;
6
6
  }
7
+ /**
8
+ * I18nLink component that automatically adds language prefix to navigation links.
9
+ * This component should be used within a :lang dynamic route context.
10
+ *
11
+ * @example
12
+ * ```tsx
13
+ * // When current language is 'en' and to="/about"
14
+ * // The actual link will be "/en/about"
15
+ * <I18nLink to="/about">About</I18nLink>
16
+ *
17
+ * // When current language is 'zh' and to="/"
18
+ * // The actual link will be "/zh"
19
+ * <I18nLink to="/">Home</I18nLink>
20
+ * ```
21
+ */
7
22
  export declare const I18nLink: React.FC<I18nLinkProps>;
8
23
  export default I18nLink;
@@ -1,4 +1,5 @@
1
1
  import type { FC, ReactNode } from 'react';
2
+ import type { LocalisedUrlsOption } from '../shared/localisedUrls';
2
3
  import type { I18nInstance } from './i18n';
3
4
  export interface ModernI18nContextValue {
4
5
  language: string;
@@ -7,6 +8,7 @@ export interface ModernI18nContextValue {
7
8
  languages?: string[];
8
9
  localePathRedirect?: boolean;
9
10
  ignoreRedirectRoutes?: string[] | ((pathname: string) => boolean);
11
+ localisedUrls?: LocalisedUrlsOption;
10
12
  updateLanguage?: (newLang: string) => void;
11
13
  }
12
14
  export interface ModernI18nProviderProps {
@@ -19,6 +21,7 @@ export interface UseModernI18nReturn {
19
21
  changeLanguage: (newLang: string) => Promise<void>;
20
22
  i18nInstance: I18nInstance;
21
23
  supportedLanguages: string[];
24
+ localisedUrls?: LocalisedUrlsOption;
22
25
  isLanguageSupported: (lang: string) => boolean;
23
26
  isResourcesReady: boolean;
24
27
  }
@@ -1,16 +1,18 @@
1
1
  import type { TRuntimeContext } from '@modern-js/runtime';
2
2
  import type React from 'react';
3
+ import type { LocalisedUrlsOption } from '../shared/localisedUrls';
3
4
  import type { I18nInstance } from './i18n';
4
5
  interface RuntimeContextWithI18n extends TRuntimeContext {
5
6
  i18nInstance?: I18nInstance;
6
7
  }
7
- export declare function createContextValue(lang: string, i18nInstance: I18nInstance | undefined, entryName: string | undefined, languages: string[], localePathRedirect: boolean, ignoreRedirectRoutes: string[] | ((pathname: string) => boolean) | undefined, setLang: (lang: string) => void): {
8
+ export declare function createContextValue(lang: string, i18nInstance: I18nInstance | undefined, entryName: string | undefined, languages: string[], localePathRedirect: boolean, ignoreRedirectRoutes: string[] | ((pathname: string) => boolean) | undefined, localisedUrls: LocalisedUrlsOption | undefined, setLang: (lang: string) => void): {
8
9
  language: string;
9
10
  i18nInstance: I18nInstance;
10
11
  entryName: string | undefined;
11
12
  languages: string[];
12
13
  localePathRedirect: boolean;
13
14
  ignoreRedirectRoutes: string[] | ((pathname: string) => boolean) | undefined;
15
+ localisedUrls: LocalisedUrlsOption | undefined;
14
16
  updateLanguage: (lang: string) => void;
15
17
  };
16
18
  export declare function useSdkResourcesLoader(i18nInstance: I18nInstance | undefined, setForceUpdate: React.Dispatch<React.SetStateAction<number>>): void;
@@ -23,6 +25,6 @@ export declare function useSdkResourcesLoader(i18nInstance: I18nInstance | undef
23
25
  * In SSR/SSG scenarios, server-side middleware handles redirects, so this hook is skipped.
24
26
  * We use process.env.MODERN_TARGET to ensure this code is only included in browser bundles.
25
27
  */
26
- export declare function useClientSideRedirect(i18nInstance: I18nInstance | undefined, localePathRedirect: boolean, languages: string[], fallbackLanguage: string, ignoreRedirectRoutes?: string[] | ((pathname: string) => boolean)): void;
28
+ export declare function useClientSideRedirect(i18nInstance: I18nInstance | undefined, localePathRedirect: boolean, languages: string[], fallbackLanguage: string, ignoreRedirectRoutes?: string[] | ((pathname: string) => boolean), localisedUrls?: LocalisedUrlsOption): void;
27
29
  export declare function useLanguageSync(i18nInstance: I18nInstance | undefined, localePathRedirect: boolean, languages: string[], runtimeContextRef: React.MutableRefObject<RuntimeContextWithI18n>, prevLangRef: React.MutableRefObject<string>, setLang: (lang: string) => void): void;
28
30
  export {};
@@ -1,4 +1,4 @@
1
- import Backend from 'i18next-fs-backend';
1
+ import Backend from 'i18next-fs-backend/cjs';
2
2
  import type { ExtendedBackendOptions } from '../../../shared/type';
3
3
  import type { I18nInstance } from '../instance';
4
4
  /**
@@ -1,7 +1,4 @@
1
1
  import type { BaseBackendOptions } from '../../shared/type';
2
- type ReactI18nextModule = typeof import('react-i18next');
3
- type InitReactI18next = ReactI18nextModule['initReactI18next'];
4
- type I18nextProviderComponent = ReactI18nextModule['I18nextProvider'];
5
2
  export interface I18nResourceStore {
6
3
  data?: {
7
4
  [language: string]: {
@@ -64,9 +61,12 @@ export interface BackendOptions extends Omit<BaseBackendOptions, 'enabled'> {
64
61
  stringify?: (data: any) => string;
65
62
  [key: string]: any;
66
63
  }
64
+ export type ResourceValue = string | {
65
+ [key: string]: ResourceValue;
66
+ };
67
67
  export interface Resources {
68
68
  [lng: string]: {
69
- [source: string]: string | Record<string, string>;
69
+ [source: string]: ResourceValue;
70
70
  };
71
71
  }
72
72
  export type I18nInitOptions = {
@@ -91,6 +91,4 @@ export type I18nInitOptions = {
91
91
  export declare function isI18nInstance(obj: any): obj is I18nInstance;
92
92
  export declare function getI18nextInstanceForProvider(instance: I18nInstance | any): any;
93
93
  export declare function getI18nInstance(userInstance?: I18nInstance | any): Promise<I18nInstance>;
94
- export declare function getInitReactI18next(): Promise<InitReactI18next | null>;
95
- export declare function getI18nextProvider(): Promise<I18nextProviderComponent | null>;
96
94
  export {};
@@ -0,0 +1,7 @@
1
+ import type React from 'react';
2
+ interface ReactI18nextIntegration {
3
+ I18nextProvider: React.ComponentType<any> | null;
4
+ initReactI18next: any | null;
5
+ }
6
+ export declare function getReactI18nextIntegration(): Promise<ReactI18nextIntegration>;
7
+ export {};
@@ -12,6 +12,7 @@ export interface I18nPluginOptions {
12
12
  changeLanguage?: (lang: string) => void;
13
13
  initOptions?: I18nInitOptions;
14
14
  htmlLangAttr?: boolean;
15
+ reactI18next?: boolean;
15
16
  [key: string]: any;
16
17
  }
17
18
  export declare const i18nPlugin: (options: I18nPluginOptions) => RuntimePlugin;
@@ -0,0 +1,26 @@
1
+ import type React from 'react';
2
+ export type I18nRouterFramework = 'react-router' | 'tanstack' | string;
3
+ export interface I18nRouterLocation {
4
+ pathname: string;
5
+ search: string;
6
+ hash: string;
7
+ }
8
+ export interface I18nRouterNavigateOptions {
9
+ replace?: boolean;
10
+ state?: unknown;
11
+ }
12
+ export type I18nRouterNavigate = (href: string, options?: I18nRouterNavigateOptions) => void | Promise<void>;
13
+ export type I18nRouterLink = React.ComponentType<{
14
+ to: string;
15
+ children?: React.ReactNode;
16
+ [key: string]: unknown;
17
+ }>;
18
+ export interface I18nRouterAdapter {
19
+ framework?: I18nRouterFramework;
20
+ hasRouter: boolean;
21
+ location: I18nRouterLocation | null;
22
+ navigate: I18nRouterNavigate | null;
23
+ Link: I18nRouterLink | null;
24
+ params: Record<string, string>;
25
+ }
26
+ export declare const useI18nRouterAdapter: () => I18nRouterAdapter;
@@ -8,7 +8,7 @@ declare module '@modern-js/runtime' {
8
8
  initOptions?: I18nInitOptions;
9
9
  };
10
10
  }
11
- interface TInternalRuntimeContext {
11
+ interface TRuntimeContext {
12
12
  i18nInstance?: I18nInstance;
13
13
  changeLanguage?: (lang: string) => Promise<void>;
14
14
  }
@@ -1,4 +1,5 @@
1
1
  import { type TInternalRuntimeContext } from '@modern-js/runtime/context';
2
+ import type { LocalisedUrlsMap } from '../shared/localisedUrls';
2
3
  export declare const getPathname: (context: TInternalRuntimeContext) => string;
3
4
  export declare const getEntryPath: () => string;
4
5
  /**
@@ -16,7 +17,7 @@ export declare const getLanguageFromPath: (pathname: string, languages: string[]
16
17
  * @param languages - Array of supported languages
17
18
  * @returns The localized URL path
18
19
  */
19
- export declare const buildLocalizedUrl: (pathname: string, language: string, languages: string[]) => string;
20
+ export declare const buildLocalizedUrl: (pathname: string, language: string, languages: string[], localisedUrls?: boolean | LocalisedUrlsMap) => string;
20
21
  export declare const detectLanguageFromPath: (pathname: string, languages: string[], localePathRedirect: boolean) => {
21
22
  detected: boolean;
22
23
  language?: string;
@@ -25,9 +26,3 @@ export declare const detectLanguageFromPath: (pathname: string, languages: strin
25
26
  * Check if the given pathname should ignore automatic locale redirect
26
27
  */
27
28
  export declare const shouldIgnoreRedirect: (pathname: string, languages: string[], ignoreRedirectRoutes?: string[] | ((pathname: string) => boolean)) => boolean;
28
- export declare const useRouterHooks: () => {
29
- navigate: any;
30
- location: any;
31
- params: any;
32
- hasRouter: boolean;
33
- };
@@ -4,5 +4,11 @@ export interface I18nPluginOptions {
4
4
  localeDetection: LocaleDetectionOptions;
5
5
  staticRoutePrefixes: string[];
6
6
  }
7
+ type ApiPrefixInput = string | string[] | undefined;
8
+ export declare const collectApiPrefixes: (routes: Array<{
9
+ isApi?: boolean;
10
+ urlPath?: string;
11
+ }>, bffPrefix?: ApiPrefixInput) => string[];
12
+ export declare const matchesApiPrefix: (pathname: string, apiPrefixes: string[]) => boolean;
7
13
  export declare const i18nServerPlugin: (options: I18nPluginOptions) => ServerPlugin;
8
14
  export default i18nServerPlugin;
@@ -0,0 +1,13 @@
1
+ import type { NestedRouteForCli, PageRoute } from '@modern-js/types';
2
+ export type LocalisedUrlPathMap = Record<string, string>;
3
+ export type LocalisedUrlsMap = Record<string, LocalisedUrlPathMap>;
4
+ export type LocalisedUrlsOption = boolean | LocalisedUrlsMap;
5
+ export interface ResolvedLocalisedUrlsConfig {
6
+ enabled: boolean;
7
+ map: LocalisedUrlsMap;
8
+ }
9
+ export declare const normalisePathPattern: (path: string) => string;
10
+ export declare const resolveLocalisedUrlsConfig: (option: LocalisedUrlsOption | undefined) => ResolvedLocalisedUrlsConfig;
11
+ export declare const validateLocalisedUrls: (routes: (NestedRouteForCli | PageRoute)[], languages: string[], localisedUrls: LocalisedUrlsMap) => void;
12
+ export declare const applyLocalisedUrlsToRoutes: (routes: (NestedRouteForCli | PageRoute)[], languages: string[], localisedUrls: LocalisedUrlsMap) => (NestedRouteForCli | PageRoute)[];
13
+ export declare const resolveLocalisedPath: (pathname: string, targetLanguage: string, languages: string[], localisedUrls: LocalisedUrlsMap) => string;
@@ -1,4 +1,5 @@
1
1
  import type { LanguageDetectorOptions, Resources } from '../runtime/i18n/instance';
2
+ import type { LocalisedUrlsOption } from './localisedUrls';
2
3
  export interface BaseLocaleDetectionOptions {
3
4
  localePathRedirect?: boolean;
4
5
  i18nextDetector?: boolean;
@@ -6,6 +7,17 @@ export interface BaseLocaleDetectionOptions {
6
7
  fallbackLanguage?: string;
7
8
  detection?: LanguageDetectorOptions;
8
9
  ignoreRedirectRoutes?: string[] | ((pathname: string) => boolean);
10
+ /**
11
+ * Enables localised pathnames in addition to the locale prefix.
12
+ *
13
+ * - `false`: keep only locale-prefix behavior (`/en/about`).
14
+ * - object: map canonical route paths to every configured language.
15
+ *
16
+ * Defaults to `true` when `localePathRedirect` is enabled, so route
17
+ * generation validates that every localisable route path has entries for all
18
+ * configured languages.
19
+ */
20
+ localisedUrls?: LocalisedUrlsOption;
9
21
  }
10
22
  export interface LocaleDetectionOptions extends BaseLocaleDetectionOptions {
11
23
  localeDetectionByEntry?: Record<string, BaseLocaleDetectionOptions>;
package/package.json CHANGED
@@ -17,7 +17,7 @@
17
17
  "modern",
18
18
  "modern.js"
19
19
  ],
20
- "version": "3.2.0-ultramodern.11",
20
+ "version": "3.2.0-ultramodern.111",
21
21
  "engines": {
22
22
  "node": ">=20"
23
23
  },
@@ -81,47 +81,43 @@
81
81
  }
82
82
  },
83
83
  "dependencies": {
84
- "@swc/helpers": "^0.5.21",
84
+ "@swc/helpers": "^0.5.23",
85
85
  "i18next-browser-languagedetector": "^8.2.1",
86
86
  "i18next-chained-backend": "^5.0.4",
87
- "i18next-fs-backend": "^2.6.5",
87
+ "i18next-fs-backend": "^2.6.6",
88
88
  "i18next-http-backend": "^4.0.0",
89
- "i18next-http-middleware": "^3.9.6",
90
- "@modern-js/plugin": "npm:@bleedingdev/modern-js-plugin@3.2.0-ultramodern.11",
91
- "@modern-js/runtime-utils": "npm:@bleedingdev/modern-js-runtime-utils@3.2.0-ultramodern.11",
92
- "@modern-js/types": "npm:@bleedingdev/modern-js-types@3.2.0-ultramodern.11",
93
- "@modern-js/server-runtime": "npm:@bleedingdev/modern-js-server-runtime@3.2.0-ultramodern.11",
94
- "@modern-js/server-core": "npm:@bleedingdev/modern-js-server-core@3.2.0-ultramodern.11",
95
- "@modern-js/utils": "npm:@bleedingdev/modern-js-utils@3.2.0-ultramodern.11"
89
+ "i18next-http-middleware": "^3.9.7",
90
+ "react-i18next": "17.0.8",
91
+ "@modern-js/server-runtime": "npm:@bleedingdev/modern-js-server-runtime@3.2.0-ultramodern.111",
92
+ "@modern-js/plugin": "npm:@bleedingdev/modern-js-plugin@3.2.0-ultramodern.111",
93
+ "@modern-js/server-core": "npm:@bleedingdev/modern-js-server-core@3.2.0-ultramodern.111",
94
+ "@modern-js/utils": "npm:@bleedingdev/modern-js-utils@3.2.0-ultramodern.111",
95
+ "@modern-js/types": "npm:@bleedingdev/modern-js-types@3.2.0-ultramodern.111",
96
+ "@modern-js/runtime-utils": "npm:@bleedingdev/modern-js-runtime-utils@3.2.0-ultramodern.111"
96
97
  },
97
98
  "peerDependencies": {
98
- "@modern-js/runtime": "3.2.0-ultramodern.11",
99
+ "@modern-js/runtime": "3.2.0-ultramodern.111",
99
100
  "i18next": ">=25.7.4",
100
- "react": "^19.2.6",
101
- "react-dom": "^19.2.6",
102
- "react-i18next": ">=15.7.4"
101
+ "react": "^19.2.7",
102
+ "react-dom": "^19.2.7"
103
103
  },
104
104
  "peerDependenciesMeta": {
105
105
  "i18next": {
106
106
  "optional": true
107
- },
108
- "react-i18next": {
109
- "optional": true
110
107
  }
111
108
  },
112
109
  "devDependencies": {
113
110
  "@rslib/core": "0.21.5",
114
111
  "@types/jest": "^30.0.0",
115
- "@types/node": "^25.8.0",
116
- "@typescript/native-preview": "7.0.0-dev.20260516.1",
117
- "i18next": "26.2.0",
112
+ "@types/node": "^25.9.1",
113
+ "@typescript/native-preview": "7.0.0-dev.20260606.1",
114
+ "i18next": "26.3.1",
118
115
  "jest": "^30.4.2",
119
- "react": "^19.2.6",
120
- "react-dom": "^19.2.6",
121
- "react-i18next": "17.0.8",
122
- "ts-jest": "^29.4.9",
123
- "@modern-js/runtime": "npm:@bleedingdev/modern-js-runtime@3.2.0-ultramodern.11",
124
- "@modern-js/app-tools": "npm:@bleedingdev/modern-js-app-tools@3.2.0-ultramodern.11"
116
+ "react": "^19.2.7",
117
+ "react-dom": "^19.2.7",
118
+ "ts-jest": "^29.4.11",
119
+ "@modern-js/app-tools": "npm:@bleedingdev/modern-js-app-tools@3.2.0-ultramodern.111",
120
+ "@modern-js/runtime": "npm:@bleedingdev/modern-js-runtime@3.2.0-ultramodern.111"
125
121
  },
126
122
  "sideEffects": false,
127
123
  "publishConfig": {
@@ -130,7 +126,7 @@
130
126
  },
131
127
  "scripts": {
132
128
  "dev": "rslib build --watch",
133
- "build": "rslib build",
129
+ "build": "rslib build && pnpm -w tsgo:dts \"$PWD\"",
134
130
  "test": "rstest --passWithNoTests"
135
131
  }
136
132
  }
@@ -0,0 +1,39 @@
1
+ import { dirname, resolve } from 'node:path';
2
+ import { fileURLToPath } from 'node:url';
3
+ import type { ProjectConfig } from '@rstest/core';
4
+ import { withTestPreset } from '@scripts/rstest-config';
5
+
6
+ const __dirname = dirname(fileURLToPath(import.meta.url));
7
+
8
+ const commonConfig: ProjectConfig = {
9
+ setupFiles: [resolve(__dirname, '../../../scripts/rstest-config/setup.ts')],
10
+ globals: true,
11
+ tools: {
12
+ swc: {
13
+ jsc: {
14
+ transform: {
15
+ react: {
16
+ runtime: 'automatic',
17
+ },
18
+ },
19
+ },
20
+ },
21
+ },
22
+ };
23
+
24
+ export default {
25
+ projects: [
26
+ withTestPreset({
27
+ name: 'plugin-i18n-node',
28
+ testEnvironment: 'node',
29
+ include: ['tests/localisedUrls.test.ts'],
30
+ extends: commonConfig,
31
+ }),
32
+ withTestPreset({
33
+ name: 'plugin-i18n-client',
34
+ testEnvironment: 'happy-dom',
35
+ include: ['tests/routerAdapter.test.tsx'],
36
+ extends: commonConfig,
37
+ }),
38
+ ],
39
+ };
package/src/cli/index.ts CHANGED
@@ -1,10 +1,19 @@
1
1
  import type { AppTools, CliPlugin } from '@modern-js/app-tools';
2
2
  import { getPublicDirRoutePrefixes } from '@modern-js/server-core';
3
- import type { Entrypoint } from '@modern-js/types';
3
+ import type {
4
+ Entrypoint,
5
+ NestedRouteForCli,
6
+ PageRoute,
7
+ } from '@modern-js/types';
4
8
  import fs from 'fs';
5
9
  import path from 'path';
10
+ import {
11
+ applyLocalisedUrlsToRoutes,
12
+ resolveLocalisedUrlsConfig,
13
+ } from '../shared/localisedUrls';
6
14
  import type { BackendOptions, LocaleDetectionOptions } from '../shared/type';
7
15
  import { getBackendOptions, getLocaleDetectionOptions } from '../shared/utils';
16
+ import '../runtime/types';
8
17
 
9
18
  export type TransformRuntimeConfigFn = (
10
19
  extendedConfig: Record<string, any>,
@@ -203,6 +212,40 @@ export const i18nPlugin = (
203
212
  };
204
213
  });
205
214
 
215
+ api.modifyFileSystemRoutes(({ entrypoint, routes }) => {
216
+ if (!localeDetection) {
217
+ return { entrypoint, routes };
218
+ }
219
+
220
+ const localeDetectionOptions = getLocaleDetectionOptions(
221
+ entrypoint.entryName,
222
+ localeDetection,
223
+ );
224
+ const {
225
+ localePathRedirect,
226
+ languages = [],
227
+ localisedUrls,
228
+ } = localeDetectionOptions;
229
+
230
+ if (!localePathRedirect || languages.length === 0) {
231
+ return { entrypoint, routes };
232
+ }
233
+
234
+ const localisedUrlsConfig = resolveLocalisedUrlsConfig(localisedUrls);
235
+ if (!localisedUrlsConfig.enabled) {
236
+ return { entrypoint, routes };
237
+ }
238
+
239
+ return {
240
+ entrypoint,
241
+ routes: applyLocalisedUrlsToRoutes(
242
+ routes as (NestedRouteForCli | PageRoute)[],
243
+ languages,
244
+ localisedUrlsConfig.map,
245
+ ),
246
+ };
247
+ });
248
+
206
249
  api._internalServerPlugins(({ plugins }) => {
207
250
  const { serverRoutes, metaName } = api.getAppContext();
208
251
  const normalizedConfig = api.getNormalizedConfig();