@bleedingdev/modern-js-plugin-i18n 3.2.0-ultramodern.5 → 3.2.0-ultramodern.50

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 (90) hide show
  1. package/dist/cjs/cli/index.js +22 -0
  2. package/dist/cjs/runtime/I18nLink.js +4 -12
  3. package/dist/cjs/runtime/context.js +32 -5
  4. package/dist/cjs/runtime/hooks.js +8 -5
  5. package/dist/cjs/runtime/i18n/backend/defaults.js +1 -1
  6. package/dist/cjs/runtime/i18n/backend/middleware.node.js +4 -4
  7. package/dist/cjs/runtime/i18n/instance.js +0 -24
  8. package/dist/cjs/runtime/i18n/react-i18next.js +52 -0
  9. package/dist/cjs/runtime/index.js +13 -7
  10. package/dist/cjs/runtime/routerAdapter.js +163 -0
  11. package/dist/cjs/runtime/utils.js +63 -94
  12. package/dist/cjs/server/index.js +60 -8
  13. package/dist/cjs/shared/localisedUrls.js +237 -0
  14. package/dist/esm/cli/index.mjs +22 -0
  15. package/dist/esm/runtime/I18nLink.mjs +4 -12
  16. package/dist/esm/runtime/context.mjs +34 -7
  17. package/dist/esm/runtime/hooks.mjs +9 -6
  18. package/dist/esm/runtime/i18n/backend/defaults.mjs +1 -1
  19. package/dist/esm/runtime/i18n/backend/middleware.node.mjs +3 -3
  20. package/dist/esm/runtime/i18n/instance.mjs +1 -19
  21. package/dist/esm/runtime/i18n/react-i18next.mjs +18 -0
  22. package/dist/esm/runtime/index.mjs +14 -8
  23. package/dist/esm/runtime/routerAdapter.mjs +129 -0
  24. package/dist/esm/runtime/utils.mjs +11 -30
  25. package/dist/esm/server/index.mjs +53 -7
  26. package/dist/esm/shared/localisedUrls.mjs +191 -0
  27. package/dist/esm-node/cli/index.mjs +22 -0
  28. package/dist/esm-node/runtime/I18nLink.mjs +4 -12
  29. package/dist/esm-node/runtime/context.mjs +34 -7
  30. package/dist/esm-node/runtime/hooks.mjs +9 -6
  31. package/dist/esm-node/runtime/i18n/backend/defaults.mjs +1 -1
  32. package/dist/esm-node/runtime/i18n/backend/middleware.node.mjs +3 -3
  33. package/dist/esm-node/runtime/i18n/instance.mjs +1 -19
  34. package/dist/esm-node/runtime/i18n/react-i18next.mjs +19 -0
  35. package/dist/esm-node/runtime/index.mjs +14 -8
  36. package/dist/esm-node/runtime/routerAdapter.mjs +130 -0
  37. package/dist/esm-node/runtime/utils.mjs +11 -30
  38. package/dist/esm-node/server/index.mjs +53 -7
  39. package/dist/esm-node/shared/localisedUrls.mjs +192 -0
  40. package/dist/types/cli/index.d.ts +21 -0
  41. package/dist/types/runtime/I18nLink.d.ts +23 -0
  42. package/dist/types/runtime/context.d.ts +41 -0
  43. package/dist/types/runtime/hooks.d.ts +30 -0
  44. package/dist/types/runtime/i18n/backend/config.d.ts +2 -0
  45. package/dist/types/runtime/i18n/backend/defaults.d.ts +13 -0
  46. package/dist/types/runtime/i18n/backend/defaults.node.d.ts +8 -0
  47. package/dist/types/runtime/i18n/backend/index.d.ts +3 -0
  48. package/dist/types/runtime/i18n/backend/middleware.common.d.ts +14 -0
  49. package/dist/types/runtime/i18n/backend/middleware.d.ts +12 -0
  50. package/dist/types/runtime/i18n/backend/middleware.node.d.ts +13 -0
  51. package/dist/types/runtime/i18n/backend/sdk-backend.d.ts +53 -0
  52. package/dist/types/runtime/i18n/backend/sdk-event.d.ts +9 -0
  53. package/dist/types/runtime/i18n/detection/config.d.ts +11 -0
  54. package/dist/types/runtime/i18n/detection/index.d.ts +50 -0
  55. package/dist/types/runtime/i18n/detection/middleware.d.ts +24 -0
  56. package/dist/types/runtime/i18n/detection/middleware.node.d.ts +17 -0
  57. package/dist/types/runtime/i18n/index.d.ts +3 -0
  58. package/dist/types/runtime/i18n/instance.d.ts +91 -0
  59. package/dist/types/runtime/i18n/react-i18next.d.ts +7 -0
  60. package/dist/types/runtime/i18n/utils.d.ts +29 -0
  61. package/dist/types/runtime/index.d.ts +21 -0
  62. package/dist/types/runtime/routerAdapter.d.ts +26 -0
  63. package/dist/types/runtime/types.d.ts +15 -0
  64. package/dist/types/runtime/utils.d.ts +28 -0
  65. package/dist/types/server/index.d.ts +14 -0
  66. package/dist/types/shared/deepMerge.d.ts +1 -0
  67. package/dist/types/shared/detection.d.ts +11 -0
  68. package/dist/types/shared/localisedUrls.d.ts +13 -0
  69. package/dist/types/shared/type.d.ts +168 -0
  70. package/dist/types/shared/utils.d.ts +5 -0
  71. package/package.json +15 -15
  72. package/rstest.config.mts +39 -0
  73. package/src/cli/index.ts +43 -1
  74. package/src/runtime/I18nLink.tsx +10 -16
  75. package/src/runtime/context.tsx +45 -7
  76. package/src/runtime/hooks.ts +13 -4
  77. package/src/runtime/i18n/backend/defaults.ts +3 -1
  78. package/src/runtime/i18n/backend/middleware.node.ts +1 -1
  79. package/src/runtime/i18n/instance.ts +0 -25
  80. package/src/runtime/i18n/react-i18next.ts +31 -0
  81. package/src/runtime/index.tsx +21 -9
  82. package/src/runtime/routerAdapter.tsx +333 -0
  83. package/src/runtime/utils.ts +22 -34
  84. package/src/server/index.ts +117 -10
  85. package/src/shared/localisedUrls.ts +393 -0
  86. package/src/shared/type.ts +12 -0
  87. package/tests/localisedUrls.test.ts +312 -0
  88. package/tests/routerAdapter.test.tsx +278 -0
  89. package/dist/esm/rslib-runtime.mjs +0 -18
  90. package/dist/esm-node/rslib-runtime.mjs +0 -19
