@bleedingdev/modern-js-plugin-i18n 3.4.0-ultramodern.1 → 3.4.0-ultramodern.3

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 (35) hide show
  1. package/dist/cjs/cli/index.js +2 -1
  2. package/dist/cjs/runtime/context.js +8 -0
  3. package/dist/cjs/runtime/core.js +204 -0
  4. package/dist/cjs/runtime/i18n/backend/middleware.node.js +22 -4
  5. package/dist/cjs/runtime/index.js +44 -173
  6. package/dist/cjs/runtime/no-react-i18next.js +76 -0
  7. package/dist/esm/cli/index.mjs +2 -1
  8. package/dist/esm/runtime/context.mjs +8 -0
  9. package/dist/esm/runtime/core.mjs +139 -0
  10. package/dist/esm/runtime/i18n/backend/middleware.node.mjs +19 -4
  11. package/dist/esm/runtime/index.mjs +5 -140
  12. package/dist/esm/runtime/no-react-i18next.mjs +6 -0
  13. package/dist/esm-node/cli/index.mjs +2 -1
  14. package/dist/esm-node/runtime/context.mjs +8 -0
  15. package/dist/esm-node/runtime/core.mjs +140 -0
  16. package/dist/esm-node/runtime/i18n/backend/middleware.node.mjs +19 -4
  17. package/dist/esm-node/runtime/index.mjs +5 -140
  18. package/dist/esm-node/runtime/no-react-i18next.mjs +7 -0
  19. package/dist/types/runtime/context.d.ts +1 -0
  20. package/dist/types/runtime/core.d.ts +30 -0
  21. package/dist/types/runtime/i18n/backend/middleware.node.d.ts +4 -1
  22. package/dist/types/runtime/index.d.ts +2 -24
  23. package/dist/types/runtime/no-react-i18next.d.ts +3 -0
  24. package/package.json +21 -11
  25. package/rstest.config.mts +1 -0
  26. package/src/cli/index.ts +6 -1
  27. package/src/runtime/context.tsx +13 -0
  28. package/src/runtime/core.tsx +335 -0
  29. package/src/runtime/i18n/backend/middleware.node.ts +31 -1
  30. package/src/runtime/index.tsx +4 -316
  31. package/src/runtime/no-react-i18next.tsx +7 -0
  32. package/tests/i18nUtils.test.ts +34 -0
  33. package/tests/localisedUrls.test.ts +35 -0
  34. package/tests/reactI18nextRuntimeBoundary.test.ts +27 -0
  35. package/tests/routerAdapter.test.tsx +58 -1
