@bleedingdev/modern-js-plugin-i18n 3.2.0-ultramodern.3 → 3.2.0-ultramodern.30

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.
Files changed (85) hide show
  1. package/dist/cjs/cli/index.js +22 -0
  2. package/dist/cjs/runtime/I18nLink.js +4 -12
  3. package/dist/cjs/runtime/context.js +32 -5
  4. package/dist/cjs/runtime/hooks.js +8 -5
  5. package/dist/cjs/runtime/i18n/backend/defaults.js +1 -1
  6. package/dist/cjs/runtime/i18n/backend/middleware.node.js +4 -4
  7. package/dist/cjs/runtime/i18n/instance.js +4 -2
  8. package/dist/cjs/runtime/index.js +7 -6
  9. package/dist/cjs/runtime/routerAdapter.js +163 -0
  10. package/dist/cjs/runtime/utils.js +63 -94
  11. package/dist/cjs/server/index.js +60 -8
  12. package/dist/cjs/shared/localisedUrls.js +237 -0
  13. package/dist/esm/cli/index.mjs +22 -0
  14. package/dist/esm/runtime/I18nLink.mjs +4 -12
  15. package/dist/esm/runtime/context.mjs +34 -7
  16. package/dist/esm/runtime/hooks.mjs +9 -6
  17. package/dist/esm/runtime/i18n/backend/defaults.mjs +1 -1
  18. package/dist/esm/runtime/i18n/backend/middleware.node.mjs +3 -3
  19. package/dist/esm/runtime/i18n/instance.mjs +4 -2
  20. package/dist/esm/runtime/index.mjs +7 -6
  21. package/dist/esm/runtime/routerAdapter.mjs +129 -0
  22. package/dist/esm/runtime/utils.mjs +11 -30
  23. package/dist/esm/server/index.mjs +53 -7
  24. package/dist/esm/shared/localisedUrls.mjs +191 -0
  25. package/dist/esm-node/cli/index.mjs +22 -0
  26. package/dist/esm-node/runtime/I18nLink.mjs +4 -12
  27. package/dist/esm-node/runtime/context.mjs +34 -7
  28. package/dist/esm-node/runtime/hooks.mjs +9 -6
  29. package/dist/esm-node/runtime/i18n/backend/defaults.mjs +1 -1
  30. package/dist/esm-node/runtime/i18n/backend/middleware.node.mjs +3 -3
  31. package/dist/esm-node/runtime/i18n/instance.mjs +4 -2
  32. package/dist/esm-node/runtime/index.mjs +7 -6
  33. package/dist/esm-node/runtime/routerAdapter.mjs +130 -0
  34. package/dist/esm-node/runtime/utils.mjs +11 -30
  35. package/dist/esm-node/server/index.mjs +53 -7
  36. package/dist/esm-node/shared/localisedUrls.mjs +192 -0
  37. package/dist/types/cli/index.d.ts +21 -0
  38. package/dist/types/runtime/I18nLink.d.ts +23 -0
  39. package/dist/types/runtime/context.d.ts +41 -0
  40. package/dist/types/runtime/hooks.d.ts +30 -0
  41. package/dist/types/runtime/i18n/backend/config.d.ts +2 -0
  42. package/dist/types/runtime/i18n/backend/defaults.d.ts +13 -0
  43. package/dist/types/runtime/i18n/backend/defaults.node.d.ts +8 -0
  44. package/dist/types/runtime/i18n/backend/index.d.ts +3 -0
  45. package/dist/types/runtime/i18n/backend/middleware.common.d.ts +14 -0
  46. package/dist/types/runtime/i18n/backend/middleware.d.ts +12 -0
  47. package/dist/types/runtime/i18n/backend/middleware.node.d.ts +13 -0
  48. package/dist/types/runtime/i18n/backend/sdk-backend.d.ts +53 -0
  49. package/dist/types/runtime/i18n/backend/sdk-event.d.ts +9 -0
  50. package/dist/types/runtime/i18n/detection/config.d.ts +11 -0
  51. package/dist/types/runtime/i18n/detection/index.d.ts +50 -0
  52. package/dist/types/runtime/i18n/detection/middleware.d.ts +24 -0
  53. package/dist/types/runtime/i18n/detection/middleware.node.d.ts +17 -0
  54. package/dist/types/runtime/i18n/index.d.ts +3 -0
  55. package/dist/types/runtime/i18n/instance.d.ts +96 -0
  56. package/dist/types/runtime/i18n/utils.d.ts +29 -0
  57. package/dist/types/runtime/index.d.ts +21 -0
  58. package/dist/types/runtime/routerAdapter.d.ts +26 -0
  59. package/dist/types/runtime/types.d.ts +15 -0
  60. package/dist/types/runtime/utils.d.ts +28 -0
  61. package/dist/types/server/index.d.ts +14 -0
  62. package/dist/types/shared/deepMerge.d.ts +1 -0
  63. package/dist/types/shared/detection.d.ts +11 -0
  64. package/dist/types/shared/localisedUrls.d.ts +13 -0
  65. package/dist/types/shared/type.d.ts +168 -0
  66. package/dist/types/shared/utils.d.ts +5 -0
  67. package/package.json +15 -15
  68. package/rstest.config.mts +39 -0
  69. package/src/cli/index.ts +43 -1
  70. package/src/runtime/I18nLink.tsx +10 -16
  71. package/src/runtime/context.tsx +45 -7
  72. package/src/runtime/hooks.ts +13 -4
  73. package/src/runtime/i18n/backend/defaults.ts +3 -1
  74. package/src/runtime/i18n/backend/middleware.node.ts +1 -1
  75. package/src/runtime/i18n/instance.ts +14 -5
  76. package/src/runtime/index.tsx +10 -2
  77. package/src/runtime/routerAdapter.tsx +333 -0
  78. package/src/runtime/utils.ts +22 -34
  79. package/src/server/index.ts +117 -10
  80. package/src/shared/localisedUrls.ts +393 -0
  81. package/src/shared/type.ts +12 -0
  82. package/tests/localisedUrls.test.ts +278 -0
  83. package/tests/routerAdapter.test.tsx +278 -0
  84. package/dist/esm/rslib-runtime.mjs +0 -18
  85. package/dist/esm-node/rslib-runtime.mjs +0 -19
