Skip to content

Commit d155538

Browse files
authored
feat: i18n plugin support change language for runtime plugin && split i18n doc for components (#7949)
1 parent 00e5d60 commit d155538

File tree

20 files changed

+1117
-9
lines changed

20 files changed

+1117
-9
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
```bash
2+
pnpm add @modern-js/plugin-i18n i18next react-i18next
3+
```
4+
5+
:::info
6+
`i18next` and `react-i18next` are peer dependencies and need to be installed manually.
7+
8+
:::
9+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
`@modern-js/plugin-i18n` is Modern.js's internationalization plugin, built on top of [i18next](https://www.i18next.com/) and [react-i18next](https://react.i18next.com/), providing a complete internationalization solution for Modern.js applications.
2+

packages/document/docs/en/components/international/platform-support.mdx

Whitespace-only changes.

packages/document/docs/en/guides/advanced-features/international.mdx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ title: Internationalization
44

55
# Internationalization
66

7-
`@modern-js/plugin-i18n` is Modern.js's internationalization plugin, built on top of [i18next](https://www.i18next.com/) and [react-i18next](https://react.i18next.com/), providing a complete internationalization solution for Modern.js applications.
7+
import I18nPluginIntroduce from '@site-docs/components/international/introduce';
8+
9+
<I18nPluginIntroduce />
810

911
## Core Features
1012

@@ -24,6 +26,7 @@ title: Internationalization
2426

2527
## Documentation Navigation
2628

29+
- [Basic Concepts](./international/basic) - Core concepts and terminology
2730
- [Quick Start](./international/quick-start) - Detailed installation and configuration guide
2831
- [Configuration](./international/configuration) - CLI and runtime configuration details
2932
- [Locale Detection](./international/locale-detection) - Multiple language detection methods and priorities

packages/document/docs/en/guides/advanced-features/international/_meta.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
[
2+
"basic",
23
"quick-start",
34
"configuration",
45
"locale-detection",

packages/document/docs/en/guides/advanced-features/international/api.mdx

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,99 @@ function Navigation() {
173173
}
174174
```
175175

176+
## Runtime Plugin API
177+
178+
In the `onBeforeRender` hook of Runtime plugins, you can modify the language using the `context.changeLanguage` method. This is useful for scenarios where you need to dynamically set the language based on request information (such as user preferences, geographic location, etc.).
179+
180+
### context.changeLanguage
181+
182+
In the `onBeforeRender` hook, the i18n plugin adds a `changeLanguage` method to the `context` for use by other Runtime plugins.
183+
184+
**Type Definition:**
185+
186+
```ts
187+
interface TInternalRuntimeContext {
188+
i18nInstance?: I18nInstance;
189+
changeLanguage?: (lang: string) => Promise<void>;
190+
}
191+
```
192+
193+
### Usage Example
194+
195+
```ts
196+
import type { RuntimePlugin } from '@modern-js/runtime';
197+
198+
const myRuntimePlugin = (): RuntimePlugin => ({
199+
name: 'my-runtime-plugin',
200+
setup: api => {
201+
api.onBeforeRender(async context => {
202+
// Check if changeLanguage method exists (ensure i18n plugin is loaded)
203+
if (context.changeLanguage) {
204+
// Determine language based on some condition
205+
const userLang = getUserLanguageFromRequest(context);
206+
207+
// Change language
208+
await context.changeLanguage(userLang);
209+
}
210+
});
211+
},
212+
});
213+
214+
function getUserLanguageFromRequest(context: any): string {
215+
// Get user language from request headers, cookies, or other sources
216+
const acceptLanguage = context.ssrContext?.req?.headers['accept-language'];
217+
// Parse and return appropriate language code
218+
return parseAcceptLanguage(acceptLanguage) || 'zh';
219+
}
220+
221+
export default myRuntimePlugin;
222+
```
223+
224+
### Notes
225+
226+
1. **Execution Order**: Ensure the i18n plugin is registered before other plugins that use `changeLanguage`, so that `context.changeLanguage` is available.
227+
228+
2. **Async Operation**: `changeLanguage` is an async method and requires using `await` to wait for completion.
229+
230+
3. **Error Handling**: If an invalid language code is passed, an error will be thrown. It's recommended to add error handling:
231+
232+
```ts
233+
api.onBeforeRender(async context => {
234+
if (context.changeLanguage) {
235+
try {
236+
await context.changeLanguage('zh');
237+
} catch (error) {
238+
console.error('Failed to change language:', error);
239+
}
240+
}
241+
});
242+
```
243+
244+
4. **Language Validation**: It's recommended to verify that the language is in the supported language list before calling `changeLanguage`:
245+
246+
```ts
247+
api.onBeforeRender(async context => {
248+
if (context.changeLanguage && context.i18nInstance) {
249+
const supportedLngs = context.i18nInstance.options?.supportedLngs || [];
250+
const targetLang = 'zh';
251+
252+
if (supportedLngs.includes(targetLang)) {
253+
await context.changeLanguage(targetLang);
254+
}
255+
}
256+
});
257+
```
258+
259+
:::info
260+
The `changeLanguage` method will:
261+
262+
- Update the language of the i18n instance
263+
- Cache the language selection in the browser environment (Cookie/LocalStorage)
264+
- Trigger callbacks related to language switching
265+
266+
However, it will not automatically update the URL path. If you need to update the URL, you need to coordinate with the routing plugin or handle it manually.
267+
:::
268+
176269
## Integration with react-i18next
177270

178271
The plugin is fully compatible with `react-i18next` and can use the `useTranslation` Hook and other `react-i18next` features.

0 commit comments

Comments
 (0)