@bleedingdev/modern-js-plugin-i18n 3.4.0-ultramodern.0 → 3.4.0-ultramodern.10
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/context.js +8 -0
- package/dist/cjs/runtime/core.js +217 -0
- package/dist/cjs/runtime/i18n/backend/middleware.node.js +22 -4
- package/dist/cjs/runtime/index.js +44 -173
- package/dist/cjs/runtime/no-react-i18next.js +76 -0
- package/dist/esm/cli/index.mjs +2 -1
- package/dist/esm/runtime/context.mjs +8 -0
- package/dist/esm/runtime/core.mjs +152 -0
- package/dist/esm/runtime/i18n/backend/middleware.node.mjs +19 -4
- package/dist/esm/runtime/index.mjs +5 -140
- package/dist/esm/runtime/no-react-i18next.mjs +6 -0
- package/dist/esm-node/cli/index.mjs +2 -1
- package/dist/esm-node/runtime/context.mjs +8 -0
- package/dist/esm-node/runtime/core.mjs +153 -0
- package/dist/esm-node/runtime/i18n/backend/middleware.node.mjs +19 -4
- package/dist/esm-node/runtime/index.mjs +5 -140
- package/dist/esm-node/runtime/no-react-i18next.mjs +7 -0
- package/dist/types/runtime/context.d.ts +1 -0
- package/dist/types/runtime/core.d.ts +30 -0
- package/dist/types/runtime/i18n/backend/middleware.node.d.ts +4 -1
- package/dist/types/runtime/index.d.ts +2 -24
- package/dist/types/runtime/no-react-i18next.d.ts +3 -0
- package/package.json +21 -11
- package/rstest.config.mts +1 -0
- package/src/cli/index.ts +6 -1
- package/src/runtime/context.tsx +13 -0
- package/src/runtime/core.tsx +360 -0
- package/src/runtime/i18n/backend/middleware.node.ts +31 -1
- package/src/runtime/index.tsx +4 -316
- package/src/runtime/no-react-i18next.tsx +7 -0
- package/tests/i18nUtils.test.ts +34 -0
- package/tests/localisedUrls.test.ts +35 -0
- package/tests/reactI18nextRuntimeBoundary.test.ts +36 -0
- package/tests/routerAdapter.test.tsx +58 -1
|
@@ -0,0 +1,360 @@
|
|
|
1
|
+
import {
|
|
2
|
+
isBrowser,
|
|
3
|
+
RuntimeContext,
|
|
4
|
+
type RuntimePlugin,
|
|
5
|
+
} from '@modern-js/runtime';
|
|
6
|
+
import { Helmet } from '@modern-js/runtime/head';
|
|
7
|
+
import type { TInternalRuntimeContext } from '@modern-js/runtime/internal';
|
|
8
|
+
import { merge } from '@modern-js/runtime-utils/merge';
|
|
9
|
+
import type React from 'react';
|
|
10
|
+
import { useContext, useEffect, useMemo, useRef, useState } from 'react';
|
|
11
|
+
import type {
|
|
12
|
+
BaseBackendOptions,
|
|
13
|
+
BaseLocaleDetectionOptions,
|
|
14
|
+
} from '../shared/type';
|
|
15
|
+
import { ModernI18nProvider } from './context';
|
|
16
|
+
import {
|
|
17
|
+
createContextValue,
|
|
18
|
+
useClientSideRedirect,
|
|
19
|
+
useLanguageSync,
|
|
20
|
+
useSdkResourcesLoader,
|
|
21
|
+
} from './hooks';
|
|
22
|
+
import type { I18nInitOptions, I18nInstance } from './i18n';
|
|
23
|
+
import { getI18nInstance } from './i18n';
|
|
24
|
+
import { mergeBackendOptions } from './i18n/backend';
|
|
25
|
+
import {
|
|
26
|
+
detectLanguageWithPriority,
|
|
27
|
+
exportServerLngToWindow,
|
|
28
|
+
mergeDetectionOptions,
|
|
29
|
+
} from './i18n/detection';
|
|
30
|
+
import { useI18nextLanguageDetector } from './i18n/detection/middleware';
|
|
31
|
+
import { getI18nextInstanceForProvider } from './i18n/instance';
|
|
32
|
+
import { getPathname } from './utils';
|
|
33
|
+
import './types';
|
|
34
|
+
|
|
35
|
+
export type { I18nSdkLoader, I18nSdkLoadOptions } from '../shared/type';
|
|
36
|
+
export type { Resources } from './i18n/instance';
|
|
37
|
+
|
|
38
|
+
type I18nLifecycleHelpers = {
|
|
39
|
+
useI18nextBackend: typeof import('./i18n/backend/middleware')['useI18nextBackend'];
|
|
40
|
+
changeI18nLanguage: typeof import('./i18n/utils')['changeI18nLanguage'];
|
|
41
|
+
ensureLanguageMatch: typeof import('./i18n/utils')['ensureLanguageMatch'];
|
|
42
|
+
initializeI18nInstance: typeof import('./i18n/utils')['initializeI18nInstance'];
|
|
43
|
+
setupClonedInstance: typeof import('./i18n/utils')['setupClonedInstance'];
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
let i18nLifecycleHelpersPromise: Promise<I18nLifecycleHelpers> | undefined;
|
|
47
|
+
|
|
48
|
+
function loadI18nLifecycleHelpers(): Promise<I18nLifecycleHelpers> {
|
|
49
|
+
i18nLifecycleHelpersPromise ??= Promise.all([
|
|
50
|
+
import('./i18n/backend/middleware'),
|
|
51
|
+
import('./i18n/utils'),
|
|
52
|
+
]).then(([backendMiddleware, utils]) => ({
|
|
53
|
+
useI18nextBackend: backendMiddleware.useI18nextBackend,
|
|
54
|
+
changeI18nLanguage: utils.changeI18nLanguage,
|
|
55
|
+
ensureLanguageMatch: utils.ensureLanguageMatch,
|
|
56
|
+
initializeI18nInstance: utils.initializeI18nInstance,
|
|
57
|
+
setupClonedInstance: utils.setupClonedInstance,
|
|
58
|
+
}));
|
|
59
|
+
|
|
60
|
+
return i18nLifecycleHelpersPromise;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export interface I18nPluginOptions {
|
|
64
|
+
entryName?: string;
|
|
65
|
+
localeDetection?: BaseLocaleDetectionOptions;
|
|
66
|
+
backend?: BaseBackendOptions;
|
|
67
|
+
i18nInstance?: I18nInstance;
|
|
68
|
+
changeLanguage?: (lang: string) => void;
|
|
69
|
+
initOptions?: I18nInitOptions;
|
|
70
|
+
htmlLangAttr?: boolean;
|
|
71
|
+
reactI18next?: boolean;
|
|
72
|
+
[key: string]: any;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
interface RuntimeContextWithI18n extends TInternalRuntimeContext {
|
|
76
|
+
i18nInstance?: I18nInstance;
|
|
77
|
+
changeLanguage?: (lang: string) => Promise<void>;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export interface ReactI18nextIntegration {
|
|
81
|
+
I18nextProvider: React.ComponentType<any> | null;
|
|
82
|
+
initReactI18next: any | null;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export type LoadReactI18nextIntegration =
|
|
86
|
+
() => Promise<ReactI18nextIntegration | null>;
|
|
87
|
+
|
|
88
|
+
export const createI18nPlugin =
|
|
89
|
+
(
|
|
90
|
+
loadReactI18nextIntegration?: LoadReactI18nextIntegration,
|
|
91
|
+
): ((options: I18nPluginOptions) => RuntimePlugin) =>
|
|
92
|
+
(options: I18nPluginOptions): RuntimePlugin => ({
|
|
93
|
+
name: '@modern-js/plugin-i18n',
|
|
94
|
+
setup: api => {
|
|
95
|
+
const {
|
|
96
|
+
entryName,
|
|
97
|
+
i18nInstance: userI18nInstance,
|
|
98
|
+
initOptions,
|
|
99
|
+
localeDetection,
|
|
100
|
+
backend,
|
|
101
|
+
htmlLangAttr = false,
|
|
102
|
+
reactI18next = true,
|
|
103
|
+
} = options;
|
|
104
|
+
const {
|
|
105
|
+
localePathRedirect = false,
|
|
106
|
+
i18nextDetector = true,
|
|
107
|
+
languages = [],
|
|
108
|
+
fallbackLanguage = 'en',
|
|
109
|
+
detection,
|
|
110
|
+
ignoreRedirectRoutes,
|
|
111
|
+
localisedUrls,
|
|
112
|
+
} = localeDetection || {};
|
|
113
|
+
const { enabled: backendEnabled = false } = backend || {};
|
|
114
|
+
let latestI18nInstance: I18nInstance | undefined;
|
|
115
|
+
let I18nextProvider: React.ComponentType<any> | null;
|
|
116
|
+
|
|
117
|
+
const resolveReactI18nextIntegration = async () => {
|
|
118
|
+
if (!reactI18next) {
|
|
119
|
+
return null;
|
|
120
|
+
}
|
|
121
|
+
return loadReactI18nextIntegration?.() ?? null;
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
api.onBeforeRender(async context => {
|
|
125
|
+
const {
|
|
126
|
+
useI18nextBackend,
|
|
127
|
+
changeI18nLanguage,
|
|
128
|
+
ensureLanguageMatch,
|
|
129
|
+
initializeI18nInstance,
|
|
130
|
+
setupClonedInstance,
|
|
131
|
+
} = await loadI18nLifecycleHelpers();
|
|
132
|
+
let i18nInstance = await getI18nInstance(userI18nInstance);
|
|
133
|
+
const { i18n: otherConfig } = api.getRuntimeConfig();
|
|
134
|
+
const { initOptions: otherInitOptions } = otherConfig || {};
|
|
135
|
+
const userInitOptions = merge(
|
|
136
|
+
otherInitOptions || {},
|
|
137
|
+
initOptions || {},
|
|
138
|
+
);
|
|
139
|
+
const reactI18nextIntegration = await resolveReactI18nextIntegration();
|
|
140
|
+
I18nextProvider = reactI18nextIntegration?.I18nextProvider ?? null;
|
|
141
|
+
if (reactI18nextIntegration?.initReactI18next) {
|
|
142
|
+
i18nInstance.use(reactI18nextIntegration.initReactI18next);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const pathname = getPathname(context);
|
|
146
|
+
|
|
147
|
+
if (i18nextDetector) {
|
|
148
|
+
useI18nextLanguageDetector(i18nInstance);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const mergedDetection = mergeDetectionOptions(
|
|
152
|
+
i18nextDetector,
|
|
153
|
+
detection,
|
|
154
|
+
localePathRedirect,
|
|
155
|
+
userInitOptions,
|
|
156
|
+
);
|
|
157
|
+
const mergedBackend = mergeBackendOptions(backend, userInitOptions);
|
|
158
|
+
|
|
159
|
+
// Register Backend BEFORE detectLanguageWithPriority
|
|
160
|
+
// This is critical because detectLanguageWithPriority may trigger init()
|
|
161
|
+
// through i18next detector, and backend must be registered before init()
|
|
162
|
+
// Register backend if:
|
|
163
|
+
// 1. enabled is true (explicitly or auto-detected), OR
|
|
164
|
+
// 2. SDK is configured (allows standalone SDK usage even without locales directory)
|
|
165
|
+
const hasSdkConfig =
|
|
166
|
+
typeof userInitOptions?.backend?.sdk === 'function' ||
|
|
167
|
+
(mergedBackend?.sdk && typeof mergedBackend.sdk === 'function');
|
|
168
|
+
if (mergedBackend && (backendEnabled || hasSdkConfig)) {
|
|
169
|
+
useI18nextBackend(i18nInstance, mergedBackend);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const { finalLanguage } = await detectLanguageWithPriority(
|
|
173
|
+
i18nInstance,
|
|
174
|
+
{
|
|
175
|
+
languages,
|
|
176
|
+
fallbackLanguage,
|
|
177
|
+
localePathRedirect,
|
|
178
|
+
i18nextDetector,
|
|
179
|
+
detection,
|
|
180
|
+
userInitOptions,
|
|
181
|
+
mergedBackend,
|
|
182
|
+
pathname,
|
|
183
|
+
ssrContext: context.ssrContext,
|
|
184
|
+
},
|
|
185
|
+
);
|
|
186
|
+
|
|
187
|
+
await initializeI18nInstance(
|
|
188
|
+
i18nInstance,
|
|
189
|
+
finalLanguage,
|
|
190
|
+
fallbackLanguage,
|
|
191
|
+
languages,
|
|
192
|
+
mergedDetection,
|
|
193
|
+
mergedBackend,
|
|
194
|
+
userInitOptions,
|
|
195
|
+
);
|
|
196
|
+
|
|
197
|
+
if (!isBrowser() && i18nInstance.cloneInstance) {
|
|
198
|
+
i18nInstance = i18nInstance.cloneInstance();
|
|
199
|
+
await setupClonedInstance(
|
|
200
|
+
i18nInstance,
|
|
201
|
+
finalLanguage,
|
|
202
|
+
fallbackLanguage,
|
|
203
|
+
languages,
|
|
204
|
+
backendEnabled,
|
|
205
|
+
backend,
|
|
206
|
+
i18nextDetector,
|
|
207
|
+
detection,
|
|
208
|
+
localePathRedirect,
|
|
209
|
+
userInitOptions,
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
if (localePathRedirect) {
|
|
214
|
+
await ensureLanguageMatch(i18nInstance, finalLanguage);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
if (!isBrowser()) {
|
|
218
|
+
exportServerLngToWindow(context, finalLanguage);
|
|
219
|
+
}
|
|
220
|
+
context.i18nInstance = i18nInstance;
|
|
221
|
+
latestI18nInstance = i18nInstance;
|
|
222
|
+
|
|
223
|
+
// Add changeLanguage method to context for other runtime plugins to use
|
|
224
|
+
context.changeLanguage = async (newLang: string) => {
|
|
225
|
+
await changeI18nLanguage(i18nInstance, newLang, {
|
|
226
|
+
detectionOptions: mergedDetection,
|
|
227
|
+
});
|
|
228
|
+
};
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
api.wrapRoot(App => {
|
|
232
|
+
return props => {
|
|
233
|
+
const runtimeContext = useContext(
|
|
234
|
+
RuntimeContext,
|
|
235
|
+
) as RuntimeContextWithI18n;
|
|
236
|
+
const i18nInstance =
|
|
237
|
+
runtimeContext.i18nInstance || latestI18nInstance;
|
|
238
|
+
const initialLang = useMemo(
|
|
239
|
+
() =>
|
|
240
|
+
i18nInstance?.language ||
|
|
241
|
+
(localeDetection?.fallbackLanguage ?? 'en'),
|
|
242
|
+
[i18nInstance?.language, localeDetection?.fallbackLanguage],
|
|
243
|
+
);
|
|
244
|
+
const [lang, setLang] = useState(initialLang);
|
|
245
|
+
const [forceUpdate, setForceUpdate] = useState(0);
|
|
246
|
+
const prevLangRef = useRef(lang);
|
|
247
|
+
const runtimeContextRef = useRef(runtimeContext);
|
|
248
|
+
runtimeContextRef.current = runtimeContext;
|
|
249
|
+
|
|
250
|
+
useEffect(() => {
|
|
251
|
+
if (i18nInstance?.language) {
|
|
252
|
+
const translator = (i18nInstance as any).translator;
|
|
253
|
+
if (translator) {
|
|
254
|
+
translator.language = i18nInstance.language;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}, [i18nInstance?.language]);
|
|
258
|
+
|
|
259
|
+
useEffect(() => {
|
|
260
|
+
prevLangRef.current = lang;
|
|
261
|
+
}, [lang]);
|
|
262
|
+
|
|
263
|
+
useSdkResourcesLoader(i18nInstance, setForceUpdate);
|
|
264
|
+
useLanguageSync(
|
|
265
|
+
i18nInstance,
|
|
266
|
+
localePathRedirect,
|
|
267
|
+
languages,
|
|
268
|
+
runtimeContextRef,
|
|
269
|
+
prevLangRef,
|
|
270
|
+
setLang,
|
|
271
|
+
);
|
|
272
|
+
// Handle client-side redirect for static deployments
|
|
273
|
+
// Note: This hook only executes in browser environment and skips SSR scenarios
|
|
274
|
+
useClientSideRedirect(
|
|
275
|
+
i18nInstance,
|
|
276
|
+
localePathRedirect,
|
|
277
|
+
languages,
|
|
278
|
+
fallbackLanguage,
|
|
279
|
+
ignoreRedirectRoutes,
|
|
280
|
+
localisedUrls,
|
|
281
|
+
);
|
|
282
|
+
|
|
283
|
+
const contextValue = useMemo(
|
|
284
|
+
() =>
|
|
285
|
+
createContextValue(
|
|
286
|
+
lang,
|
|
287
|
+
i18nInstance,
|
|
288
|
+
entryName,
|
|
289
|
+
languages,
|
|
290
|
+
localePathRedirect,
|
|
291
|
+
ignoreRedirectRoutes,
|
|
292
|
+
localisedUrls,
|
|
293
|
+
setLang,
|
|
294
|
+
),
|
|
295
|
+
[
|
|
296
|
+
lang,
|
|
297
|
+
i18nInstance,
|
|
298
|
+
entryName,
|
|
299
|
+
languages,
|
|
300
|
+
localePathRedirect,
|
|
301
|
+
ignoreRedirectRoutes,
|
|
302
|
+
localisedUrls,
|
|
303
|
+
forceUpdate,
|
|
304
|
+
],
|
|
305
|
+
);
|
|
306
|
+
|
|
307
|
+
const children = (props as React.PropsWithChildren).children;
|
|
308
|
+
const appContent = (
|
|
309
|
+
<>
|
|
310
|
+
{Boolean(htmlLangAttr) && <Helmet htmlAttributes={{ lang }} />}
|
|
311
|
+
<ModernI18nProvider value={contextValue}>
|
|
312
|
+
{App ? <App {...props}>{children}</App> : children}
|
|
313
|
+
</ModernI18nProvider>
|
|
314
|
+
</>
|
|
315
|
+
);
|
|
316
|
+
|
|
317
|
+
if (!i18nInstance) {
|
|
318
|
+
return appContent;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
if (I18nextProvider) {
|
|
322
|
+
const i18nextInstanceForProvider =
|
|
323
|
+
getI18nextInstanceForProvider(i18nInstance);
|
|
324
|
+
return (
|
|
325
|
+
<I18nextProvider i18n={i18nextInstanceForProvider}>
|
|
326
|
+
{appContent}
|
|
327
|
+
</I18nextProvider>
|
|
328
|
+
);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
return appContent;
|
|
332
|
+
};
|
|
333
|
+
});
|
|
334
|
+
},
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
export type {
|
|
338
|
+
AllowedLinkTarget,
|
|
339
|
+
CanonicalRoutePath,
|
|
340
|
+
UltramodernCanonicalRoutes,
|
|
341
|
+
} from './canonicalRoutes';
|
|
342
|
+
export { useModernI18n } from './context';
|
|
343
|
+
export { I18nLink, type I18nLinkProps } from './I18nLink';
|
|
344
|
+
export {
|
|
345
|
+
Link,
|
|
346
|
+
type LinkActiveOptions,
|
|
347
|
+
type LinkBaseProps,
|
|
348
|
+
type LinkParams,
|
|
349
|
+
type LinkProps,
|
|
350
|
+
} from './Link';
|
|
351
|
+
export {
|
|
352
|
+
canonicalPath,
|
|
353
|
+
type LocalizedPathsConfig,
|
|
354
|
+
localizePath,
|
|
355
|
+
type UseLocalizedLocationReturn,
|
|
356
|
+
type UseLocalizedPathsReturn,
|
|
357
|
+
useLocalizedLocation,
|
|
358
|
+
useLocalizedPaths,
|
|
359
|
+
} from './localizedPaths';
|
|
360
|
+
export { buildLocalizedUrl, splitUrlTarget } from './utils';
|
|
@@ -1,8 +1,38 @@
|
|
|
1
|
-
import
|
|
1
|
+
import FsBackendModule from 'i18next-fs-backend';
|
|
2
2
|
import type { ExtendedBackendOptions } from '../../../shared/type';
|
|
3
3
|
import type { I18nInstance } from '../instance';
|
|
4
4
|
import { useI18nextBackendCommon } from './middleware.common';
|
|
5
5
|
|
|
6
|
+
type BackendConstructor = new (...args: any[]) => any;
|
|
7
|
+
|
|
8
|
+
export const resolveFsBackendConstructor = (
|
|
9
|
+
backendModule: unknown,
|
|
10
|
+
): BackendConstructor => {
|
|
11
|
+
const nestedDefault = (backendModule as { default?: { default?: unknown } })
|
|
12
|
+
?.default?.default;
|
|
13
|
+
const nestedModuleExports = (
|
|
14
|
+
backendModule as { default?: { 'module.exports'?: unknown } }
|
|
15
|
+
)?.default?.['module.exports'];
|
|
16
|
+
const candidates = [
|
|
17
|
+
backendModule,
|
|
18
|
+
(backendModule as { default?: unknown })?.default,
|
|
19
|
+
(backendModule as { 'module.exports'?: unknown })?.['module.exports'],
|
|
20
|
+
nestedDefault,
|
|
21
|
+
nestedModuleExports,
|
|
22
|
+
];
|
|
23
|
+
const Backend = candidates.find(candidate => typeof candidate === 'function');
|
|
24
|
+
|
|
25
|
+
if (!Backend) {
|
|
26
|
+
throw new Error(
|
|
27
|
+
'Failed to resolve i18next-fs-backend constructor for the i18n Node backend.',
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return Backend as BackendConstructor;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const Backend = resolveFsBackendConstructor(FsBackendModule);
|
|
35
|
+
|
|
6
36
|
/**
|
|
7
37
|
* Wrapper for FS backend to add a no-op save method
|
|
8
38
|
* This is required for i18next-chained-backend to trigger refresh logic
|