You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`@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.
Copy file name to clipboardExpand all lines: packages/document/docs/en/guides/advanced-features/international.mdx
+4-1Lines changed: 4 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,9 @@ title: Internationalization
4
4
5
5
# Internationalization
6
6
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.
Copy file name to clipboardExpand all lines: packages/document/docs/en/guides/advanced-features/international/api.mdx
+93Lines changed: 93 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -173,6 +173,99 @@ function Navigation() {
173
173
}
174
174
```
175
175
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.
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(asynccontext=> {
234
+
if (context.changeLanguage) {
235
+
try {
236
+
awaitcontext.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(asynccontext=> {
248
+
if (context.changeLanguage&&context.i18nInstance) {
- 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
+
176
269
## Integration with react-i18next
177
270
178
271
The plugin is fully compatible with `react-i18next` and can use the `useTranslation` Hook and other `react-i18next` features.
0 commit comments