@bleedingdev/modern-js-plugin-i18n 3.2.0-ultramodern.2 → 3.2.0-ultramodern.23
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 +22 -0
- package/dist/cjs/runtime/I18nLink.js +4 -12
- package/dist/cjs/runtime/context.js +32 -5
- package/dist/cjs/runtime/hooks.js +8 -5
- package/dist/cjs/runtime/i18n/backend/defaults.js +1 -1
- package/dist/cjs/runtime/i18n/backend/middleware.node.js +4 -4
- package/dist/cjs/runtime/i18n/instance.js +4 -2
- package/dist/cjs/runtime/index.js +7 -6
- package/dist/cjs/runtime/routerAdapter.js +163 -0
- package/dist/cjs/runtime/utils.js +63 -94
- package/dist/cjs/server/index.js +64 -8
- package/dist/cjs/shared/localisedUrls.js +237 -0
- package/dist/esm/cli/index.mjs +22 -0
- package/dist/esm/runtime/I18nLink.mjs +4 -12
- package/dist/esm/runtime/context.mjs +34 -7
- package/dist/esm/runtime/hooks.mjs +9 -6
- package/dist/esm/runtime/i18n/backend/defaults.mjs +1 -1
- package/dist/esm/runtime/i18n/backend/middleware.node.mjs +3 -3
- package/dist/esm/runtime/i18n/instance.mjs +4 -2
- package/dist/esm/runtime/index.mjs +7 -6
- package/dist/esm/runtime/routerAdapter.mjs +129 -0
- package/dist/esm/runtime/utils.mjs +11 -30
- package/dist/esm/server/index.mjs +57 -7
- package/dist/esm/shared/localisedUrls.mjs +191 -0
- package/dist/esm-node/cli/index.mjs +22 -0
- package/dist/esm-node/runtime/I18nLink.mjs +4 -12
- package/dist/esm-node/runtime/context.mjs +34 -7
- package/dist/esm-node/runtime/hooks.mjs +9 -6
- package/dist/esm-node/runtime/i18n/backend/defaults.mjs +1 -1
- package/dist/esm-node/runtime/i18n/backend/middleware.node.mjs +3 -3
- package/dist/esm-node/runtime/i18n/instance.mjs +4 -2
- package/dist/esm-node/runtime/index.mjs +7 -6
- package/dist/esm-node/runtime/routerAdapter.mjs +130 -0
- package/dist/esm-node/runtime/utils.mjs +11 -30
- package/dist/esm-node/server/index.mjs +57 -7
- package/dist/esm-node/shared/localisedUrls.mjs +192 -0
- package/dist/types/cli/index.d.ts +21 -0
- package/dist/types/runtime/I18nLink.d.ts +23 -0
- package/dist/types/runtime/context.d.ts +41 -0
- package/dist/types/runtime/hooks.d.ts +30 -0
- package/dist/types/runtime/i18n/backend/config.d.ts +2 -0
- package/dist/types/runtime/i18n/backend/defaults.d.ts +13 -0
- package/dist/types/runtime/i18n/backend/defaults.node.d.ts +8 -0
- package/dist/types/runtime/i18n/backend/index.d.ts +3 -0
- package/dist/types/runtime/i18n/backend/middleware.common.d.ts +14 -0
- package/dist/types/runtime/i18n/backend/middleware.d.ts +12 -0
- package/dist/types/runtime/i18n/backend/middleware.node.d.ts +13 -0
- package/dist/types/runtime/i18n/backend/sdk-backend.d.ts +53 -0
- package/dist/types/runtime/i18n/backend/sdk-event.d.ts +9 -0
- package/dist/types/runtime/i18n/detection/config.d.ts +11 -0
- package/dist/types/runtime/i18n/detection/index.d.ts +50 -0
- package/dist/types/runtime/i18n/detection/middleware.d.ts +24 -0
- package/dist/types/runtime/i18n/detection/middleware.node.d.ts +17 -0
- package/dist/types/runtime/i18n/index.d.ts +3 -0
- package/dist/types/runtime/i18n/instance.d.ts +96 -0
- package/dist/types/runtime/i18n/utils.d.ts +29 -0
- package/dist/types/runtime/index.d.ts +21 -0
- package/dist/types/runtime/routerAdapter.d.ts +26 -0
- package/dist/types/runtime/types.d.ts +15 -0
- package/dist/types/runtime/utils.d.ts +28 -0
- package/dist/types/server/index.d.ts +14 -0
- package/dist/types/shared/deepMerge.d.ts +1 -0
- package/dist/types/shared/detection.d.ts +11 -0
- package/dist/types/shared/localisedUrls.d.ts +13 -0
- package/dist/types/shared/type.d.ts +168 -0
- package/dist/types/shared/utils.d.ts +5 -0
- package/package.json +15 -15
- package/rstest.config.mts +39 -0
- package/src/cli/index.ts +43 -1
- package/src/runtime/I18nLink.tsx +10 -16
- package/src/runtime/context.tsx +45 -7
- package/src/runtime/hooks.ts +13 -4
- package/src/runtime/i18n/backend/defaults.ts +3 -1
- package/src/runtime/i18n/backend/middleware.node.ts +1 -1
- package/src/runtime/i18n/instance.ts +14 -5
- package/src/runtime/index.tsx +10 -2
- package/src/runtime/routerAdapter.tsx +333 -0
- package/src/runtime/utils.ts +22 -34
- package/src/server/index.ts +135 -10
- package/src/shared/localisedUrls.ts +393 -0
- package/src/shared/type.ts +12 -0
- package/tests/localisedUrls.test.ts +278 -0
- package/tests/routerAdapter.test.tsx +278 -0
- package/dist/esm/rslib-runtime.mjs +0 -18
- package/dist/esm-node/rslib-runtime.mjs +0 -19
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { BaseBackendOptions } from '../../shared/type';
|
|
2
|
+
import type { I18nInitOptions, I18nInstance } from './instance';
|
|
3
|
+
export declare function assertI18nInstance(obj: any): asserts obj is I18nInstance;
|
|
4
|
+
/**
|
|
5
|
+
* Build initialization options for i18n instance
|
|
6
|
+
*/
|
|
7
|
+
export declare const buildInitOptions: (finalLanguage: string, fallbackLanguage: string, languages: string[], mergedDetection: any, mergedBackend: any, userInitOptions?: I18nInitOptions, useSuspense?: boolean, i18nInstance?: I18nInstance) => Promise<I18nInitOptions>;
|
|
8
|
+
/**
|
|
9
|
+
* Ensure i18n instance language matches the final detected language
|
|
10
|
+
*/
|
|
11
|
+
export declare const ensureLanguageMatch: (i18nInstance: I18nInstance, finalLanguage: string) => Promise<void>;
|
|
12
|
+
/**
|
|
13
|
+
* Change language for i18n instance in onBeforeRender hook
|
|
14
|
+
* This function can be used by other runtime plugins to change language
|
|
15
|
+
* @param i18nInstance - The i18n instance
|
|
16
|
+
* @param newLang - The new language code to switch to
|
|
17
|
+
* @param options - Optional configuration
|
|
18
|
+
*/
|
|
19
|
+
export declare const changeI18nLanguage: (i18nInstance: I18nInstance, newLang: string, options?: {
|
|
20
|
+
detectionOptions?: any;
|
|
21
|
+
}) => Promise<void>;
|
|
22
|
+
/**
|
|
23
|
+
* Initialize i18n instance if not already initialized
|
|
24
|
+
*/
|
|
25
|
+
export declare const initializeI18nInstance: (i18nInstance: I18nInstance, finalLanguage: string, fallbackLanguage: string, languages: string[], mergedDetection: any, mergedBackend: any, userInitOptions?: I18nInitOptions, useSuspense?: boolean) => Promise<void>;
|
|
26
|
+
/**
|
|
27
|
+
* Setup cloned instance for SSR with backend support
|
|
28
|
+
*/
|
|
29
|
+
export declare const setupClonedInstance: (i18nInstance: I18nInstance, finalLanguage: string, fallbackLanguage: string, languages: string[], backendEnabled: boolean, backend: BaseBackendOptions | undefined, i18nextDetector: boolean, detection: any, localePathRedirect: boolean, userInitOptions: I18nInitOptions | undefined) => Promise<void>;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { type RuntimePlugin } from '@modern-js/runtime';
|
|
2
|
+
import type { BaseBackendOptions, BaseLocaleDetectionOptions } from '../shared/type';
|
|
3
|
+
import type { I18nInitOptions, I18nInstance } from './i18n';
|
|
4
|
+
import './types';
|
|
5
|
+
export type { I18nSdkLoader, I18nSdkLoadOptions } from '../shared/type';
|
|
6
|
+
export type { Resources } from './i18n/instance';
|
|
7
|
+
export interface I18nPluginOptions {
|
|
8
|
+
entryName?: string;
|
|
9
|
+
localeDetection?: BaseLocaleDetectionOptions;
|
|
10
|
+
backend?: BaseBackendOptions;
|
|
11
|
+
i18nInstance?: I18nInstance;
|
|
12
|
+
changeLanguage?: (lang: string) => void;
|
|
13
|
+
initOptions?: I18nInitOptions;
|
|
14
|
+
htmlLangAttr?: boolean;
|
|
15
|
+
reactI18next?: boolean;
|
|
16
|
+
[key: string]: any;
|
|
17
|
+
}
|
|
18
|
+
export declare const i18nPlugin: (options: I18nPluginOptions) => RuntimePlugin;
|
|
19
|
+
export { useModernI18n } from './context';
|
|
20
|
+
export { I18nLink } from './I18nLink';
|
|
21
|
+
export default i18nPlugin;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type React from 'react';
|
|
2
|
+
export type I18nRouterFramework = 'react-router' | 'tanstack' | string;
|
|
3
|
+
export interface I18nRouterLocation {
|
|
4
|
+
pathname: string;
|
|
5
|
+
search: string;
|
|
6
|
+
hash: string;
|
|
7
|
+
}
|
|
8
|
+
export interface I18nRouterNavigateOptions {
|
|
9
|
+
replace?: boolean;
|
|
10
|
+
state?: unknown;
|
|
11
|
+
}
|
|
12
|
+
export type I18nRouterNavigate = (href: string, options?: I18nRouterNavigateOptions) => void | Promise<void>;
|
|
13
|
+
export type I18nRouterLink = React.ComponentType<{
|
|
14
|
+
to: string;
|
|
15
|
+
children?: React.ReactNode;
|
|
16
|
+
[key: string]: unknown;
|
|
17
|
+
}>;
|
|
18
|
+
export interface I18nRouterAdapter {
|
|
19
|
+
framework?: I18nRouterFramework;
|
|
20
|
+
hasRouter: boolean;
|
|
21
|
+
location: I18nRouterLocation | null;
|
|
22
|
+
navigate: I18nRouterNavigate | null;
|
|
23
|
+
Link: I18nRouterLink | null;
|
|
24
|
+
params: Record<string, string>;
|
|
25
|
+
}
|
|
26
|
+
export declare const useI18nRouterAdapter: () => I18nRouterAdapter;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { I18nInitOptions, I18nInstance } from './i18n';
|
|
2
|
+
declare module '@modern-js/runtime' {
|
|
3
|
+
interface RuntimeConfig {
|
|
4
|
+
i18n?: {
|
|
5
|
+
i18nInstance?: I18nInstance;
|
|
6
|
+
changeLanguage?: (lang: string) => void;
|
|
7
|
+
setLang?: (lang: string) => void;
|
|
8
|
+
initOptions?: I18nInitOptions;
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
interface TInternalRuntimeContext {
|
|
12
|
+
i18nInstance?: I18nInstance;
|
|
13
|
+
changeLanguage?: (lang: string) => Promise<void>;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { type TInternalRuntimeContext } from '@modern-js/runtime/context';
|
|
2
|
+
import type { LocalisedUrlsMap } from '../shared/localisedUrls';
|
|
3
|
+
export declare const getPathname: (context: TInternalRuntimeContext) => string;
|
|
4
|
+
export declare const getEntryPath: () => string;
|
|
5
|
+
/**
|
|
6
|
+
* Helper function to get language from current pathname
|
|
7
|
+
* @param pathname - The current pathname
|
|
8
|
+
* @param languages - Array of supported languages
|
|
9
|
+
* @param fallbackLanguage - Fallback language when no language is detected
|
|
10
|
+
* @returns The detected language or fallback language
|
|
11
|
+
*/
|
|
12
|
+
export declare const getLanguageFromPath: (pathname: string, languages: string[], fallbackLanguage: string) => string;
|
|
13
|
+
/**
|
|
14
|
+
* Helper function to build localized URL
|
|
15
|
+
* @param pathname - The current pathname
|
|
16
|
+
* @param language - The target language
|
|
17
|
+
* @param languages - Array of supported languages
|
|
18
|
+
* @returns The localized URL path
|
|
19
|
+
*/
|
|
20
|
+
export declare const buildLocalizedUrl: (pathname: string, language: string, languages: string[], localisedUrls?: boolean | LocalisedUrlsMap) => string;
|
|
21
|
+
export declare const detectLanguageFromPath: (pathname: string, languages: string[], localePathRedirect: boolean) => {
|
|
22
|
+
detected: boolean;
|
|
23
|
+
language?: string;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Check if the given pathname should ignore automatic locale redirect
|
|
27
|
+
*/
|
|
28
|
+
export declare const shouldIgnoreRedirect: (pathname: string, languages: string[], ignoreRedirectRoutes?: string[] | ((pathname: string) => boolean)) => boolean;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { ServerPlugin } from '@modern-js/server-runtime';
|
|
2
|
+
import type { LocaleDetectionOptions } from '../shared/type';
|
|
3
|
+
export interface I18nPluginOptions {
|
|
4
|
+
localeDetection: LocaleDetectionOptions;
|
|
5
|
+
staticRoutePrefixes: string[];
|
|
6
|
+
}
|
|
7
|
+
type ApiPrefixInput = string | string[] | undefined;
|
|
8
|
+
export declare const collectApiPrefixes: (routes: Array<{
|
|
9
|
+
isApi?: boolean;
|
|
10
|
+
urlPath?: string;
|
|
11
|
+
}>, bffPrefix?: ApiPrefixInput) => string[];
|
|
12
|
+
export declare const matchesApiPrefix: (pathname: string, apiPrefixes: string[]) => boolean;
|
|
13
|
+
export declare const i18nServerPlugin: (options: I18nPluginOptions) => ServerPlugin;
|
|
14
|
+
export default i18nServerPlugin;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function deepMerge<T extends Record<string, any>>(defaultOptions: T, userOptions?: Partial<T>): T;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { LanguageDetectorOptions } from '../runtime/i18n/instance';
|
|
2
|
+
/**
|
|
3
|
+
* Detect language from request using the same detection logic as i18next
|
|
4
|
+
* This ensures consistency between server-side and client-side detection
|
|
5
|
+
*/
|
|
6
|
+
export declare function detectLanguageFromRequest(req: {
|
|
7
|
+
url: string;
|
|
8
|
+
headers: {
|
|
9
|
+
get: (name: string) => string | null;
|
|
10
|
+
} | Headers;
|
|
11
|
+
}, languages: string[], detectionOptions?: LanguageDetectorOptions): string | null;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { NestedRouteForCli, PageRoute } from '@modern-js/types';
|
|
2
|
+
export type LocalisedUrlPathMap = Record<string, string>;
|
|
3
|
+
export type LocalisedUrlsMap = Record<string, LocalisedUrlPathMap>;
|
|
4
|
+
export type LocalisedUrlsOption = boolean | LocalisedUrlsMap;
|
|
5
|
+
export interface ResolvedLocalisedUrlsConfig {
|
|
6
|
+
enabled: boolean;
|
|
7
|
+
map: LocalisedUrlsMap;
|
|
8
|
+
}
|
|
9
|
+
export declare const normalisePathPattern: (path: string) => string;
|
|
10
|
+
export declare const resolveLocalisedUrlsConfig: (option: LocalisedUrlsOption | undefined) => ResolvedLocalisedUrlsConfig;
|
|
11
|
+
export declare const validateLocalisedUrls: (routes: (NestedRouteForCli | PageRoute)[], languages: string[], localisedUrls: LocalisedUrlsMap) => void;
|
|
12
|
+
export declare const applyLocalisedUrlsToRoutes: (routes: (NestedRouteForCli | PageRoute)[], languages: string[], localisedUrls: LocalisedUrlsMap) => (NestedRouteForCli | PageRoute)[];
|
|
13
|
+
export declare const resolveLocalisedPath: (pathname: string, targetLanguage: string, languages: string[], localisedUrls: LocalisedUrlsMap) => string;
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import type { LanguageDetectorOptions, Resources } from '../runtime/i18n/instance';
|
|
2
|
+
import type { LocalisedUrlsOption } from './localisedUrls';
|
|
3
|
+
export interface BaseLocaleDetectionOptions {
|
|
4
|
+
localePathRedirect?: boolean;
|
|
5
|
+
i18nextDetector?: boolean;
|
|
6
|
+
languages?: string[];
|
|
7
|
+
fallbackLanguage?: string;
|
|
8
|
+
detection?: LanguageDetectorOptions;
|
|
9
|
+
ignoreRedirectRoutes?: string[] | ((pathname: string) => boolean);
|
|
10
|
+
/**
|
|
11
|
+
* Enables localised pathnames in addition to the locale prefix.
|
|
12
|
+
*
|
|
13
|
+
* - `false`: keep only locale-prefix behavior (`/en/about`).
|
|
14
|
+
* - object: map canonical route paths to every configured language.
|
|
15
|
+
*
|
|
16
|
+
* Defaults to `true` when `localePathRedirect` is enabled, so route
|
|
17
|
+
* generation validates that every localisable route path has entries for all
|
|
18
|
+
* configured languages.
|
|
19
|
+
*/
|
|
20
|
+
localisedUrls?: LocalisedUrlsOption;
|
|
21
|
+
}
|
|
22
|
+
export interface LocaleDetectionOptions extends BaseLocaleDetectionOptions {
|
|
23
|
+
localeDetectionByEntry?: Record<string, BaseLocaleDetectionOptions>;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Options for loading i18n resources via SDK
|
|
27
|
+
*/
|
|
28
|
+
export interface I18nSdkLoadOptions {
|
|
29
|
+
/** Single language code to load (e.g., 'en', 'zh') */
|
|
30
|
+
lng?: string;
|
|
31
|
+
/** Single namespace to load (e.g., 'translation', 'common') */
|
|
32
|
+
ns?: string;
|
|
33
|
+
/** Multiple language codes to load */
|
|
34
|
+
lngs?: string[];
|
|
35
|
+
/** Multiple namespaces to load */
|
|
36
|
+
nss?: string[];
|
|
37
|
+
/** Load all available languages and namespaces */
|
|
38
|
+
all?: boolean;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* SDK function to load i18n resources
|
|
42
|
+
* Supports multiple loading modes:
|
|
43
|
+
* 1. Single resource: sdk({ lng: 'en', ns: 'translation' })
|
|
44
|
+
* 2. Batch by languages: sdk({ lngs: ['en', 'zh'], ns: 'translation' })
|
|
45
|
+
* 3. Batch by namespaces: sdk({ lng: 'en', nss: ['translation', 'common'] })
|
|
46
|
+
* 4. Batch by both: sdk({ lngs: ['en', 'zh'], nss: ['translation', 'common'] })
|
|
47
|
+
* 5. Load all: sdk({ all: true }) or sdk()
|
|
48
|
+
*
|
|
49
|
+
* @param options - Loading options
|
|
50
|
+
* @returns Promise that resolves to resources object or the resources object directly
|
|
51
|
+
* Resources format: { [lng]: { [ns]: { [key]: value } } }
|
|
52
|
+
*/
|
|
53
|
+
export type I18nSdkLoader = (options: I18nSdkLoadOptions) => Promise<Resources> | Resources;
|
|
54
|
+
/**
|
|
55
|
+
* Chained backend configuration
|
|
56
|
+
* Used internally when both loadPath and sdk are provided
|
|
57
|
+
*/
|
|
58
|
+
export interface ChainedBackendConfig {
|
|
59
|
+
_useChainedBackend: boolean;
|
|
60
|
+
_chainedBackendConfig: {
|
|
61
|
+
backendOptions: Array<Record<string, unknown>>;
|
|
62
|
+
};
|
|
63
|
+
cacheHitMode?: 'none' | 'refresh' | 'refreshAndUpdateStore';
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Extended backend options that may include chained backend configuration
|
|
67
|
+
*/
|
|
68
|
+
export type ExtendedBackendOptions = BaseBackendOptions & Partial<ChainedBackendConfig>;
|
|
69
|
+
export interface BaseBackendOptions {
|
|
70
|
+
enabled?: boolean;
|
|
71
|
+
loadPath?: string;
|
|
72
|
+
addPath?: string;
|
|
73
|
+
/**
|
|
74
|
+
* Cache hit mode for chained backend (only effective when both `loadPath` and `sdk` are provided)
|
|
75
|
+
*
|
|
76
|
+
* - `'none'` (default): If the first backend returns resources, stop and don't try the next backend
|
|
77
|
+
* - `'refresh'`: Try to refresh the cache by loading from the next backend and update the cache
|
|
78
|
+
* - `'refreshAndUpdateStore'`: Try to refresh the cache by loading from the next backend,
|
|
79
|
+
* update the cache and also update the i18next resource store. This allows FS/HTTP resources
|
|
80
|
+
* to be displayed first, then SDK resources will update them asynchronously.
|
|
81
|
+
*
|
|
82
|
+
* @default 'refreshAndUpdateStore' when both loadPath and sdk are provided
|
|
83
|
+
*/
|
|
84
|
+
cacheHitMode?: 'none' | 'refresh' | 'refreshAndUpdateStore';
|
|
85
|
+
/**
|
|
86
|
+
* SDK function to load i18n resources dynamically
|
|
87
|
+
*
|
|
88
|
+
* **Important**: In `modern.config.ts`, you can only set this to `true` or any identifier
|
|
89
|
+
* to enable SDK mode. The actual SDK function must be provided in `modern.runtime.ts`
|
|
90
|
+
* via `initOptions.backend.sdk`.
|
|
91
|
+
*
|
|
92
|
+
* When both `loadPath` (or FS backend) and `sdk` are provided, the plugin will automatically
|
|
93
|
+
* use `i18next-chained-backend` to chain multiple backends. The order will be:
|
|
94
|
+
* 1. HTTP/FS backend (primary) - loads from `loadPath` or file system first for quick initial display
|
|
95
|
+
* 2. SDK backend (fallback/update) - loads from the SDK function to update/refresh translations
|
|
96
|
+
*
|
|
97
|
+
* With `cacheHitMode: 'refreshAndUpdateStore'` (default), FS/HTTP resources will be displayed
|
|
98
|
+
* immediately, then SDK resources will be loaded asynchronously to update the translations.
|
|
99
|
+
*
|
|
100
|
+
* If only `sdk` is provided, it will be used instead of the default HTTP/FS backend
|
|
101
|
+
*
|
|
102
|
+
* @example In modern.config.ts - enable SDK mode
|
|
103
|
+
* ```ts
|
|
104
|
+
* backend: {
|
|
105
|
+
* enabled: true,
|
|
106
|
+
* sdk: true, // or any identifier, just to enable SDK mode
|
|
107
|
+
* }
|
|
108
|
+
* ```
|
|
109
|
+
*
|
|
110
|
+
* @example In modern.runtime.ts - provide the actual SDK function
|
|
111
|
+
* ```ts
|
|
112
|
+
* export default defineRuntimeConfig({
|
|
113
|
+
* i18n: {
|
|
114
|
+
* initOptions: {
|
|
115
|
+
* backend: {
|
|
116
|
+
* sdk: async (options) => {
|
|
117
|
+
* // Your SDK implementation
|
|
118
|
+
* if (options.all) {
|
|
119
|
+
* return await mySdk.getAllResources();
|
|
120
|
+
* }
|
|
121
|
+
* if (options.lng && options.ns) {
|
|
122
|
+
* return await mySdk.getResource(options.lng, options.ns);
|
|
123
|
+
* }
|
|
124
|
+
* // Handle other cases...
|
|
125
|
+
* }
|
|
126
|
+
* }
|
|
127
|
+
* }
|
|
128
|
+
* }
|
|
129
|
+
* });
|
|
130
|
+
* ```
|
|
131
|
+
*
|
|
132
|
+
* @example Single resource loading
|
|
133
|
+
* ```ts
|
|
134
|
+
* sdk: async (options) => {
|
|
135
|
+
* if (options.lng && options.ns) {
|
|
136
|
+
* const response = await fetch(`/api/i18n/${options.lng}/${options.ns}`);
|
|
137
|
+
* return response.json();
|
|
138
|
+
* }
|
|
139
|
+
* }
|
|
140
|
+
* ```
|
|
141
|
+
*
|
|
142
|
+
* @example Load all resources at once
|
|
143
|
+
* ```ts
|
|
144
|
+
* sdk: async (options) => {
|
|
145
|
+
* if (options?.all) {
|
|
146
|
+
* // Load all languages and namespaces
|
|
147
|
+
* return await mySdk.getAllResources();
|
|
148
|
+
* }
|
|
149
|
+
* // Handle other cases...
|
|
150
|
+
* }
|
|
151
|
+
* ```
|
|
152
|
+
*
|
|
153
|
+
* @example Batch loading
|
|
154
|
+
* ```ts
|
|
155
|
+
* sdk: async (options) => {
|
|
156
|
+
* if (options?.lngs && options?.nss) {
|
|
157
|
+
* // Load multiple languages and namespaces
|
|
158
|
+
* return await mySdk.getBatchResources(options.lngs, options.nss);
|
|
159
|
+
* }
|
|
160
|
+
* // Handle single or other cases...
|
|
161
|
+
* }
|
|
162
|
+
* ```
|
|
163
|
+
*/
|
|
164
|
+
sdk?: I18nSdkLoader | boolean | string;
|
|
165
|
+
}
|
|
166
|
+
export interface BackendOptions extends BaseBackendOptions {
|
|
167
|
+
backendOptionsByEntry?: Record<string, BaseBackendOptions>;
|
|
168
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { BaseBackendOptions, BaseLocaleDetectionOptions } from './type';
|
|
2
|
+
export declare function getEntryConfig<T extends Record<string, any>>(entryName: string, config: T, entryKey: string): T | undefined;
|
|
3
|
+
export declare function removeEntryConfigKey<T extends Record<string, any>>(config: T, entryKey: string): Omit<T, typeof entryKey>;
|
|
4
|
+
export declare function getLocaleDetectionOptions(entryName: string, localeDetection: BaseLocaleDetectionOptions): BaseLocaleDetectionOptions;
|
|
5
|
+
export declare function getBackendOptions(entryName: string, backend: BaseBackendOptions): BaseBackendOptions;
|
package/package.json
CHANGED
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"modern",
|
|
18
18
|
"modern.js"
|
|
19
19
|
],
|
|
20
|
-
"version": "3.2.0-ultramodern.
|
|
20
|
+
"version": "3.2.0-ultramodern.23",
|
|
21
21
|
"engines": {
|
|
22
22
|
"node": ">=20"
|
|
23
23
|
},
|
|
@@ -84,18 +84,18 @@
|
|
|
84
84
|
"@swc/helpers": "^0.5.21",
|
|
85
85
|
"i18next-browser-languagedetector": "^8.2.1",
|
|
86
86
|
"i18next-chained-backend": "^5.0.4",
|
|
87
|
-
"i18next-fs-backend": "^2.6.
|
|
87
|
+
"i18next-fs-backend": "^2.6.6",
|
|
88
88
|
"i18next-http-backend": "^4.0.0",
|
|
89
|
-
"i18next-http-middleware": "^3.9.
|
|
90
|
-
"@modern-js/
|
|
91
|
-
"@modern-js/
|
|
92
|
-
"@modern-js/utils": "npm:@bleedingdev/modern-js-utils@3.2.0-ultramodern.
|
|
93
|
-
"@modern-js/types": "npm:@bleedingdev/modern-js-types@3.2.0-ultramodern.
|
|
94
|
-
"@modern-js/
|
|
95
|
-
"@modern-js/
|
|
89
|
+
"i18next-http-middleware": "^3.9.7",
|
|
90
|
+
"@modern-js/plugin": "npm:@bleedingdev/modern-js-plugin@3.2.0-ultramodern.23",
|
|
91
|
+
"@modern-js/runtime-utils": "npm:@bleedingdev/modern-js-runtime-utils@3.2.0-ultramodern.23",
|
|
92
|
+
"@modern-js/utils": "npm:@bleedingdev/modern-js-utils@3.2.0-ultramodern.23",
|
|
93
|
+
"@modern-js/types": "npm:@bleedingdev/modern-js-types@3.2.0-ultramodern.23",
|
|
94
|
+
"@modern-js/server-runtime": "npm:@bleedingdev/modern-js-server-runtime@3.2.0-ultramodern.23",
|
|
95
|
+
"@modern-js/server-core": "npm:@bleedingdev/modern-js-server-core@3.2.0-ultramodern.23"
|
|
96
96
|
},
|
|
97
97
|
"peerDependencies": {
|
|
98
|
-
"@modern-js/runtime": "3.2.0-ultramodern.
|
|
98
|
+
"@modern-js/runtime": "3.2.0-ultramodern.23",
|
|
99
99
|
"i18next": ">=25.7.4",
|
|
100
100
|
"react": "^19.2.6",
|
|
101
101
|
"react-dom": "^19.2.6",
|
|
@@ -112,16 +112,16 @@
|
|
|
112
112
|
"devDependencies": {
|
|
113
113
|
"@rslib/core": "0.21.5",
|
|
114
114
|
"@types/jest": "^30.0.0",
|
|
115
|
-
"@types/node": "^25.
|
|
116
|
-
"@typescript/native-preview": "7.0.0-dev.
|
|
115
|
+
"@types/node": "^25.9.1",
|
|
116
|
+
"@typescript/native-preview": "7.0.0-dev.20260526.1",
|
|
117
117
|
"i18next": "26.2.0",
|
|
118
118
|
"jest": "^30.4.2",
|
|
119
119
|
"react": "^19.2.6",
|
|
120
120
|
"react-dom": "^19.2.6",
|
|
121
121
|
"react-i18next": "17.0.8",
|
|
122
|
-
"ts-jest": "^29.4.
|
|
123
|
-
"@modern-js/app-tools": "npm:@bleedingdev/modern-js-app-tools@3.2.0-ultramodern.
|
|
124
|
-
"@modern-js/runtime": "npm:@bleedingdev/modern-js-runtime@3.2.0-ultramodern.
|
|
122
|
+
"ts-jest": "^29.4.11",
|
|
123
|
+
"@modern-js/app-tools": "npm:@bleedingdev/modern-js-app-tools@3.2.0-ultramodern.23",
|
|
124
|
+
"@modern-js/runtime": "npm:@bleedingdev/modern-js-runtime@3.2.0-ultramodern.23"
|
|
125
125
|
},
|
|
126
126
|
"sideEffects": false,
|
|
127
127
|
"publishConfig": {
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { dirname, resolve } from 'node:path';
|
|
2
|
+
import { fileURLToPath } from 'node:url';
|
|
3
|
+
import type { ProjectConfig } from '@rstest/core';
|
|
4
|
+
import { withTestPreset } from '@scripts/rstest-config';
|
|
5
|
+
|
|
6
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
7
|
+
|
|
8
|
+
const commonConfig: ProjectConfig = {
|
|
9
|
+
setupFiles: [resolve(__dirname, '../../../scripts/rstest-config/setup.ts')],
|
|
10
|
+
globals: true,
|
|
11
|
+
tools: {
|
|
12
|
+
swc: {
|
|
13
|
+
jsc: {
|
|
14
|
+
transform: {
|
|
15
|
+
react: {
|
|
16
|
+
runtime: 'automatic',
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export default {
|
|
25
|
+
projects: [
|
|
26
|
+
withTestPreset({
|
|
27
|
+
name: 'plugin-i18n-node',
|
|
28
|
+
testEnvironment: 'node',
|
|
29
|
+
include: ['tests/localisedUrls.test.ts'],
|
|
30
|
+
extends: commonConfig,
|
|
31
|
+
}),
|
|
32
|
+
withTestPreset({
|
|
33
|
+
name: 'plugin-i18n-client',
|
|
34
|
+
testEnvironment: 'happy-dom',
|
|
35
|
+
include: ['tests/routerAdapter.test.tsx'],
|
|
36
|
+
extends: commonConfig,
|
|
37
|
+
}),
|
|
38
|
+
],
|
|
39
|
+
};
|
package/src/cli/index.ts
CHANGED
|
@@ -1,8 +1,16 @@
|
|
|
1
1
|
import type { AppTools, CliPlugin } from '@modern-js/app-tools';
|
|
2
2
|
import { getPublicDirRoutePrefixes } from '@modern-js/server-core';
|
|
3
|
-
import type {
|
|
3
|
+
import type {
|
|
4
|
+
Entrypoint,
|
|
5
|
+
NestedRouteForCli,
|
|
6
|
+
PageRoute,
|
|
7
|
+
} from '@modern-js/types';
|
|
4
8
|
import fs from 'fs';
|
|
5
9
|
import path from 'path';
|
|
10
|
+
import {
|
|
11
|
+
applyLocalisedUrlsToRoutes,
|
|
12
|
+
resolveLocalisedUrlsConfig,
|
|
13
|
+
} from '../shared/localisedUrls';
|
|
6
14
|
import type { BackendOptions, LocaleDetectionOptions } from '../shared/type';
|
|
7
15
|
import { getBackendOptions, getLocaleDetectionOptions } from '../shared/utils';
|
|
8
16
|
|
|
@@ -203,6 +211,40 @@ export const i18nPlugin = (
|
|
|
203
211
|
};
|
|
204
212
|
});
|
|
205
213
|
|
|
214
|
+
api.modifyFileSystemRoutes(({ entrypoint, routes }) => {
|
|
215
|
+
if (!localeDetection) {
|
|
216
|
+
return { entrypoint, routes };
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
const localeDetectionOptions = getLocaleDetectionOptions(
|
|
220
|
+
entrypoint.entryName,
|
|
221
|
+
localeDetection,
|
|
222
|
+
);
|
|
223
|
+
const {
|
|
224
|
+
localePathRedirect,
|
|
225
|
+
languages = [],
|
|
226
|
+
localisedUrls,
|
|
227
|
+
} = localeDetectionOptions;
|
|
228
|
+
|
|
229
|
+
if (!localePathRedirect || languages.length === 0) {
|
|
230
|
+
return { entrypoint, routes };
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
const localisedUrlsConfig = resolveLocalisedUrlsConfig(localisedUrls);
|
|
234
|
+
if (!localisedUrlsConfig.enabled) {
|
|
235
|
+
return { entrypoint, routes };
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
return {
|
|
239
|
+
entrypoint,
|
|
240
|
+
routes: applyLocalisedUrlsToRoutes(
|
|
241
|
+
routes as (NestedRouteForCli | PageRoute)[],
|
|
242
|
+
languages,
|
|
243
|
+
localisedUrlsConfig.map,
|
|
244
|
+
),
|
|
245
|
+
};
|
|
246
|
+
});
|
|
247
|
+
|
|
206
248
|
api._internalServerPlugins(({ plugins }) => {
|
|
207
249
|
const { serverRoutes, metaName } = api.getAppContext();
|
|
208
250
|
const normalizedConfig = api.getNormalizedConfig();
|
package/src/runtime/I18nLink.tsx
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { Link, useInRouterContext, useParams } from '@modern-js/runtime/router';
|
|
2
1
|
import type React from 'react';
|
|
3
2
|
import { useModernI18n } from './context';
|
|
3
|
+
import { useI18nRouterAdapter } from './routerAdapter';
|
|
4
4
|
import { buildLocalizedUrl } from './utils';
|
|
5
5
|
|
|
6
6
|
export interface I18nLinkProps {
|
|
7
7
|
to: string;
|
|
8
8
|
children: React.ReactNode;
|
|
9
|
-
[key: string]: any;
|
|
9
|
+
[key: string]: any;
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
/**
|
|
@@ -24,30 +24,25 @@ export interface I18nLinkProps {
|
|
|
24
24
|
* <I18nLink to="/">Home</I18nLink>
|
|
25
25
|
* ```
|
|
26
26
|
*/
|
|
27
|
-
// Use static imports to avoid breaking router tree-shaking. Detect router context via useInRouterContext.
|
|
28
|
-
const useRouterHooks = () => {
|
|
29
|
-
const inRouter = useInRouterContext();
|
|
30
|
-
return {
|
|
31
|
-
Link: inRouter ? Link : null,
|
|
32
|
-
params: inRouter ? useParams() : ({} as any),
|
|
33
|
-
hasRouter: inRouter,
|
|
34
|
-
};
|
|
35
|
-
};
|
|
36
|
-
|
|
37
27
|
export const I18nLink: React.FC<I18nLinkProps> = ({
|
|
38
28
|
to,
|
|
39
29
|
children,
|
|
40
30
|
...props
|
|
41
31
|
}) => {
|
|
42
|
-
const { Link, params, hasRouter } =
|
|
43
|
-
const { language, supportedLanguages } = useModernI18n();
|
|
32
|
+
const { Link, params, hasRouter } = useI18nRouterAdapter();
|
|
33
|
+
const { language, supportedLanguages, localisedUrls } = useModernI18n();
|
|
44
34
|
|
|
45
35
|
// Get the current language from context (which reflects the actual current language)
|
|
46
36
|
// URL params might be stale after language changes, so we prioritize the context language
|
|
47
37
|
const currentLang = language;
|
|
48
38
|
|
|
49
39
|
// Build the localized URL by adding language prefix
|
|
50
|
-
const localizedTo = buildLocalizedUrl(
|
|
40
|
+
const localizedTo = buildLocalizedUrl(
|
|
41
|
+
to,
|
|
42
|
+
currentLang,
|
|
43
|
+
supportedLanguages,
|
|
44
|
+
localisedUrls,
|
|
45
|
+
);
|
|
51
46
|
|
|
52
47
|
// In development mode, warn if used outside of :lang route context
|
|
53
48
|
if (process.env.NODE_ENV === 'development' && hasRouter && !params.lang) {
|
|
@@ -57,7 +52,6 @@ export const I18nLink: React.FC<I18nLinkProps> = ({
|
|
|
57
52
|
);
|
|
58
53
|
}
|
|
59
54
|
|
|
60
|
-
// If router is not available, render as a regular anchor tag
|
|
61
55
|
if (!hasRouter || !Link) {
|
|
62
56
|
return (
|
|
63
57
|
<a href={localizedTo} {...props}>
|