@bleedingdev/modern-js-plugin-i18n 3.2.0-ultramodern.99 → 3.4.0-ultramodern.0
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 +17 -64
- package/dist/cjs/cli/locales.js +132 -0
- package/dist/cjs/runtime/I18nLink.js +17 -20
- package/dist/cjs/runtime/Link.js +264 -0
- package/dist/cjs/runtime/canonicalRoutes.js +18 -0
- package/dist/cjs/runtime/context.js +9 -5
- package/dist/cjs/runtime/hooks.js +9 -5
- package/dist/cjs/runtime/i18n/backend/config.js +9 -5
- package/dist/cjs/runtime/i18n/backend/defaults.js +20 -11
- package/dist/cjs/runtime/i18n/backend/defaults.node.js +79 -10
- 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 +9 -5
- 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 -13
- package/dist/cjs/runtime/i18n/react-i18next.js +12 -8
- package/dist/cjs/runtime/i18n/utils.js +9 -5
- package/dist/cjs/runtime/index.js +32 -5
- package/dist/cjs/runtime/localizedPaths.js +102 -0
- package/dist/cjs/runtime/routerAdapter.js +11 -7
- package/dist/cjs/runtime/utils.js +31 -17
- package/dist/cjs/server/index.js +10 -14
- package/dist/cjs/shared/deepMerge.js +12 -8
- package/dist/cjs/shared/detection.js +9 -5
- package/dist/cjs/shared/localisedUrls.js +148 -34
- package/dist/cjs/shared/utils.js +15 -11
- package/dist/esm/cli/index.mjs +8 -48
- package/dist/esm/cli/locales.mjs +80 -0
- package/dist/esm/runtime/I18nLink.mjs +7 -14
- package/dist/esm/runtime/Link.mjs +221 -0
- package/dist/esm/runtime/canonicalRoutes.mjs +0 -0
- package/dist/esm/runtime/i18n/backend/defaults.mjs +6 -2
- package/dist/esm/runtime/i18n/backend/defaults.node.mjs +56 -5
- package/dist/esm/runtime/index.mjs +4 -2
- package/dist/esm/runtime/localizedPaths.mjs +55 -0
- package/dist/esm/runtime/routerAdapter.mjs +3 -3
- package/dist/esm/runtime/utils.mjs +19 -12
- package/dist/esm/server/index.mjs +2 -10
- package/dist/esm/shared/localisedUrls.mjs +115 -23
- package/dist/esm-node/cli/index.mjs +8 -48
- package/dist/esm-node/cli/locales.mjs +81 -0
- package/dist/esm-node/runtime/I18nLink.mjs +7 -14
- package/dist/esm-node/runtime/Link.mjs +222 -0
- package/dist/esm-node/runtime/canonicalRoutes.mjs +1 -0
- package/dist/esm-node/runtime/i18n/backend/defaults.mjs +6 -2
- package/dist/esm-node/runtime/i18n/backend/defaults.node.mjs +56 -5
- package/dist/esm-node/runtime/index.mjs +4 -2
- package/dist/esm-node/runtime/localizedPaths.mjs +56 -0
- package/dist/esm-node/runtime/routerAdapter.mjs +3 -3
- package/dist/esm-node/runtime/utils.mjs +19 -12
- package/dist/esm-node/server/index.mjs +2 -10
- package/dist/esm-node/shared/localisedUrls.mjs +115 -23
- package/dist/types/cli/index.d.ts +1 -0
- package/dist/types/cli/locales.d.ts +17 -0
- package/dist/types/runtime/I18nLink.d.ts +4 -13
- package/dist/types/runtime/Link.d.ts +66 -0
- package/dist/types/runtime/canonicalRoutes.d.ts +60 -0
- package/dist/types/runtime/i18n/backend/defaults.d.ts +10 -7
- package/dist/types/runtime/i18n/backend/defaults.node.d.ts +13 -4
- package/dist/types/runtime/index.d.ts +5 -1
- package/dist/types/runtime/localizedPaths.d.ts +39 -0
- package/dist/types/runtime/types.d.ts +1 -1
- package/dist/types/runtime/utils.d.ts +13 -4
- package/dist/types/shared/localisedUrls.d.ts +23 -0
- package/dist/types/shared/type.d.ts +27 -5
- package/package.json +28 -25
- package/rstest.config.mts +7 -2
- package/src/cli/index.ts +25 -98
- package/src/cli/locales.ts +186 -0
- package/src/runtime/I18nLink.tsx +13 -44
- package/src/runtime/Link.tsx +430 -0
- package/src/runtime/canonicalRoutes.ts +93 -0
- package/src/runtime/i18n/backend/defaults.node.ts +112 -7
- package/src/runtime/i18n/backend/defaults.ts +20 -18
- package/src/runtime/index.tsx +24 -2
- package/src/runtime/localizedPaths.ts +107 -0
- package/src/runtime/routerAdapter.tsx +4 -5
- package/src/runtime/types.ts +1 -1
- package/src/runtime/utils.ts +33 -26
- package/src/server/index.ts +7 -17
- package/src/shared/localisedUrls.ts +256 -26
- package/src/shared/type.ts +27 -5
- package/tests/backendDefaults.test.ts +51 -0
- package/tests/i18nUtils.test.ts +10 -3
- package/tests/link.test.tsx +525 -0
- package/tests/linkTypes.test.ts +28 -0
- package/tests/localisedUrls.test.ts +224 -0
- package/tests/routerAdapter.test.tsx +86 -12
- package/tests/type-fixture/linkTypes.fixture.tsx +51 -0
- package/tests/type-fixture/tsconfig.json +15 -0
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import { jsx } from "react/jsx-runtime";
|
|
2
|
+
import { useMemo } from "react";
|
|
3
|
+
import { useModernI18n } from "./context.mjs";
|
|
4
|
+
import { canonicalPath } from "./localizedPaths.mjs";
|
|
5
|
+
import { useI18nRouterAdapter } from "./routerAdapter.mjs";
|
|
6
|
+
import { buildLocalizedUrl, splitUrlTarget } from "./utils.mjs";
|
|
7
|
+
const EXTERNAL_TARGET_RE = /^(?:[a-z][a-z0-9+.-]*:|\/\/)/i;
|
|
8
|
+
const warnedTargets = new Set();
|
|
9
|
+
const warnOnce = (key, message)=>{
|
|
10
|
+
if ('development' !== process.env.NODE_ENV || warnedTargets.has(key)) return;
|
|
11
|
+
warnedTargets.add(key);
|
|
12
|
+
console.warn(message);
|
|
13
|
+
};
|
|
14
|
+
const interpolateRouteParams = (pathname, params)=>{
|
|
15
|
+
if (!/[$:*{]/.test(pathname)) return pathname;
|
|
16
|
+
const resolveParam = (name)=>{
|
|
17
|
+
const value = params?.[name];
|
|
18
|
+
return void 0 === value ? void 0 : String(value);
|
|
19
|
+
};
|
|
20
|
+
const segments = pathname.split('/').map((segment)=>{
|
|
21
|
+
if (!segment) return segment;
|
|
22
|
+
if (segment.startsWith('{-$') && segment.endsWith('}')) {
|
|
23
|
+
const value = resolveParam(segment.slice(3, -1));
|
|
24
|
+
return void 0 === value ? null : encodeURIComponent(value);
|
|
25
|
+
}
|
|
26
|
+
if ('$' === segment || '*' === segment) {
|
|
27
|
+
const value = resolveParam('_splat') ?? resolveParam('*');
|
|
28
|
+
return void 0 === value ? null : value.split('/').map(encodeURIComponent).join('/');
|
|
29
|
+
}
|
|
30
|
+
if (segment.startsWith('$')) {
|
|
31
|
+
const value = resolveParam(segment.slice(1));
|
|
32
|
+
if (void 0 === value) {
|
|
33
|
+
warnOnce(`missing-param:${pathname}:${segment}`, `[plugin-i18n] <Link to="${pathname}"> is missing required param "${segment.slice(1)}".`);
|
|
34
|
+
return segment;
|
|
35
|
+
}
|
|
36
|
+
return encodeURIComponent(value);
|
|
37
|
+
}
|
|
38
|
+
if (segment.startsWith(':')) {
|
|
39
|
+
const optional = segment.endsWith('?');
|
|
40
|
+
const name = segment.slice(1, optional ? -1 : void 0);
|
|
41
|
+
const value = resolveParam(name);
|
|
42
|
+
if (void 0 === value) {
|
|
43
|
+
if (optional) return null;
|
|
44
|
+
warnOnce(`missing-param:${pathname}:${segment}`, `[plugin-i18n] <Link to="${pathname}"> is missing required param "${name}".`);
|
|
45
|
+
return segment;
|
|
46
|
+
}
|
|
47
|
+
return encodeURIComponent(value);
|
|
48
|
+
}
|
|
49
|
+
return segment;
|
|
50
|
+
}).filter((segment)=>null !== segment);
|
|
51
|
+
return segments.join('/') || '/';
|
|
52
|
+
};
|
|
53
|
+
const normalizeSearch = (search, searchFromTo)=>{
|
|
54
|
+
if (search && 'object' == typeof search) {
|
|
55
|
+
const entries = Object.entries(search).filter(([, value])=>null != value);
|
|
56
|
+
const searchObject = Object.fromEntries(entries.map(([key, value])=>[
|
|
57
|
+
key,
|
|
58
|
+
String(value)
|
|
59
|
+
]));
|
|
60
|
+
const params = new URLSearchParams(searchObject);
|
|
61
|
+
const serialized = params.toString();
|
|
62
|
+
return {
|
|
63
|
+
searchString: serialized ? `?${serialized}` : '',
|
|
64
|
+
searchObject
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
const raw = 'string' == typeof search && search ? search : searchFromTo;
|
|
68
|
+
if (!raw) return {
|
|
69
|
+
searchString: '',
|
|
70
|
+
searchObject: void 0
|
|
71
|
+
};
|
|
72
|
+
const searchString = raw.startsWith('?') ? raw : `?${raw}`;
|
|
73
|
+
const searchObject = {};
|
|
74
|
+
new URLSearchParams(searchString).forEach((value, key)=>{
|
|
75
|
+
searchObject[key] = value;
|
|
76
|
+
});
|
|
77
|
+
return {
|
|
78
|
+
searchString,
|
|
79
|
+
searchObject
|
|
80
|
+
};
|
|
81
|
+
};
|
|
82
|
+
const splitActiveProps = (active, activeProps)=>{
|
|
83
|
+
if (!active || !activeProps) return {};
|
|
84
|
+
return activeProps;
|
|
85
|
+
};
|
|
86
|
+
const mergeClassNames = (...values)=>{
|
|
87
|
+
const classNames = values.filter((value)=>'string' == typeof value && value.length > 0);
|
|
88
|
+
return classNames.length > 0 ? classNames.join(' ') : void 0;
|
|
89
|
+
};
|
|
90
|
+
const Link = (props)=>{
|
|
91
|
+
const { to, params, children, hash: hashProp, search: searchProp, hashScrollIntoView, activeOptions, activeProps, prefetch, preload, ...rest } = props;
|
|
92
|
+
const adapter = useI18nRouterAdapter();
|
|
93
|
+
const { language, supportedLanguages, localisedUrls } = useModernI18n();
|
|
94
|
+
const config = {
|
|
95
|
+
languages: supportedLanguages,
|
|
96
|
+
localisedUrls
|
|
97
|
+
};
|
|
98
|
+
const isExternal = EXTERNAL_TARGET_RE.test(to);
|
|
99
|
+
const isBareHash = to.startsWith('#');
|
|
100
|
+
const target = useMemo(()=>{
|
|
101
|
+
if (isExternal || isBareHash) return null;
|
|
102
|
+
const { pathname, search: searchFromTo, hash: hashFromTo } = splitUrlTarget(to);
|
|
103
|
+
const interpolated = interpolateRouteParams(pathname || '/', params);
|
|
104
|
+
const firstSegment = interpolated.split('/').filter(Boolean)[0];
|
|
105
|
+
if (firstSegment && supportedLanguages.includes(firstSegment)) warnOnce(`lang-prefix:${to}`, `[plugin-i18n] <Link to="${to}"> starts with a language prefix. Write language-agnostic canonical paths; the Link localizes them automatically.`);
|
|
106
|
+
const localizedPathname = buildLocalizedUrl(interpolated, language, supportedLanguages, localisedUrls);
|
|
107
|
+
const hash = hashProp ?? (hashFromTo ? hashFromTo.slice(1) : '');
|
|
108
|
+
const { searchString, searchObject } = normalizeSearch(searchProp, searchFromTo);
|
|
109
|
+
return {
|
|
110
|
+
canonicalPathname: interpolated,
|
|
111
|
+
localizedPathname,
|
|
112
|
+
hash,
|
|
113
|
+
searchString,
|
|
114
|
+
searchObject,
|
|
115
|
+
href: `${localizedPathname}${searchString}${hash ? `#${hash}` : ''}`
|
|
116
|
+
};
|
|
117
|
+
}, [
|
|
118
|
+
to,
|
|
119
|
+
params,
|
|
120
|
+
hashProp,
|
|
121
|
+
searchProp,
|
|
122
|
+
isExternal,
|
|
123
|
+
isBareHash,
|
|
124
|
+
language,
|
|
125
|
+
supportedLanguages,
|
|
126
|
+
localisedUrls
|
|
127
|
+
]);
|
|
128
|
+
const isActive = useMemo(()=>{
|
|
129
|
+
if (!target || !adapter.location) return false;
|
|
130
|
+
const current = canonicalPath(adapter.location.pathname, config);
|
|
131
|
+
const targetCanonical = canonicalPath(target.canonicalPathname, config);
|
|
132
|
+
const exact = activeOptions?.exact ?? '/' === targetCanonical;
|
|
133
|
+
if (current === targetCanonical) return true;
|
|
134
|
+
if (exact) return false;
|
|
135
|
+
return current.startsWith('/' === targetCanonical ? '/' : `${targetCanonical}/`);
|
|
136
|
+
}, [
|
|
137
|
+
target,
|
|
138
|
+
adapter.location,
|
|
139
|
+
activeOptions?.exact,
|
|
140
|
+
supportedLanguages,
|
|
141
|
+
localisedUrls
|
|
142
|
+
]);
|
|
143
|
+
const resolvedActiveProps = splitActiveProps(isActive, activeProps);
|
|
144
|
+
const activeAttributes = isActive ? {
|
|
145
|
+
'data-status': 'active',
|
|
146
|
+
'aria-current': rest['aria-current'] ?? resolvedActiveProps['aria-current'] ?? 'page'
|
|
147
|
+
} : {};
|
|
148
|
+
if (!target) {
|
|
149
|
+
const { replace: _replace, ...anchorProps } = rest;
|
|
150
|
+
return /*#__PURE__*/ jsx("a", {
|
|
151
|
+
href: to,
|
|
152
|
+
...anchorProps,
|
|
153
|
+
children: children
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
const { Link: RouterLink, hasRouter, framework } = adapter;
|
|
157
|
+
if (!hasRouter || !RouterLink) {
|
|
158
|
+
const { replace: _replace, ...anchorProps } = rest;
|
|
159
|
+
const { className: activeClassName, style: activeStyle, ...activeRest } = resolvedActiveProps;
|
|
160
|
+
return /*#__PURE__*/ jsx("a", {
|
|
161
|
+
href: target.href,
|
|
162
|
+
...anchorProps,
|
|
163
|
+
...activeRest,
|
|
164
|
+
...activeAttributes,
|
|
165
|
+
className: mergeClassNames(rest.className, activeClassName),
|
|
166
|
+
style: {
|
|
167
|
+
...rest.style,
|
|
168
|
+
...activeStyle
|
|
169
|
+
},
|
|
170
|
+
children: children
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
const { className: activeClassName, style: activeStyle, ...activeRest } = resolvedActiveProps;
|
|
174
|
+
const mergedClassName = mergeClassNames(rest.className, activeClassName);
|
|
175
|
+
const mergedStyle = {
|
|
176
|
+
...rest.style,
|
|
177
|
+
...activeStyle
|
|
178
|
+
};
|
|
179
|
+
if ('tanstack' === framework) {
|
|
180
|
+
const tanstackPreload = void 0 !== preload ? preload : void 0 === prefetch ? void 0 : 'none' === prefetch ? false : prefetch;
|
|
181
|
+
return /*#__PURE__*/ jsx(RouterLink, {
|
|
182
|
+
to: target.localizedPathname,
|
|
183
|
+
...target.searchObject ? {
|
|
184
|
+
search: target.searchObject
|
|
185
|
+
} : {},
|
|
186
|
+
...target.hash ? {
|
|
187
|
+
hash: target.hash
|
|
188
|
+
} : {},
|
|
189
|
+
...void 0 === hashScrollIntoView ? {} : {
|
|
190
|
+
hashScrollIntoView
|
|
191
|
+
},
|
|
192
|
+
...void 0 === tanstackPreload ? {} : {
|
|
193
|
+
preload: tanstackPreload
|
|
194
|
+
},
|
|
195
|
+
...rest,
|
|
196
|
+
...activeRest,
|
|
197
|
+
...activeAttributes,
|
|
198
|
+
className: mergedClassName,
|
|
199
|
+
style: mergedStyle,
|
|
200
|
+
children: children
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
return /*#__PURE__*/ jsx(RouterLink, {
|
|
204
|
+
to: target.href,
|
|
205
|
+
...void 0 === prefetch ? {} : {
|
|
206
|
+
prefetch
|
|
207
|
+
},
|
|
208
|
+
...void 0 === preload ? {} : {
|
|
209
|
+
preload
|
|
210
|
+
},
|
|
211
|
+
...rest,
|
|
212
|
+
...activeRest,
|
|
213
|
+
...activeAttributes,
|
|
214
|
+
className: mergedClassName,
|
|
215
|
+
style: mergedStyle,
|
|
216
|
+
children: children
|
|
217
|
+
});
|
|
218
|
+
};
|
|
219
|
+
const runtime_Link = Link;
|
|
220
|
+
export default runtime_Link;
|
|
221
|
+
export { Link, interpolateRouteParams };
|
|
File without changes
|
|
@@ -3,8 +3,6 @@ const DEFAULT_I18NEXT_BACKEND_OPTIONS = {
|
|
|
3
3
|
addPath: '/locales/{{lng}}/{{ns}}.json'
|
|
4
4
|
};
|
|
5
5
|
function convertPath(path) {
|
|
6
|
-
if (!path) return path;
|
|
7
|
-
if (path.startsWith('/')) return "u" < typeof window ? path : `${window.__assetPrefix__ || ''}${path}`;
|
|
8
6
|
return path;
|
|
9
7
|
}
|
|
10
8
|
function convertBackendOptions(options) {
|
|
@@ -12,6 +10,12 @@ function convertBackendOptions(options) {
|
|
|
12
10
|
const converted = {
|
|
13
11
|
...options
|
|
14
12
|
};
|
|
13
|
+
delete converted.serverLoadPath;
|
|
14
|
+
delete converted.serverAddPath;
|
|
15
|
+
delete converted.serverLoadPaths;
|
|
16
|
+
delete converted.serverAddPaths;
|
|
17
|
+
delete converted._detectedLoadPath;
|
|
18
|
+
delete converted._detectedAddPath;
|
|
15
19
|
if (converted.loadPath) converted.loadPath = convertPath(converted.loadPath);
|
|
16
20
|
if (converted.addPath) converted.addPath = convertPath(converted.addPath);
|
|
17
21
|
return converted;
|
|
@@ -1,19 +1,70 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import path_0 from "path";
|
|
3
|
+
const CONVENTIONAL_LOCALES_DIRS = [
|
|
4
|
+
'./locales',
|
|
5
|
+
'./config/public/locales'
|
|
6
|
+
];
|
|
7
|
+
const isDirectory = (dirPath)=>{
|
|
8
|
+
try {
|
|
9
|
+
return fs.statSync(dirPath).isDirectory();
|
|
10
|
+
} catch {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
const resolveDefaultLocalesDir = (cwd = process.cwd())=>{
|
|
15
|
+
for (const dir of CONVENTIONAL_LOCALES_DIRS)if (isDirectory(path_0.resolve(cwd, dir))) return dir;
|
|
16
|
+
return CONVENTIONAL_LOCALES_DIRS[0];
|
|
17
|
+
};
|
|
1
18
|
const DEFAULT_I18NEXT_BACKEND_OPTIONS = {
|
|
2
|
-
loadPath
|
|
3
|
-
|
|
19
|
+
get loadPath () {
|
|
20
|
+
return `${resolveDefaultLocalesDir()}/{{lng}}/{{ns}}.json`;
|
|
21
|
+
},
|
|
22
|
+
get addPath () {
|
|
23
|
+
return `${resolveDefaultLocalesDir()}/{{lng}}/{{ns}}.json`;
|
|
24
|
+
}
|
|
4
25
|
};
|
|
5
26
|
function convertPath(path) {
|
|
6
27
|
if (!path) return path;
|
|
7
28
|
if (path.startsWith('/')) return `.${path}`;
|
|
8
29
|
return path;
|
|
9
30
|
}
|
|
31
|
+
function shouldUseServerPath(currentPath, detectedPath) {
|
|
32
|
+
return !detectedPath || currentPath === detectedPath;
|
|
33
|
+
}
|
|
34
|
+
function getResourceBasePath(resourcePath) {
|
|
35
|
+
const markerIndex = resourcePath.indexOf('{{lng}}');
|
|
36
|
+
if (markerIndex < 0) return resourcePath;
|
|
37
|
+
return resourcePath.slice(0, markerIndex).replace(/[\\/]+$/, '');
|
|
38
|
+
}
|
|
39
|
+
function pathExists(resourcePath) {
|
|
40
|
+
try {
|
|
41
|
+
return fs.existsSync(getResourceBasePath(resourcePath));
|
|
42
|
+
} catch {
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
function getServerPath(pathCandidates, fallbackPath) {
|
|
47
|
+
const candidates = Array.from(new Set([
|
|
48
|
+
...pathCandidates || [],
|
|
49
|
+
fallbackPath
|
|
50
|
+
].filter(Boolean)));
|
|
51
|
+
return candidates.find(pathExists) || candidates[0];
|
|
52
|
+
}
|
|
10
53
|
function convertBackendOptions(options) {
|
|
11
54
|
if (!options) return options;
|
|
12
55
|
const converted = {
|
|
13
56
|
...options
|
|
14
57
|
};
|
|
15
|
-
if (converted.loadPath) converted.loadPath =
|
|
16
|
-
if (converted.
|
|
58
|
+
if ((converted.serverLoadPath || converted.serverLoadPaths) && shouldUseServerPath(converted.loadPath, converted._detectedLoadPath)) converted.loadPath = getServerPath(converted.serverLoadPaths, converted.serverLoadPath);
|
|
59
|
+
else if (converted.loadPath) converted.loadPath = convertPath(converted.loadPath);
|
|
60
|
+
if ((converted.serverAddPath || converted.serverAddPaths) && shouldUseServerPath(converted.addPath, converted._detectedAddPath)) converted.addPath = getServerPath(converted.serverAddPaths, converted.serverAddPath);
|
|
61
|
+
else if (converted.addPath) converted.addPath = convertPath(converted.addPath);
|
|
62
|
+
delete converted.serverLoadPath;
|
|
63
|
+
delete converted.serverAddPath;
|
|
64
|
+
delete converted.serverLoadPaths;
|
|
65
|
+
delete converted.serverAddPaths;
|
|
66
|
+
delete converted._detectedLoadPath;
|
|
67
|
+
delete converted._detectedAddPath;
|
|
17
68
|
return converted;
|
|
18
69
|
}
|
|
19
|
-
export { DEFAULT_I18NEXT_BACKEND_OPTIONS, convertBackendOptions };
|
|
70
|
+
export { DEFAULT_I18NEXT_BACKEND_OPTIONS, convertBackendOptions, resolveDefaultLocalesDir };
|
|
@@ -12,7 +12,7 @@ import { detectLanguageWithPriority, exportServerLngToWindow, mergeDetectionOpti
|
|
|
12
12
|
import { useI18nextLanguageDetector } from "./i18n/detection/middleware.mjs";
|
|
13
13
|
import { getI18nextInstanceForProvider } from "./i18n/instance.mjs";
|
|
14
14
|
import { changeI18nLanguage, ensureLanguageMatch, initializeI18nInstance, setupClonedInstance } from "./i18n/utils.mjs";
|
|
15
|
-
import { getPathname } from "./utils.mjs";
|
|
15
|
+
import { buildLocalizedUrl, getPathname, splitUrlTarget } from "./utils.mjs";
|
|
16
16
|
import "./types.mjs";
|
|
17
17
|
const i18nPlugin = (options)=>({
|
|
18
18
|
name: '@modern-js/plugin-i18n',
|
|
@@ -136,5 +136,7 @@ const i18nPlugin = (options)=>({
|
|
|
136
136
|
});
|
|
137
137
|
const runtime = i18nPlugin;
|
|
138
138
|
export { I18nLink } from "./I18nLink.mjs";
|
|
139
|
+
export { Link } from "./Link.mjs";
|
|
140
|
+
export { canonicalPath, localizePath, useLocalizedLocation, useLocalizedPaths } from "./localizedPaths.mjs";
|
|
139
141
|
export default runtime;
|
|
140
|
-
export { i18nPlugin, useModernI18n };
|
|
142
|
+
export { buildLocalizedUrl, i18nPlugin, splitUrlTarget, useModernI18n };
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { useMemo } from "react";
|
|
2
|
+
import { canonicalTargetPathname } from "../shared/localisedUrls.mjs";
|
|
3
|
+
import { useModernI18n } from "./context.mjs";
|
|
4
|
+
import { useI18nRouterAdapter } from "./routerAdapter.mjs";
|
|
5
|
+
import { buildLocalizedUrl, splitUrlTarget } from "./utils.mjs";
|
|
6
|
+
const localizePath = (pathname, language, config)=>buildLocalizedUrl(pathname, language, config.languages, config.localisedUrls);
|
|
7
|
+
const canonicalPath = (target, config)=>{
|
|
8
|
+
const { pathname, search, hash } = splitUrlTarget(target);
|
|
9
|
+
const resolvedPath = canonicalTargetPathname(pathname, config.languages, config.localisedUrls);
|
|
10
|
+
return `${resolvedPath}${search}${hash}`;
|
|
11
|
+
};
|
|
12
|
+
const useLocalizedPaths = ()=>{
|
|
13
|
+
const { supportedLanguages, localisedUrls } = useModernI18n();
|
|
14
|
+
return useMemo(()=>{
|
|
15
|
+
const config = {
|
|
16
|
+
languages: supportedLanguages,
|
|
17
|
+
localisedUrls
|
|
18
|
+
};
|
|
19
|
+
return {
|
|
20
|
+
localizePath: (pathname, language)=>localizePath(pathname, language, config),
|
|
21
|
+
canonicalPath: (pathname)=>canonicalPath(pathname, config)
|
|
22
|
+
};
|
|
23
|
+
}, [
|
|
24
|
+
supportedLanguages,
|
|
25
|
+
localisedUrls
|
|
26
|
+
]);
|
|
27
|
+
};
|
|
28
|
+
const useLocalizedLocation = ()=>{
|
|
29
|
+
const { language, supportedLanguages, localisedUrls } = useModernI18n();
|
|
30
|
+
const { location } = useI18nRouterAdapter();
|
|
31
|
+
const pathname = location?.pathname ?? '/';
|
|
32
|
+
const search = location?.search ?? '';
|
|
33
|
+
const hash = location?.hash ?? '';
|
|
34
|
+
return useMemo(()=>{
|
|
35
|
+
const config = {
|
|
36
|
+
languages: supportedLanguages,
|
|
37
|
+
localisedUrls
|
|
38
|
+
};
|
|
39
|
+
const alternates = {};
|
|
40
|
+
for (const supportedLanguage of supportedLanguages)alternates[supportedLanguage] = `${localizePath(pathname, supportedLanguage, config)}${search}${hash}`;
|
|
41
|
+
return {
|
|
42
|
+
language,
|
|
43
|
+
canonical: canonicalPath(pathname, config),
|
|
44
|
+
alternates
|
|
45
|
+
};
|
|
46
|
+
}, [
|
|
47
|
+
language,
|
|
48
|
+
supportedLanguages,
|
|
49
|
+
localisedUrls,
|
|
50
|
+
pathname,
|
|
51
|
+
search,
|
|
52
|
+
hash
|
|
53
|
+
]);
|
|
54
|
+
};
|
|
55
|
+
export { canonicalPath, localizePath, useLocalizedLocation, useLocalizedPaths };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { RuntimeContext, isBrowser } from "@modern-js/runtime";
|
|
2
|
-
import { InternalRuntimeContext } from "@modern-js/runtime/context";
|
|
2
|
+
import { InternalRuntimeContext, getRouterRuntimeState } from "@modern-js/runtime/context";
|
|
3
3
|
import { Link as router_Link, useInRouterContext, useLocation, useNavigate, useParams } from "@modern-js/runtime/router";
|
|
4
4
|
import { useCallback, useContext, useEffect, useState } from "react";
|
|
5
5
|
const normalizeUrlPart = (value, prefix)=>{
|
|
@@ -25,7 +25,7 @@ const getWindowLocation = ()=>{
|
|
|
25
25
|
};
|
|
26
26
|
};
|
|
27
27
|
const getRouterFramework = (runtimeContext, internalContext, inReactRouter)=>{
|
|
28
|
-
const framework = internalContext
|
|
28
|
+
const framework = getRouterRuntimeState(internalContext)?.framework || getRouterRuntimeState(runtimeContext)?.framework;
|
|
29
29
|
if (framework) return framework;
|
|
30
30
|
if (internalContext.router?.useRouter || runtimeContext.router?.useRouter) return 'tanstack';
|
|
31
31
|
if (internalContext.router?.useLocation || internalContext.router?.useHref || runtimeContext.router?.useLocation || runtimeContext.router?.useHref) return 'react-router';
|
|
@@ -33,7 +33,7 @@ const getRouterFramework = (runtimeContext, internalContext, inReactRouter)=>{
|
|
|
33
33
|
};
|
|
34
34
|
const getRouterInstance = (internalContext, contextRouter)=>{
|
|
35
35
|
if (contextRouter) return contextRouter;
|
|
36
|
-
const router = internalContext
|
|
36
|
+
const router = getRouterRuntimeState(internalContext)?.instance;
|
|
37
37
|
if (!router || 'object' != typeof router) return null;
|
|
38
38
|
return router;
|
|
39
39
|
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { isBrowser } from "@modern-js/runtime";
|
|
2
2
|
import { getGlobalBasename } from "@modern-js/runtime/context";
|
|
3
|
-
import {
|
|
3
|
+
import { localiseTargetPathname } from "../shared/localisedUrls.mjs";
|
|
4
4
|
const getPathname = (context)=>{
|
|
5
5
|
if (isBrowser()) return window.location.pathname;
|
|
6
6
|
return context.ssrContext?.request?.pathname || '/';
|
|
@@ -16,16 +16,23 @@ const getLanguageFromPath = (pathname, languages, fallbackLanguage)=>{
|
|
|
16
16
|
if (languages.includes(firstSegment)) return firstSegment;
|
|
17
17
|
return fallbackLanguage;
|
|
18
18
|
};
|
|
19
|
-
const
|
|
20
|
-
const
|
|
21
|
-
const
|
|
22
|
-
const
|
|
23
|
-
const
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
19
|
+
const splitUrlTarget = (target)=>{
|
|
20
|
+
const hashIndex = target.indexOf('#');
|
|
21
|
+
const hash = hashIndex >= 0 ? target.slice(hashIndex) : '';
|
|
22
|
+
const beforeHash = hashIndex >= 0 ? target.slice(0, hashIndex) : target;
|
|
23
|
+
const searchIndex = beforeHash.indexOf('?');
|
|
24
|
+
const search = searchIndex >= 0 ? beforeHash.slice(searchIndex) : '';
|
|
25
|
+
const pathname = searchIndex >= 0 ? beforeHash.slice(0, searchIndex) : beforeHash;
|
|
26
|
+
return {
|
|
27
|
+
pathname,
|
|
28
|
+
search,
|
|
29
|
+
hash
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
const buildLocalizedUrl = (target, language, languages, localisedUrls)=>{
|
|
33
|
+
const { pathname, search, hash } = splitUrlTarget(target);
|
|
34
|
+
const localizedPathname = localiseTargetPathname(pathname, language, languages, localisedUrls);
|
|
35
|
+
return `${localizedPathname}${search}${hash}`;
|
|
29
36
|
};
|
|
30
37
|
const detectLanguageFromPath = (pathname, languages, localePathRedirect)=>{
|
|
31
38
|
if (!localePathRedirect) return {
|
|
@@ -53,4 +60,4 @@ const shouldIgnoreRedirect = (pathname, languages, ignoreRedirectRoutes)=>{
|
|
|
53
60
|
if ('function' == typeof ignoreRedirectRoutes) return ignoreRedirectRoutes(normalizedPath);
|
|
54
61
|
return ignoreRedirectRoutes.some((pattern)=>normalizedPath === pattern || normalizedPath.startsWith(`${pattern}/`));
|
|
55
62
|
};
|
|
56
|
-
export { buildLocalizedUrl, detectLanguageFromPath, getEntryPath, getLanguageFromPath, getPathname, shouldIgnoreRedirect };
|
|
63
|
+
export { buildLocalizedUrl, detectLanguageFromPath, getEntryPath, getLanguageFromPath, getPathname, shouldIgnoreRedirect, splitUrlTarget };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { DEFAULT_I18NEXT_DETECTION_OPTIONS, mergeDetectionOptions } from "../runtime/i18n/detection/config.mjs";
|
|
2
|
-
import {
|
|
2
|
+
import { localiseTargetPathname, resolveLocalisedUrlsConfig } from "../shared/localisedUrls.mjs";
|
|
3
3
|
import { getLocaleDetectionOptions } from "../shared/utils.mjs";
|
|
4
4
|
import * as __rspack_external__modern_js_server_core_hono_a76ca254 from "@modern-js/server-core/hono";
|
|
5
5
|
const { languageDetector: languageDetector } = __rspack_external__modern_js_server_core_hono_a76ca254;
|
|
@@ -127,15 +127,7 @@ const buildLocalizedUrl = (req, urlPath, language, languages, localisedUrls)=>{
|
|
|
127
127
|
const pathname = url.pathname;
|
|
128
128
|
const basePath = urlPath.replace('/*', '');
|
|
129
129
|
const remainingPath = pathname.startsWith(basePath) ? pathname.slice(basePath.length) : pathname;
|
|
130
|
-
const
|
|
131
|
-
const localisedUrlsConfig = resolveLocalisedUrlsConfig(localisedUrls);
|
|
132
|
-
const pathWithoutLanguage = segments.length > 0 && languages.includes(segments[0]) ? `/${segments.slice(1).join('/')}` : remainingPath;
|
|
133
|
-
const resolvedPath = localisedUrlsConfig.enabled ? resolveLocalisedPath(pathWithoutLanguage, language, languages, localisedUrlsConfig.map) : pathWithoutLanguage;
|
|
134
|
-
const resolvedSegments = resolvedPath.split('/').filter(Boolean);
|
|
135
|
-
const newPathname = `/${[
|
|
136
|
-
language,
|
|
137
|
-
...resolvedSegments
|
|
138
|
-
].join('/')}`;
|
|
130
|
+
const newPathname = localiseTargetPathname(remainingPath, language, languages, localisedUrls);
|
|
139
131
|
const suffix = `${url.search}${url.hash}`;
|
|
140
132
|
const localizedUrl = '/' === basePath ? newPathname + suffix : basePath + newPathname + suffix;
|
|
141
133
|
return localizedUrl;
|