@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 type { BaseBackendOptions } from '../../shared/type.js';
|
|
2
|
-
import type { I18nInitOptions,
|
|
2
|
+
import type { I18nInitOptions, I18nRuntimeInstance } from './instance.js';
|
|
3
3
|
type MergedBackendOptions = NonNullable<I18nInitOptions['backend']> & {
|
|
4
4
|
_useChainedBackend?: boolean;
|
|
5
5
|
_chainedBackendConfig?: {
|
|
@@ -8,15 +8,15 @@ type MergedBackendOptions = NonNullable<I18nInitOptions['backend']> & {
|
|
|
8
8
|
backends?: unknown[];
|
|
9
9
|
backendOptions?: unknown;
|
|
10
10
|
};
|
|
11
|
-
export declare function assertI18nInstance(obj: unknown): asserts obj is
|
|
11
|
+
export declare function assertI18nInstance(obj: unknown): asserts obj is I18nRuntimeInstance;
|
|
12
12
|
/**
|
|
13
13
|
* Build initialization options for i18n instance
|
|
14
14
|
*/
|
|
15
|
-
export declare const buildInitOptions: (finalLanguage: string, fallbackLanguage: string, languages: string[], mergedDetection: I18nInitOptions['detection'], mergedBackend: MergedBackendOptions | undefined, userInitOptions?: I18nInitOptions, useSuspense?: boolean, i18nInstance?:
|
|
15
|
+
export declare const buildInitOptions: (finalLanguage: string, fallbackLanguage: string, languages: string[], mergedDetection: I18nInitOptions['detection'], mergedBackend: MergedBackendOptions | undefined, userInitOptions?: I18nInitOptions, useSuspense?: boolean, i18nInstance?: I18nRuntimeInstance) => Promise<I18nInitOptions>;
|
|
16
16
|
/**
|
|
17
17
|
* Ensure i18n instance language matches the final detected language
|
|
18
18
|
*/
|
|
19
|
-
export declare const ensureLanguageMatch: (i18nInstance:
|
|
19
|
+
export declare const ensureLanguageMatch: (i18nInstance: I18nRuntimeInstance, finalLanguage: string) => Promise<void>;
|
|
20
20
|
/**
|
|
21
21
|
* Change language for i18n instance in onBeforeRender hook
|
|
22
22
|
* This function can be used by other runtime plugins to change language
|
|
@@ -24,15 +24,15 @@ export declare const ensureLanguageMatch: (i18nInstance: I18nInstance, finalLang
|
|
|
24
24
|
* @param newLang - The new language code to switch to
|
|
25
25
|
* @param options - Optional configuration
|
|
26
26
|
*/
|
|
27
|
-
export declare const changeI18nLanguage: (i18nInstance:
|
|
27
|
+
export declare const changeI18nLanguage: (i18nInstance: I18nRuntimeInstance, newLang: string, options?: {
|
|
28
28
|
detectionOptions?: I18nInitOptions['detection'];
|
|
29
29
|
}) => Promise<void>;
|
|
30
30
|
/**
|
|
31
31
|
* Initialize i18n instance if not already initialized
|
|
32
32
|
*/
|
|
33
|
-
export declare const initializeI18nInstance: (i18nInstance:
|
|
33
|
+
export declare const initializeI18nInstance: (i18nInstance: I18nRuntimeInstance, finalLanguage: string, fallbackLanguage: string, languages: string[], mergedDetection: I18nInitOptions['detection'], mergedBackend: MergedBackendOptions | undefined, userInitOptions?: I18nInitOptions, useSuspense?: boolean) => Promise<void>;
|
|
34
34
|
/**
|
|
35
35
|
* Setup cloned instance for SSR with backend support
|
|
36
36
|
*/
|
|
37
|
-
export declare const setupClonedInstance: (i18nInstance:
|
|
37
|
+
export declare const setupClonedInstance: (i18nInstance: I18nRuntimeInstance, finalLanguage: string, fallbackLanguage: string, languages: string[], backendEnabled: boolean, backend: BaseBackendOptions | undefined, i18nextDetector: boolean, detection: I18nInitOptions['detection'], localePathRedirect: boolean, userInitOptions: I18nInitOptions | undefined) => Promise<void>;
|
|
38
38
|
export {};
|
|
@@ -7,4 +7,7 @@ import { type I18nRouterAdapter } from './routerAdapter.js';
|
|
|
7
7
|
* factory takes this package's own primitives as arguments, so the fork
|
|
8
8
|
* package never imports back into it and the dependency stays one-way.
|
|
9
9
|
*/
|
|
10
|
-
export declare const
|
|
10
|
+
export declare const { I18nRouterNavigationProvider, useIntegratedRouterAdapter }: {
|
|
11
|
+
useIntegratedRouterAdapter: () => I18nRouterAdapter;
|
|
12
|
+
I18nRouterNavigationProvider: ({ children }: React.PropsWithChildren) => React.JSX.Element;
|
|
13
|
+
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { TInternalRuntimeContext } from '@modern-js/runtime/internal';
|
|
2
2
|
import type { BaseBackendOptions, BaseLocaleDetectionOptions } from '../shared/type.js';
|
|
3
|
-
import type { I18nInitOptions,
|
|
3
|
+
import type { I18nInitOptions, I18nRuntimeInstance } from './i18n/index.js';
|
|
4
4
|
import type { ReactI18nextIntegration } from './reactI18next.js';
|
|
5
5
|
interface RuntimeConfigWithI18n {
|
|
6
6
|
i18n?: {
|
|
@@ -11,12 +11,12 @@ interface I18nRuntimeApi {
|
|
|
11
11
|
getRuntimeConfig: () => RuntimeConfigWithI18n;
|
|
12
12
|
}
|
|
13
13
|
export interface RuntimeContextWithI18n extends TInternalRuntimeContext {
|
|
14
|
-
i18nInstance?:
|
|
14
|
+
i18nInstance?: I18nRuntimeInstance;
|
|
15
15
|
changeLanguage?: (lang: string) => Promise<void>;
|
|
16
16
|
}
|
|
17
17
|
interface SetupI18nBeforeRenderOptions {
|
|
18
18
|
api: I18nRuntimeApi;
|
|
19
|
-
userI18nInstance?:
|
|
19
|
+
userI18nInstance?: I18nRuntimeInstance;
|
|
20
20
|
initOptions?: I18nInitOptions;
|
|
21
21
|
backend?: BaseBackendOptions;
|
|
22
22
|
backendEnabled: boolean;
|
|
@@ -28,5 +28,5 @@ interface SetupI18nBeforeRenderOptions {
|
|
|
28
28
|
resolveReactI18nextIntegration: () => Promise<ReactI18nextIntegration | null>;
|
|
29
29
|
setI18nextProvider: (provider: ReactI18nextIntegration['I18nextProvider']) => void;
|
|
30
30
|
}
|
|
31
|
-
export declare function setupI18nBeforeRender(context: RuntimeContextWithI18n, options: SetupI18nBeforeRenderOptions): Promise<
|
|
31
|
+
export declare function setupI18nBeforeRender(context: RuntimeContextWithI18n, options: SetupI18nBeforeRenderOptions): Promise<I18nRuntimeInstance>;
|
|
32
32
|
export {};
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import type React from 'react';
|
|
2
2
|
import type { BaseLocaleDetectionOptions } from '../shared/type.js';
|
|
3
3
|
import type { I18nUrlStrategy } from '../shared/urlStrategy.js';
|
|
4
|
-
import type {
|
|
4
|
+
import type { I18nRuntimeInstance } from './i18n/index.js';
|
|
5
5
|
export interface I18nLanguageSynchronizationProps {
|
|
6
|
-
i18nInstance:
|
|
6
|
+
i18nInstance: I18nRuntimeInstance | undefined;
|
|
7
7
|
localePathRedirect: boolean;
|
|
8
8
|
languages: string[];
|
|
9
9
|
pathname: string | undefined;
|
|
@@ -21,7 +21,7 @@ interface I18nRootWrapperOptions {
|
|
|
21
21
|
urlStrategy?: I18nUrlStrategy;
|
|
22
22
|
NavigationProvider?: React.ComponentType<React.PropsWithChildren>;
|
|
23
23
|
LanguageSynchronization?: React.ComponentType<I18nLanguageSynchronizationProps>;
|
|
24
|
-
getLatestI18nInstance: () =>
|
|
24
|
+
getLatestI18nInstance: () => I18nRuntimeInstance | undefined;
|
|
25
25
|
getI18nextProvider: () => React.ComponentType<any> | null | undefined;
|
|
26
26
|
}
|
|
27
27
|
export declare const createI18nRootWrapper: (options: I18nRootWrapperOptions) => (App: React.ComponentType<any>) => (props: Record<string, unknown>) => React.JSX.Element;
|
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import type { I18nInitOptions,
|
|
1
|
+
import type { I18nInitOptions, I18nRuntimeInstance } from './i18n/index.js';
|
|
2
2
|
declare module '@modern-js/runtime' {
|
|
3
3
|
interface RuntimeConfig {
|
|
4
4
|
i18n?: {
|
|
5
|
-
i18nInstance?:
|
|
5
|
+
i18nInstance?: I18nRuntimeInstance;
|
|
6
6
|
changeLanguage?: (lang: string) => void;
|
|
7
7
|
setLang?: (lang: string) => void;
|
|
8
8
|
initOptions?: I18nInitOptions;
|
|
9
9
|
};
|
|
10
10
|
}
|
|
11
11
|
interface TRuntimeContext {
|
|
12
|
-
i18nInstance?:
|
|
12
|
+
i18nInstance?: I18nRuntimeInstance;
|
|
13
13
|
changeLanguage?: (lang: string) => Promise<void>;
|
|
14
14
|
}
|
|
15
15
|
}
|
package/package.json
CHANGED
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"modern",
|
|
18
18
|
"modern.js"
|
|
19
19
|
],
|
|
20
|
-
"version": "3.
|
|
20
|
+
"version": "3.11.3-ultramodern.10",
|
|
21
21
|
"engines": {
|
|
22
22
|
"node": ">=20"
|
|
23
23
|
},
|
|
@@ -132,22 +132,22 @@
|
|
|
132
132
|
}
|
|
133
133
|
},
|
|
134
134
|
"dependencies": {
|
|
135
|
-
"@modern-js/i18n-runtime-extensions": "npm:@bleedingdev/modern-js-i18n-runtime-extensions@3.
|
|
136
|
-
"@modern-js/plugin": "npm:@bleedingdev/modern-js-plugin@3.
|
|
137
|
-
"@modern-js/runtime-utils": "npm:@bleedingdev/modern-js-runtime-utils@3.
|
|
138
|
-
"@modern-js/server-core": "npm:@bleedingdev/modern-js-server-core@3.
|
|
139
|
-
"@modern-js/server-runtime": "npm:@bleedingdev/modern-js-server-runtime@3.
|
|
140
|
-
"@modern-js/types": "npm:@bleedingdev/modern-js-types@3.
|
|
141
|
-
"@modern-js/utils": "npm:@bleedingdev/modern-js-utils@3.
|
|
142
|
-
"@swc/helpers": "^0.5.
|
|
143
|
-
"i18next-browser-languagedetector": "^8.
|
|
144
|
-
"i18next-chained-backend": "^5.0.
|
|
145
|
-
"i18next-fs-backend": "^2.6.
|
|
146
|
-
"i18next-http-backend": "^4.0.
|
|
147
|
-
"i18next-http-middleware": "^3.
|
|
135
|
+
"@modern-js/i18n-runtime-extensions": "npm:@bleedingdev/modern-js-i18n-runtime-extensions@3.11.3-ultramodern.10",
|
|
136
|
+
"@modern-js/plugin": "npm:@bleedingdev/modern-js-plugin@3.11.3-ultramodern.10",
|
|
137
|
+
"@modern-js/runtime-utils": "npm:@bleedingdev/modern-js-runtime-utils@3.11.3-ultramodern.10",
|
|
138
|
+
"@modern-js/server-core": "npm:@bleedingdev/modern-js-server-core@3.11.3-ultramodern.10",
|
|
139
|
+
"@modern-js/server-runtime": "npm:@bleedingdev/modern-js-server-runtime@3.11.3-ultramodern.10",
|
|
140
|
+
"@modern-js/types": "npm:@bleedingdev/modern-js-types@3.11.3-ultramodern.10",
|
|
141
|
+
"@modern-js/utils": "npm:@bleedingdev/modern-js-utils@3.11.3-ultramodern.10",
|
|
142
|
+
"@swc/helpers": "^0.5.24",
|
|
143
|
+
"i18next-browser-languagedetector": "^8.3.1",
|
|
144
|
+
"i18next-chained-backend": "^5.0.8",
|
|
145
|
+
"i18next-fs-backend": "^2.6.12",
|
|
146
|
+
"i18next-http-backend": "^4.0.10",
|
|
147
|
+
"i18next-http-middleware": "^3.10.3"
|
|
148
148
|
},
|
|
149
149
|
"peerDependencies": {
|
|
150
|
-
"@modern-js/runtime": "3.
|
|
150
|
+
"@modern-js/runtime": "3.11.3-ultramodern.10",
|
|
151
151
|
"i18next": ">=25.10.10",
|
|
152
152
|
"react": "^19.3.1",
|
|
153
153
|
"react-dom": "^19.3.1",
|
|
@@ -162,17 +162,17 @@
|
|
|
162
162
|
}
|
|
163
163
|
},
|
|
164
164
|
"devDependencies": {
|
|
165
|
-
"@modern-js/app-tools": "npm:@bleedingdev/modern-js-app-tools@3.
|
|
166
|
-
"@modern-js/runtime": "npm:@bleedingdev/modern-js-runtime@3.
|
|
167
|
-
"@modern-js/runtime-extensions": "npm:@bleedingdev/modern-js-runtime-extensions@3.
|
|
168
|
-
"@rslib/core": "1.0.
|
|
169
|
-
"@scripts/rstest-config": "2.66.
|
|
170
|
-
"@types/node": "^26.6.
|
|
165
|
+
"@modern-js/app-tools": "npm:@bleedingdev/modern-js-app-tools@3.11.3-ultramodern.10",
|
|
166
|
+
"@modern-js/runtime": "npm:@bleedingdev/modern-js-runtime@3.11.3-ultramodern.10",
|
|
167
|
+
"@modern-js/runtime-extensions": "npm:@bleedingdev/modern-js-runtime-extensions@3.11.3-ultramodern.10",
|
|
168
|
+
"@rslib/core": "1.0.7",
|
|
169
|
+
"@scripts/rstest-config": "2.66.7",
|
|
170
|
+
"@types/node": "^26.6.6",
|
|
171
171
|
"esbuild": "^0.28.2",
|
|
172
|
-
"i18next": "
|
|
172
|
+
"i18next": "27.1.5",
|
|
173
173
|
"react": "^19.3.1",
|
|
174
174
|
"react-dom": "^19.3.1",
|
|
175
|
-
"react-i18next": "
|
|
175
|
+
"react-i18next": "18.0.2",
|
|
176
176
|
"ts-node": "^10.9.2",
|
|
177
177
|
"typescript": "7.0.2"
|
|
178
178
|
},
|
|
@@ -187,7 +187,8 @@
|
|
|
187
187
|
"scripts": {
|
|
188
188
|
"dev": "rslib build --watch",
|
|
189
189
|
"build": "rslib build",
|
|
190
|
-
"test": "rstest"
|
|
190
|
+
"test": "rstest",
|
|
191
|
+
"test:types": "node ../../../scripts/with-effect-tsrs.mjs --project tests/types/tsconfig.json && node ../../../scripts/with-effect-tsrs.mjs --project tests/types/selector/tsconfig.json"
|
|
191
192
|
},
|
|
192
193
|
"ultramodern": {
|
|
193
194
|
"sourceName": "@modern-js/plugin-i18n"
|
package/src/cli/index.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import fs from 'node:fs';
|
|
2
2
|
import path from 'node:path';
|
|
3
|
-
import type { AppTools, CliPlugin } from '@modern-js/app-tools';
|
|
3
|
+
import type { AppTools, AppUserConfig, CliPlugin } from '@modern-js/app-tools';
|
|
4
4
|
import { getPublicDirRoutePrefixes } from '@modern-js/server-core';
|
|
5
5
|
import type { Entrypoint } from '@modern-js/types';
|
|
6
6
|
import { logger } from '@modern-js/utils';
|
|
@@ -11,6 +11,22 @@ import { applyDetectedBackendPaths, detectLocalesDirectory } from './locales';
|
|
|
11
11
|
import { applyLocalisedRoutes } from './localisedRoutes';
|
|
12
12
|
import '../runtime/types';
|
|
13
13
|
|
|
14
|
+
type BundlerChain = NonNullable<AppUserConfig['tools']>['bundlerChain'];
|
|
15
|
+
|
|
16
|
+
// Before the first render the runtime loads react-i18next with a dynamic import.
|
|
17
|
+
// Bundle that module eagerly so it doesn't become an extra async request. Only
|
|
18
|
+
// runtime/i18n/react-i18next is matched: the i18next fallback import in
|
|
19
|
+
// runtime/i18n/instance stays lazy, so apps that pass their own instance don't
|
|
20
|
+
// get an unused copy of i18next in the initial chunk.
|
|
21
|
+
const eagerReactI18next: BundlerChain = chain => {
|
|
22
|
+
chain.module
|
|
23
|
+
.rule('plugin-i18n-react-i18next')
|
|
24
|
+
.test(
|
|
25
|
+
/[\\/]plugin-i18n[\\/]dist[\\/][^\\/]+[\\/]runtime[\\/]i18n[\\/]react-i18next\.m?js$/,
|
|
26
|
+
)
|
|
27
|
+
.parser({ dynamicImportMode: 'eager' });
|
|
28
|
+
};
|
|
29
|
+
|
|
14
30
|
export type TransformRuntimeConfigFn = (
|
|
15
31
|
extendedConfig: Record<string, any>,
|
|
16
32
|
entrypoint: Entrypoint,
|
|
@@ -45,6 +61,8 @@ export const i18nPlugin = (
|
|
|
45
61
|
...restOptions
|
|
46
62
|
} = options;
|
|
47
63
|
|
|
64
|
+
api.config(() => ({ tools: { bundlerChain: eagerReactI18next } }));
|
|
65
|
+
|
|
48
66
|
api._internalRuntimePlugins(({ entrypoint, plugins }) => {
|
|
49
67
|
const localeDetectionOptions = localeDetection
|
|
50
68
|
? getLocaleDetectionOptions(entrypoint.entryName, localeDetection)
|
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') {
|