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