@@ -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 };
@@ -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 };
@@ -0,0 +1,21 @@
1
+ import type { AppTools, CliPlugin } from '@modern-js/app-tools';
2
+ import type { Entrypoint } from '@modern-js/types';
3
+ import type { BackendOptions, LocaleDetectionOptions } from '../shared/type';
4
+ export type TransformRuntimeConfigFn = (extendedConfig: Record<string, any>, entrypoint: Entrypoint) => Record<string, any>;
5
+ export interface I18nPluginOptions {
6
+ localeDetection?: LocaleDetectionOptions;
7
+ backend?: BackendOptions;
8
+ transformRuntimeConfig?: TransformRuntimeConfigFn;
9
+ customPlugin?: {
10
+ runtime?: {
11
+ name?: string;
12
+ path?: string;
13
+ };
14
+ server?: {
15
+ name?: string;
16
+ };
17
+ };
18
+ [key: string]: any;
19
+ }
20
+ export declare const i18nPlugin: (options?: I18nPluginOptions) => CliPlugin<AppTools>;
21
+ export default i18nPlugin;
@@ -0,0 +1,23 @@
1
+ import type React from 'react';
2
+ export interface I18nLinkProps {
3
+ to: string;
4
+ children: React.ReactNode;
5
+ [key: string]: any;
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
+ */
22
+ export declare const I18nLink: React.FC<I18nLinkProps>;
23
+ export default I18nLink;
@@ -0,0 +1,41 @@
1
+ import type { FC, ReactNode } from 'react';
2
+ import type { LocalisedUrlsOption } from '../shared/localisedUrls';
3
+ import type { I18nInstance } from './i18n';
4
+ export interface ModernI18nContextValue {
5
+ language: string;
6
+ i18nInstance: I18nInstance;
7
+ entryName?: string;
8
+ languages?: string[];
9
+ localePathRedirect?: boolean;
10
+ ignoreRedirectRoutes?: string[] | ((pathname: string) => boolean);
11
+ localisedUrls?: LocalisedUrlsOption;
12
+ updateLanguage?: (newLang: string) => void;
13
+ }
14
+ export interface ModernI18nProviderProps {
15
+ children: ReactNode;
16
+ value: ModernI18nContextValue;
17
+ }
18
+ export declare const ModernI18nProvider: FC<ModernI18nProviderProps>;
19
+ export interface UseModernI18nReturn {
20
+ language: string;
21
+ changeLanguage: (newLang: string) => Promise<void>;
22
+ i18nInstance: I18nInstance;
23
+ supportedLanguages: string[];
24
+ localisedUrls?: LocalisedUrlsOption;
25
+ isLanguageSupported: (lang: string) => boolean;
26
+ isResourcesReady: boolean;
27
+ }
28
+ /**
29
+ * Hook for accessing i18n functionality in Modern.js applications.
30
+ *
31
+ * This hook provides:
32
+ * - Current language from URL params or i18n context
33
+ * - changeLanguage function that updates both i18n instance and URL
34
+ * - Direct access to the i18n instance
35
+ * - List of supported languages
36
+ * - Helper function to check if a language is supported
37
+ *
38
+ * @param options - Optional configuration to override context settings
39
+ * @returns Object containing i18n functionality and utilities
40
+ */
41
+ export declare const useModernI18n: () => UseModernI18nReturn;
@@ -0,0 +1,30 @@
1
+ import type { TRuntimeContext } from '@modern-js/runtime';
2
+ import type React from 'react';
3
+ import type { LocalisedUrlsOption } from '../shared/localisedUrls';
4
+ import type { I18nInstance } from './i18n';
5
+ interface RuntimeContextWithI18n extends TRuntimeContext {
6
+ i18nInstance?: I18nInstance;
7
+ }
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): {
9
+ language: string;
10
+ i18nInstance: I18nInstance;
11
+ entryName: string | undefined;
12
+ languages: string[];
13
+ localePathRedirect: boolean;
14
+ ignoreRedirectRoutes: string[] | ((pathname: string) => boolean) | undefined;
15
+ localisedUrls: LocalisedUrlsOption | undefined;
16
+ updateLanguage: (lang: string) => void;
17
+ };
18
+ export declare function useSdkResourcesLoader(i18nInstance: I18nInstance | undefined, setForceUpdate: React.Dispatch<React.SetStateAction<number>>): void;
19
+ /**
20
+ * Hook to handle client-side redirect for locale path redirect in static deployments
21
+ * This ensures that when users access paths without language prefix, they are redirected
22
+ * to the localized version of the path
23
+ *
24
+ * Note: This hook only runs in CSR (Client-Side Rendering) scenarios.
25
+ * In SSR/SSG scenarios, server-side middleware handles redirects, so this hook is skipped.
26
+ * We use process.env.MODERN_TARGET to ensure this code is only included in browser bundles.
27
+ */
28
+ export declare function useClientSideRedirect(i18nInstance: I18nInstance | undefined, localePathRedirect: boolean, languages: string[], fallbackLanguage: string, ignoreRedirectRoutes?: string[] | ((pathname: string) => boolean), localisedUrls?: LocalisedUrlsOption): void;
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;
30
+ export {};
@@ -0,0 +1,2 @@
1
+ import type { BackendOptions } from '../instance';
2
+ export declare function mergeBackendOptions(defaultOptions: BackendOptions, cliOptions?: BackendOptions, userOptions?: BackendOptions): BackendOptions;
@@ -0,0 +1,13 @@
1
+ export declare const DEFAULT_I18NEXT_BACKEND_OPTIONS: {
2
+ loadPath: string;
3
+ addPath: string;
4
+ };
5
+ declare global {
6
+ interface Window {
7
+ __assetPrefix__?: string;
8
+ }
9
+ }
10
+ export declare function convertBackendOptions<T extends {
11
+ loadPath?: string;
12
+ addPath?: string;
13
+ }>(options: T): T;
@@ -0,0 +1,8 @@
1
+ export declare const DEFAULT_I18NEXT_BACKEND_OPTIONS: {
2
+ loadPath: string;
3
+ addPath: string;
4
+ };
5
+ export declare function convertBackendOptions<T extends {
6
+ loadPath?: string;
7
+ addPath?: string;
8
+ }>(options: T): T;
@@ -0,0 +1,3 @@
1
+ import type { BaseBackendOptions } from '../../../shared/type';
2
+ import type { BackendOptions, I18nInitOptions } from '../instance';
3
+ export declare const mergeBackendOptions: (backend?: BaseBackendOptions, userInitOptions?: I18nInitOptions) => BackendOptions | undefined;
@@ -0,0 +1,14 @@
1
+ import type { BaseBackendOptions, ChainedBackendConfig } from '../../../shared/type';
2
+ import type { I18nInstance } from '../instance';
3
+ type BackendConfigWithChained = BaseBackendOptions & Partial<ChainedBackendConfig>;
4
+ /**
5
+ * Common logic for using i18next backend
6
+ * This function handles the backend selection and chained backend configuration
7
+ *
8
+ * @param i18nInstance - The i18n instance to configure
9
+ * @param BackendWithSave - The wrapped backend class with save method (required for chained backend refresh logic)
10
+ * @param BackendBase - The base backend class (for non-chained use)
11
+ * @param backend - Optional backend configuration
12
+ */
13
+ export declare function useI18nextBackendCommon(i18nInstance: I18nInstance, BackendWithSave: new (...args: any[]) => any, BackendBase: new (...args: any[]) => any, backend?: BackendConfigWithChained): void;
14
+ export {};
@@ -0,0 +1,12 @@
1
+ import Backend from 'i18next-http-backend';
2
+ import type { ExtendedBackendOptions } from '../../../shared/type';
3
+ import type { I18nInstance } from '../instance';
4
+ /**
5
+ * Wrapper for HTTP backend to add a no-op save method
6
+ * This is required for i18next-chained-backend to trigger refresh logic
7
+ * when cacheHitMode is 'refresh' or 'refreshAndUpdateStore'
8
+ */
9
+ export declare class HttpBackendWithSave extends Backend {
10
+ save(_language: string, _namespace: string, _data: unknown): void;
11
+ }
12
+ export declare const useI18nextBackend: (i18nInstance: I18nInstance, backend?: ExtendedBackendOptions) => void;
@@ -0,0 +1,13 @@
1
+ import Backend from 'i18next-fs-backend/cjs';
2
+ import type { ExtendedBackendOptions } from '../../../shared/type';
3
+ import type { I18nInstance } from '../instance';
4
+ /**
5
+ * Wrapper for FS backend to add a no-op save method
6
+ * This is required for i18next-chained-backend to trigger refresh logic
7
+ * when cacheHitMode is 'refresh' or 'refreshAndUpdateStore'
8
+ */
9
+ export declare class FsBackendWithSave extends Backend {
10
+ save(_language: string, _namespace: string, _data: unknown): void;
11
+ }
12
+ export declare const HttpBackendWithSave: typeof FsBackendWithSave;
13
+ export declare const useI18nextBackend: (i18nInstance: I18nInstance, backend?: ExtendedBackendOptions) => void;
@@ -0,0 +1,53 @@
1
+ import type { I18nSdkLoader } from '../../../shared/type';
2
+ interface BackendOptions {
3
+ sdk?: I18nSdkLoader;
4
+ [key: string]: unknown;
5
+ }
6
+ interface I18nextServices {
7
+ resourceStore?: {
8
+ data?: {
9
+ [language: string]: {
10
+ [namespace: string]: Record<string, string>;
11
+ };
12
+ };
13
+ };
14
+ store?: {
15
+ data?: {
16
+ [language: string]: {
17
+ [namespace: string]: Record<string, string>;
18
+ };
19
+ };
20
+ };
21
+ [key: string]: any;
22
+ }
23
+ export declare class SdkBackend {
24
+ static type: string;
25
+ type: 'backend';
26
+ sdk?: I18nSdkLoader;
27
+ private allResourcesCache;
28
+ private backendId;
29
+ private loadingPromises;
30
+ private services?;
31
+ constructor(_services: unknown, _options: Record<string, unknown>);
32
+ init(services: I18nextServices, backendOptions: BackendOptions, _i18nextOptions: unknown): void;
33
+ read(language: string, namespace: string, callback: (error: Error | null, data: unknown) => void): void;
34
+ create(_languages: string[], _namespace: string, _key: string, _fallbackValue: string): void;
35
+ isLoading(language: string, namespace: string): boolean;
36
+ getLoadingResources(): Array<{
37
+ language: string;
38
+ namespace: string;
39
+ }>;
40
+ hasLoadingResources(): boolean;
41
+ private getCacheKey;
42
+ private loadResource;
43
+ private handlePromise;
44
+ private normalizeError;
45
+ private callSdk;
46
+ private extractFromCache;
47
+ private updateCache;
48
+ private formatResources;
49
+ private isObject;
50
+ private mergeWithExistingResources;
51
+ private triggerI18nextUpdate;
52
+ }
53
+ export {};
@@ -0,0 +1,9 @@
1
+ export declare const I18N_SDK_RESOURCES_LOADED_EVENT = "i18n-sdk-resources-loaded";
2
+ export interface I18nSdkResourcesLoadedEventDetail {
3
+ language: string;
4
+ namespace: string;
5
+ backendId?: string;
6
+ }
7
+ export declare function createI18nSdkBackendId(): string;
8
+ export declare function setI18nSdkBackendId(target: unknown, backendId: string): void;
9
+ export declare function getI18nSdkBackendId(target: unknown): string | undefined;
@@ -0,0 +1,11 @@
1
+ import type { LanguageDetectorOptions } from '../instance';
2
+ export declare const DEFAULT_I18NEXT_DETECTION_OPTIONS: {
3
+ caches: string[];
4
+ order: string[];
5
+ cookieMinutes: number;
6
+ lookupQuerystring: string;
7
+ lookupCookie: string;
8
+ lookupLocalStorage: string;
9
+ lookupHeader: string;
10
+ };
11
+ export declare function mergeDetectionOptions(cliOptions?: LanguageDetectorOptions, userOptions?: LanguageDetectorOptions, defaultOptions?: LanguageDetectorOptions): LanguageDetectorOptions;