@bleedingdev/modern-js-plugin-i18n 3.9.3-ultramodern.9 → 3.11.3-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 +10 -0
- package/dist/cjs/runtime/context.js +5 -5
- package/dist/cjs/runtime/contextHelpers.js +2 -10
- package/dist/cjs/runtime/hooks.js +1 -16
- package/dist/cjs/runtime/i18n/backend/defaults.js +3 -4
- package/dist/cjs/runtime/i18n/backend/sdk-event.js +5 -6
- package/dist/cjs/runtime/i18n/instance.js +9 -22
- package/dist/cjs/runtime/i18n/react-i18next.js +2 -2
- package/dist/cjs/runtime/i18n/utils.js +1 -1
- package/dist/cjs/runtime/pluginSetup.js +9 -5
- package/dist/cjs/runtime/providerComposition.js +3 -3
- package/dist/cjs/shared/deepMerge.js +2 -2
- package/dist/cjs/shared/utils.js +3 -3
- package/dist/esm/cli/index.mjs +10 -0
- package/dist/esm/runtime/context.mjs +6 -6
- package/dist/esm/runtime/contextHelpers.mjs +1 -6
- package/dist/esm/runtime/hooks.mjs +1 -16
- package/dist/esm/runtime/i18n/instance.mjs +3 -16
- package/dist/esm/runtime/i18n/utils.mjs +1 -1
- package/dist/esm/runtime/navigation.mjs +1 -1
- package/dist/esm/runtime/pluginSetup.mjs +6 -2
- package/dist/esm/runtime/providerComposition.mjs +3 -3
- package/dist/esm/server/index.mjs +1 -1
- package/dist/esm-node/cli/index.mjs +10 -0
- package/dist/esm-node/runtime/context.mjs +6 -6
- package/dist/esm-node/runtime/contextHelpers.mjs +1 -6
- package/dist/esm-node/runtime/hooks.mjs +1 -16
- package/dist/esm-node/runtime/i18n/instance.mjs +3 -16
- package/dist/esm-node/runtime/i18n/utils.mjs +1 -1
- package/dist/esm-node/runtime/navigation.mjs +1 -1
- package/dist/esm-node/runtime/pluginSetup.mjs +6 -2
- package/dist/esm-node/runtime/providerComposition.mjs +3 -3
- package/dist/esm-node/server/index.mjs +1 -1
- package/dist/types/runtime/context.d.ts +16 -14
- package/dist/types/runtime/contextHelpers.d.ts +5 -6
- package/dist/types/runtime/core.d.ts +3 -3
- package/dist/types/runtime/hooks.d.ts +6 -6
- package/dist/types/runtime/i18n/backend/middleware.common.d.ts +2 -2
- package/dist/types/runtime/i18n/backend/middleware.d.ts +2 -2
- package/dist/types/runtime/i18n/backend/middleware.node.d.ts +2 -2
- package/dist/types/runtime/i18n/detection/cache.d.ts +5 -5
- package/dist/types/runtime/i18n/detection/detector.d.ts +2 -2
- package/dist/types/runtime/i18n/detection/middleware.d.ts +4 -4
- package/dist/types/runtime/i18n/detection/middleware.node.d.ts +4 -4
- package/dist/types/runtime/i18n/detection/priority.d.ts +2 -2
- package/dist/types/runtime/i18n/index.d.ts +1 -1
- package/dist/types/runtime/i18n/instance.d.ts +28 -11
- package/dist/types/runtime/i18n/utils.d.ts +7 -7
- package/dist/types/runtime/navigation.d.ts +4 -1
- package/dist/types/runtime/pluginSetup.d.ts +4 -4
- package/dist/types/runtime/providerComposition.d.ts +3 -3
- package/dist/types/runtime/types.d.ts +3 -3
- package/package.json +25 -24
- package/src/cli/index.ts +19 -1
- package/src/runtime/context.tsx +26 -24
- package/src/runtime/contextHelpers.ts +5 -26
- package/src/runtime/core.tsx +5 -3
- package/src/runtime/hooks.ts +7 -27
- package/src/runtime/i18n/backend/middleware.common.ts +3 -3
- package/src/runtime/i18n/backend/middleware.node.ts +2 -2
- package/src/runtime/i18n/backend/middleware.ts +2 -2
- package/src/runtime/i18n/detection/cache.ts +8 -8
- package/src/runtime/i18n/detection/detector.ts +5 -5
- package/src/runtime/i18n/detection/middleware.node.ts +7 -5
- package/src/runtime/i18n/detection/middleware.ts +6 -4
- package/src/runtime/i18n/detection/priority.ts +2 -2
- package/src/runtime/i18n/index.ts +2 -0
- package/src/runtime/i18n/instance.ts +50 -49
- package/src/runtime/i18n/utils.ts +14 -10
- package/src/runtime/pluginSetup.ts +12 -6
- package/src/runtime/providerComposition.tsx +31 -21
- package/src/runtime/types.ts +3 -3
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { isBrowser } from '@modern-js/runtime';
|
|
2
|
-
import type { I18nInitOptions,
|
|
2
|
+
import type { I18nInitOptions, I18nRuntimeInstance } from '../instance';
|
|
3
3
|
|
|
4
4
|
import {
|
|
5
5
|
buildDetectorConfigKey,
|
|
@@ -23,16 +23,16 @@ import type {
|
|
|
23
23
|
* Initialize i18n instance for detector if needed
|
|
24
24
|
*/
|
|
25
25
|
interface DetectorInitResult {
|
|
26
|
-
detectorInstance:
|
|
26
|
+
detectorInstance: I18nRuntimeInstance;
|
|
27
27
|
isTemporary: boolean;
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
type MutableDetectorInstanceState = Omit<
|
|
30
|
+
type MutableDetectorInstanceState = Omit<I18nRuntimeInstance, 'language'> & {
|
|
31
31
|
language?: string;
|
|
32
32
|
};
|
|
33
33
|
|
|
34
34
|
const initializeI18nForDetector = async (
|
|
35
|
-
i18nInstance:
|
|
35
|
+
i18nInstance: I18nRuntimeInstance,
|
|
36
36
|
options: BaseLanguageDetectionOptions,
|
|
37
37
|
): Promise<DetectorInitResult> => {
|
|
38
38
|
const mergedDetection = mergeDetectionOptions(
|
|
@@ -87,7 +87,7 @@ const initializeI18nForDetector = async (
|
|
|
87
87
|
* Priority 3: Detect language using i18next detector
|
|
88
88
|
*/
|
|
89
89
|
export const detectLanguageFromI18nextDetector = async (
|
|
90
|
-
i18nInstance:
|
|
90
|
+
i18nInstance: I18nRuntimeInstance,
|
|
91
91
|
options: BaseLanguageDetectionOptions & {
|
|
92
92
|
ssrContext?: LanguageDetectionSsrContext;
|
|
93
93
|
},
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { LanguageDetector } from 'i18next-http-middleware';
|
|
2
2
|
import { detectLanguageFromRequest } from '../../../shared/detection.js';
|
|
3
|
-
import type {
|
|
3
|
+
import type { I18nRuntimeInstance, LanguageDetectorOptions } from '../instance';
|
|
4
4
|
|
|
5
5
|
type HttpDetectorInit = (
|
|
6
|
-
services: NonNullable<
|
|
6
|
+
services: NonNullable<I18nRuntimeInstance['services']>,
|
|
7
7
|
options?: unknown,
|
|
8
8
|
allOptions?: unknown,
|
|
9
9
|
) => void;
|
|
@@ -29,7 +29,7 @@ const isDetectionRequest = (request: unknown): request is DetectionRequest => {
|
|
|
29
29
|
};
|
|
30
30
|
|
|
31
31
|
export const cacheUserLanguage = (
|
|
32
|
-
_i18nInstance:
|
|
32
|
+
_i18nInstance: I18nRuntimeInstance,
|
|
33
33
|
_language: string,
|
|
34
34
|
_detectionOptions?: unknown,
|
|
35
35
|
): void => {
|
|
@@ -53,7 +53,9 @@ export const readLanguageFromStorage = (
|
|
|
53
53
|
* Register LanguageDetector plugin to i18n instance
|
|
54
54
|
* Must be called before init() to properly register the detector
|
|
55
55
|
*/
|
|
56
|
-
export const useI18nextLanguageDetector = (
|
|
56
|
+
export const useI18nextLanguageDetector = (
|
|
57
|
+
i18nInstance: I18nRuntimeInstance,
|
|
58
|
+
) => {
|
|
57
59
|
if (!i18nInstance.isInitialized) {
|
|
58
60
|
return i18nInstance.use(LanguageDetector);
|
|
59
61
|
}
|
|
@@ -65,7 +67,7 @@ export const useI18nextLanguageDetector = (i18nInstance: I18nInstance) => {
|
|
|
65
67
|
* For initialized instances without detector in services, manually create a detector instance
|
|
66
68
|
*/
|
|
67
69
|
export const detectLanguage = (
|
|
68
|
-
i18nInstance:
|
|
70
|
+
i18nInstance: I18nRuntimeInstance,
|
|
69
71
|
request?: unknown,
|
|
70
72
|
detectionOptions?: LanguageDetectorOptions,
|
|
71
73
|
): string | undefined => {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { isBrowser } from '@modern-js/runtime';
|
|
2
2
|
import LanguageDetector from 'i18next-browser-languagedetector';
|
|
3
|
-
import type {
|
|
3
|
+
import type { I18nRuntimeInstance, LanguageDetectorOptions } from '../instance';
|
|
4
4
|
import { getActualI18nextInstance, isI18nWrapperInstance } from '../instance';
|
|
5
5
|
|
|
6
6
|
type BrowserDetectorInitOptions = Parameters<LanguageDetector['init']>[1];
|
|
@@ -10,7 +10,9 @@ type BrowserDetectorInitOptions = Parameters<LanguageDetector['init']>[1];
|
|
|
10
10
|
* Must be called before init() to properly register the detector
|
|
11
11
|
* For wrapper instances, ensure detector is registered on the underlying i18next instance
|
|
12
12
|
*/
|
|
13
|
-
export const useI18nextLanguageDetector = (
|
|
13
|
+
export const useI18nextLanguageDetector = (
|
|
14
|
+
i18nInstance: I18nRuntimeInstance,
|
|
15
|
+
) => {
|
|
14
16
|
if (!i18nInstance.isInitialized) {
|
|
15
17
|
// For wrapper instances, also register on the underlying instance
|
|
16
18
|
if (isI18nWrapperInstance(i18nInstance)) {
|
|
@@ -115,7 +117,7 @@ export const readLanguageFromStorage = (
|
|
|
115
117
|
* For wrapper instances, access the underlying i18next instance's services
|
|
116
118
|
*/
|
|
117
119
|
export const detectLanguage = (
|
|
118
|
-
i18nInstance:
|
|
120
|
+
i18nInstance: I18nRuntimeInstance,
|
|
119
121
|
_request?: unknown,
|
|
120
122
|
detectionOptions?: LanguageDetectorOptions,
|
|
121
123
|
): string | undefined => {
|
|
@@ -191,7 +193,7 @@ export const detectLanguage = (
|
|
|
191
193
|
* For wrapper instances, access the underlying i18next instance's services
|
|
192
194
|
*/
|
|
193
195
|
export const cacheUserLanguage = (
|
|
194
|
-
i18nInstance:
|
|
196
|
+
i18nInstance: I18nRuntimeInstance,
|
|
195
197
|
language: string,
|
|
196
198
|
detectionOptions?: unknown,
|
|
197
199
|
): void => {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { type I18nRuntimeInstance, isI18nWrapperInstance } from '../instance';
|
|
2
2
|
|
|
3
3
|
import { detectLanguageFromI18nextDetector } from './detector';
|
|
4
4
|
|
|
@@ -20,7 +20,7 @@ import type {
|
|
|
20
20
|
* Priority 4: User config language or fallback
|
|
21
21
|
*/
|
|
22
22
|
export const detectLanguageWithPriority = async (
|
|
23
|
-
i18nInstance:
|
|
23
|
+
i18nInstance: I18nRuntimeInstance,
|
|
24
24
|
options: LanguageDetectionOptions,
|
|
25
25
|
): Promise<LanguageDetectionResult> => {
|
|
26
26
|
const {
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { CloneOptions, InitOptions, i18n } from 'i18next';
|
|
1
2
|
import type { BaseBackendOptions } from '../../shared/type';
|
|
2
3
|
|
|
3
4
|
interface I18nResourceStore {
|
|
@@ -15,12 +16,6 @@ interface I18nResourceStore {
|
|
|
15
16
|
) => void;
|
|
16
17
|
}
|
|
17
18
|
|
|
18
|
-
type I18nWrapperInstance = I18nInstance & {
|
|
19
|
-
i18nInstance: {
|
|
20
|
-
instance: I18nInstance;
|
|
21
|
-
};
|
|
22
|
-
};
|
|
23
|
-
|
|
24
19
|
export function isI18nWrapperInstance(
|
|
25
20
|
obj: unknown,
|
|
26
21
|
): obj is I18nWrapperInstance {
|
|
@@ -48,27 +43,20 @@ export function isI18nWrapperInstance(
|
|
|
48
43
|
return true;
|
|
49
44
|
}
|
|
50
45
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
export function getActualI18nextInstance(instance: I18nInstance): I18nInstance {
|
|
61
|
-
if (isI18nWrapperInstance(instance)) {
|
|
62
|
-
const i18nextInstance = getI18nWrapperI18nextInstance(instance);
|
|
63
|
-
return i18nextInstance || instance;
|
|
64
|
-
}
|
|
65
|
-
return instance;
|
|
46
|
+
/** The i18next instance that owns resources, detection and translation. */
|
|
47
|
+
export function getActualI18nextInstance(
|
|
48
|
+
instance: I18nRuntimeInstance,
|
|
49
|
+
): I18nInstance {
|
|
50
|
+
return isI18nWrapperInstance(instance)
|
|
51
|
+
? instance.i18nInstance.instance
|
|
52
|
+
: instance;
|
|
66
53
|
}
|
|
67
54
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
)
|
|
55
|
+
// i18next's own `t`, so keys, overloads, selectors and the application's
|
|
56
|
+
// `CustomTypeOptions` augmentation follow the installed i18next release. A
|
|
57
|
+
// hand-written signature drifts on every i18next release (27 replaced the
|
|
58
|
+
// overloads with a rest-tuple union) and rejects strict-key and selector apps.
|
|
59
|
+
export type TranslateFn = i18n['t'];
|
|
72
60
|
|
|
73
61
|
// FORK: intentionally diverges from upstream @modern-js/plugin-i18n. Upstream
|
|
74
62
|
// declares a top-level `[key: string]: any` and overloaded call-signature
|
|
@@ -90,12 +78,7 @@ export interface I18nInstance {
|
|
|
90
78
|
key: string | string[],
|
|
91
79
|
options?: Record<string, unknown>,
|
|
92
80
|
) => boolean;
|
|
93
|
-
|
|
94
|
-
getFixedT?: (
|
|
95
|
-
lng: string | readonly string[] | null,
|
|
96
|
-
ns?: string | readonly string[] | null,
|
|
97
|
-
keyPrefix?: string,
|
|
98
|
-
) => TranslateFn;
|
|
81
|
+
getFixedT?: i18n['getFixedT'];
|
|
99
82
|
hasLoadedNamespace?: (
|
|
100
83
|
ns: string | readonly string[],
|
|
101
84
|
options?: Record<string, unknown>,
|
|
@@ -133,6 +116,9 @@ export interface I18nInstance {
|
|
|
133
116
|
cloneInstance?(options?: any, callback?: any): I18nInstance; // ssr need
|
|
134
117
|
// i18next store (may not be in the type definition but exists at runtime)
|
|
135
118
|
store?: I18nResourceStore;
|
|
119
|
+
// i18next's own event API.
|
|
120
|
+
on?: i18n['on'];
|
|
121
|
+
off?: i18n['off'];
|
|
136
122
|
emit?(event: string, ...args: any[]): unknown;
|
|
137
123
|
reloadResources?(
|
|
138
124
|
language?: any,
|
|
@@ -161,6 +147,29 @@ export interface I18nInstance {
|
|
|
161
147
|
// undeclared properties off I18nInstance.
|
|
162
148
|
}
|
|
163
149
|
|
|
150
|
+
/**
|
|
151
|
+
* A custom instance owning a native i18next one at `i18nInstance.instance`.
|
|
152
|
+
* The plugin initialises, switches and clones the wrapper, whose own `t` is
|
|
153
|
+
* `useModernI18n<Wrapper>().t`; resources and detection use the native one.
|
|
154
|
+
*/
|
|
155
|
+
export interface I18nWrapperInstance
|
|
156
|
+
extends Omit<
|
|
157
|
+
I18nInstance,
|
|
158
|
+
't' | 'on' | 'off' | 'cloneInstance' | 'createInstance'
|
|
159
|
+
> {
|
|
160
|
+
i18nInstance: { instance: I18nInstance };
|
|
161
|
+
/** The wrapper's own translate API; it may differ from i18next's `t`. */
|
|
162
|
+
t(key: string): string;
|
|
163
|
+
on?(...args: Parameters<i18n['on']>): unknown;
|
|
164
|
+
off?(...args: Parameters<i18n['off']>): unknown;
|
|
165
|
+
// SSR clones the wrapper per request; detection may create or clone one.
|
|
166
|
+
createInstance?(options?: InitOptions): I18nWrapperInstance;
|
|
167
|
+
cloneInstance?(options?: CloneOptions): I18nWrapperInstance;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** The instance a Modern app configures: i18next itself, or a wrapper of it. */
|
|
171
|
+
export type I18nRuntimeInstance = I18nInstance | I18nWrapperInstance;
|
|
172
|
+
|
|
164
173
|
type LanguageDetectorOrder = string[];
|
|
165
174
|
type LanguageDetectorCaches = boolean | string[];
|
|
166
175
|
export interface LanguageDetectorOptions {
|
|
@@ -211,7 +220,7 @@ export type I18nInitOptions = {
|
|
|
211
220
|
forkResourceStore?: boolean;
|
|
212
221
|
};
|
|
213
222
|
|
|
214
|
-
export function isI18nInstance(obj: unknown): obj is
|
|
223
|
+
export function isI18nInstance(obj: unknown): obj is I18nRuntimeInstance {
|
|
215
224
|
if (!obj || typeof obj !== 'object') {
|
|
216
225
|
return false;
|
|
217
226
|
}
|
|
@@ -229,7 +238,7 @@ export function isI18nInstance(obj: unknown): obj is I18nInstance {
|
|
|
229
238
|
async function tryImportI18next(): Promise<I18nInstance | null> {
|
|
230
239
|
try {
|
|
231
240
|
const i18next = await import('i18next');
|
|
232
|
-
return i18next.default
|
|
241
|
+
return i18next.default;
|
|
233
242
|
} catch (error) {
|
|
234
243
|
return null;
|
|
235
244
|
}
|
|
@@ -241,25 +250,21 @@ async function createI18nextInstance(): Promise<I18nInstance | null> {
|
|
|
241
250
|
if (!i18next) {
|
|
242
251
|
return null;
|
|
243
252
|
}
|
|
244
|
-
return
|
|
245
|
-
|
|
246
|
-
|
|
253
|
+
return (
|
|
254
|
+
i18next.createInstance?.({
|
|
255
|
+
initImmediate: false,
|
|
256
|
+
}) ?? null
|
|
257
|
+
);
|
|
247
258
|
} catch (error) {
|
|
248
259
|
return null;
|
|
249
260
|
}
|
|
250
261
|
}
|
|
251
262
|
|
|
252
263
|
export function getI18nextInstanceForProvider(
|
|
253
|
-
instance:
|
|
264
|
+
instance: I18nRuntimeInstance,
|
|
254
265
|
language?: string,
|
|
255
266
|
): I18nInstance {
|
|
256
|
-
|
|
257
|
-
if (isI18nWrapperInstance(instance)) {
|
|
258
|
-
const i18nextInstance = getI18nWrapperI18nextInstance(instance);
|
|
259
|
-
if (i18nextInstance) {
|
|
260
|
-
providerInstance = i18nextInstance;
|
|
261
|
-
}
|
|
262
|
-
}
|
|
267
|
+
const providerInstance = getActualI18nextInstance(instance);
|
|
263
268
|
|
|
264
269
|
if (!language || typeof Proxy === 'undefined') {
|
|
265
270
|
return providerInstance;
|
|
@@ -301,12 +306,8 @@ export function getI18nextInstanceForProvider(
|
|
|
301
306
|
|
|
302
307
|
export async function getI18nInstance(
|
|
303
308
|
userInstance?: unknown,
|
|
304
|
-
): Promise<
|
|
309
|
+
): Promise<I18nRuntimeInstance> {
|
|
305
310
|
if (userInstance) {
|
|
306
|
-
if (isI18nWrapperInstance(userInstance)) {
|
|
307
|
-
return userInstance as I18nInstance;
|
|
308
|
-
}
|
|
309
|
-
|
|
310
311
|
if (isI18nInstance(userInstance)) {
|
|
311
312
|
return userInstance;
|
|
312
313
|
}
|
|
@@ -4,7 +4,7 @@ import { mergeBackendOptions } from './backend';
|
|
|
4
4
|
import { HttpBackendWithSave, useI18nextBackend } from './backend/middleware';
|
|
5
5
|
import { SdkBackend } from './backend/sdk-backend';
|
|
6
6
|
import { cacheUserLanguage, mergeDetectionOptions } from './detection';
|
|
7
|
-
import type { I18nInitOptions,
|
|
7
|
+
import type { I18nInitOptions, I18nRuntimeInstance } from './instance';
|
|
8
8
|
import {
|
|
9
9
|
getActualI18nextInstance,
|
|
10
10
|
isI18nInstance,
|
|
@@ -20,9 +20,11 @@ type MergedBackendOptions = NonNullable<I18nInitOptions['backend']> & {
|
|
|
20
20
|
backendOptions?: unknown;
|
|
21
21
|
};
|
|
22
22
|
|
|
23
|
-
export function assertI18nInstance(
|
|
23
|
+
export function assertI18nInstance(
|
|
24
|
+
obj: unknown,
|
|
25
|
+
): asserts obj is I18nRuntimeInstance {
|
|
24
26
|
if (!isI18nInstance(obj)) {
|
|
25
|
-
throw new Error('Object does not implement
|
|
27
|
+
throw new Error('Object does not implement I18nRuntimeInstance interface');
|
|
26
28
|
}
|
|
27
29
|
}
|
|
28
30
|
|
|
@@ -37,7 +39,7 @@ export const buildInitOptions = async (
|
|
|
37
39
|
mergedBackend: MergedBackendOptions | undefined,
|
|
38
40
|
userInitOptions?: I18nInitOptions,
|
|
39
41
|
useSuspense?: boolean,
|
|
40
|
-
i18nInstance?:
|
|
42
|
+
i18nInstance?: I18nRuntimeInstance,
|
|
41
43
|
): Promise<I18nInitOptions> => {
|
|
42
44
|
const defaultUseSuspense =
|
|
43
45
|
useSuspense !== undefined
|
|
@@ -137,7 +139,7 @@ export const buildInitOptions = async (
|
|
|
137
139
|
* Ensure i18n instance language matches the final detected language
|
|
138
140
|
*/
|
|
139
141
|
export const ensureLanguageMatch = async (
|
|
140
|
-
i18nInstance:
|
|
142
|
+
i18nInstance: I18nRuntimeInstance,
|
|
141
143
|
finalLanguage: string,
|
|
142
144
|
): Promise<void> => {
|
|
143
145
|
if (i18nInstance.language !== finalLanguage) {
|
|
@@ -154,7 +156,7 @@ export const ensureLanguageMatch = async (
|
|
|
154
156
|
* @param options - Optional configuration
|
|
155
157
|
*/
|
|
156
158
|
export const changeI18nLanguage = async (
|
|
157
|
-
i18nInstance:
|
|
159
|
+
i18nInstance: I18nRuntimeInstance,
|
|
158
160
|
newLang: string,
|
|
159
161
|
options?: {
|
|
160
162
|
detectionOptions?: I18nInitOptions['detection'];
|
|
@@ -184,7 +186,7 @@ export const changeI18nLanguage = async (
|
|
|
184
186
|
* Initialize i18n instance if not already initialized
|
|
185
187
|
*/
|
|
186
188
|
export const initializeI18nInstance = async (
|
|
187
|
-
i18nInstance:
|
|
189
|
+
i18nInstance: I18nRuntimeInstance,
|
|
188
190
|
finalLanguage: string,
|
|
189
191
|
fallbackLanguage: string,
|
|
190
192
|
languages: string[],
|
|
@@ -257,8 +259,10 @@ export const initializeI18nInstance = async (
|
|
|
257
259
|
/**
|
|
258
260
|
* Type guard to check if i18n instance has options property
|
|
259
261
|
*/
|
|
260
|
-
function hasOptions(
|
|
261
|
-
|
|
262
|
+
function hasOptions(
|
|
263
|
+
instance: I18nRuntimeInstance,
|
|
264
|
+
): instance is I18nRuntimeInstance & {
|
|
265
|
+
options: NonNullable<I18nRuntimeInstance['options']>;
|
|
262
266
|
} {
|
|
263
267
|
return instance.options !== undefined && instance.options !== null;
|
|
264
268
|
}
|
|
@@ -267,7 +271,7 @@ function hasOptions(instance: I18nInstance): instance is I18nInstance & {
|
|
|
267
271
|
* Setup cloned instance for SSR with backend support
|
|
268
272
|
*/
|
|
269
273
|
export const setupClonedInstance = async (
|
|
270
|
-
i18nInstance:
|
|
274
|
+
i18nInstance: I18nRuntimeInstance,
|
|
271
275
|
finalLanguage: string,
|
|
272
276
|
fallbackLanguage: string,
|
|
273
277
|
languages: string[],
|
|
@@ -5,8 +5,7 @@ import type {
|
|
|
5
5
|
BaseBackendOptions,
|
|
6
6
|
BaseLocaleDetectionOptions,
|
|
7
7
|
} from '../shared/type';
|
|
8
|
-
import type { I18nInitOptions,
|
|
9
|
-
import { getI18nInstance } from './i18n';
|
|
8
|
+
import type { I18nInitOptions, I18nRuntimeInstance } from './i18n';
|
|
10
9
|
import { mergeBackendOptions } from './i18n/backend';
|
|
11
10
|
import {
|
|
12
11
|
detectLanguageWithPriority,
|
|
@@ -14,6 +13,7 @@ import {
|
|
|
14
13
|
mergeDetectionOptions,
|
|
15
14
|
} from './i18n/detection';
|
|
16
15
|
import { useI18nextLanguageDetector } from './i18n/detection/middleware';
|
|
16
|
+
import { getI18nInstance } from './i18n/instance';
|
|
17
17
|
import type { ReactI18nextIntegration } from './reactI18next';
|
|
18
18
|
import { getPathname } from './utils';
|
|
19
19
|
|
|
@@ -53,13 +53,13 @@ interface I18nRuntimeApi {
|
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
export interface RuntimeContextWithI18n extends TInternalRuntimeContext {
|
|
56
|
-
i18nInstance?:
|
|
56
|
+
i18nInstance?: I18nRuntimeInstance;
|
|
57
57
|
changeLanguage?: (lang: string) => Promise<void>;
|
|
58
58
|
}
|
|
59
59
|
|
|
60
60
|
interface SetupI18nBeforeRenderOptions {
|
|
61
61
|
api: I18nRuntimeApi;
|
|
62
|
-
userI18nInstance?:
|
|
62
|
+
userI18nInstance?: I18nRuntimeInstance;
|
|
63
63
|
initOptions?: I18nInitOptions;
|
|
64
64
|
backend?: BaseBackendOptions;
|
|
65
65
|
backendEnabled: boolean;
|
|
@@ -74,10 +74,12 @@ interface SetupI18nBeforeRenderOptions {
|
|
|
74
74
|
) => void;
|
|
75
75
|
}
|
|
76
76
|
|
|
77
|
+
const reactI18nextInstances = new WeakSet<object>();
|
|
78
|
+
|
|
77
79
|
export async function setupI18nBeforeRender(
|
|
78
80
|
context: RuntimeContextWithI18n,
|
|
79
81
|
options: SetupI18nBeforeRenderOptions,
|
|
80
|
-
): Promise<
|
|
82
|
+
): Promise<I18nRuntimeInstance> {
|
|
81
83
|
const {
|
|
82
84
|
api,
|
|
83
85
|
userI18nInstance,
|
|
@@ -106,8 +108,12 @@ export async function setupI18nBeforeRender(
|
|
|
106
108
|
|
|
107
109
|
const reactI18nextIntegration = await resolveReactI18nextIntegration();
|
|
108
110
|
setI18nextProvider(reactI18nextIntegration?.I18nextProvider ?? null);
|
|
109
|
-
if (
|
|
111
|
+
if (
|
|
112
|
+
reactI18nextIntegration?.initReactI18next &&
|
|
113
|
+
!reactI18nextInstances.has(i18nInstance)
|
|
114
|
+
) {
|
|
110
115
|
i18nInstance.use(reactI18nextIntegration.initReactI18next);
|
|
116
|
+
reactI18nextInstances.add(i18nInstance);
|
|
111
117
|
}
|
|
112
118
|
|
|
113
119
|
const pathname = getPathname(context);
|
|
@@ -11,13 +11,13 @@ import {
|
|
|
11
11
|
useLanguageSync,
|
|
12
12
|
useSdkResourcesLoader,
|
|
13
13
|
} from './hooks';
|
|
14
|
-
import type {
|
|
14
|
+
import type { I18nRuntimeInstance } from './i18n';
|
|
15
15
|
import { getI18nextInstanceForProvider } from './i18n/instance';
|
|
16
16
|
import type { RuntimeContextWithI18n } from './pluginSetup';
|
|
17
17
|
import { useI18nRouterAdapter } from './routerAdapter';
|
|
18
18
|
|
|
19
19
|
export interface I18nLanguageSynchronizationProps {
|
|
20
|
-
i18nInstance:
|
|
20
|
+
i18nInstance: I18nRuntimeInstance | undefined;
|
|
21
21
|
localePathRedirect: boolean;
|
|
22
22
|
languages: string[];
|
|
23
23
|
pathname: string | undefined;
|
|
@@ -58,7 +58,7 @@ interface I18nRootWrapperOptions {
|
|
|
58
58
|
urlStrategy?: I18nUrlStrategy;
|
|
59
59
|
NavigationProvider?: React.ComponentType<React.PropsWithChildren>;
|
|
60
60
|
LanguageSynchronization?: React.ComponentType<I18nLanguageSynchronizationProps>;
|
|
61
|
-
getLatestI18nInstance: () =>
|
|
61
|
+
getLatestI18nInstance: () => I18nRuntimeInstance | undefined;
|
|
62
62
|
getI18nextProvider: () => React.ComponentType<any> | null | undefined;
|
|
63
63
|
}
|
|
64
64
|
|
|
@@ -116,18 +116,24 @@ export const createI18nRootWrapper =
|
|
|
116
116
|
urlStrategy,
|
|
117
117
|
);
|
|
118
118
|
|
|
119
|
+
// No instance means this root rendered outside the plugin's
|
|
120
|
+
// onBeforeRender lifecycle. Modern i18n context is only provided for a
|
|
121
|
+
// real instance, so `useModernI18n` reports the missing provider instead
|
|
122
|
+
// of handing out a placeholder that has no translator.
|
|
119
123
|
const contextValue = useMemo(
|
|
120
124
|
() =>
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
125
|
+
i18nInstance
|
|
126
|
+
? createContextValue(
|
|
127
|
+
lang,
|
|
128
|
+
i18nInstance,
|
|
129
|
+
entryName,
|
|
130
|
+
languages,
|
|
131
|
+
localePathRedirect,
|
|
132
|
+
ignoreRedirectRoutes,
|
|
133
|
+
setLang,
|
|
134
|
+
urlStrategy,
|
|
135
|
+
)
|
|
136
|
+
: undefined,
|
|
131
137
|
[
|
|
132
138
|
lang,
|
|
133
139
|
i18nInstance,
|
|
@@ -174,14 +180,18 @@ export const createI18nRootWrapper =
|
|
|
174
180
|
prevLangRef={prevLangRef}
|
|
175
181
|
setLang={setLang}
|
|
176
182
|
>
|
|
177
|
-
{synchronizeLanguage =>
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
183
|
+
{synchronizeLanguage =>
|
|
184
|
+
contextValue ? (
|
|
185
|
+
<ModernI18nProvider
|
|
186
|
+
i18nextProvider={I18nextProvider}
|
|
187
|
+
value={{ ...contextValue, synchronizeLanguage }}
|
|
188
|
+
>
|
|
189
|
+
{appContent}
|
|
190
|
+
</ModernI18nProvider>
|
|
191
|
+
) : (
|
|
192
|
+
appContent
|
|
193
|
+
)
|
|
194
|
+
}
|
|
185
195
|
</LanguageSynchronization>
|
|
186
196
|
</>
|
|
187
197
|
);
|
package/src/runtime/types.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import type { I18nInitOptions,
|
|
1
|
+
import type { I18nInitOptions, I18nRuntimeInstance } from './i18n';
|
|
2
2
|
|
|
3
3
|
declare module '@modern-js/runtime' {
|
|
4
4
|
interface RuntimeConfig {
|
|
5
5
|
i18n?: {
|
|
6
|
-
i18nInstance?:
|
|
6
|
+
i18nInstance?: I18nRuntimeInstance;
|
|
7
7
|
changeLanguage?: (lang: string) => void;
|
|
8
8
|
setLang?: (lang: string) => void;
|
|
9
9
|
initOptions?: I18nInitOptions;
|
|
@@ -11,7 +11,7 @@ declare module '@modern-js/runtime' {
|
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
interface TRuntimeContext {
|
|
14
|
-
i18nInstance?:
|
|
14
|
+
i18nInstance?: I18nRuntimeInstance;
|
|
15
15
|
changeLanguage?: (lang: string) => Promise<void>;
|
|
16
16
|
}
|
|
17
17
|
}
|