@@ -0,0 +1,335 @@
1
+ import {
2
+ isBrowser,
3
+ RuntimeContext,
4
+ type RuntimePlugin,
5
+ } from '@modern-js/runtime';
6
+ import { Helmet } from '@modern-js/runtime/head';
7
+ import type { TInternalRuntimeContext } from '@modern-js/runtime/internal';
8
+ import { merge } from '@modern-js/runtime-utils/merge';
9
+ import type React from 'react';
10
+ import { useContext, useEffect, useMemo, useRef, useState } from 'react';
11
+ import type {
12
+ BaseBackendOptions,
13
+ BaseLocaleDetectionOptions,
14
+ } from '../shared/type';
15
+ import { ModernI18nProvider } from './context';
16
+ import {
17
+ createContextValue,
18
+ useClientSideRedirect,
19
+ useLanguageSync,
20
+ useSdkResourcesLoader,
21
+ } from './hooks';
22
+ import type { I18nInitOptions, I18nInstance } from './i18n';
23
+ import { getI18nInstance } from './i18n';
24
+ import { mergeBackendOptions } from './i18n/backend';
25
+ import { useI18nextBackend } from './i18n/backend/middleware';
26
+ import {
27
+ detectLanguageWithPriority,
28
+ exportServerLngToWindow,
29
+ mergeDetectionOptions,
30
+ } from './i18n/detection';
31
+ import { useI18nextLanguageDetector } from './i18n/detection/middleware';
32
+ import { getI18nextInstanceForProvider } from './i18n/instance';
33
+ import {
34
+ changeI18nLanguage,
35
+ ensureLanguageMatch,
36
+ initializeI18nInstance,
37
+ setupClonedInstance,
38
+ } from './i18n/utils';
39
+ import { getPathname } from './utils';
40
+ import './types';
41
+
42
+ export type { I18nSdkLoader, I18nSdkLoadOptions } from '../shared/type';
43
+ export type { Resources } from './i18n/instance';
44
+
45
+ export interface I18nPluginOptions {
46
+ entryName?: string;
47
+ localeDetection?: BaseLocaleDetectionOptions;
48
+ backend?: BaseBackendOptions;
49
+ i18nInstance?: I18nInstance;
50
+ changeLanguage?: (lang: string) => void;
51
+ initOptions?: I18nInitOptions;
52
+ htmlLangAttr?: boolean;
53
+ reactI18next?: boolean;
54
+ [key: string]: any;
55
+ }
56
+
57
+ interface RuntimeContextWithI18n extends TInternalRuntimeContext {
58
+ i18nInstance?: I18nInstance;
59
+ changeLanguage?: (lang: string) => Promise<void>;
60
+ }
61
+
62
+ export interface ReactI18nextIntegration {
63
+ I18nextProvider: React.ComponentType<any> | null;
64
+ initReactI18next: any | null;
65
+ }
66
+
67
+ export type LoadReactI18nextIntegration =
68
+ () => Promise<ReactI18nextIntegration | null>;
69
+
70
+ export const createI18nPlugin =
71
+ (
72
+ loadReactI18nextIntegration?: LoadReactI18nextIntegration,
73
+ ): ((options: I18nPluginOptions) => RuntimePlugin) =>
74
+ (options: I18nPluginOptions): RuntimePlugin => ({
75
+ name: '@modern-js/plugin-i18n',
76
+ setup: api => {
77
+ const {
78
+ entryName,
79
+ i18nInstance: userI18nInstance,
80
+ initOptions,
81
+ localeDetection,
82
+ backend,
83
+ htmlLangAttr = false,
84
+ reactI18next = true,
85
+ } = options;
86
+ const {
87
+ localePathRedirect = false,
88
+ i18nextDetector = true,
89
+ languages = [],
90
+ fallbackLanguage = 'en',
91
+ detection,
92
+ ignoreRedirectRoutes,
93
+ localisedUrls,
94
+ } = localeDetection || {};
95
+ const { enabled: backendEnabled = false } = backend || {};
96
+ let latestI18nInstance: I18nInstance | undefined;
97
+ let I18nextProvider: React.ComponentType<any> | null;
98
+
99
+ const resolveReactI18nextIntegration = async () => {
100
+ if (!reactI18next) {
101
+ return null;
102
+ }
103
+ return loadReactI18nextIntegration?.() ?? null;
104
+ };
105
+
106
+ api.onBeforeRender(async context => {
107
+ let i18nInstance = await getI18nInstance(userI18nInstance);
108
+ const { i18n: otherConfig } = api.getRuntimeConfig();
109
+ const { initOptions: otherInitOptions } = otherConfig || {};
110
+ const userInitOptions = merge(
111
+ otherInitOptions || {},
112
+ initOptions || {},
113
+ );
114
+ const reactI18nextIntegration = await resolveReactI18nextIntegration();
115
+ I18nextProvider = reactI18nextIntegration?.I18nextProvider ?? null;
116
+ if (reactI18nextIntegration?.initReactI18next) {
117
+ i18nInstance.use(reactI18nextIntegration.initReactI18next);
118
+ }
119
+
120
+ const pathname = getPathname(context);
121
+
122
+ if (i18nextDetector) {
123
+ useI18nextLanguageDetector(i18nInstance);
124
+ }
125
+
126
+ const mergedDetection = mergeDetectionOptions(
127
+ i18nextDetector,
128
+ detection,
129
+ localePathRedirect,
130
+ userInitOptions,
131
+ );
132
+ const mergedBackend = mergeBackendOptions(backend, userInitOptions);
133
+
134
+ // Register Backend BEFORE detectLanguageWithPriority
135
+ // This is critical because detectLanguageWithPriority may trigger init()
136
+ // through i18next detector, and backend must be registered before init()
137
+ // Register backend if:
138
+ // 1. enabled is true (explicitly or auto-detected), OR
139
+ // 2. SDK is configured (allows standalone SDK usage even without locales directory)
140
+ const hasSdkConfig =
141
+ typeof userInitOptions?.backend?.sdk === 'function' ||
142
+ (mergedBackend?.sdk && typeof mergedBackend.sdk === 'function');
143
+ if (mergedBackend && (backendEnabled || hasSdkConfig)) {
144
+ useI18nextBackend(i18nInstance, mergedBackend);
145
+ }
146
+
147
+ const { finalLanguage } = await detectLanguageWithPriority(
148
+ i18nInstance,
149
+ {
150
+ languages,
151
+ fallbackLanguage,
152
+ localePathRedirect,
153
+ i18nextDetector,
154
+ detection,
155
+ userInitOptions,
156
+ mergedBackend,
157
+ pathname,
158
+ ssrContext: context.ssrContext,
159
+ },
160
+ );
161
+
162
+ await initializeI18nInstance(
163
+ i18nInstance,
164
+ finalLanguage,
165
+ fallbackLanguage,
166
+ languages,
167
+ mergedDetection,
168
+ mergedBackend,
169
+ userInitOptions,
170
+ );
171
+
172
+ if (!isBrowser() && i18nInstance.cloneInstance) {
173
+ i18nInstance = i18nInstance.cloneInstance();
174
+ await setupClonedInstance(
175
+ i18nInstance,
176
+ finalLanguage,
177
+ fallbackLanguage,
178
+ languages,
179
+ backendEnabled,
180
+ backend,
181
+ i18nextDetector,
182
+ detection,
183
+ localePathRedirect,
184
+ userInitOptions,
185
+ );
186
+ }
187
+
188
+ if (localePathRedirect) {
189
+ await ensureLanguageMatch(i18nInstance, finalLanguage);
190
+ }
191
+
192
+ if (!isBrowser()) {
193
+ exportServerLngToWindow(context, finalLanguage);
194
+ }
195
+ context.i18nInstance = i18nInstance;
196
+ latestI18nInstance = i18nInstance;
197
+
198
+ // Add changeLanguage method to context for other runtime plugins to use
199
+ context.changeLanguage = async (newLang: string) => {
200
+ await changeI18nLanguage(i18nInstance, newLang, {
201
+ detectionOptions: mergedDetection,
202
+ });
203
+ };
204
+ });
205
+
206
+ api.wrapRoot(App => {
207
+ return props => {
208
+ const runtimeContext = useContext(
209
+ RuntimeContext,
210
+ ) as RuntimeContextWithI18n;
211
+ const i18nInstance =
212
+ runtimeContext.i18nInstance || latestI18nInstance;
213
+ const initialLang = useMemo(
214
+ () =>
215
+ i18nInstance?.language ||
216
+ (localeDetection?.fallbackLanguage ?? 'en'),
217
+ [i18nInstance?.language, localeDetection?.fallbackLanguage],
218
+ );
219
+ const [lang, setLang] = useState(initialLang);
220
+ const [forceUpdate, setForceUpdate] = useState(0);
221
+ const prevLangRef = useRef(lang);
222
+ const runtimeContextRef = useRef(runtimeContext);
223
+ runtimeContextRef.current = runtimeContext;
224
+
225
+ useEffect(() => {
226
+ if (i18nInstance?.language) {
227
+ const translator = (i18nInstance as any).translator;
228
+ if (translator) {
229
+ translator.language = i18nInstance.language;
230
+ }
231
+ }
232
+ }, [i18nInstance?.language]);
233
+
234
+ useEffect(() => {
235
+ prevLangRef.current = lang;
236
+ }, [lang]);
237
+
238
+ useSdkResourcesLoader(i18nInstance, setForceUpdate);
239
+ useLanguageSync(
240
+ i18nInstance,
241
+ localePathRedirect,
242
+ languages,
243
+ runtimeContextRef,
244
+ prevLangRef,
245
+ setLang,
246
+ );
247
+ // Handle client-side redirect for static deployments
248
+ // Note: This hook only executes in browser environment and skips SSR scenarios
249
+ useClientSideRedirect(
250
+ i18nInstance,
251
+ localePathRedirect,
252
+ languages,
253
+ fallbackLanguage,
254
+ ignoreRedirectRoutes,
255
+ localisedUrls,
256
+ );
257
+
258
+ const contextValue = useMemo(
259
+ () =>
260
+ createContextValue(
261
+ lang,
262
+ i18nInstance,
263
+ entryName,
264
+ languages,
265
+ localePathRedirect,
266
+ ignoreRedirectRoutes,
267
+ localisedUrls,
268
+ setLang,
269
+ ),
270
+ [
271
+ lang,
272
+ i18nInstance,
273
+ entryName,
274
+ languages,
275
+ localePathRedirect,
276
+ ignoreRedirectRoutes,
277
+ localisedUrls,
278
+ forceUpdate,
279
+ ],
280
+ );
281
+
282
+ const children = (props as React.PropsWithChildren).children;
283
+ const appContent = (
284
+ <>
285
+ {Boolean(htmlLangAttr) && <Helmet htmlAttributes={{ lang }} />}
286
+ <ModernI18nProvider value={contextValue}>
287
+ {App ? <App {...props}>{children}</App> : children}
288
+ </ModernI18nProvider>
289
+ </>
290
+ );
291
+
292
+ if (!i18nInstance) {
293
+ return appContent;
294
+ }
295
+
296
+ if (I18nextProvider) {
297
+ const i18nextInstanceForProvider =
298
+ getI18nextInstanceForProvider(i18nInstance);
299
+ return (
300
+ <I18nextProvider i18n={i18nextInstanceForProvider}>
301
+ {appContent}
302
+ </I18nextProvider>
303
+ );
304
+ }
305
+
306
+ return appContent;
307
+ };
308
+ });
309
+ },
310
+ });
311
+
312
+ export type {
313
+ AllowedLinkTarget,
314
+ CanonicalRoutePath,
315
+ UltramodernCanonicalRoutes,
316
+ } from './canonicalRoutes';
317
+ export { useModernI18n } from './context';
318
+ export { I18nLink, type I18nLinkProps } from './I18nLink';
319
+ export {
320
+ Link,
321
+ type LinkActiveOptions,
322
+ type LinkBaseProps,
323
+ type LinkParams,
324
+ type LinkProps,
325
+ } from './Link';
326
+ export {
327
+ canonicalPath,
328
+ type LocalizedPathsConfig,
329
+ localizePath,
330
+ type UseLocalizedLocationReturn,
331
+ type UseLocalizedPathsReturn,
332
+ useLocalizedLocation,
333
+ useLocalizedPaths,
334
+ } from './localizedPaths';
335
+ export { buildLocalizedUrl, splitUrlTarget } from './utils';
@@ -1,8 +1,38 @@
1
- import Backend from 'i18next-fs-backend/cjs';
1
+ import FsBackendModule from 'i18next-fs-backend';
2
2
  import type { ExtendedBackendOptions } from '../../../shared/type';
