@bleedingdev/modern-js-plugin-i18n 3.5.0-ultramodern.3 → 3.5.0-ultramodern.31
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/dist/cjs/cli/index.js +2 -1
- package/dist/cjs/runtime/Link.js +8 -89
- package/dist/cjs/runtime/context.js +18 -82
- package/dist/cjs/runtime/contextHelpers.js +137 -0
- package/dist/cjs/runtime/core.js +29 -132
- package/dist/cjs/runtime/i18n/detection/cache.js +149 -0
- package/dist/cjs/runtime/i18n/detection/detector.js +100 -0
- package/dist/cjs/runtime/i18n/detection/index.js +8 -263
- package/dist/cjs/runtime/i18n/detection/initOptions.js +69 -0
- package/dist/cjs/runtime/i18n/detection/language.js +62 -0
- package/dist/cjs/runtime/i18n/detection/path.js +49 -0
- package/dist/cjs/runtime/i18n/detection/priority.js +66 -0
- package/dist/cjs/runtime/i18n/detection/ssr.js +70 -0
- package/dist/cjs/runtime/i18n/detection/types.js +18 -0
- package/dist/cjs/runtime/linkHelpers.js +148 -0
- package/dist/cjs/runtime/pluginSetup.js +102 -0
- package/dist/cjs/runtime/providerComposition.js +113 -0
- package/dist/cjs/runtime/reactI18next.js +43 -0
- package/dist/cjs/server/apiPrefix.js +72 -0
- package/dist/cjs/server/detectorOptions.js +97 -0
- package/dist/cjs/server/index.js +17 -144
- package/dist/cjs/server/redirectPolicy.js +102 -0
- package/dist/cjs/shared/localisedUrls/config.js +49 -0
- package/dist/cjs/shared/localisedUrls/index.js +75 -0
- package/dist/cjs/shared/localisedUrls/normalise.js +87 -0
- package/dist/cjs/shared/localisedUrls/pathname.js +65 -0
- package/dist/cjs/shared/localisedUrls/patterns.js +130 -0
- package/dist/cjs/shared/localisedUrls/resolve.js +91 -0
- package/dist/cjs/shared/localisedUrls/routes.js +129 -0
- package/dist/cjs/shared/localisedUrls/types.js +18 -0
- package/dist/cjs/shared/localisedUrls.js +13 -294
- package/dist/esm/cli/index.mjs +2 -1
- package/dist/esm/runtime/Link.mjs +1 -82
- package/dist/esm/runtime/context.mjs +18 -82
- package/dist/esm/runtime/contextHelpers.mjs +84 -0
- package/dist/esm/runtime/core.mjs +30 -133
- package/dist/esm/runtime/i18n/detection/cache.mjs +102 -0
- package/dist/esm/runtime/i18n/detection/detector.mjs +62 -0
- package/dist/esm/runtime/i18n/detection/index.mjs +4 -260
- package/dist/esm/runtime/i18n/detection/initOptions.mjs +28 -0
- package/dist/esm/runtime/i18n/detection/language.mjs +21 -0
- package/dist/esm/runtime/i18n/detection/path.mjs +11 -0
- package/dist/esm/runtime/i18n/detection/priority.mjs +28 -0
- package/dist/esm/runtime/i18n/detection/ssr.mjs +26 -0
- package/dist/esm/runtime/i18n/detection/types.mjs +0 -0
- package/dist/esm/runtime/linkHelpers.mjs +98 -0
- package/dist/esm/runtime/pluginSetup.mjs +64 -0
- package/dist/esm/runtime/providerComposition.mjs +75 -0
- package/dist/esm/runtime/reactI18next.mjs +5 -0
- package/dist/esm/server/apiPrefix.mjs +31 -0
- package/dist/esm/server/detectorOptions.mjs +59 -0
- package/dist/esm/server/index.mjs +6 -133
- package/dist/esm/server/redirectPolicy.mjs +52 -0
- package/dist/esm/shared/localisedUrls/config.mjs +11 -0
- package/dist/esm/shared/localisedUrls/index.mjs +6 -0
- package/dist/esm/shared/localisedUrls/normalise.mjs +37 -0
- package/dist/esm/shared/localisedUrls/pathname.mjs +24 -0
- package/dist/esm/shared/localisedUrls/patterns.mjs +86 -0
- package/dist/esm/shared/localisedUrls/resolve.mjs +50 -0
- package/dist/esm/shared/localisedUrls/routes.mjs +88 -0
- package/dist/esm/shared/localisedUrls/types.mjs +0 -0
- package/dist/esm/shared/localisedUrls.mjs +1 -283
- package/dist/esm-node/cli/index.mjs +2 -1
- package/dist/esm-node/runtime/Link.mjs +1 -82
- package/dist/esm-node/runtime/context.mjs +18 -82
- package/dist/esm-node/runtime/contextHelpers.mjs +85 -0
- package/dist/esm-node/runtime/core.mjs +30 -133
- package/dist/esm-node/runtime/i18n/detection/cache.mjs +103 -0
- package/dist/esm-node/runtime/i18n/detection/detector.mjs +63 -0
- package/dist/esm-node/runtime/i18n/detection/index.mjs +4 -260
- package/dist/esm-node/runtime/i18n/detection/initOptions.mjs +29 -0
- package/dist/esm-node/runtime/i18n/detection/language.mjs +22 -0
- package/dist/esm-node/runtime/i18n/detection/path.mjs +12 -0
- package/dist/esm-node/runtime/i18n/detection/priority.mjs +29 -0
- package/dist/esm-node/runtime/i18n/detection/ssr.mjs +27 -0
- package/dist/esm-node/runtime/i18n/detection/types.mjs +1 -0
- package/dist/esm-node/runtime/linkHelpers.mjs +99 -0
- package/dist/esm-node/runtime/pluginSetup.mjs +65 -0
- package/dist/esm-node/runtime/providerComposition.mjs +76 -0
- package/dist/esm-node/runtime/reactI18next.mjs +6 -0
- package/dist/esm-node/server/apiPrefix.mjs +32 -0
- package/dist/esm-node/server/detectorOptions.mjs +60 -0
- package/dist/esm-node/server/index.mjs +6 -133
- package/dist/esm-node/server/redirectPolicy.mjs +53 -0
- package/dist/esm-node/shared/localisedUrls/config.mjs +12 -0
- package/dist/esm-node/shared/localisedUrls/index.mjs +7 -0
- package/dist/esm-node/shared/localisedUrls/normalise.mjs +38 -0
- package/dist/esm-node/shared/localisedUrls/pathname.mjs +25 -0
- package/dist/esm-node/shared/localisedUrls/patterns.mjs +87 -0
- package/dist/esm-node/shared/localisedUrls/resolve.mjs +51 -0
- package/dist/esm-node/shared/localisedUrls/routes.mjs +89 -0
- package/dist/esm-node/shared/localisedUrls/types.mjs +1 -0
- package/dist/esm-node/shared/localisedUrls.mjs +1 -283
- package/dist/types/runtime/Link.d.ts +3 -7
- package/dist/types/runtime/context.d.ts +2 -2
- package/dist/types/runtime/contextHelpers.d.ts +28 -0
- package/dist/types/runtime/core.d.ts +3 -7
- package/dist/types/runtime/hooks.d.ts +5 -0
- package/dist/types/runtime/i18n/detection/cache.d.ts +14 -0
- package/dist/types/runtime/i18n/detection/detector.d.ts +8 -0
- package/dist/types/runtime/i18n/detection/index.d.ts +5 -50
- package/dist/types/runtime/i18n/detection/initOptions.d.ts +10 -0
- package/dist/types/runtime/i18n/detection/language.d.ts +11 -0
- package/dist/types/runtime/i18n/detection/path.d.ts +5 -0
- package/dist/types/runtime/i18n/detection/priority.d.ts +10 -0
- package/dist/types/runtime/i18n/detection/ssr.d.ts +9 -0
- package/dist/types/runtime/i18n/detection/types.d.ts +29 -0
- package/dist/types/runtime/i18n/react-i18next.d.ts +1 -6
- package/dist/types/runtime/linkHelpers.d.ts +17 -0
- package/dist/types/runtime/pluginSetup.d.ts +32 -0
- package/dist/types/runtime/providerComposition.d.ts +15 -0
- package/dist/types/runtime/reactI18next.d.ts +7 -0
- package/dist/types/runtime/utils.d.ts +7 -2
- package/dist/types/server/apiPrefix.d.ts +7 -0
- package/dist/types/server/detectorOptions.d.ts +21 -0
- package/dist/types/server/index.d.ts +1 -6
- package/dist/types/server/redirectPolicy.d.ts +16 -0
- package/dist/types/shared/localisedUrls/config.d.ts +9 -0
- package/dist/types/shared/localisedUrls/index.d.ts +7 -0
- package/dist/types/shared/localisedUrls/normalise.d.ts +10 -0
- package/dist/types/shared/localisedUrls/pathname.d.ts +3 -0
- package/dist/types/shared/localisedUrls/patterns.d.ts +5 -0
- package/dist/types/shared/localisedUrls/resolve.d.ts +8 -0
- package/dist/types/shared/localisedUrls/routes.d.ts +3 -0
- package/dist/types/shared/localisedUrls/types.d.ts +14 -0
- package/dist/types/shared/localisedUrls.d.ts +2 -36
- package/package.json +10 -10
- package/src/cli/index.ts +3 -2
- package/src/runtime/Link.tsx +11 -138
- package/src/runtime/context.tsx +42 -196
- package/src/runtime/contextHelpers.ts +232 -0
- package/src/runtime/core.tsx +43 -274
- package/src/runtime/hooks.ts +3 -0
- package/src/runtime/i18n/detection/cache.ts +175 -0
- package/src/runtime/i18n/detection/detector.ts +157 -0
- package/src/runtime/i18n/detection/index.ts +10 -641
- package/src/runtime/i18n/detection/initOptions.ts +74 -0
- package/src/runtime/i18n/detection/language.ts +64 -0
- package/src/runtime/i18n/detection/path.ts +41 -0
- package/src/runtime/i18n/detection/priority.ts +82 -0
- package/src/runtime/i18n/detection/ssr.ts +47 -0
- package/src/runtime/i18n/detection/types.ts +33 -0
- package/src/runtime/i18n/react-i18next.ts +1 -6
- package/src/runtime/linkHelpers.ts +174 -0
- package/src/runtime/pluginSetup.ts +189 -0
- package/src/runtime/providerComposition.tsx +142 -0
- package/src/runtime/reactI18next.ts +20 -0
- package/src/runtime/utils.ts +7 -5
- package/src/server/apiPrefix.ts +67 -0
- package/src/server/detectorOptions.ts +105 -0
- package/src/server/index.ts +14 -319
- package/src/server/redirectPolicy.ts +152 -0
- package/src/shared/localisedUrls/config.ts +18 -0
- package/src/shared/localisedUrls/index.ts +13 -0
- package/src/shared/localisedUrls/normalise.ts +67 -0
- package/src/shared/localisedUrls/pathname.ts +57 -0
- package/src/shared/localisedUrls/patterns.ts +160 -0
- package/src/shared/localisedUrls/resolve.ts +118 -0
- package/src/shared/localisedUrls/routes.ts +226 -0
- package/src/shared/localisedUrls/types.ts +18 -0
- package/src/shared/localisedUrls.ts +20 -623
- package/tests/link.test.tsx +111 -0
- package/tests/linkTypes.test.ts +15 -4
- package/tests/localisedUrls.test.ts +54 -2
- package/tests/reactI18nextRuntimeBoundary.test.ts +6 -3
- package/tests/routerAdapter.test.tsx +56 -0
- package/tests/type-fixture/fixture-globals.d.ts +11 -0
- package/tests/type-fixture/tsconfig.json +1 -0
package/src/server/index.ts
CHANGED
|
@@ -3,331 +3,26 @@ import * as honoPkg from '@modern-js/server-core/hono';
|
|
|
3
3
|
const { languageDetector } = honoPkg;
|
|
4
4
|
|
|
5
5
|
import type { Context, Next, ServerPlugin } from '@modern-js/server-runtime';
|
|
6
|
-
import {
|
|
7
|
-
DEFAULT_I18NEXT_DETECTION_OPTIONS,
|
|
8
|
-
mergeDetectionOptions,
|
|
9
|
-
} from '../runtime/i18n/detection/config.js';
|
|
10
|
-
import type { LanguageDetectorOptions } from '../runtime/i18n/instance';
|
|
11
|
-
import {
|
|
12
|
-
localiseTargetPathname,
|
|
13
|
-
resolveLocalisedUrlsConfig,
|
|
14
|
-
} from '../shared/localisedUrls.js';
|
|
6
|
+
import { resolveLocalisedUrlsConfig } from '../shared/localisedUrls.js';
|
|
15
7
|
import type { LocaleDetectionOptions } from '../shared/type';
|
|
16
8
|
import { getLocaleDetectionOptions } from '../shared/utils.js';
|
|
9
|
+
import { collectApiPrefixes, matchesApiPrefix } from './apiPrefix.js';
|
|
10
|
+
import { convertToHonoLanguageDetectorOptions } from './detectorOptions.js';
|
|
11
|
+
import {
|
|
12
|
+
buildLocalizedUrl,
|
|
13
|
+
createLocaleRedirectResponse,
|
|
14
|
+
getLanguageFromPath,
|
|
15
|
+
isStaticResourceRequest,
|
|
16
|
+
shouldIgnoreRedirect,
|
|
17
|
+
} from './redirectPolicy.js';
|
|
18
|
+
|
|
19
|
+
export { collectApiPrefixes, matchesApiPrefix } from './apiPrefix.js';
|
|
17
20
|
|
|
18
21
|
export interface I18nPluginOptions {
|
|
19
22
|
localeDetection: LocaleDetectionOptions;
|
|
20
23
|
staticRoutePrefixes: string[];
|
|
21
24
|
}
|
|
22
25
|
|
|
23
|
-
type ApiPrefixInput = string | string[] | undefined;
|
|
24
|
-
|
|
25
|
-
const normalizeApiPrefix = (prefix: string): string | null => {
|
|
26
|
-
const trimmedPrefix = prefix.trim();
|
|
27
|
-
if (!trimmedPrefix) {
|
|
28
|
-
return null;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
const prefixedPath = trimmedPrefix.startsWith('/')
|
|
32
|
-
? trimmedPrefix
|
|
33
|
-
: `/${trimmedPrefix}`;
|
|
34
|
-
const withoutWildcard = prefixedPath.replace(/\/\*$/, '');
|
|
35
|
-
const normalizedPrefix =
|
|
36
|
-
withoutWildcard.length > 1
|
|
37
|
-
? withoutWildcard.replace(/\/+$/, '')
|
|
38
|
-
: withoutWildcard;
|
|
39
|
-
|
|
40
|
-
return normalizedPrefix === '/' ? null : normalizedPrefix;
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
export const collectApiPrefixes = (
|
|
44
|
-
routes: Array<{ isApi?: boolean; urlPath?: string }>,
|
|
45
|
-
bffPrefix?: ApiPrefixInput,
|
|
46
|
-
): string[] => {
|
|
47
|
-
const prefixes = new Set<string>();
|
|
48
|
-
|
|
49
|
-
for (const route of routes) {
|
|
50
|
-
if (!route.isApi || !route.urlPath) {
|
|
51
|
-
continue;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
const normalizedPrefix = normalizeApiPrefix(route.urlPath);
|
|
55
|
-
if (normalizedPrefix) {
|
|
56
|
-
prefixes.add(normalizedPrefix);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
const bffPrefixes = Array.isArray(bffPrefix)
|
|
61
|
-
? bffPrefix
|
|
62
|
-
: bffPrefix
|
|
63
|
-
? [bffPrefix]
|
|
64
|
-
: [];
|
|
65
|
-
|
|
66
|
-
for (const prefix of bffPrefixes) {
|
|
67
|
-
const normalizedPrefix = normalizeApiPrefix(prefix);
|
|
68
|
-
if (normalizedPrefix) {
|
|
69
|
-
prefixes.add(normalizedPrefix);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
return [...prefixes];
|
|
74
|
-
};
|
|
75
|
-
|
|
76
|
-
export const matchesApiPrefix = (
|
|
77
|
-
pathname: string,
|
|
78
|
-
apiPrefixes: string[],
|
|
79
|
-
): boolean => {
|
|
80
|
-
const normalizedPathname = pathname.startsWith('/')
|
|
81
|
-
? pathname
|
|
82
|
-
: `/${pathname}`;
|
|
83
|
-
|
|
84
|
-
return apiPrefixes.some(
|
|
85
|
-
prefix =>
|
|
86
|
-
normalizedPathname === prefix ||
|
|
87
|
-
normalizedPathname.startsWith(`${prefix}/`),
|
|
88
|
-
);
|
|
89
|
-
};
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* Convert i18next detection options to hono languageDetector options
|
|
93
|
-
*/
|
|
94
|
-
const convertToHonoLanguageDetectorOptions = (
|
|
95
|
-
languages: string[],
|
|
96
|
-
fallbackLanguage: string,
|
|
97
|
-
detectionOptions?: LanguageDetectorOptions,
|
|
98
|
-
) => {
|
|
99
|
-
// Merge user detection options with defaults
|
|
100
|
-
const mergedDetection = detectionOptions
|
|
101
|
-
? mergeDetectionOptions(detectionOptions)
|
|
102
|
-
: DEFAULT_I18NEXT_DETECTION_OPTIONS;
|
|
103
|
-
|
|
104
|
-
// Get detection order, excluding 'path' and browser-only detectors
|
|
105
|
-
const order = (mergedDetection.order || []).filter(
|
|
106
|
-
(item: string) =>
|
|
107
|
-
!['path', 'localStorage', 'navigator', 'htmlTag', 'subdomain'].includes(
|
|
108
|
-
item,
|
|
109
|
-
),
|
|
110
|
-
);
|
|
111
|
-
|
|
112
|
-
// If no order specified, use default server-side order
|
|
113
|
-
const detectionOrder =
|
|
114
|
-
order.length > 0 ? order : ['querystring', 'cookie', 'header'];
|
|
115
|
-
|
|
116
|
-
// Map i18next order to hono order
|
|
117
|
-
const honoOrder = detectionOrder.map(item => {
|
|
118
|
-
// Map 'querystring' to 'querystring', 'cookie' to 'cookie', 'header' to 'header'
|
|
119
|
-
if (item === 'querystring') return 'querystring';
|
|
120
|
-
if (item === 'cookie') return 'cookie';
|
|
121
|
-
if (item === 'header') return 'header';
|
|
122
|
-
return item;
|
|
123
|
-
}) as ('querystring' | 'cookie' | 'header' | 'path')[];
|
|
124
|
-
|
|
125
|
-
// Determine caches option
|
|
126
|
-
// hono languageDetector expects: false | "cookie"[] | undefined
|
|
127
|
-
const caches: false | ['cookie'] | undefined =
|
|
128
|
-
mergedDetection.caches === false
|
|
129
|
-
? false
|
|
130
|
-
: Array.isArray(mergedDetection.caches) &&
|
|
131
|
-
!mergedDetection.caches.includes('cookie')
|
|
132
|
-
? false
|
|
133
|
-
: (['cookie'] as ['cookie']);
|
|
134
|
-
|
|
135
|
-
const cookieMinutes = (mergedDetection as Record<string, unknown>)
|
|
136
|
-
.cookieMinutes;
|
|
137
|
-
const cookieMaxAge =
|
|
138
|
-
typeof cookieMinutes === 'number' && Number.isFinite(cookieMinutes)
|
|
139
|
-
? Math.max(0, Math.floor(cookieMinutes * 60))
|
|
140
|
-
: DEFAULT_I18NEXT_DETECTION_OPTIONS.cookieMinutes * 60;
|
|
141
|
-
|
|
142
|
-
const cookieDomain = (mergedDetection as Record<string, unknown>)
|
|
143
|
-
.cookieDomain;
|
|
144
|
-
const cookieSecure = (mergedDetection as Record<string, unknown>)
|
|
145
|
-
.cookieSecure;
|
|
146
|
-
const cookieHttpOnly = (mergedDetection as Record<string, unknown>)
|
|
147
|
-
.cookieHttpOnly;
|
|
148
|
-
const cookieSameSite = (mergedDetection as Record<string, unknown>)
|
|
149
|
-
.cookieSameSite;
|
|
150
|
-
|
|
151
|
-
const normalizedCookieDomain =
|
|
152
|
-
typeof cookieDomain === 'string' ? cookieDomain : undefined;
|
|
153
|
-
|
|
154
|
-
// Keep cookie defaults aligned with i18next language detector behavior:
|
|
155
|
-
// language cookie should be readable from browser-side detector.
|
|
156
|
-
const cookieOptions = {
|
|
157
|
-
maxAge: cookieMaxAge,
|
|
158
|
-
sameSite:
|
|
159
|
-
cookieSameSite === 'None' || cookieSameSite === 'none'
|
|
160
|
-
? ('None' as const)
|
|
161
|
-
: cookieSameSite === 'Lax' || cookieSameSite === 'lax'
|
|
162
|
-
? ('Lax' as const)
|
|
163
|
-
: ('Strict' as const),
|
|
164
|
-
secure: typeof cookieSecure === 'boolean' ? cookieSecure : false,
|
|
165
|
-
httpOnly: typeof cookieHttpOnly === 'boolean' ? cookieHttpOnly : false,
|
|
166
|
-
...(normalizedCookieDomain ? { domain: normalizedCookieDomain } : {}),
|
|
167
|
-
};
|
|
168
|
-
|
|
169
|
-
return {
|
|
170
|
-
supportedLanguages: languages.length > 0 ? languages : [fallbackLanguage],
|
|
171
|
-
fallbackLanguage,
|
|
172
|
-
order: honoOrder,
|
|
173
|
-
lookupQueryString:
|
|
174
|
-
mergedDetection.lookupQuerystring ||
|
|
175
|
-
DEFAULT_I18NEXT_DETECTION_OPTIONS.lookupQuerystring ||
|
|
176
|
-
'lng',
|
|
177
|
-
lookupCookie:
|
|
178
|
-
mergedDetection.lookupCookie ||
|
|
179
|
-
DEFAULT_I18NEXT_DETECTION_OPTIONS.lookupCookie ||
|
|
180
|
-
'i18next',
|
|
181
|
-
lookupFromHeaderKey:
|
|
182
|
-
mergedDetection.lookupHeader ||
|
|
183
|
-
DEFAULT_I18NEXT_DETECTION_OPTIONS.lookupHeader ||
|
|
184
|
-
'accept-language',
|
|
185
|
-
...(caches !== undefined && { caches }),
|
|
186
|
-
...(caches !== false && { cookieOptions }),
|
|
187
|
-
ignoreCase: true,
|
|
188
|
-
};
|
|
189
|
-
};
|
|
190
|
-
|
|
191
|
-
/**
|
|
192
|
-
* Check if the given pathname should ignore automatic locale redirect
|
|
193
|
-
*/
|
|
194
|
-
const shouldIgnoreRedirect = (
|
|
195
|
-
pathname: string,
|
|
196
|
-
urlPath: string,
|
|
197
|
-
ignoreRedirectRoutes?: string[] | ((pathname: string) => boolean),
|
|
198
|
-
): boolean => {
|
|
199
|
-
if (!ignoreRedirectRoutes) {
|
|
200
|
-
return false;
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
// Remove urlPath prefix to get remaining path for matching
|
|
204
|
-
const basePath = urlPath.replace('/*', '');
|
|
205
|
-
const remainingPath = pathname.startsWith(basePath)
|
|
206
|
-
? pathname.slice(basePath.length)
|
|
207
|
-
: pathname;
|
|
208
|
-
|
|
209
|
-
// Normalize path (ensure it starts with /)
|
|
210
|
-
const normalizedPath = remainingPath.startsWith('/')
|
|
211
|
-
? remainingPath
|
|
212
|
-
: `/${remainingPath}`;
|
|
213
|
-
|
|
214
|
-
if (typeof ignoreRedirectRoutes === 'function') {
|
|
215
|
-
return ignoreRedirectRoutes(normalizedPath);
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
// Check if pathname matches any of the ignore patterns
|
|
219
|
-
return ignoreRedirectRoutes.some(pattern => {
|
|
220
|
-
// Support both exact match and prefix match
|
|
221
|
-
return (
|
|
222
|
-
normalizedPath === pattern || normalizedPath.startsWith(`${pattern}/`)
|
|
223
|
-
);
|
|
224
|
-
});
|
|
225
|
-
};
|
|
226
|
-
|
|
227
|
-
/**
|
|
228
|
-
* Check if the given pathname is a static resource request
|
|
229
|
-
* This includes:
|
|
230
|
-
* 1. Paths matching staticRoutePrefixes (from public directories)
|
|
231
|
-
* 2. Standard static resource paths like /static/, /upload/
|
|
232
|
-
* 3. Paths with language prefix like /en/static/, /zh/static/
|
|
233
|
-
*/
|
|
234
|
-
const isStaticResourceRequest = (
|
|
235
|
-
pathname: string,
|
|
236
|
-
staticRoutePrefixes: string[],
|
|
237
|
-
languages: string[] = [],
|
|
238
|
-
): boolean => {
|
|
239
|
-
// Check against staticRoutePrefixes (from public directories)
|
|
240
|
-
if (
|
|
241
|
-
staticRoutePrefixes.some(
|
|
242
|
-
prefix => pathname.startsWith(`${prefix}/`) || pathname === prefix,
|
|
243
|
-
)
|
|
244
|
-
) {
|
|
245
|
-
return true;
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
// Check standard static resource paths
|
|
249
|
-
const standardStaticPrefixes = ['/static/', '/upload/'];
|
|
250
|
-
if (standardStaticPrefixes.some(prefix => pathname.startsWith(prefix))) {
|
|
251
|
-
return true;
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
// Check paths with language prefix (e.g., /en/static/, /zh/static/)
|
|
255
|
-
// Remove language prefix if present and check again
|
|
256
|
-
const pathSegments = pathname.split('/').filter(Boolean);
|
|
257
|
-
if (pathSegments.length > 0 && languages.includes(pathSegments[0])) {
|
|
258
|
-
const pathWithoutLang = '/' + pathSegments.slice(1).join('/');
|
|
259
|
-
if (
|
|
260
|
-
standardStaticPrefixes.some(prefix =>
|
|
261
|
-
pathWithoutLang.startsWith(prefix),
|
|
262
|
-
) ||
|
|
263
|
-
staticRoutePrefixes.some(
|
|
264
|
-
prefix =>
|
|
265
|
-
pathWithoutLang.startsWith(`${prefix}/`) ||
|
|
266
|
-
pathWithoutLang === prefix,
|
|
267
|
-
)
|
|
268
|
-
) {
|
|
269
|
-
return true;
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
return false;
|
|
274
|
-
};
|
|
275
|
-
|
|
276
|
-
const getLanguageFromPath = (
|
|
277
|
-
req: any,
|
|
278
|
-
urlPath: string,
|
|
279
|
-
languages: string[],
|
|
280
|
-
): string | null => {
|
|
281
|
-
const url = new URL(req.url, `http://${req.header().host}`);
|
|
282
|
-
const pathname = url.pathname;
|
|
283
|
-
|
|
284
|
-
// Remove urlPath prefix to get remaining path
|
|
285
|
-
// urlPath format is /lang/*, need to remove /lang part
|
|
286
|
-
const basePath = urlPath.replace('/*', '');
|
|
287
|
-
const remainingPath = pathname.startsWith(basePath)
|
|
288
|
-
? pathname.slice(basePath.length)
|
|
289
|
-
: pathname;
|
|
290
|
-
|
|
291
|
-
const segments = remainingPath.split('/').filter(Boolean);
|
|
292
|
-
const firstSegment = segments[0];
|
|
293
|
-
|
|
294
|
-
if (languages.includes(firstSegment)) {
|
|
295
|
-
return firstSegment;
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
return null;
|
|
299
|
-
};
|
|
300
|
-
|
|
301
|
-
const buildLocalizedUrl = (
|
|
302
|
-
req: any,
|
|
303
|
-
urlPath: string,
|
|
304
|
-
language: string,
|
|
305
|
-
languages: string[],
|
|
306
|
-
localisedUrls?: LocaleDetectionOptions['localisedUrls'],
|
|
307
|
-
): string => {
|
|
308
|
-
const url = new URL(req.url);
|
|
309
|
-
const pathname = url.pathname;
|
|
310
|
-
|
|
311
|
-
// Remove urlPath prefix to get remaining path
|
|
312
|
-
const basePath = urlPath.replace('/*', '');
|
|
313
|
-
const remainingPath = pathname.startsWith(basePath)
|
|
314
|
-
? pathname.slice(basePath.length)
|
|
315
|
-
: pathname;
|
|
316
|
-
|
|
317
|
-
const newPathname = localiseTargetPathname(
|
|
318
|
-
remainingPath,
|
|
319
|
-
language,
|
|
320
|
-
languages,
|
|
321
|
-
localisedUrls,
|
|
322
|
-
);
|
|
323
|
-
// Handle root path case to avoid double slashes like //en
|
|
324
|
-
const suffix = `${url.search}${url.hash}`;
|
|
325
|
-
const localizedUrl =
|
|
326
|
-
basePath === '/' ? newPathname + suffix : basePath + newPathname + suffix;
|
|
327
|
-
|
|
328
|
-
return localizedUrl;
|
|
329
|
-
};
|
|
330
|
-
|
|
331
26
|
export const i18nServerPlugin = (options: I18nPluginOptions): ServerPlugin => ({
|
|
332
27
|
name: '@modern-js/plugin-i18n/server',
|
|
333
28
|
setup: api => {
|
|
@@ -475,7 +170,7 @@ export const i18nServerPlugin = (options: I18nPluginOptions): ServerPlugin => ({
|
|
|
475
170
|
languages,
|
|
476
171
|
localisedUrls,
|
|
477
172
|
);
|
|
478
|
-
return
|
|
173
|
+
return createLocaleRedirectResponse(localizedUrl);
|
|
479
174
|
}
|
|
480
175
|
const localisedUrlsConfig =
|
|
481
176
|
resolveLocalisedUrlsConfig(localisedUrls);
|
|
@@ -488,7 +183,7 @@ export const i18nServerPlugin = (options: I18nPluginOptions): ServerPlugin => ({
|
|
|
488
183
|
localisedUrls,
|
|
489
184
|
);
|
|
490
185
|
if (expectedUrl !== `${pathname}${url.search}${url.hash}`) {
|
|
491
|
-
return
|
|
186
|
+
return createLocaleRedirectResponse(expectedUrl);
|
|
492
187
|
}
|
|
493
188
|
}
|
|
494
189
|
await next();
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { localiseTargetPathname } from '../shared/localisedUrls.js';
|
|
2
|
+
import type { LocaleDetectionOptions } from '../shared/type';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Check if the given pathname should ignore automatic locale redirect
|
|
6
|
+
*/
|
|
7
|
+
export const shouldIgnoreRedirect = (
|
|
8
|
+
pathname: string,
|
|
9
|
+
urlPath: string,
|
|
10
|
+
ignoreRedirectRoutes?: string[] | ((pathname: string) => boolean),
|
|
11
|
+
): boolean => {
|
|
12
|
+
if (!ignoreRedirectRoutes) {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// Remove urlPath prefix to get remaining path for matching
|
|
17
|
+
const basePath = urlPath.replace('/*', '');
|
|
18
|
+
const remainingPath = pathname.startsWith(basePath)
|
|
19
|
+
? pathname.slice(basePath.length)
|
|
20
|
+
: pathname;
|
|
21
|
+
|
|
22
|
+
// Normalize path (ensure it starts with /)
|
|
23
|
+
const normalizedPath = remainingPath.startsWith('/')
|
|
24
|
+
? remainingPath
|
|
25
|
+
: `/${remainingPath}`;
|
|
26
|
+
|
|
27
|
+
if (typeof ignoreRedirectRoutes === 'function') {
|
|
28
|
+
return ignoreRedirectRoutes(normalizedPath);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Check if pathname matches any of the ignore patterns
|
|
32
|
+
return ignoreRedirectRoutes.some(pattern => {
|
|
33
|
+
// Support both exact match and prefix match
|
|
34
|
+
return (
|
|
35
|
+
normalizedPath === pattern || normalizedPath.startsWith(`${pattern}/`)
|
|
36
|
+
);
|
|
37
|
+
});
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Check if the given pathname is a static resource request
|
|
42
|
+
* This includes:
|
|
43
|
+
* 1. Paths matching staticRoutePrefixes (from public directories)
|
|
44
|
+
* 2. Standard static resource paths like /static/, /upload/
|
|
45
|
+
* 3. Paths with language prefix like /en/static/, /zh/static/
|
|
46
|
+
*/
|
|
47
|
+
export const isStaticResourceRequest = (
|
|
48
|
+
pathname: string,
|
|
49
|
+
staticRoutePrefixes: string[],
|
|
50
|
+
languages: string[] = [],
|
|
51
|
+
): boolean => {
|
|
52
|
+
// Check against staticRoutePrefixes (from public directories)
|
|
53
|
+
if (
|
|
54
|
+
staticRoutePrefixes.some(
|
|
55
|
+
prefix => pathname.startsWith(`${prefix}/`) || pathname === prefix,
|
|
56
|
+
)
|
|
57
|
+
) {
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Check standard static resource paths
|
|
62
|
+
const standardStaticPrefixes = ['/static/', '/upload/'];
|
|
63
|
+
if (standardStaticPrefixes.some(prefix => pathname.startsWith(prefix))) {
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Check paths with language prefix (e.g., /en/static/, /zh/static/)
|
|
68
|
+
// Remove language prefix if present and check again
|
|
69
|
+
const pathSegments = pathname.split('/').filter(Boolean);
|
|
70
|
+
if (pathSegments.length > 0 && languages.includes(pathSegments[0])) {
|
|
71
|
+
const pathWithoutLang = '/' + pathSegments.slice(1).join('/');
|
|
72
|
+
if (
|
|
73
|
+
standardStaticPrefixes.some(prefix =>
|
|
74
|
+
pathWithoutLang.startsWith(prefix),
|
|
75
|
+
) ||
|
|
76
|
+
staticRoutePrefixes.some(
|
|
77
|
+
prefix =>
|
|
78
|
+
pathWithoutLang.startsWith(`${prefix}/`) ||
|
|
79
|
+
pathWithoutLang === prefix,
|
|
80
|
+
)
|
|
81
|
+
) {
|
|
82
|
+
return true;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return false;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
export const getLanguageFromPath = (
|
|
90
|
+
req: any,
|
|
91
|
+
urlPath: string,
|
|
92
|
+
languages: string[],
|
|
93
|
+
): string | null => {
|
|
94
|
+
const url = new URL(req.url, `http://${req.header().host}`);
|
|
95
|
+
const pathname = url.pathname;
|
|
96
|
+
|
|
97
|
+
// Remove urlPath prefix to get remaining path
|
|
98
|
+
// urlPath format is /lang/*, need to remove /lang part
|
|
99
|
+
const basePath = urlPath.replace('/*', '');
|
|
100
|
+
const remainingPath = pathname.startsWith(basePath)
|
|
101
|
+
? pathname.slice(basePath.length)
|
|
102
|
+
: pathname;
|
|
103
|
+
|
|
104
|
+
const segments = remainingPath.split('/').filter(Boolean);
|
|
105
|
+
const firstSegment = segments[0];
|
|
106
|
+
|
|
107
|
+
if (languages.includes(firstSegment)) {
|
|
108
|
+
return firstSegment;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return null;
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
export const buildLocalizedUrl = (
|
|
115
|
+
req: any,
|
|
116
|
+
urlPath: string,
|
|
117
|
+
language: string,
|
|
118
|
+
languages: string[],
|
|
119
|
+
localisedUrls?: LocaleDetectionOptions['localisedUrls'],
|
|
120
|
+
): string => {
|
|
121
|
+
const url = new URL(req.url);
|
|
122
|
+
const pathname = url.pathname;
|
|
123
|
+
|
|
124
|
+
// Remove urlPath prefix to get remaining path
|
|
125
|
+
const basePath = urlPath.replace('/*', '');
|
|
126
|
+
const remainingPath = pathname.startsWith(basePath)
|
|
127
|
+
? pathname.slice(basePath.length)
|
|
128
|
+
: pathname;
|
|
129
|
+
|
|
130
|
+
const newPathname = localiseTargetPathname(
|
|
131
|
+
remainingPath,
|
|
132
|
+
language,
|
|
133
|
+
languages,
|
|
134
|
+
localisedUrls,
|
|
135
|
+
);
|
|
136
|
+
// Handle root path case to avoid double slashes like //en
|
|
137
|
+
const suffix = `${url.search}${url.hash}`;
|
|
138
|
+
const localizedUrl =
|
|
139
|
+
basePath === '/' ? newPathname + suffix : basePath + newPathname + suffix;
|
|
140
|
+
|
|
141
|
+
return localizedUrl;
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
export const createLocaleRedirectResponse = (location: string): Response =>
|
|
145
|
+
new Response(null, {
|
|
146
|
+
status: 302,
|
|
147
|
+
headers: {
|
|
148
|
+
'Cache-Control': 'private, no-store',
|
|
149
|
+
Location: location,
|
|
150
|
+
Vary: 'Accept-Language, Cookie',
|
|
151
|
+
},
|
|
152
|
+
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { LocalisedUrlsOption, ResolvedLocalisedUrlsConfig } from './types';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Localised URLs are strictly opt-in: only an explicit, non-empty map enables
|
|
5
|
+
* route expansion and validation. `true`, `false`, an empty map and absence
|
|
6
|
+
* all resolve to disabled, so upstream-style configs (`localePathRedirect` +
|
|
7
|
+
* `languages` without a map) keep plain locale-prefix behavior instead of
|
|
8
|
+
* failing the build for every route missing from a map they never wrote.
|
|
9
|
+
*/
|
|
10
|
+
export const resolveLocalisedUrlsConfig = (
|
|
11
|
+
option: LocalisedUrlsOption | undefined,
|
|
12
|
+
): ResolvedLocalisedUrlsConfig => {
|
|
13
|
+
if (option && typeof option === 'object' && Object.keys(option).length > 0) {
|
|
14
|
+
return { enabled: true, map: option };
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return { enabled: false, map: {} };
|
|
18
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export { resolveLocalisedUrlsConfig } from './config';
|
|
2
|
+
export { normalisePathname, normalisePathPattern } from './normalise';
|
|
3
|
+
export { canonicalTargetPathname, localiseTargetPathname } from './pathname';
|
|
4
|
+
export { buildPathFromPattern, matchPathPattern } from './patterns';
|
|
5
|
+
export { resolveCanonicalLocalisedPath, resolveLocalisedPath } from './resolve';
|
|
6
|
+
export { applyLocalisedUrlsToRoutes, validateLocalisedUrls } from './routes';
|
|
7
|
+
export type {
|
|
8
|
+
LocalisedRoute,
|
|
9
|
+
LocalisedUrlPathMap,
|
|
10
|
+
LocalisedUrlsMap,
|
|
11
|
+
LocalisedUrlsOption,
|
|
12
|
+
ResolvedLocalisedUrlsConfig,
|
|
13
|
+
} from './types';
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
const LOCALE_PARAM_NAMES = new Set(['lang', 'locale', 'language']);
|
|
2
|
+
|
|
3
|
+
const normaliseSlashes = (path: string): string => {
|
|
4
|
+
const withoutDuplicateSlashes = path.replace(/\/+/g, '/');
|
|
5
|
+
const withLeadingSlash = withoutDuplicateSlashes.startsWith('/')
|
|
6
|
+
? withoutDuplicateSlashes
|
|
7
|
+
: `/${withoutDuplicateSlashes}`;
|
|
8
|
+
|
|
9
|
+
return withLeadingSlash.length > 1
|
|
10
|
+
? withLeadingSlash.replace(/\/+$/, '')
|
|
11
|
+
: withLeadingSlash;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export const normalisePathPattern = (path: string): string =>
|
|
15
|
+
normaliseSlashes(path).replace(/\[(.+?)\]/g, ':$1');
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Normalise a concrete request pathname: slash cleanup only. Unlike
|
|
19
|
+
* {@link normalisePathPattern} it must not rewrite literal `[x]` segments to
|
|
20
|
+
* `:x` params — pathnames are values, not patterns.
|
|
21
|
+
*/
|
|
22
|
+
export const normalisePathname = (pathname: string): string =>
|
|
23
|
+
normaliseSlashes(pathname);
|
|
24
|
+
|
|
25
|
+
export const normaliseRoutePath = (path: string): string => {
|
|
26
|
+
const normalized = normalisePathPattern(path);
|
|
27
|
+
return normalized === '/' ? '' : normalized.slice(1);
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const getLocaleParamSegment = (segment: string): string | null => {
|
|
31
|
+
if (!segment.startsWith(':')) {
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const paramName = segment.slice(1).replace(/\?$/, '');
|
|
36
|
+
return LOCALE_PARAM_NAMES.has(paramName) ? segment : null;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const splitPathSegments = (path?: string): string[] => {
|
|
40
|
+
if (!path) {
|
|
41
|
+
return [];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return normalisePathPattern(path).split('/').filter(Boolean);
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export const stripLeadingLocaleParam = (path?: string): string | undefined => {
|
|
48
|
+
const segments = splitPathSegments(path);
|
|
49
|
+
const leadingLocaleParam = getLocaleParamSegment(segments[0] || '');
|
|
50
|
+
|
|
51
|
+
if (!leadingLocaleParam) {
|
|
52
|
+
return path;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const remainingPath = segments.slice(1).join('/');
|
|
56
|
+
return remainingPath ? `/${remainingPath}` : undefined;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
export const getLeadingLocaleParam = (path?: string): string | null => {
|
|
60
|
+
const segments = splitPathSegments(path);
|
|
61
|
+
return getLocaleParamSegment(segments[0] || '');
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const isLocaleParamPath = (path?: string): boolean => {
|
|
65
|
+
const segments = splitPathSegments(path);
|
|
66
|
+
return segments.length === 1 && Boolean(getLocaleParamSegment(segments[0]));
|
|
67
|
+
};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { resolveLocalisedUrlsConfig } from './config';
|
|
2
|
+
|
|
3
|
+
import { resolveCanonicalLocalisedPath, resolveLocalisedPath } from './resolve';
|
|
4
|
+
|
|
5
|
+
import type { LocalisedUrlsOption } from './types';
|
|
6
|
+
|
|
7
|
+
const stripLanguagePrefix = (pathname: string, languages: string[]): string => {
|
|
8
|
+
const segments = pathname.split('/').filter(Boolean);
|
|
9
|
+
const firstSegment = segments[0]?.toLowerCase();
|
|
10
|
+
|
|
11
|
+
if (
|
|
12
|
+
firstSegment &&
|
|
13
|
+
languages.some(language => language.toLowerCase() === firstSegment)
|
|
14
|
+
) {
|
|
15
|
+
return `/${segments.slice(1).join('/')}`;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return pathname || '/';
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export const localiseTargetPathname = (
|
|
22
|
+
pathname: string,
|
|
23
|
+
language: string,
|
|
24
|
+
languages: string[],
|
|
25
|
+
localisedUrls?: LocalisedUrlsOption,
|
|
26
|
+
): string => {
|
|
27
|
+
const pathWithoutLanguage = stripLanguagePrefix(pathname, languages);
|
|
28
|
+
const localisedUrlsConfig = resolveLocalisedUrlsConfig(localisedUrls);
|
|
29
|
+
const resolvedPath = localisedUrlsConfig.enabled
|
|
30
|
+
? resolveLocalisedPath(
|
|
31
|
+
pathWithoutLanguage,
|
|
32
|
+
language,
|
|
33
|
+
languages,
|
|
34
|
+
localisedUrlsConfig.map,
|
|
35
|
+
)
|
|
36
|
+
: pathWithoutLanguage;
|
|
37
|
+
const resolvedSegments = resolvedPath.split('/').filter(Boolean);
|
|
38
|
+
|
|
39
|
+
return `/${[language, ...resolvedSegments].join('/')}`;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export const canonicalTargetPathname = (
|
|
43
|
+
pathname: string,
|
|
44
|
+
languages: string[],
|
|
45
|
+
localisedUrls?: LocalisedUrlsOption,
|
|
46
|
+
): string => {
|
|
47
|
+
const pathWithoutLanguage = stripLanguagePrefix(pathname, languages);
|
|
48
|
+
const localisedUrlsConfig = resolveLocalisedUrlsConfig(localisedUrls);
|
|
49
|
+
|
|
50
|
+
return localisedUrlsConfig.enabled
|
|
51
|
+
? resolveCanonicalLocalisedPath(
|
|
52
|
+
pathWithoutLanguage,
|
|
53
|
+
languages,
|
|
54
|
+
localisedUrlsConfig.map,
|
|
55
|
+
)
|
|
56
|
+
: pathWithoutLanguage;
|
|
57
|
+
};
|