@bleedingdev/modern-js-plugin-i18n 3.2.0-ultramodern.12 → 3.2.0-ultramodern.120
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.
- package/README.md +221 -11
- package/dist/cjs/cli/index.js +32 -5
- package/dist/cjs/runtime/I18nLink.js +17 -28
- package/dist/cjs/runtime/Link.js +252 -0
- package/dist/cjs/runtime/canonicalRoutes.js +18 -0
- package/dist/cjs/runtime/context.js +41 -10
- package/dist/cjs/runtime/hooks.js +17 -10
- package/dist/cjs/runtime/i18n/backend/config.js +9 -5
- package/dist/cjs/runtime/i18n/backend/defaults.js +15 -10
- package/dist/cjs/runtime/i18n/backend/defaults.node.js +16 -11
- package/dist/cjs/runtime/i18n/backend/index.js +9 -5
- package/dist/cjs/runtime/i18n/backend/middleware.common.js +9 -5
- package/dist/cjs/runtime/i18n/backend/middleware.js +9 -5
- package/dist/cjs/runtime/i18n/backend/middleware.node.js +13 -9
- package/dist/cjs/runtime/i18n/backend/sdk-backend.js +9 -5
- package/dist/cjs/runtime/i18n/backend/sdk-event.js +16 -11
- package/dist/cjs/runtime/i18n/detection/config.js +9 -5
- package/dist/cjs/runtime/i18n/detection/index.js +9 -5
- package/dist/cjs/runtime/i18n/detection/middleware.js +9 -5
- package/dist/cjs/runtime/i18n/detection/middleware.node.js +9 -5
- package/dist/cjs/runtime/i18n/index.js +9 -5
- package/dist/cjs/runtime/i18n/instance.js +17 -37
- package/dist/cjs/runtime/i18n/react-i18next.js +53 -0
- package/dist/cjs/runtime/i18n/utils.js +9 -17
- package/dist/cjs/runtime/index.js +50 -15
- package/dist/cjs/runtime/localizedPaths.js +105 -0
- package/dist/cjs/runtime/routerAdapter.js +167 -0
- package/dist/cjs/runtime/utils.js +87 -97
- package/dist/cjs/server/index.js +69 -13
- package/dist/cjs/shared/deepMerge.js +12 -8
- package/dist/cjs/shared/detection.js +9 -5
- package/dist/cjs/shared/localisedUrls.js +271 -0
- package/dist/cjs/shared/utils.js +15 -11
- package/dist/esm/cli/index.mjs +23 -0
- package/dist/esm/runtime/I18nLink.mjs +7 -22
- package/dist/esm/runtime/Link.mjs +209 -0
- package/dist/esm/runtime/canonicalRoutes.mjs +0 -0
- package/dist/esm/runtime/context.mjs +34 -7
- package/dist/esm/runtime/hooks.mjs +9 -6
- package/dist/esm/runtime/i18n/backend/defaults.mjs +1 -1
- package/dist/esm/runtime/i18n/backend/defaults.node.mjs +2 -2
- package/dist/esm/runtime/i18n/backend/middleware.node.mjs +3 -3
- package/dist/esm/runtime/i18n/instance.mjs +1 -19
- package/dist/esm/runtime/i18n/react-i18next.mjs +15 -0
- package/dist/esm/runtime/i18n/utils.mjs +0 -12
- package/dist/esm/runtime/index.mjs +23 -13
- package/dist/esm/runtime/localizedPaths.mjs +58 -0
- package/dist/esm/runtime/routerAdapter.mjs +129 -0
- package/dist/esm/runtime/utils.mjs +25 -30
- package/dist/esm/server/index.mjs +53 -7
- package/dist/esm/shared/localisedUrls.mjs +212 -0
- package/dist/esm-node/cli/index.mjs +23 -0
- package/dist/esm-node/runtime/I18nLink.mjs +7 -22
- package/dist/esm-node/runtime/Link.mjs +210 -0
- package/dist/esm-node/runtime/canonicalRoutes.mjs +1 -0
- package/dist/esm-node/runtime/context.mjs +34 -7
- package/dist/esm-node/runtime/hooks.mjs +9 -6
- package/dist/esm-node/runtime/i18n/backend/defaults.mjs +1 -1
- package/dist/esm-node/runtime/i18n/backend/defaults.node.mjs +2 -2
- package/dist/esm-node/runtime/i18n/backend/middleware.node.mjs +3 -3
- package/dist/esm-node/runtime/i18n/instance.mjs +1 -19
- package/dist/esm-node/runtime/i18n/react-i18next.mjs +16 -0
- package/dist/esm-node/runtime/i18n/utils.mjs +0 -12
- package/dist/esm-node/runtime/index.mjs +23 -13
- package/dist/esm-node/runtime/localizedPaths.mjs +59 -0
- package/dist/esm-node/runtime/routerAdapter.mjs +130 -0
- package/dist/esm-node/runtime/utils.mjs +25 -30
- package/dist/esm-node/server/index.mjs +53 -7
- package/dist/esm-node/shared/localisedUrls.mjs +213 -0
- package/dist/types/cli/index.d.ts +1 -0
- package/dist/types/runtime/I18nLink.d.ts +6 -0
- package/dist/types/runtime/Link.d.ts +56 -0
- package/dist/types/runtime/canonicalRoutes.d.ts +60 -0
- package/dist/types/runtime/context.d.ts +3 -0
- package/dist/types/runtime/hooks.d.ts +4 -2
- package/dist/types/runtime/i18n/backend/middleware.node.d.ts +1 -1
- package/dist/types/runtime/i18n/instance.d.ts +4 -6
- package/dist/types/runtime/i18n/react-i18next.d.ts +7 -0
- package/dist/types/runtime/index.d.ts +6 -1
- package/dist/types/runtime/localizedPaths.d.ts +39 -0
- package/dist/types/runtime/routerAdapter.d.ts +26 -0
- package/dist/types/runtime/types.d.ts +1 -1
- package/dist/types/runtime/utils.d.ts +13 -9
- package/dist/types/server/index.d.ts +6 -0
- package/dist/types/shared/localisedUrls.d.ts +21 -0
- package/dist/types/shared/type.d.ts +12 -0
- package/package.json +24 -28
- package/rstest.config.mts +39 -0
- package/src/cli/index.ts +44 -1
- package/src/runtime/I18nLink.tsx +14 -51
- package/src/runtime/Link.tsx +414 -0
- package/src/runtime/canonicalRoutes.ts +93 -0
- package/src/runtime/context.tsx +45 -7
- package/src/runtime/hooks.ts +13 -4
- package/src/runtime/i18n/backend/defaults.node.ts +2 -2
- package/src/runtime/i18n/backend/defaults.ts +3 -1
- package/src/runtime/i18n/backend/middleware.node.ts +1 -1
- package/src/runtime/i18n/instance.ts +3 -30
- package/src/runtime/i18n/react-i18next.ts +25 -0
- package/src/runtime/i18n/utils.ts +4 -26
- package/src/runtime/index.tsx +47 -12
- package/src/runtime/localizedPaths.ts +118 -0
- package/src/runtime/routerAdapter.tsx +333 -0
- package/src/runtime/types.ts +1 -1
- package/src/runtime/utils.ts +44 -37
- package/src/server/index.ts +117 -10
- package/src/shared/localisedUrls.ts +453 -0
- package/src/shared/type.ts +12 -0
- package/tests/i18nUtils.test.ts +52 -0
- package/tests/link.test.tsx +475 -0
- package/tests/linkTypes.test.ts +28 -0
- package/tests/localisedUrls.test.ts +312 -0
- package/tests/routerAdapter.test.tsx +452 -0
- package/tests/type-fixture/linkTypes.fixture.tsx +51 -0
- package/tests/type-fixture/tsconfig.json +15 -0
- package/dist/esm/rslib-runtime.mjs +0 -18
- package/dist/esm-node/rslib-runtime.mjs +0 -19
|
@@ -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
|
-
|
|
102
|
-
|
|
103
|
-
const
|
|
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,213 @@
|
|
|
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, canonicalPath));
|
|
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, canonicalPath)=>{
|
|
120
|
+
const leadingLocaleParam = getLeadingLocaleParam(route.path);
|
|
121
|
+
const localisedPath = leadingLocaleParam ? normaliseRoutePath(`${leadingLocaleParam}/${path}`) : path;
|
|
122
|
+
const routeWithPath = {
|
|
123
|
+
...route,
|
|
124
|
+
path: localisedPath
|
|
125
|
+
};
|
|
126
|
+
routeWithPath.modernCanonicalPath = canonicalPath;
|
|
127
|
+
return 0 === index ? routeWithPath : suffixRouteIds(routeWithPath, legalRouteIdPart(localisedPath));
|
|
128
|
+
};
|
|
129
|
+
const applyLocalisedUrlsToRoutes = (routes, languages, localisedUrls)=>{
|
|
130
|
+
const rootLocalisedPaths = languages.reduce((acc, language)=>{
|
|
131
|
+
acc[language] = '/';
|
|
132
|
+
return acc;
|
|
133
|
+
}, {});
|
|
134
|
+
validateLocalisedUrls(routes, languages, localisedUrls);
|
|
135
|
+
return routes.flatMap((route)=>transformLocalisedRoute(route, '', rootLocalisedPaths, languages, localisedUrls));
|
|
136
|
+
};
|
|
137
|
+
const escapeRegExp = (value)=>value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
138
|
+
const getParamName = (segment)=>segment.slice(1).replace(/\?$/, '');
|
|
139
|
+
const compilePathPattern = (pattern)=>{
|
|
140
|
+
const names = [];
|
|
141
|
+
const segments = normalisePathPattern(pattern).split('/').filter(Boolean);
|
|
142
|
+
const source = segments.map((segment)=>{
|
|
143
|
+
if (segment.startsWith(':')) {
|
|
144
|
+
names.push(getParamName(segment));
|
|
145
|
+
const paramPattern = '([^/]+)';
|
|
146
|
+
return segment.endsWith('?') ? `(?:/${paramPattern})?` : `/${paramPattern}`;
|
|
147
|
+
}
|
|
148
|
+
if ('*' === segment) {
|
|
149
|
+
names.push('*');
|
|
150
|
+
return '/(.*)';
|
|
151
|
+
}
|
|
152
|
+
return `/${escapeRegExp(segment)}`;
|
|
153
|
+
}).join('');
|
|
154
|
+
return {
|
|
155
|
+
names,
|
|
156
|
+
regexp: new RegExp(`^${source || '/'}$`)
|
|
157
|
+
};
|
|
158
|
+
};
|
|
159
|
+
const matchPathPattern = (pathname, pattern)=>{
|
|
160
|
+
const { names, regexp } = compilePathPattern(pattern);
|
|
161
|
+
const match = regexp.exec(normalisePathPattern(pathname));
|
|
162
|
+
if (!match) return null;
|
|
163
|
+
return names.reduce((params, name, index)=>{
|
|
164
|
+
params[name] = decodeURIComponent(match[index + 1] || '');
|
|
165
|
+
return params;
|
|
166
|
+
}, {});
|
|
167
|
+
};
|
|
168
|
+
const buildPathFromPattern = (pattern, params)=>{
|
|
169
|
+
const segments = normalisePathPattern(pattern).split('/').filter(Boolean);
|
|
170
|
+
const path = segments.map((segment)=>{
|
|
171
|
+
if (segment.startsWith(':')) {
|
|
172
|
+
const param = params[getParamName(segment)];
|
|
173
|
+
return param ? encodeURIComponent(param) : '';
|
|
174
|
+
}
|
|
175
|
+
if ('*' === segment) return params['*'] || '';
|
|
176
|
+
return segment;
|
|
177
|
+
}).filter(Boolean).join('/');
|
|
178
|
+
return `/${path}`;
|
|
179
|
+
};
|
|
180
|
+
const resolveLocalisedPath = (pathname, targetLanguage, languages, localisedUrls)=>{
|
|
181
|
+
const normalizedPathname = normalisePathPattern(pathname);
|
|
182
|
+
for (const [canonicalPattern, localisedUrlEntry] of Object.entries(localisedUrls)){
|
|
183
|
+
const targetPattern = localisedUrlEntry[targetLanguage];
|
|
184
|
+
if (!targetPattern) continue;
|
|
185
|
+
const params = matchPathPattern(normalizedPathname, canonicalPattern);
|
|
186
|
+
if (params) return buildPathFromPattern(targetPattern, params);
|
|
187
|
+
}
|
|
188
|
+
for (const localisedUrlEntry of Object.values(localisedUrls)){
|
|
189
|
+
const targetPattern = localisedUrlEntry[targetLanguage];
|
|
190
|
+
if (targetPattern) for (const language of languages){
|
|
191
|
+
const sourcePattern = localisedUrlEntry[language];
|
|
192
|
+
if (!sourcePattern) continue;
|
|
193
|
+
const params = matchPathPattern(normalizedPathname, sourcePattern);
|
|
194
|
+
if (params) return buildPathFromPattern(targetPattern, params);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
return normalizedPathname;
|
|
198
|
+
};
|
|
199
|
+
const resolveCanonicalLocalisedPath = (pathname, languages, localisedUrls)=>{
|
|
200
|
+
const normalizedPathname = normalisePathPattern(pathname);
|
|
201
|
+
for (const [canonicalPattern, localisedUrlEntry] of Object.entries(localisedUrls)){
|
|
202
|
+
const canonicalParams = matchPathPattern(normalizedPathname, canonicalPattern);
|
|
203
|
+
if (canonicalParams) return buildPathFromPattern(canonicalPattern, canonicalParams);
|
|
204
|
+
for (const language of languages){
|
|
205
|
+
const sourcePattern = localisedUrlEntry[language];
|
|
206
|
+
if (!sourcePattern) continue;
|
|
207
|
+
const params = matchPathPattern(normalizedPathname, sourcePattern);
|
|
208
|
+
if (params) return buildPathFromPattern(canonicalPattern, params);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
return normalizedPathname;
|
|
212
|
+
};
|
|
213
|
+
export { applyLocalisedUrlsToRoutes, buildPathFromPattern, matchPathPattern, normalisePathPattern, resolveCanonicalLocalisedPath, 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,11 @@ export interface I18nLinkProps {
|
|
|
4
4
|
children: React.ReactNode;
|
|
5
5
|
[key: string]: any;
|
|
6
6
|
}
|
|
7
|
+
/**
|
|
8
|
+
* @deprecated Use {@link Link} from `@modern-js/plugin-i18n/runtime` instead.
|
|
9
|
+
* `Link` accepts the same language-agnostic `to` values and additionally
|
|
10
|
+
* supports `#hash`/`?query` targets, typed canonical routes, `params`
|
|
11
|
+
* interpolation and language-invariant active state.
|
|
12
|
+
*/
|
|
7
13
|
export declare const I18nLink: React.FC<I18nLinkProps>;
|
|
8
14
|
export default I18nLink;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type React from 'react';
|
|
2
|
+
import type { LinkParamsProp, LinkTargetPathname, ValidateLinkTo } from './canonicalRoutes';
|
|
3
|
+
export type LinkParams = Record<string, string | number | undefined>;
|
|
4
|
+
/**
|
|
5
|
+
* Interpolate `$param`, `:param`, optional (`{-$param}` / `:param?`) and splat
|
|
6
|
+
* (`$` / `*`) segments with concrete values before localization, so
|
|
7
|
+
* pattern-mapped slugs localize correctly.
|
|
8
|
+
*/
|
|
9
|
+
export declare const interpolateRouteParams: (pathname: string, params?: LinkParams) => string;
|
|
10
|
+
export interface LinkActiveOptions {
|
|
11
|
+
/**
|
|
12
|
+
* `true`: active only when the location matches the target exactly.
|
|
13
|
+
* `false`: also active when the location is nested under the target.
|
|
14
|
+
* Defaults to prefix matching, except for `/` which defaults to exact.
|
|
15
|
+
*/
|
|
16
|
+
exact?: boolean;
|
|
17
|
+
}
|
|
18
|
+
type AnchorRest = Omit<React.AnchorHTMLAttributes<HTMLAnchorElement>, 'href' | 'children'>;
|
|
19
|
+
export interface LinkBaseProps extends AnchorRest {
|
|
20
|
+
children?: React.ReactNode;
|
|
21
|
+
/** Hash fragment without the leading `#`. Overrides a `#hash` inside `to`. */
|
|
22
|
+
hash?: string;
|
|
23
|
+
/** Search params. Object form is passed natively to TanStack Link. */
|
|
24
|
+
search?: string | Record<string, unknown>;
|
|
25
|
+
hashScrollIntoView?: boolean | ScrollIntoViewOptions;
|
|
26
|
+
replace?: boolean;
|
|
27
|
+
prefetch?: 'intent' | 'render' | 'viewport' | 'none';
|
|
28
|
+
preload?: unknown;
|
|
29
|
+
activeOptions?: LinkActiveOptions;
|
|
30
|
+
/** Extra anchor props applied when the link is active. */
|
|
31
|
+
activeProps?: AnchorRest & Record<string, unknown>;
|
|
32
|
+
[key: string]: unknown;
|
|
33
|
+
}
|
|
34
|
+
export type LinkProps<TTo extends string = string> = LinkBaseProps & {
|
|
35
|
+
to: TTo;
|
|
36
|
+
} & ValidateLinkTo<TTo> & LinkParamsProp<LinkTargetPathname<TTo>>;
|
|
37
|
+
/**
|
|
38
|
+
* The standard UltraModern link: a vanilla link in every respect except that
|
|
39
|
+
* it localizes canonical, language-agnostic paths automatically.
|
|
40
|
+
*
|
|
41
|
+
* - `to` accepts canonical routes (`/talks/$slug`), optionally with `#hash`
|
|
42
|
+
* and `?query` suffixes; both survive localization.
|
|
43
|
+
* - External URLs and bare `#hash` targets render a plain `<a>`.
|
|
44
|
+
* - Active state is language-invariant: a canonical `to` is active when the
|
|
45
|
+
* current location matches any localized variant of that route.
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```tsx
|
|
49
|
+
* <Link to="/talks/$slug" params={{ slug: talk.slug }} hash="abstract" />
|
|
50
|
+
* <Link to="/platform" /> // -> /cs/platforma under cs
|
|
51
|
+
* <Link to="/#work-with-me" /> // cross-page hash, SPA navigation
|
|
52
|
+
* <Link to="https://ai.bleeding.dev" /> // external -> plain <a>
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
export declare const Link: <TTo extends string = string>(props: LinkProps<TTo>) => React.ReactElement;
|
|
56
|
+
export default Link;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Canonical (language-agnostic) route map.
|
|
3
|
+
*
|
|
4
|
+
* Empty by default; populated via declaration merging by the generated
|
|
5
|
+
* `register.gen.d.ts` that `@modern-js/plugin-tanstack` emits:
|
|
6
|
+
*
|
|
7
|
+
* ```ts
|
|
8
|
+
* declare module '@modern-js/plugin-i18n/runtime' {
|
|
9
|
+
* interface UltramodernCanonicalRoutes {
|
|
10
|
+
* '/': Record<string, never>;
|
|
11
|
+
* '/talks': Record<string, never>;
|
|
12
|
+
* '/talks/$slug': { slug: string };
|
|
13
|
+
* }
|
|
14
|
+
* }
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* Keys are canonical route patterns in TanStack notation (`$param`,
|
|
18
|
+
* `{-$param}`); values describe the route's path params.
|
|
19
|
+
*/
|
|
20
|
+
export interface UltramodernCanonicalRoutes {
|
|
21
|
+
}
|
|
22
|
+
export type CanonicalRoutePath = keyof UltramodernCanonicalRoutes & string;
|
|
23
|
+
type HasCanonicalRoutes = [keyof UltramodernCanonicalRoutes] extends [never] ? false : true;
|
|
24
|
+
/**
|
|
25
|
+
* Targets that bypass canonical-route validation: external URLs, same-page
|
|
26
|
+
* hash anchors, and canonical paths with a `?search` and/or `#hash` suffix
|
|
27
|
+
* (the pathname part of suffixed targets is still validated).
|
|
28
|
+
*/
|
|
29
|
+
type ExternalLinkTarget = `http://${string}` | `https://${string}` | `mailto:${string}` | `tel:${string}` | `//${string}`;
|
|
30
|
+
type SuffixedCanonicalTarget = `${CanonicalRoutePath}?${string}` | `${CanonicalRoutePath}#${string}`;
|
|
31
|
+
export type AllowedLinkTarget = CanonicalRoutePath | SuffixedCanonicalTarget | ExternalLinkTarget | `#${string}`;
|
|
32
|
+
/**
|
|
33
|
+
* Validates a literal `to` against the canonical route map. Computed strings
|
|
34
|
+
* (type `string`) always pass — the escape hatch for dynamic values. When no
|
|
35
|
+
* canonical map has been generated, everything passes.
|
|
36
|
+
*/
|
|
37
|
+
export type ValidateLinkTo<TTo extends string> = HasCanonicalRoutes extends false ? unknown : string extends TTo ? unknown : TTo extends AllowedLinkTarget ? unknown : {
|
|
38
|
+
to: {
|
|
39
|
+
error: 'Not a canonical route. Authors must write language-agnostic paths; see UltramodernCanonicalRoutes.';
|
|
40
|
+
received: TTo;
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
/** Strip `?search`/`#hash` suffixes from a link target type. */
|
|
44
|
+
export type LinkTargetPathname<TTo extends string> = TTo extends `${infer TPath}#${string}` ? TPath extends `${infer TPure}?${string}` ? TPure : TPath : TTo extends `${infer TPath}?${string}` ? TPath : TTo;
|
|
45
|
+
/**
|
|
46
|
+
* `params` prop contract for a canonical target: required when the route has
|
|
47
|
+
* required params, optional when all params are optional, forbidden when the
|
|
48
|
+
* route has none. Non-canonical (computed/external) targets accept a loose
|
|
49
|
+
* record.
|
|
50
|
+
*/
|
|
51
|
+
export type LinkParamsProp<TPath extends string> = TPath extends CanonicalRoutePath ? UltramodernCanonicalRoutes[TPath] extends Record<string, never> ? {
|
|
52
|
+
params?: undefined;
|
|
53
|
+
} : Record<string, never> extends UltramodernCanonicalRoutes[TPath] ? {
|
|
54
|
+
params?: UltramodernCanonicalRoutes[TPath];
|
|
55
|
+
} : {
|
|
56
|
+
params: UltramodernCanonicalRoutes[TPath];
|
|
57
|
+
} : {
|
|
58
|
+
params?: Record<string, string | number | undefined>;
|
|
59
|
+
};
|
|
60
|
+
export {};
|
|
@@ -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,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]:
|
|
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 {};
|
|
@@ -12,9 +12,14 @@ 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;
|
|
19
|
+
export type { AllowedLinkTarget, CanonicalRoutePath, UltramodernCanonicalRoutes, } from './canonicalRoutes';
|
|
18
20
|
export { useModernI18n } from './context';
|
|
19
|
-
export { I18nLink } from './I18nLink';
|
|
21
|
+
export { I18nLink, type I18nLinkProps } from './I18nLink';
|
|
22
|
+
export { Link, type LinkActiveOptions, type LinkBaseProps, type LinkParams, type LinkProps, } from './Link';
|
|
23
|
+
export { canonicalPath, type LocalizedPathsConfig, localizePath, type UseLocalizedLocationReturn, type UseLocalizedPathsReturn, useLocalizedLocation, useLocalizedPaths, } from './localizedPaths';
|
|
24
|
+
export { buildLocalizedUrl, splitUrlTarget } from './utils';
|
|
20
25
|
export default i18nPlugin;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { LocalisedUrlsOption } from '../shared/localisedUrls';
|
|
2
|
+
export interface LocalizedPathsConfig {
|
|
3
|
+
languages: string[];
|
|
4
|
+
localisedUrls?: LocalisedUrlsOption;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Localize a canonical, language-agnostic target for the given language:
|
|
8
|
+
* adds the language prefix and applies `localisedUrls` pattern mapping.
|
|
9
|
+
* `?search`/`#hash` suffixes are preserved verbatim.
|
|
10
|
+
*/
|
|
11
|
+
export declare const localizePath: (pathname: string, language: string, config: LocalizedPathsConfig) => string;
|
|
12
|
+
/**
|
|
13
|
+
* Reverse of {@link localizePath}: strip the language prefix and map localized
|
|
14
|
+
* slugs back to the canonical pattern's path. `?search`/`#hash` suffixes are
|
|
15
|
+
* preserved verbatim.
|
|
16
|
+
*/
|
|
17
|
+
export declare const canonicalPath: (target: string, config: LocalizedPathsConfig) => string;
|
|
18
|
+
export interface UseLocalizedPathsReturn {
|
|
19
|
+
localizePath: (pathname: string, language: string) => string;
|
|
20
|
+
canonicalPath: (pathname: string) => string;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Context-bound versions of {@link localizePath} and {@link canonicalPath} —
|
|
24
|
+
* the plugin configuration (languages, localisedUrls) is read from the i18n
|
|
25
|
+
* provider, so apps never copy pattern-matching helpers again.
|
|
26
|
+
*/
|
|
27
|
+
export declare const useLocalizedPaths: () => UseLocalizedPathsReturn;
|
|
28
|
+
export interface UseLocalizedLocationReturn {
|
|
29
|
+
language: string;
|
|
30
|
+
/** Canonical (language-agnostic) path of the current location. */
|
|
31
|
+
canonical: string;
|
|
32
|
+
/** Per-language hrefs for the current location, search+hash preserved. */
|
|
33
|
+
alternates: Record<string, string>;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Per-language hrefs for the current location — for hreflang `<link>` tags and
|
|
37
|
+
* language switchers. SSR-safe: the location comes from the router adapter.
|
|
38
|
+
*/
|
|
39
|
+
export declare const useLocalizedLocation: () => UseLocalizedLocationReturn;
|
|
@@ -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;
|