3
3
  import type { I18nInstance } from '../instance';
4
4
  import { useI18nextBackendCommon } from './middleware.common';
5
5
 
6
+ type BackendConstructor = new (...args: any[]) => any;
7
+
8
+ export const resolveFsBackendConstructor = (
9
+ backendModule: unknown,
10
+ ): BackendConstructor => {
11
+ const nestedDefault = (backendModule as { default?: { default?: unknown } })
12
+ ?.default?.default;
13
+ const nestedModuleExports = (
14
+ backendModule as { default?: { 'module.exports'?: unknown } }
15
+ )?.default?.['module.exports'];
16
+ const candidates = [
17
+ backendModule,
18
+ (backendModule as { default?: unknown })?.default,
19
+ (backendModule as { 'module.exports'?: unknown })?.['module.exports'],
20
+ nestedDefault,
21
+ nestedModuleExports,
22
+ ];
23
+ const Backend = candidates.find(candidate => typeof candidate === 'function');
24
+
25
+ if (!Backend) {
26
+ throw new Error(
27
+ 'Failed to resolve i18next-fs-backend constructor for the i18n Node backend.',
28
+ );
29
+ }
30
+
31
+ return Backend as BackendConstructor;
32
+ };
33
+
34
+ const Backend = resolveFsBackendConstructor(FsBackendModule);
35
+
6
36
  /**
7
37
  * Wrapper for FS backend to add a no-op save method
8
38
  * This is required for i18next-chained-backend to trigger refresh logic
@@ -1,320 +1,8 @@
1
- import {
2
- isBrowser,
3
- RuntimeContext,
4
- type RuntimePlugin,
5
- } from '@modern-js/runtime';
6
- import { Helmet } from '@modern-js/runtime/head';
7
- import type { TInternalRuntimeContext } from '@modern-js/runtime/internal';
8
- import { merge } from '@modern-js/runtime-utils/merge';
9
- import type React from 'react';
10
- import { useContext, useEffect, useMemo, useRef, useState } from 'react';
11
- import type {
12
- BaseBackendOptions,
13
- BaseLocaleDetectionOptions,
14
- } from '../shared/type';
15
- import { ModernI18nProvider } from './context';
16
- import {
17
- createContextValue,
18
- useClientSideRedirect,
19
- useLanguageSync,
20
- useSdkResourcesLoader,
21
- } from './hooks';
22
- import type { I18nInitOptions, I18nInstance } from './i18n';
23
- import { getI18nInstance } from './i18n';
24
- import { mergeBackendOptions } from './i18n/backend';
25
- import { useI18nextBackend } from './i18n/backend/middleware';
26
- import {
27
- detectLanguageWithPriority,
28
- exportServerLngToWindow,
29
- mergeDetectionOptions,
30
- } from './i18n/detection';
31
- import { useI18nextLanguageDetector } from './i18n/detection/middleware';
32
- import { getI18nextInstanceForProvider } from './i18n/instance';
33
- import {
34
- changeI18nLanguage,
35
- ensureLanguageMatch,
36
- initializeI18nInstance,
37
- setupClonedInstance,
38
- } from './i18n/utils';
39
- import { getPathname } from './utils';
40
- import './types';
1
+ import { createI18nPlugin } from './core';
2
+ import { getReactI18nextIntegration } from './i18n/react-i18next';
41
3
 
42
- export type { I18nSdkLoader, I18nSdkLoadOptions } from '../shared/type';
43
- export type { Resources } from './i18n/instance';
4
+ export * from './core';
44
5
 
45
- export interface I18nPluginOptions {
46
- entryName?: string;
47
- localeDetection?: BaseLocaleDetectionOptions;
48
- backend?: BaseBackendOptions;
49
- i18nInstance?: I18nInstance;
50
- changeLanguage?: (lang: string) => void;
51
- initOptions?: I18nInitOptions;
52
- htmlLangAttr?: boolean;
53
- reactI18next?: boolean;
54
- [key: string]: any;
55
- }
6
+ export const i18nPlugin = createI18nPlugin(getReactI18nextIntegration);
56
7
 
57
- interface RuntimeContextWithI18n extends TInternalRuntimeContext {
58
- i18nInstance?: I18nInstance;
59
- changeLanguage?: (lang: string) => Promise<void>;
60
- }
61
-
62
- export const i18nPlugin = (options: I18nPluginOptions): RuntimePlugin => ({
63
- name: '@modern-js/plugin-i18n',
64
- setup: api => {
65
- const {
66
- entryName,
67
- i18nInstance: userI18nInstance,
68
- initOptions,
69
- localeDetection,
70
- backend,
71
- htmlLangAttr = false,
72
- reactI18next = true,
73
- } = options;
74
- const {
75
- localePathRedirect = false,
76
- i18nextDetector = true,
77
- languages = [],
78
- fallbackLanguage = 'en',
79
- detection,
80
- ignoreRedirectRoutes,
81
- localisedUrls,
82
- } = localeDetection || {};
83
- const { enabled: backendEnabled = false } = backend || {};
84
- let latestI18nInstance: I18nInstance | undefined;
85
- let I18nextProvider: React.ComponentType<any> | null;
86
-
87
- const loadReactI18nextIntegration = async () => {
88
- if (!reactI18next) {
89
- return null;
90
- }
91
- const { getReactI18nextIntegration } = await import(
92
- './i18n/react-i18next'
93
- );
94
- return getReactI18nextIntegration();
95
- };
96
-
97
- api.onBeforeRender(async context => {
98
- let i18nInstance = await getI18nInstance(userI18nInstance);
99
- const { i18n: otherConfig } = api.getRuntimeConfig();
100
- const { initOptions: otherInitOptions } = otherConfig || {};
101
- const userInitOptions = merge(otherInitOptions || {}, initOptions || {});
102
- const reactI18nextIntegration = await loadReactI18nextIntegration();
103
- I18nextProvider = reactI18nextIntegration?.I18nextProvider ?? null;
104
- if (reactI18nextIntegration?.initReactI18next) {
105
- i18nInstance.use(reactI18nextIntegration.initReactI18next);
106
- }
107
-
108
- const pathname = getPathname(context);
109
-
110
- if (i18nextDetector) {
111
- useI18nextLanguageDetector(i18nInstance);
112
- }
113
-
114
- const mergedDetection = mergeDetectionOptions(
115
- i18nextDetector,
116
- detection,
117
- localePathRedirect,
118
- userInitOptions,
119
- );
120
- const mergedBackend = mergeBackendOptions(backend, userInitOptions);
121
-
122
- // Register Backend BEFORE detectLanguageWithPriority
123
- // This is critical because detectLanguageWithPriority may trigger init()
124
- // through i18next detector, and backend must be registered before init()
125
- // Register backend if:
126
- // 1. enabled is true (explicitly or auto-detected), OR
127
- // 2. SDK is configured (allows standalone SDK usage even without locales directory)
128
- const hasSdkConfig =
129
- typeof userInitOptions?.backend?.sdk === 'function' ||
130
- (mergedBackend?.sdk && typeof mergedBackend.sdk === 'function');
131
- if (mergedBackend && (backendEnabled || hasSdkConfig)) {
132
- useI18nextBackend(i18nInstance, mergedBackend);
133
- }
134
-
135
- const { finalLanguage } = await detectLanguageWithPriority(i18nInstance, {
136
- languages,
137
- fallbackLanguage,
138
- localePathRedirect,
139
- i18nextDetector,
140
- detection,
141
- userInitOptions,
142
- mergedBackend,
143
- pathname,
144
- ssrContext: context.ssrContext,
145
- });
146
-
147
- await initializeI18nInstance(
148
- i18nInstance,
149
- finalLanguage,
150
- fallbackLanguage,
151
- languages,
152
- mergedDetection,
153
- mergedBackend,
154
- userInitOptions,
155
- );
156
-
157
- if (!isBrowser() && i18nInstance.cloneInstance) {
158
- i18nInstance = i18nInstance.cloneInstance();
159
- await setupClonedInstance(
160
- i18nInstance,
161
- finalLanguage,
162
- fallbackLanguage,
163
- languages,
164
- backendEnabled,
165
- backend,
166
- i18nextDetector,
167
- detection,
168
- localePathRedirect,
169
- userInitOptions,
170
- );
171
- }
172
-
173
- if (localePathRedirect) {
174
- await ensureLanguageMatch(i18nInstance, finalLanguage);
175
- }
176
-
177
- if (!isBrowser()) {
178
- exportServerLngToWindow(context, finalLanguage);
179
- }
180
- context.i18nInstance = i18nInstance;
181
- latestI18nInstance = i18nInstance;
182
-
183
- // Add changeLanguage method to context for other runtime plugins to use
184
- context.changeLanguage = async (newLang: string) => {
185
- await changeI18nLanguage(i18nInstance, newLang, {
186
- detectionOptions: mergedDetection,
187
- });
188
- };
189
- });
190
-
191
- api.wrapRoot(App => {
192
- return props => {
193
- const runtimeContext = useContext(
194
- RuntimeContext,
195
- ) as RuntimeContextWithI18n;
196
- const i18nInstance = runtimeContext.i18nInstance || latestI18nInstance;
197
- const initialLang = useMemo(
198
- () =>
199
- i18nInstance?.language ||
200
- (localeDetection?.fallbackLanguage ?? 'en'),
201
- [i18nInstance?.language, localeDetection?.fallbackLanguage],
202
- );
203
- const [lang, setLang] = useState(initialLang);
204
- const [forceUpdate, setForceUpdate] = useState(0);
205
- const prevLangRef = useRef(lang);
206
- const runtimeContextRef = useRef(runtimeContext);
207
- runtimeContextRef.current = runtimeContext;
208
-
209
- useEffect(() => {
210
- if (i18nInstance?.language) {
211
- const translator = (i18nInstance as any).translator;
212
- if (translator) {
213
- translator.language = i18nInstance.language;
214
- }
215
- }
216
- }, [i18nInstance?.language]);
217
-
218
- useEffect(() => {
219
- prevLangRef.current = lang;
220
- }, [lang]);
221
-
222
- useSdkResourcesLoader(i18nInstance, setForceUpdate);
223
- useLanguageSync(
224
- i18nInstance,
225
- localePathRedirect,
226
- languages,
227
- runtimeContextRef,
228
- prevLangRef,
229
- setLang,
230
- );
231
- // Handle client-side redirect for static deployments
232
- // Note: This hook only executes in browser environment and skips SSR scenarios
233
- useClientSideRedirect(
234
- i18nInstance,
235
- localePathRedirect,
236
- languages,
237
- fallbackLanguage,
238
- ignoreRedirectRoutes,
239
- localisedUrls,
240
- );
241
-
242
- const contextValue = useMemo(
243
- () =>
244
- createContextValue(
245
- lang,
246
- i18nInstance,
247
- entryName,
248
- languages,
249
- localePathRedirect,
250
- ignoreRedirectRoutes,
251
- localisedUrls,
252
- setLang,
253
- ),
254
- [
255
- lang,
256
- i18nInstance,
257
- entryName,
258
- languages,
259
- localePathRedirect,
260
- ignoreRedirectRoutes,
261
- localisedUrls,
262
- forceUpdate,
263
- ],
264
- );
265
-
266
- const children = (props as React.PropsWithChildren).children;
267
- const appContent = (
268
- <>
269
- {Boolean(htmlLangAttr) && <Helmet htmlAttributes={{ lang }} />}
270
- <ModernI18nProvider value={contextValue}>
271
- {App ? <App {...props}>{children}</App> : children}
272
- </ModernI18nProvider>
273
- </>
274
- );
275
-
276
- if (!i18nInstance) {
277
- return appContent;
278
- }
279
-
280
- if (I18nextProvider) {
281
- const i18nextInstanceForProvider =
282
- getI18nextInstanceForProvider(i18nInstance);
283
- return (
284
- <I18nextProvider i18n={i18nextInstanceForProvider}>
285
- {appContent}
286
- </I18nextProvider>
287
- );
288
- }
289
-
290
- return appContent;
291
- };
292
- });
293
- },
294
- });
295
-
296
- export type {
297
- AllowedLinkTarget,
298
- CanonicalRoutePath,
299
- UltramodernCanonicalRoutes,
300
- } from './canonicalRoutes';
301
- export { useModernI18n } from './context';
302
- export { I18nLink, type I18nLinkProps } from './I18nLink';
303
- export {
304
- Link,
305
- type LinkActiveOptions,
306
- type LinkBaseProps,
307
- type LinkParams,
308
- type LinkProps,
309
- } from './Link';
310
- export {
311
- canonicalPath,
312
- type LocalizedPathsConfig,
313
- localizePath,
314
- type UseLocalizedLocationReturn,
315
- type UseLocalizedPathsReturn,
316
- useLocalizedLocation,
317
- useLocalizedPaths,
318
- } from './localizedPaths';
319
- export { buildLocalizedUrl, splitUrlTarget } from './utils';
320
8
  export default i18nPlugin;
@@ -0,0 +1,7 @@
1
+ import { createI18nPlugin } from './core';
2
+
3
+ export * from './core';
4
+
5
+ export const i18nPlugin = createI18nPlugin();
6
+
7
+ export default i18nPlugin;