@bleedingdev/modern-js-plugin-i18n 3.11.3-ultramodern.1 → 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/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/providerComposition.js +3 -3
- package/dist/cjs/shared/deepMerge.js +2 -2
- package/dist/cjs/shared/utils.js +3 -3
- 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/providerComposition.mjs +3 -3
- package/dist/esm/server/index.mjs +1 -1
- 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/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/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 +24 -23
- 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 +4 -4
- package/src/runtime/providerComposition.tsx +31 -21
- package/src/runtime/types.ts +3 -3
package/src/runtime/context.tsx
CHANGED
|
@@ -7,10 +7,9 @@ import {
|
|
|
7
7
|
getPathLanguage,
|
|
8
8
|
isI18nLanguageSupported,
|
|
9
9
|
isI18nResourcesReady,
|
|
10
|
-
translateI18n,
|
|
11
10
|
} from './contextHelpers';
|
|
12
|
-
import type { I18nInstance } from './i18n';
|
|
13
|
-
import type
|
|
11
|
+
import type { I18nInstance, I18nRuntimeInstance } from './i18n';
|
|
12
|
+
import { getActualI18nextInstance, type Resources } from './i18n/instance';
|
|
14
13
|
|
|
15
14
|
import { useI18nRouterAdapter } from './routerAdapter';
|
|
16
15
|
import {
|
|
@@ -18,11 +17,11 @@ import {
|
|
|
18
17
|
ReactI18nextProviderContext,
|
|
19
18
|
} from './sharedContexts';
|
|
20
19
|
|
|
21
|
-
export { getActualI18nextInstance }
|
|
20
|
+
export { getActualI18nextInstance };
|
|
22
21
|
|
|
23
22
|
export interface ModernI18nContextValue {
|
|
24
23
|
language: string;
|
|
25
|
-
i18nInstance:
|
|
24
|
+
i18nInstance: I18nRuntimeInstance;
|
|
26
25
|
// Plugin configuration for useModernI18n hook
|
|
27
26
|
entryName?: string;
|
|
28
27
|
languages?: string[];
|
|
@@ -88,11 +87,15 @@ export const FederatedI18nBoundary: FC<FederatedI18nBoundaryProps> = props => {
|
|
|
88
87
|
};
|
|
89
88
|
|
|
90
89
|
export interface UseModernI18nReturn<
|
|
91
|
-
TInstance extends
|
|
90
|
+
TInstance extends I18nRuntimeInstance = I18nInstance,
|
|
92
91
|
> {
|
|
93
92
|
language: string;
|
|
94
93
|
changeLanguage: (newLang: string) => Promise<void>;
|
|
95
|
-
|
|
94
|
+
/**
|
|
95
|
+
* The configured instance's own `t`, bound to it: i18next's `t` for an
|
|
96
|
+
* i18next instance, the wrapper's translate API for a wrapper.
|
|
97
|
+
*/
|
|
98
|
+
t: OmitThisParameter<TInstance['t']>;
|
|
96
99
|
i18nInstance: TInstance;
|
|
97
100
|
supportedLanguages: string[];
|
|
98
101
|
urlStrategy?: I18nUrlStrategy;
|
|
@@ -111,18 +114,17 @@ export interface UseModernI18nReturn<
|
|
|
111
114
|
* - List of supported languages
|
|
112
115
|
* - Helper function to check if language is supported
|
|
113
116
|
*
|
|
114
|
-
*
|
|
115
|
-
*
|
|
116
|
-
*
|
|
117
|
-
* is
|
|
118
|
-
*
|
|
119
|
-
* instance the provider was given.
|
|
120
|
-
* @param options - Optional configuration to override context settings
|
|
117
|
+
* Without a type argument, `i18nInstance` and `t` are typed for an i18next
|
|
118
|
+
* instance, so `t` keeps i18next's keys, options and selectors. An app that
|
|
119
|
+
* configures a wrapper passes its type (e.g. `useModernI18n<MyWrapper>()`).
|
|
120
|
+
* Either way the type is a caller assertion that the overload records; it is
|
|
121
|
+
* not verified at runtime.
|
|
121
122
|
* @returns Object containing i18n functionality and utilities
|
|
122
123
|
*/
|
|
123
|
-
export
|
|
124
|
-
TInstance extends
|
|
125
|
-
>(): UseModernI18nReturn<TInstance
|
|
124
|
+
export function useModernI18n<
|
|
125
|
+
TInstance extends I18nRuntimeInstance = I18nInstance,
|
|
126
|
+
>(): UseModernI18nReturn<TInstance>;
|
|
127
|
+
export function useModernI18n(): UseModernI18nReturn<I18nRuntimeInstance> {
|
|
126
128
|
const context = useContext(ModernI18nContext);
|
|
127
129
|
if (!context) {
|
|
128
130
|
throw new Error('useModernI18n must be used within ModernI18nProvider');
|
|
@@ -190,9 +192,11 @@ export const useModernI18n = <
|
|
|
190
192
|
],
|
|
191
193
|
);
|
|
192
194
|
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
195
|
+
// The configured instance's own `t`, bound so a destructured `t` keeps its
|
|
196
|
+
// receiver: a wrapper's translate API stays in charge of its output. A new
|
|
197
|
+
// function per language lets memoized consumers re-render on switch.
|
|
198
|
+
const t = useMemo(
|
|
199
|
+
() => i18nInstance.t.bind(i18nInstance),
|
|
196
200
|
[currentLanguage, i18nInstance],
|
|
197
201
|
);
|
|
198
202
|
|
|
@@ -213,12 +217,10 @@ export const useModernI18n = <
|
|
|
213
217
|
language: currentLanguage,
|
|
214
218
|
changeLanguage,
|
|
215
219
|
t,
|
|
216
|
-
|
|
217
|
-
// narrows to the concrete instance type via the TInstance type argument.
|
|
218
|
-
i18nInstance: i18nInstance as TInstance,
|
|
220
|
+
i18nInstance,
|
|
219
221
|
supportedLanguages: languages || [],
|
|
220
222
|
urlStrategy,
|
|
221
223
|
isLanguageSupported,
|
|
222
224
|
isResourcesReady,
|
|
223
225
|
};
|
|
224
|
-
}
|
|
226
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { isBrowser } from '@modern-js/runtime';
|
|
2
2
|
import type { I18nUrlStrategy } from '../shared/urlStrategy';
|
|
3
|
-
import type {
|
|
3
|
+
import type { I18nRuntimeInstance } from './i18n';
|
|
4
4
|
import type { SdkBackend } from './i18n/backend/sdk-backend';
|
|
5
5
|
import { cacheUserLanguage } from './i18n/detection';
|
|
6
6
|
import {
|
|
@@ -41,7 +41,7 @@ export function getPathLanguage(
|
|
|
41
41
|
}
|
|
42
42
|
|
|
43
43
|
export function cacheI18nLanguage(
|
|
44
|
-
i18nInstance:
|
|
44
|
+
i18nInstance: I18nRuntimeInstance,
|
|
45
45
|
language: string,
|
|
46
46
|
) {
|
|
47
47
|
if (isBrowser()) {
|
|
@@ -51,7 +51,7 @@ export function cacheI18nLanguage(
|
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
export async function changeI18nInstanceLanguage(
|
|
54
|
-
i18nInstance:
|
|
54
|
+
i18nInstance: I18nRuntimeInstance,
|
|
55
55
|
language: string,
|
|
56
56
|
): Promise<void> {
|
|
57
57
|
if (i18nInstance.setLang) {
|
|
@@ -62,7 +62,7 @@ export async function changeI18nInstanceLanguage(
|
|
|
62
62
|
}
|
|
63
63
|
|
|
64
64
|
interface ChangeModernI18nLanguageOptions {
|
|
65
|
-
i18nInstance:
|
|
65
|
+
i18nInstance: I18nRuntimeInstance;
|
|
66
66
|
updateLanguage?: (newLang: string) => void;
|
|
67
67
|
localePathRedirect?: boolean;
|
|
68
68
|
ignoreRedirectRoutes?: string[] | ((pathname: string) => boolean);
|
|
@@ -181,27 +181,6 @@ export async function changeModernI18nLanguage(
|
|
|
181
181
|
}
|
|
182
182
|
}
|
|
183
183
|
|
|
184
|
-
export function translateI18n(
|
|
185
|
-
i18nInstance: I18nInstance,
|
|
186
|
-
key: string | string[],
|
|
187
|
-
...args: unknown[]
|
|
188
|
-
): string {
|
|
189
|
-
if (typeof i18nInstance.t !== 'function') {
|
|
190
|
-
throw new Error('i18nInstance.t required');
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
// Called through `.call` with the original receiver: `I18nInstance.t` is a
|
|
194
|
-
// method, and custom instances (wrappers, class-based translators) may read
|
|
195
|
-
// `this`. Extracting the function and calling it bare would silently drop the
|
|
196
|
-
// receiver — i18next's own `t` happens to be bound, which would hide it.
|
|
197
|
-
const translate = i18nInstance.t as (
|
|
198
|
-
this: I18nInstance,
|
|
199
|
-
key: string | string[],
|
|
200
|
-
...rest: unknown[]
|
|
201
|
-
) => string;
|
|
202
|
-
return translate.call(i18nInstance, key, ...args);
|
|
203
|
-
}
|
|
204
|
-
|
|
205
184
|
export function isI18nLanguageSupported(
|
|
206
185
|
languages: string[] | undefined,
|
|
207
186
|
lang: string,
|
|
@@ -210,7 +189,7 @@ export function isI18nLanguageSupported(
|
|
|
210
189
|
}
|
|
211
190
|
|
|
212
191
|
export function isI18nResourcesReady(
|
|
213
|
-
i18nInstance:
|
|
192
|
+
i18nInstance: I18nRuntimeInstance,
|
|
214
193
|
currentLanguage: string,
|
|
215
194
|
): boolean {
|
|
216
195
|
if (!i18nInstance?.isInitialized) {
|
package/src/runtime/core.tsx
CHANGED
|
@@ -6,7 +6,7 @@ import type {
|
|
|
6
6
|
BaseLocaleDetectionOptions,
|
|
7
7
|
} from '../shared/type';
|
|
8
8
|
import { asI18nUrlStrategy, type I18nUrlStrategy } from '../shared/urlStrategy';
|
|
9
|
-
import type { I18nInitOptions,
|
|
9
|
+
import type { I18nInitOptions, I18nRuntimeInstance } from './i18n';
|
|
10
10
|
import { I18nRouterNavigationProvider } from './navigation';
|
|
11
11
|
import {
|
|
12
12
|
type RuntimeContextWithI18n,
|
|
@@ -30,6 +30,8 @@ export type { I18nUrlStrategy } from '../shared/urlStrategy';
|
|
|
30
30
|
export type {
|
|
31
31
|
I18nInitOptions,
|
|
32
32
|
I18nInstance,
|
|
33
|
+
I18nRuntimeInstance,
|
|
34
|
+
I18nWrapperInstance,
|
|
33
35
|
Resources,
|
|
34
36
|
TranslateFn,
|
|
35
37
|
} from './i18n/instance';
|
|
@@ -53,7 +55,7 @@ export interface I18nPluginOptions {
|
|
|
53
55
|
entryName?: string;
|
|
54
56
|
localeDetection?: BaseLocaleDetectionOptions;
|
|
55
57
|
backend?: BaseBackendOptions;
|
|
56
|
-
i18nInstance?:
|
|
58
|
+
i18nInstance?: I18nRuntimeInstance;
|
|
57
59
|
changeLanguage?: (lang: string) => void;
|
|
58
60
|
initOptions?: I18nInitOptions;
|
|
59
61
|
htmlLangAttr?: boolean;
|
|
@@ -107,7 +109,7 @@ export const createI18nPlugin =
|
|
|
107
109
|
const resolvedUrlStrategy =
|
|
108
110
|
asI18nUrlStrategy(urlStrategy) ??
|
|
109
111
|
resolveMappedUrlStrategy(localeDetection);
|
|
110
|
-
let latestI18nInstance:
|
|
112
|
+
let latestI18nInstance: I18nRuntimeInstance | undefined;
|
|
111
113
|
let I18nextProvider: React.ComponentType<any> | null;
|
|
112
114
|
|
|
113
115
|
api.onBeforeRender(async (context: RuntimeContextWithI18n) => {
|
package/src/runtime/hooks.ts
CHANGED
|
@@ -6,7 +6,7 @@ import {
|
|
|
6
6
|
cacheI18nLanguage,
|
|
7
7
|
changeI18nInstanceLanguage,
|
|
8
8
|
} from './contextHelpers';
|
|
9
|
-
import type {
|
|
9
|
+
import type { I18nRuntimeInstance } from './i18n';
|
|
10
10
|
import {
|
|
11
11
|
getI18nSdkBackendId,
|
|
12
12
|
I18N_SDK_RESOURCES_LOADED_EVENT,
|
|
@@ -24,32 +24,13 @@ type WindowWithSSRData = Window & {
|
|
|
24
24
|
_SSR_DATA?: unknown;
|
|
25
25
|
};
|
|
26
26
|
|
|
27
|
-
type ResourceStoreWithEvents = NonNullable<
|
|
27
|
+
type ResourceStoreWithEvents = NonNullable<I18nRuntimeInstance['store']> & {
|
|
28
28
|
emit?: (event: string, ...args: unknown[]) => void;
|
|
29
29
|
};
|
|
30
30
|
|
|
31
|
-
function createMinimalI18nInstance(language: string): I18nInstance {
|
|
32
|
-
const minimalInstance: I18nInstance = {
|
|
33
|
-
language,
|
|
34
|
-
isInitialized: false,
|
|
35
|
-
init: () => Promise.resolve(undefined),
|
|
36
|
-
// Keep the pre-existing loud failure: an uninitialised instance has no
|
|
37
|
-
// translator. Never echo the key back — that would silently render raw
|
|
38
|
-
// keys as if they were translations. This throws the same error that
|
|
39
|
-
// translateI18n (contextHelpers.ts:187) already throws today.
|
|
40
|
-
t: () => {
|
|
41
|
-
throw new Error('i18nInstance.t required');
|
|
42
|
-
},
|
|
43
|
-
use: () => {},
|
|
44
|
-
createInstance: () => minimalInstance,
|
|
45
|
-
services: {},
|
|
46
|
-
};
|
|
47
|
-
return minimalInstance;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
31
|
export function createContextValue(
|
|
51
32
|
lang: string,
|
|
52
|
-
i18nInstance:
|
|
33
|
+
i18nInstance: I18nRuntimeInstance,
|
|
53
34
|
entryName: string | undefined,
|
|
54
35
|
languages: string[],
|
|
55
36
|
localePathRedirect: boolean,
|
|
@@ -57,10 +38,9 @@ export function createContextValue(
|
|
|
57
38
|
setLang: (lang: string) => void,
|
|
58
39
|
urlStrategy?: I18nUrlStrategy,
|
|
59
40
|
) {
|
|
60
|
-
const instance = i18nInstance || createMinimalI18nInstance(lang);
|
|
61
41
|
return {
|
|
62
42
|
language: lang,
|
|
63
|
-
i18nInstance
|
|
43
|
+
i18nInstance,
|
|
64
44
|
entryName,
|
|
65
45
|
languages,
|
|
66
46
|
localePathRedirect,
|
|
@@ -71,7 +51,7 @@ export function createContextValue(
|
|
|
71
51
|
}
|
|
72
52
|
|
|
73
53
|
export function useSdkResourcesLoader(
|
|
74
|
-
i18nInstance:
|
|
54
|
+
i18nInstance: I18nRuntimeInstance | undefined,
|
|
75
55
|
setForceUpdate: React.Dispatch<React.SetStateAction<number>>,
|
|
76
56
|
) {
|
|
77
57
|
useEffect(() => {
|
|
@@ -172,7 +152,7 @@ export function useSdkResourcesLoader(
|
|
|
172
152
|
* We use process.env.MODERN_TARGET to ensure this code is only included in browser bundles.
|
|
173
153
|
*/
|
|
174
154
|
export function useClientSideRedirect(
|
|
175
|
-
i18nInstance:
|
|
155
|
+
i18nInstance: I18nRuntimeInstance | undefined,
|
|
176
156
|
localePathRedirect: boolean,
|
|
177
157
|
languages: string[],
|
|
178
158
|
fallbackLanguage: string,
|
|
@@ -275,7 +255,7 @@ export function useClientSideRedirect(
|
|
|
275
255
|
}
|
|
276
256
|
|
|
277
257
|
export function useLanguageSync(
|
|
278
|
-
i18nInstance:
|
|
258
|
+
i18nInstance: I18nRuntimeInstance | undefined,
|
|
279
259
|
localePathRedirect: boolean,
|
|
280
260
|
languages: string[],
|
|
281
261
|
pathname: string | undefined,
|
|
@@ -3,7 +3,7 @@ import type {
|
|
|
3
3
|
BaseBackendOptions,
|
|
4
4
|
ChainedBackendConfig,
|
|
5
5
|
} from '../../../shared/type';
|
|
6
|
-
import type {
|
|
6
|
+
import type { I18nRuntimeInstance } from '../instance';
|
|
7
7
|
import { getActualI18nextInstance } from '../instance';
|
|
8
8
|
import { SdkBackend } from './sdk-backend';
|
|
9
9
|
|
|
@@ -50,7 +50,7 @@ function buildChainedBackendConfig(
|
|
|
50
50
|
}
|
|
51
51
|
|
|
52
52
|
function setupChainedBackend(
|
|
53
|
-
i18nInstance:
|
|
53
|
+
i18nInstance: I18nRuntimeInstance,
|
|
54
54
|
backend: BackendConfigWithChained,
|
|
55
55
|
BackendWithSave: BackendConstructor,
|
|
56
56
|
) {
|
|
@@ -86,7 +86,7 @@ function cleanBackendConfig(backend: BackendConfigWithChained) {
|
|
|
86
86
|
* @param backend - Optional backend configuration
|
|
87
87
|
*/
|
|
88
88
|
export function useI18nextBackendCommon(
|
|
89
|
-
i18nInstance:
|
|
89
|
+
i18nInstance: I18nRuntimeInstance,
|
|
90
90
|
BackendWithSave: BackendConstructor,
|
|
91
91
|
BackendBase: BackendConstructor,
|
|
92
92
|
backend?: BackendConfigWithChained,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import FsBackendModule from 'i18next-fs-backend';
|
|
2
2
|
import type { ExtendedBackendOptions } from '../../../shared/type';
|
|
3
|
-
import type {
|
|
3
|
+
import type { I18nRuntimeInstance } from '../instance';
|
|
4
4
|
import { useI18nextBackendCommon } from './middleware.common';
|
|
5
5
|
|
|
6
6
|
type BackendConstructor = new (...args: any[]) => any;
|
|
@@ -50,7 +50,7 @@ export class FsBackendWithSave extends Backend {
|
|
|
50
50
|
export const HttpBackendWithSave = FsBackendWithSave;
|
|
51
51
|
|
|
52
52
|
export const useI18nextBackend = (
|
|
53
|
-
i18nInstance:
|
|
53
|
+
i18nInstance: I18nRuntimeInstance,
|
|
54
54
|
backend?: ExtendedBackendOptions,
|
|
55
55
|
) => {
|
|
56
56
|
return useI18nextBackendCommon(
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import Backend from 'i18next-http-backend';
|
|
2
2
|
import type { ExtendedBackendOptions } from '../../../shared/type';
|
|
3
|
-
import type {
|
|
3
|
+
import type { I18nRuntimeInstance } from '../instance';
|
|
4
4
|
import { useI18nextBackendCommon } from './middleware.common';
|
|
5
5
|
|
|
6
6
|
/**
|
|
@@ -16,7 +16,7 @@ export class HttpBackendWithSave extends Backend {
|
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
export const useI18nextBackend = (
|
|
19
|
-
i18nInstance:
|
|
19
|
+
i18nInstance: I18nRuntimeInstance,
|
|
20
20
|
backend?: ExtendedBackendOptions,
|
|
21
21
|
) => {
|
|
22
22
|
return useI18nextBackendCommon(
|
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
I18nInitOptions,
|
|
3
|
-
|
|
3
|
+
I18nRuntimeInstance,
|
|
4
4
|
LanguageDetectorOptions,
|
|
5
5
|
} from '../instance';
|
|
6
6
|
|
|
7
7
|
interface DetectorCacheEntry {
|
|
8
|
-
instance:
|
|
8
|
+
instance: I18nRuntimeInstance;
|
|
9
9
|
isTemporary: boolean;
|
|
10
10
|
configKey: string;
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
-
type DetectorCleanupInstance =
|
|
13
|
+
type DetectorCleanupInstance = I18nRuntimeInstance & {
|
|
14
14
|
removeAllListeners?: () => void;
|
|
15
15
|
off?: (event: string) => void;
|
|
16
|
-
services?:
|
|
16
|
+
services?: I18nRuntimeInstance['services'] & {
|
|
17
17
|
backendConnector?: {
|
|
18
18
|
backend?: {
|
|
19
19
|
stop?: () => void;
|
|
@@ -24,7 +24,7 @@ type DetectorCleanupInstance = I18nInstance & {
|
|
|
24
24
|
};
|
|
25
25
|
|
|
26
26
|
export const detectorInstanceCache = new WeakMap<
|
|
27
|
-
|
|
27
|
+
I18nRuntimeInstance,
|
|
28
28
|
DetectorCacheEntry
|
|
29
29
|
>();
|
|
30
30
|
|
|
@@ -135,9 +135,9 @@ const cleanupDetectorCacheEntry = (entry?: DetectorCacheEntry) => {
|
|
|
135
135
|
};
|
|
136
136
|
|
|
137
137
|
export const createDetectorInstance = (
|
|
138
|
-
baseInstance:
|
|
138
|
+
baseInstance: I18nRuntimeInstance,
|
|
139
139
|
configKey: string,
|
|
140
|
-
): { instance:
|
|
140
|
+
): { instance: I18nRuntimeInstance; isTemporary: boolean } => {
|
|
141
141
|
const cached = detectorInstanceCache.get(baseInstance);
|
|
142
142
|
if (cached && cached.configKey === configKey) {
|
|
143
143
|
return { instance: cached.instance, isTemporary: cached.isTemporary };
|
|
@@ -149,7 +149,7 @@ export const createDetectorInstance = (
|
|
|
149
149
|
}
|
|
150
150
|
|
|
151
151
|
const createNewInstance = (): {
|
|
152
|
-
instance:
|
|
152
|
+
instance: I18nRuntimeInstance;
|
|
153
153
|
isTemporary: boolean;
|
|
154
154
|
} => {
|
|
155
155
|
if (typeof baseInstance.createInstance === 'function') {
|
|
@@ -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 {
|