@@ -0,0 +1,21 @@
1
+ import type { AppTools, CliPlugin } from '@modern-js/app-tools';
2
+ import type { Entrypoint } from '@modern-js/types';
3
+ import type { BackendOptions, LocaleDetectionOptions } from '../shared/type';
4
+ export type TransformRuntimeConfigFn = (extendedConfig: Record<string, any>, entrypoint: Entrypoint) => Record<string, any>;
5
+ export interface I18nPluginOptions {
6
+ localeDetection?: LocaleDetectionOptions;
7
+ backend?: BackendOptions;
8
+ transformRuntimeConfig?: TransformRuntimeConfigFn;
9
+ customPlugin?: {
10
+ runtime?: {
11
+ name?: string;
12
+ path?: string;
13
+ };
14
+ server?: {
15
+ name?: string;
16
+ };
17
+ };
18
+ [key: string]: any;
19
+ }
20
+ export declare const i18nPlugin: (options?: I18nPluginOptions) => CliPlugin<AppTools>;
21
+ export default i18nPlugin;
@@ -0,0 +1,23 @@
1
+ import type React from 'react';
2
+ export interface I18nLinkProps {
3
+ to: string;
4
+ children: React.ReactNode;
5
+ [key: string]: any;
6
+ }
7
+ /**
8
+ * I18nLink component that automatically adds language prefix to navigation links.
9
+ * This component should be used within a :lang dynamic route context.
10
+ *
11
+ * @example
12
+ * ```tsx
13
+ * // When current language is 'en' and to="/about"
14
+ * // The actual link will be "/en/about"
15
+ * <I18nLink to="/about">About</I18nLink>
16
+ *
17
+ * // When current language is 'zh' and to="/"
18
+ * // The actual link will be "/zh"
19
+ * <I18nLink to="/">Home</I18nLink>
20
+ * ```
21
+ */
22
+ export declare const I18nLink: React.FC<I18nLinkProps>;
23
+ export default I18nLink;
@@ -0,0 +1,41 @@
1
+ import type { FC, ReactNode } from 'react';
2
+ import type { LocalisedUrlsOption } from '../shared/localisedUrls';
3
+ import type { I18nInstance } from './i18n';
4
+ export interface ModernI18nContextValue {
5
+ language: string;
6
+ i18nInstance: I18nInstance;
7
+ entryName?: string;
8
+ languages?: string[];
9
+ localePathRedirect?: boolean;
10
+ ignoreRedirectRoutes?: string[] | ((pathname: string) => boolean);
11
+ localisedUrls?: LocalisedUrlsOption;
12
+ updateLanguage?: (newLang: string) => void;
13
+ }
14
+ export interface ModernI18nProviderProps {
15
+ children: ReactNode;
16
+ value: ModernI18nContextValue;
17
+ }
18
+ export declare const ModernI18nProvider: FC<ModernI18nProviderProps>;
19
+ export interface UseModernI18nReturn {
20
+ language: string;
21
+ changeLanguage: (newLang: string) => Promise<void>;
22
+ i18nInstance: I18nInstance;
23
+ supportedLanguages: string[];
24
+ localisedUrls?: LocalisedUrlsOption;
25
+ isLanguageSupported: (lang: string) => boolean;
26
+ isResourcesReady: boolean;
27
+ }
28
+ /**
29
+ * Hook for accessing i18n functionality in Modern.js applications.
30
+ *
31
+ * This hook provides:
32
+ * - Current language from URL params or i18n context
33
+ * - changeLanguage function that updates both i18n instance and URL
34
+ * - Direct access to the i18n instance
35
+ * - List of supported languages
36
+ * - Helper function to check if a language is supported
37
+ *
38
+ * @param options - Optional configuration to override context settings
39
+ * @returns Object containing i18n functionality and utilities
40
+ */
41
+ export declare const useModernI18n: () => UseModernI18nReturn;
@@ -0,0 +1,30 @@
1
+ import type { TRuntimeContext } from '@modern-js/runtime';
2
+ import type React from 'react';
3
+ import type { LocalisedUrlsOption } from '../shared/localisedUrls';
4
+ import type { I18nInstance } from './i18n';
5
+ interface RuntimeContextWithI18n extends TRuntimeContext {
6
+ i18nInstance?: I18nInstance;
7
+ }
8
+ export declare function createContextValue(lang: string, i18nInstance: I18nInstance | undefined, entryName: string | undefined, languages: string[], localePathRedirect: boolean, ignoreRedirectRoutes: string[] | ((pathname: string) => boolean) | undefined, localisedUrls: LocalisedUrlsOption | undefined, setLang: (lang: string) => void): {
9
+ language: string;
10
+ i18nInstance: I18nInstance;
11
+ entryName: string | undefined;
12
+ languages: string[];
13
+ localePathRedirect: boolean;
14
+ ignoreRedirectRoutes: string[] | ((pathname: string) => boolean) | undefined;
15
+ localisedUrls: LocalisedUrlsOption | undefined;
16
+ updateLanguage: (lang: string) => void;
17
+ };
18
+ export declare function useSdkResourcesLoader(i18nInstance: I18nInstance | undefined, setForceUpdate: React.Dispatch<React.SetStateAction<number>>): void;
19
+ /**
20
+ * Hook to handle client-side redirect for locale path redirect in static deployments
21
+ * This ensures that when users access paths without language prefix, they are redirected
22
+ * to the localized version of the path
23
+ *
24
+ * Note: This hook only runs in CSR (Client-Side Rendering) scenarios.
25
+ * In SSR/SSG scenarios, server-side middleware handles redirects, so this hook is skipped.
26
+ * We use process.env.MODERN_TARGET to ensure this code is only included in browser bundles.
27
+ */
28
+ export declare function useClientSideRedirect(i18nInstance: I18nInstance | undefined, localePathRedirect: boolean, languages: string[], fallbackLanguage: string, ignoreRedirectRoutes?: string[] | ((pathname: string) => boolean), localisedUrls?: LocalisedUrlsOption): void;
29
+ export declare function useLanguageSync(i18nInstance: I18nInstance | undefined, localePathRedirect: boolean, languages: string[], runtimeContextRef: React.MutableRefObject<RuntimeContextWithI18n>, prevLangRef: React.MutableRefObject<string>, setLang: (lang: string) => void): void;
30
+ export {};
@@ -0,0 +1,2 @@
1
+ import type { BackendOptions } from '../instance';
2
+ export declare function mergeBackendOptions(defaultOptions: BackendOptions, cliOptions?: BackendOptions, userOptions?: BackendOptions): BackendOptions;
@@ -0,0 +1,13 @@
1
+ export declare const DEFAULT_I18NEXT_BACKEND_OPTIONS: {
2
+ loadPath: string;
3
+ addPath: string;
4
+ };
5
+ declare global {
6
+ interface Window {
7
+ __assetPrefix__?: string;
8
+ }
9
+ }
10
+ export declare function convertBackendOptions<T extends {
11
+ loadPath?: string;
12
+ addPath?: string;
13
+ }>(options: T): T;
@@ -0,0 +1,8 @@
1
+ export declare const DEFAULT_I18NEXT_BACKEND_OPTIONS: {
2
+ loadPath: string;
3
+ addPath: string;
4
+ };
5
+ export declare function convertBackendOptions<T extends {
6
+ loadPath?: string;
7
+ addPath?: string;
8
+ }>(options: T): T;
@@ -0,0 +1,3 @@
1
+ import type { BaseBackendOptions } from '../../../shared/type';
2
+ import type { BackendOptions, I18nInitOptions } from '../instance';
3
+ export declare const mergeBackendOptions: (backend?: BaseBackendOptions, userInitOptions?: I18nInitOptions) => BackendOptions | undefined;
@@ -0,0 +1,14 @@
1
+ import type { BaseBackendOptions, ChainedBackendConfig } from '../../../shared/type';
2
+ import type { I18nInstance } from '../instance';
3
+ type BackendConfigWithChained = BaseBackendOptions & Partial<ChainedBackendConfig>;
4
+ /**
5
+ * Common logic for using i18next backend
6
+ * This function handles the backend selection and chained backend configuration
7
+ *
8
+ * @param i18nInstance - The i18n instance to configure
9
+ * @param BackendWithSave - The wrapped backend class with save method (required for chained backend refresh logic)
10
+ * @param BackendBase - The base backend class (for non-chained use)
11
+ * @param backend - Optional backend configuration
12
+ */
13
+ export declare function useI18nextBackendCommon(i18nInstance: I18nInstance, BackendWithSave: new (...args: any[]) => any, BackendBase: new (...args: any[]) => any, backend?: BackendConfigWithChained): void;
14
+ export {};
@@ -0,0 +1,12 @@
1
+ import Backend from 'i18next-http-backend';
2
+ import type { ExtendedBackendOptions } from '../../../shared/type';
3
+ import type { I18nInstance } from '../instance';
4
+ /**
5
+ * Wrapper for HTTP backend to add a no-op save method
6
+ * This is required for i18next-chained-backend to trigger refresh logic
7
+ * when cacheHitMode is 'refresh' or 'refreshAndUpdateStore'
8
+ */
9
+ export declare class HttpBackendWithSave extends Backend {
10
+ save(_language: string, _namespace: string, _data: unknown): void;
11
+ }
12
+ export declare const useI18nextBackend: (i18nInstance: I18nInstance, backend?: ExtendedBackendOptions) => void;
@@ -0,0 +1,13 @@
1
+ import Backend from 'i18next-fs-backend/cjs';
2
+ import type { ExtendedBackendOptions } from '../../../shared/type';
3
+ import type { I18nInstance } from '../instance';
4
+ /**
5
+ * Wrapper for FS backend to add a no-op save method
6
+ * This is required for i18next-chained-backend to trigger refresh logic
7
+ * when cacheHitMode is 'refresh' or 'refreshAndUpdateStore'
8
+ */
9
+ export declare class FsBackendWithSave extends Backend {
10
+ save(_language: string, _namespace: string, _data: unknown): void;
11
+ }
12
+ export declare const HttpBackendWithSave: typeof FsBackendWithSave;
13
+ export declare const useI18nextBackend: (i18nInstance: I18nInstance, backend?: ExtendedBackendOptions) => void;
@@ -0,0 +1,53 @@
1
+ import type { I18nSdkLoader } from '../../../shared/type';
2
+ interface BackendOptions {
3
+ sdk?: I18nSdkLoader;
4
+ [key: string]: unknown;
5
+ }
6
+ interface I18nextServices {
7
+ resourceStore?: {
8
+ data?: {
9
+ [language: string]: {
10
+ [namespace: string]: Record<string, string>;
11
+ };
12
+ };
13
+ };
14
+ store?: {
15
+ data?: {
16
+ [language: string]: {
17
+ [namespace: string]: Record<string, string>;
18
+ };
19
+ };
20
+ };
21
+ [key: string]: any;
22
+ }
23
+ export declare class SdkBackend {
24
+ static type: string;
25
+ type: 'backend';
26
+ sdk?: I18nSdkLoader;
27
+ private allResourcesCache;
28
+ private backendId;
29
+ private loadingPromises;
30
+ private services?;
31
+ constructor(_services: unknown, _options: Record<string, unknown>);
32
+ init(services: I18nextServices, backendOptions: BackendOptions, _i18nextOptions: unknown): void;
33
+ read(language: string, namespace: string, callback: (error: Error | null, data: unknown) => void): void;
34
+ create(_languages: string[], _namespace: string, _key: string, _fallbackValue: string): void;
35
+ isLoading(language: string, namespace: string): boolean;
36
+ getLoadingResources(): Array<{
37
+ language: string;
38
+ namespace: string;
39
+ }>;
40
+ hasLoadingResources(): boolean;
41
+ private getCacheKey;
42
+ private loadResource;
43
+ private handlePromise;
44
+ private normalizeError;
45
+ private callSdk;
46
+ private extractFromCache;
47
+ private updateCache;
48
+ private formatResources;
49
+ private isObject;
50
+ private mergeWithExistingResources;
51
+ private triggerI18nextUpdate;
52
+ }
53
+ export {};
@@ -0,0 +1,9 @@
1
+ export declare const I18N_SDK_RESOURCES_LOADED_EVENT = "i18n-sdk-resources-loaded";
2
+ export interface I18nSdkResourcesLoadedEventDetail {
3
+ language: string;
4
+ namespace: string;
5
+ backendId?: string;
6
+ }
7
+ export declare function createI18nSdkBackendId(): string;
8
+ export declare function setI18nSdkBackendId(target: unknown, backendId: string): void;
9
+ export declare function getI18nSdkBackendId(target: unknown): string | undefined;
@@ -0,0 +1,11 @@
1
+ import type { LanguageDetectorOptions } from '../instance';
2
+ export declare const DEFAULT_I18NEXT_DETECTION_OPTIONS: {
3
+ caches: string[];
4
+ order: string[];
5
+ cookieMinutes: number;
6
+ lookupQuerystring: string;
7
+ lookupCookie: string;
8
+ lookupLocalStorage: string;
9
+ lookupHeader: string;
10
+ };
11
+ export declare function mergeDetectionOptions(cliOptions?: LanguageDetectorOptions, userOptions?: LanguageDetectorOptions, defaultOptions?: LanguageDetectorOptions): LanguageDetectorOptions;
@@ -0,0 +1,50 @@
1
+ import { type TRuntimeContext } from '@modern-js/runtime';
2
+ import type { I18nInitOptions, I18nInstance, LanguageDetectorOptions } from '../instance';
3
+ import { cacheUserLanguage } from './middleware';
4
+ export { cacheUserLanguage };
5
+ export declare function exportServerLngToWindow(context: TRuntimeContext, lng: string): void;
6
+ export declare const getLanguageFromSSRData: (window: Window) => string | undefined;
7
+ export interface BaseLanguageDetectionOptions {
8
+ languages: string[];
9
+ fallbackLanguage: string;
10
+ localePathRedirect: boolean;
11
+ i18nextDetector: boolean;
12
+ detection?: LanguageDetectorOptions;
13
+ userInitOptions?: I18nInitOptions;
14
+ mergedBackend?: any;
15
+ }
16
+ export interface LanguageDetectionOptions extends BaseLanguageDetectionOptions {
17
+ pathname: string;
18
+ ssrContext?: any;
19
+ }
20
+ export interface LanguageDetectionResult {
21
+ detectedLanguage?: string;
22
+ finalLanguage: string;
23
+ }
24
+ /**
25
+ * Detect language with priority:
26
+ * Priority 1: SSR data (try window._SSR_DATA first, works for both SSR and CSR)
27
+ * Priority 2: Path detection
28
+ * Priority 3: i18next detector (reads from cookie/localStorage)
29
+ * Priority 4: User config language or fallback
30
+ */
31
+ export declare const detectLanguageWithPriority: (i18nInstance: I18nInstance, options: LanguageDetectionOptions) => Promise<LanguageDetectionResult>;
32
+ /**
33
+ * Options for building i18n init options
34
+ */
35
+ export interface BuildInitOptionsParams {
36
+ finalLanguage: string;
37
+ fallbackLanguage: string;
38
+ languages: string[];
39
+ userInitOptions?: I18nInitOptions;
40
+ mergedDetection?: any;
41
+ mergeBackend?: any;
42
+ }
43
+ /**
44
+ * Build i18n initialization options
45
+ */
46
+ export declare const buildInitOptions: (params: BuildInitOptionsParams) => I18nInitOptions;
47
+ /**
48
+ * Merge detection and backend options
49
+ */
50
+ export declare const mergeDetectionOptions: (i18nextDetector: boolean, detection?: LanguageDetectorOptions, localePathRedirect?: boolean, userInitOptions?: I18nInitOptions) => LanguageDetectorOptions;
@@ -0,0 +1,24 @@
1
+ import type { I18nInstance } from '../instance';
2
+ /**
3
+ * Register LanguageDetector plugin to i18n instance
4
+ * Must be called before init() to properly register the detector
5
+ * For wrapper instances, ensure detector is registered on the underlying i18next instance
6
+ */
7
+ export declare const useI18nextLanguageDetector: (i18nInstance: I18nInstance) => void | I18nInstance;
8
+ /**
9
+ * Read language directly from localStorage/cookie
10
+ * Fallback when detector is not available in services
11
+ */
12
+ export declare const readLanguageFromStorage: (detectionOptions?: any) => string | undefined;
13
+ /**
14
+ * Detect language using i18next-browser-languagedetector
15
+ * For initialized instances without detector in services, manually create a detector instance
16
+ * For wrapper instances, access the underlying i18next instance's services
17
+ */
18
+ export declare const detectLanguage: (i18nInstance: I18nInstance, _request?: any, detectionOptions?: any) => string | undefined;
19
+ /**
20
+ * Cache user language to localStorage/cookie
21
+ * Uses LanguageDetector's cacheUserLanguage method when available
22
+ * For wrapper instances, access the underlying i18next instance's services
23
+ */
24
+ export declare const cacheUserLanguage: (i18nInstance: I18nInstance, language: string, detectionOptions?: any) => void;
@@ -0,0 +1,17 @@
1
+ import type { I18nInstance } from '../instance';
2
+ export declare const cacheUserLanguage: (_i18nInstance: I18nInstance, _language: string, _detectionOptions?: any) => void;
3
+ /**
4
+ * Read language directly from storage (localStorage/cookie)
5
+ * Not available in Node.js environment, returns undefined
6
+ */
7
+ export declare const readLanguageFromStorage: (_detectionOptions?: any) => string | undefined;
8
+ /**
9
+ * Register LanguageDetector plugin to i18n instance
10
+ * Must be called before init() to properly register the detector
11
+ */
12
+ export declare const useI18nextLanguageDetector: (i18nInstance: I18nInstance) => void | I18nInstance;
13
+ /**
14
+ * Detect language using i18next-http-middleware LanguageDetector
15
+ * For initialized instances without detector in services, manually create a detector instance
16
+ */
17
+ export declare const detectLanguage: (i18nInstance: I18nInstance, request?: any, detectionOptions?: any) => string | undefined;
@@ -0,0 +1,3 @@
1
+ export type { I18nInitOptions, I18nInstance, } from './instance';
2
+ export { getI18nInstance, isI18nInstance } from './instance';
3
+ export { assertI18nInstance } from './utils';
@@ -0,0 +1,96 @@
1
+ import type { BaseBackendOptions } from '../../shared/type';
2
+ type ReactI18nextModule = typeof import('react-i18next');
3
+ type InitReactI18next = ReactI18nextModule['initReactI18next'];
4
+ type I18nextProviderComponent = ReactI18nextModule['I18nextProvider'];
5
+ export interface I18nResourceStore {
6
+ data?: {
7
+ [language: string]: {
8
+ [namespace: string]: string | {
9
+ [key: string]: any;
10
+ };
11
+ };
12
+ };
13
+ addResourceBundle?: (language: string, namespace: string, resources: Record<string, string>, deep?: boolean, overwrite?: boolean) => void;
14
+ }
15
+ export declare function isI18nWrapperInstance(obj: any): boolean;
16
+ export declare function getI18nWrapperI18nextInstance(wrapperInstance: any): any;
17
+ export declare function getActualI18nextInstance(instance: I18nInstance | any): any;
18
+ export interface I18nInstance {
19
+ language: string;
20
+ isInitialized?: boolean;
21
+ init: {
22
+ (callback?: (error: any, t: any) => void): Promise<any>;
23
+ (options: I18nInitOptions, callback?: (error: any, t: any) => void): Promise<any>;
24
+ };
25
+ changeLanguage?: (lng?: string, callback?: (error: any, t: any) => void) => Promise<any>;
26
+ setLang?: (lang: string) => void | Promise<void>;
27
+ use: (plugin: any) => void;
28
+ createInstance?: (options?: I18nInitOptions) => I18nInstance;
29
+ cloneInstance?: () => I18nInstance;
30
+ store?: I18nResourceStore;
31
+ emit?: (event: string, ...args: any[]) => void;
32
+ reloadResources?: (language?: string, namespace?: string) => Promise<void>;
33
+ services?: {
34
+ languageDetector?: {
35
+ detect: (request?: any, options?: any) => string | string[] | undefined;
36
+ [key: string]: any;
37
+ };
38
+ resourceStore?: I18nResourceStore;
39
+ backend?: any;
40
+ [key: string]: any;
41
+ };
42
+ options?: {
43
+ backend?: BackendOptions;
44
+ [key: string]: any;
45
+ };
46
+ [key: string]: any;
47
+ }
48
+ type LanguageDetectorOrder = string[];
49
+ type LanguageDetectorCaches = boolean | string[];
50
+ export interface LanguageDetectorOptions {
51
+ order?: LanguageDetectorOrder;
52
+ lookupQuerystring?: string;
53
+ lookupCookie?: string;
54
+ lookupLocalStorage?: string;
55
+ lookupSession?: string;
56
+ lookupFromPathIndex?: number;
57
+ caches?: LanguageDetectorCaches;
58
+ cookieExpirationDate?: Date;
59
+ cookieDomain?: string;
60
+ lookupHeader?: string;
61
+ }
62
+ export interface BackendOptions extends Omit<BaseBackendOptions, 'enabled'> {
63
+ parse?: (data: string) => any;
64
+ stringify?: (data: any) => string;
65
+ [key: string]: any;
66
+ }
67
+ export interface Resources {
68
+ [lng: string]: {
69
+ [source: string]: string | Record<string, string>;
70
+ };
71
+ }
72
+ export type I18nInitOptions = {
73
+ lng?: string;
74
+ fallbackLng?: string;
75
+ supportedLngs?: string[];
76
+ initImmediate?: boolean;
77
+ detection?: LanguageDetectorOptions;
78
+ backend?: BackendOptions;
79
+ resources?: Resources;
80
+ ns?: string | string[];
81
+ defaultNS?: string | string[];
82
+ interpolation?: {
83
+ escapeValue?: boolean;
84
+ [key: string]: any;
85
+ };
86
+ react?: {
87
+ useSuspense?: boolean;
88
+ [key: string]: any;
89
+ };
90
+ };
91
+ export declare function isI18nInstance(obj: any): obj is I18nInstance;
92
+ export declare function getI18nextInstanceForProvider(instance: I18nInstance | any): any;
93
+ export declare function getI18nInstance(userInstance?: I18nInstance | any): Promise<I18nInstance>;
94
+ export declare function getInitReactI18next(): Promise<InitReactI18next | null>;
95
+ export declare function getI18nextProvider(): Promise<I18nextProviderComponent | null>;
96
+ export {};
@@ -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;