@modern-js/main-doc 3.1.4 → 3.2.0
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/docs/en/components/international/init-options-desc.mdx +1 -1
- package/docs/en/components/international/install-command.mdx +4 -17
- package/docs/en/components/international/instance-code.mdx +4 -14
- package/docs/en/components/international/introduce.mdx +4 -1
- package/docs/en/configure/app/source/enable-async-pre-entry.mdx +30 -0
- package/docs/en/configure/app/tools/dev-server.mdx +0 -4
- package/docs/en/guides/advanced-features/international/_meta.json +0 -1
- package/docs/en/guides/advanced-features/international/advanced.mdx +48 -109
- package/docs/en/guides/advanced-features/international/api.mdx +125 -290
- package/docs/en/guides/advanced-features/international/best-practices.mdx +203 -48
- package/docs/en/guides/advanced-features/international/configuration.mdx +108 -315
- package/docs/en/guides/advanced-features/international/locale-detection.mdx +62 -208
- package/docs/en/guides/advanced-features/international/quick-start.mdx +41 -55
- package/docs/en/guides/advanced-features/international/resource-loading.mdx +63 -322
- package/docs/en/guides/advanced-features/international/routing.mdx +60 -138
- package/docs/en/guides/advanced-features/international.mdx +19 -27
- package/docs/en/guides/basic-features/alias.mdx +1 -1
- package/docs/en/guides/basic-features/html.mdx +2 -2
- package/docs/en/guides/basic-features/static-assets.mdx +1 -2
- package/docs/en/guides/concept/entries.mdx +2 -2
- package/docs/zh/components/international/init-options-desc.mdx +1 -1
- package/docs/zh/components/international/install-command.mdx +4 -16
- package/docs/zh/components/international/instance-code.mdx +4 -14
- package/docs/zh/components/international/introduce.mdx +5 -2
- package/docs/zh/configure/app/source/enable-async-pre-entry.mdx +77 -0
- package/docs/zh/configure/app/tools/dev-server.mdx +0 -4
- package/docs/zh/guides/advanced-features/bff/function.mdx +2 -2
- package/docs/zh/guides/advanced-features/international/_meta.json +0 -1
- package/docs/zh/guides/advanced-features/international/advanced.mdx +48 -109
- package/docs/zh/guides/advanced-features/international/api.mdx +126 -292
- package/docs/zh/guides/advanced-features/international/best-practices.mdx +204 -49
- package/docs/zh/guides/advanced-features/international/configuration.mdx +105 -318
- package/docs/zh/guides/advanced-features/international/locale-detection.mdx +62 -236
- package/docs/zh/guides/advanced-features/international/quick-start.mdx +40 -54
- package/docs/zh/guides/advanced-features/international/resource-loading.mdx +62 -324
- package/docs/zh/guides/advanced-features/international/routing.mdx +58 -136
- package/docs/zh/guides/advanced-features/international.mdx +19 -26
- package/docs/zh/guides/basic-features/alias.mdx +1 -1
- package/docs/zh/guides/basic-features/html.mdx +2 -2
- package/docs/zh/guides/basic-features/static-assets.mdx +1 -2
- package/docs/zh/guides/concept/entries.mdx +2 -2
- package/package.json +5 -4
- package/rspress.config.ts +45 -26
- package/docs/en/components/rspackPrecautions.mdx +0 -6
- package/docs/en/guides/advanced-features/international/basic.mdx +0 -417
- package/docs/zh/components/rspackPrecautions.mdx +0 -6
- package/docs/zh/guides/advanced-features/international/basic.mdx +0 -416
|
@@ -4,21 +4,29 @@ title: Locale Detection
|
|
|
4
4
|
|
|
5
5
|
# Locale Detection
|
|
6
6
|
|
|
7
|
-
The plugin
|
|
7
|
+
Locale detection means automatically inferring which language to use when the user has not manually selected one. The plugin can detect language from URL path, Cookie, request headers, browser settings, and other sources, and these methods can be combined.
|
|
8
8
|
|
|
9
|
-
## Detection
|
|
9
|
+
## Detection Priority
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
No matter which detection methods are enabled, the priority is fixed as follows, from high to low:
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
1. **SSR data**: Reads the language detected during server-side rendering from `window._SSR_DATA`, ensuring client and server consistency and avoiding language flicker.
|
|
14
|
+
2. **Path detection**: Reads the locale prefix from the URL path. Requires `localePathRedirect`.
|
|
15
|
+
3. **i18next detector**: Detects in the order configured by `detection.order` (Cookie, LocalStorage, query parameters, etc.).
|
|
16
|
+
4. **initOptions.lng**: A language forced by runtime configuration.
|
|
17
|
+
5. **fallbackLanguage**: The fallback language used when all detection methods fail.
|
|
14
18
|
|
|
15
|
-
|
|
19
|
+
## Detection Methods
|
|
16
20
|
|
|
17
|
-
|
|
18
|
-
- `/en/about` → Detected language: `en`
|
|
19
|
-
- `/about` → If there's no language prefix, will redirect to the default language path
|
|
21
|
+
### URL Path Detection
|
|
20
22
|
|
|
21
|
-
|
|
23
|
+
After setting `localePathRedirect: true`, the plugin recognizes the language from the URL path prefix and automatically redirects paths without a prefix to the default language:
|
|
24
|
+
|
|
25
|
+
```
|
|
26
|
+
/about -> redirects to /en/about (when fallbackLanguage is en)
|
|
27
|
+
/zh/about -> recognizes zh, no redirect
|
|
28
|
+
/en/about -> recognizes en, no redirect
|
|
29
|
+
```
|
|
22
30
|
|
|
23
31
|
```ts
|
|
24
32
|
i18nPlugin({
|
|
@@ -30,61 +38,11 @@ i18nPlugin({
|
|
|
30
38
|
});
|
|
31
39
|
```
|
|
32
40
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
When using convention-based routing, you need to create a `[lang]` directory under the `routes/` directory to represent the language parameter:
|
|
36
|
-
|
|
37
|
-
```bash
|
|
38
|
-
routes/
|
|
39
|
-
├── [lang]/
|
|
40
|
-
│ ├── layout.tsx # Layout component
|
|
41
|
-
│ ├── page.tsx # Home page
|
|
42
|
-
│ └── about/
|
|
43
|
-
│ └── page.tsx # About page
|
|
44
|
-
```
|
|
45
|
-
|
|
46
|
-
**routes/[lang]/layout.tsx**:
|
|
47
|
-
|
|
48
|
-
```tsx
|
|
49
|
-
import { Outlet } from '@modern-js/runtime/router';
|
|
50
|
-
|
|
51
|
-
export default function Layout() {
|
|
52
|
-
return <Outlet />;
|
|
53
|
-
}
|
|
54
|
-
```
|
|
55
|
-
|
|
56
|
-
**routes/[lang]/page.tsx**:
|
|
57
|
-
|
|
58
|
-
```tsx
|
|
59
|
-
export default function Home() {
|
|
60
|
-
return <div>Home</div>;
|
|
61
|
-
}
|
|
62
|
-
```
|
|
63
|
-
|
|
64
|
-
**routes/[lang]/about/page.tsx**:
|
|
65
|
-
|
|
66
|
-
```tsx
|
|
67
|
-
export default function About() {
|
|
68
|
-
return <div>About</div>;
|
|
69
|
-
}
|
|
70
|
-
```
|
|
71
|
-
|
|
72
|
-
:::info
|
|
73
|
-
If using custom routing (`modern.routes.ts`), you need to add `:lang` dynamic parameter in the route configuration. Convention-based routing will automatically generate corresponding routes based on the file structure.
|
|
74
|
-
:::
|
|
75
|
-
|
|
76
|
-
### 2. i18next Language Detector
|
|
41
|
+
After enabling path detection, you also need to configure a `[lang]` dynamic parameter in your routes. See [Routing Integration](./routing.md).
|
|
77
42
|
|
|
78
|
-
|
|
43
|
+
### i18next Detector
|
|
79
44
|
|
|
80
|
-
|
|
81
|
-
- **LocalStorage**: Read from browser LocalStorage
|
|
82
|
-
- **Query Parameters**: Read from URL query parameters (e.g., `?lng=en`)
|
|
83
|
-
- **Request Headers**: Read from HTTP request headers (e.g., `Accept-Language`)
|
|
84
|
-
- **HTML Tag**: Read from the `lang` attribute of HTML tags
|
|
85
|
-
- **Subdomain**: Read from subdomain (e.g., `en.example.com`)
|
|
86
|
-
|
|
87
|
-
**Configuration**:
|
|
45
|
+
Set `i18nextDetector: true` to enable language detection from Cookie, LocalStorage, query parameters, request headers, and other sources:
|
|
88
46
|
|
|
89
47
|
```ts
|
|
90
48
|
i18nPlugin({
|
|
@@ -101,176 +59,72 @@ i18nPlugin({
|
|
|
101
59
|
});
|
|
102
60
|
```
|
|
103
61
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
You can customize detection behavior through the `detection` option:
|
|
62
|
+
Both methods can be enabled at the same time:
|
|
107
63
|
|
|
108
64
|
```ts
|
|
109
65
|
i18nPlugin({
|
|
110
66
|
localeDetection: {
|
|
111
|
-
|
|
67
|
+
localePathRedirect: true, // Path prefix
|
|
68
|
+
i18nextDetector: true, // Cookie / Header, etc.
|
|
69
|
+
languages: ['zh', 'en'],
|
|
70
|
+
fallbackLanguage: 'en',
|
|
112
71
|
detection: {
|
|
113
|
-
|
|
114
|
-
order: ['path', 'cookie', 'querystring', 'header'],
|
|
115
|
-
|
|
116
|
-
// Cookie related
|
|
117
|
-
lookupCookie: 'i18next',
|
|
118
|
-
cookieExpirationDate: new Date(Date.now() + 365 * 24 * 60 * 60 * 1000), // Expires in 1 year
|
|
119
|
-
cookieDomain: '.example.com',
|
|
120
|
-
|
|
121
|
-
// Query parameter related
|
|
122
|
-
lookupQuerystring: 'lng',
|
|
123
|
-
|
|
124
|
-
// Request header related
|
|
125
|
-
lookupHeader: 'accept-language',
|
|
126
|
-
|
|
127
|
-
// Cache configuration
|
|
128
|
-
caches: ['cookie', 'localStorage'],
|
|
72
|
+
order: ['cookie', 'header'],
|
|
129
73
|
},
|
|
130
74
|
},
|
|
131
75
|
});
|
|
132
76
|
```
|
|
133
77
|
|
|
134
|
-
## Detection
|
|
135
|
-
|
|
136
|
-
The plugin's language detection follows the following priority order (from highest to lowest):
|
|
137
|
-
|
|
138
|
-
1. **SSR Data** (highest priority): Read language from `window._SSR_DATA` set during server-side rendering, applicable to both SSR and CSR projects
|
|
139
|
-
2. **Path Detection**: If `localePathRedirect` is `true`, detect language prefix from URL path
|
|
140
|
-
3. **i18next Detector**: Execute detection according to the order configured in `detection.order` (Cookie, LocalStorage, query parameters, request headers, etc.)
|
|
141
|
-
4. **User Configured Language**: Use the language configured in `initOptions.lng`
|
|
142
|
-
5. **Fallback Language**: Use `fallbackLanguage` as the final fallback
|
|
143
|
-
|
|
144
|
-
:::info
|
|
145
|
-
SSR data detection has the highest priority to ensure the client uses the language detected during server-side rendering, avoiding language flickering issues caused by client-side re-detection.
|
|
146
|
-
:::
|
|
147
|
-
|
|
148
|
-
**Example**:
|
|
149
|
-
|
|
150
|
-
```ts
|
|
151
|
-
// Configured detection order (only affects priority within i18next detector)
|
|
152
|
-
detection: {
|
|
153
|
-
order: ['path', 'cookie', 'querystring', 'header'],
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
// Actual detection flow:
|
|
157
|
-
// 1. First check SSR data (window._SSR_DATA)
|
|
158
|
-
// 2. Then check URL path (if localePathRedirect is enabled)
|
|
159
|
-
// 3. Then check i18next detector according to order:
|
|
160
|
-
// - Cookie
|
|
161
|
-
// - Query parameters
|
|
162
|
-
// - Request headers
|
|
163
|
-
// 4. Then use initOptions.lng (if configured)
|
|
164
|
-
// 5. Finally use fallbackLanguage
|
|
165
|
-
```
|
|
166
|
-
|
|
167
|
-
## Detection Options
|
|
168
|
-
|
|
169
|
-
### order (Detection Order)
|
|
170
|
-
|
|
171
|
-
Specifies the order of language detection, optional values:
|
|
172
|
-
|
|
173
|
-
- `path`: Detect from URL path
|
|
174
|
-
- `querystring`: Detect from query parameters
|
|
175
|
-
- `cookie`: Detect from cookies
|
|
176
|
-
- `localStorage`: Detect from LocalStorage
|
|
177
|
-
- `sessionStorage`: Detect from SessionStorage
|
|
178
|
-
- `navigator`: Detect from browser language settings
|
|
179
|
-
- `htmlTag`: Detect from HTML tags
|
|
180
|
-
- `header`: Detect from HTTP request headers
|
|
181
|
-
- `subdomain`: Detect from subdomain
|
|
182
|
-
|
|
183
|
-
:::warning
|
|
184
|
-
`path` detection requires `localePathRedirect` to be `true`. `localStorage`, `sessionStorage`, `navigator`, and `htmlTag` are only available in browser environments.
|
|
185
|
-
:::
|
|
186
|
-
|
|
187
|
-
### caches (Cache Method)
|
|
188
|
-
|
|
189
|
-
Specifies where the detected language should be cached, optional values:
|
|
190
|
-
|
|
191
|
-
- `false`: No caching
|
|
192
|
-
- `['cookie']`: Cache to Cookie
|
|
193
|
-
- `['localStorage']`: Cache to LocalStorage (browser only)
|
|
194
|
-
- `['cookie', 'localStorage']`: Cache to both Cookie and LocalStorage
|
|
195
|
-
|
|
196
|
-
### lookupQuerystring, lookupCookie, lookupLocalStorage, lookupSession, lookupHeader
|
|
197
|
-
|
|
198
|
-
Specifies the key name used when reading language from query parameters, cookies, LocalStorage, SessionStorage, or request headers:
|
|
199
|
-
|
|
200
|
-
- `lookupQuerystring`: Default `'lng'`, e.g., `?lng=en`
|
|
201
|
-
- `lookupCookie`: Default `'i18next'`
|
|
202
|
-
- `lookupLocalStorage`: Default `'i18nextLng'` (browser only)
|
|
203
|
-
- `lookupSession`: SessionStorage key name (browser only)
|
|
204
|
-
- `lookupHeader`: Default `'accept-language'`
|
|
205
|
-
|
|
206
|
-
### lookupFromPathIndex
|
|
78
|
+
## Detection Options (`detection`)
|
|
207
79
|
|
|
208
|
-
|
|
80
|
+
| Option | Type | Default | Description |
|
|
81
|
+
| --- | --- | --- | --- |
|
|
82
|
+
| `order` | `string[]` | See below | Detection order inside the i18next detector |
|
|
83
|
+
| `lookupQuerystring` | `string` | `'lng'` | Query parameter key, such as `?lng=en` |
|
|
84
|
+
| `lookupCookie` | `string` | `'i18next'` | Cookie key |
|
|
85
|
+
| `lookupLocalStorage` | `string` | `'i18nextLng'` | LocalStorage key (browser only) |
|
|
86
|
+
| `lookupSession` | `string` | - | SessionStorage key (browser only) |
|
|
87
|
+
| `lookupHeader` | `string` | `'accept-language'` | HTTP request header key |
|
|
88
|
+
| `lookupFromPathIndex` | `number` | `0` | Which URL path segment to start recognizing language from |
|
|
89
|
+
| `caches` | `false \| string[]` | - | Where to cache the detected language, such as `['cookie']` |
|
|
90
|
+
| `cookieMinutes` | `number` | `525600` (1 year) | Cookie expiration time in minutes |
|
|
91
|
+
| `cookieExpirationDate` | `Date` | - | Cookie expiration date. Takes precedence over `cookieMinutes` |
|
|
92
|
+
| `cookieDomain` | `string` | - | Cookie domain |
|
|
209
93
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
**Example**:
|
|
94
|
+
**Default detection order when `order` is not configured:**
|
|
213
95
|
|
|
214
96
|
```ts
|
|
215
|
-
|
|
216
|
-
// If lookupFromPathIndex = 2, detection starts from the third path segment ('en')
|
|
217
|
-
detection: {
|
|
218
|
-
order: ['path'],
|
|
219
|
-
lookupFromPathIndex: 2,
|
|
220
|
-
}
|
|
97
|
+
['querystring', 'cookie', 'localStorage', 'header', 'navigator', 'htmlTag', 'path', 'subdomain']
|
|
221
98
|
```
|
|
222
99
|
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
Controls Cookie expiration time:
|
|
226
|
-
|
|
227
|
-
- `cookieMinutes`: Cookie expiration time (minutes), default `525600` (1 year)
|
|
228
|
-
- `cookieExpirationDate`: Cookie expiration date (Date object), takes precedence over `cookieMinutes`
|
|
100
|
+
**Available `order` values:**
|
|
229
101
|
|
|
230
|
-
|
|
102
|
+
| Value | Source | Limitation |
|
|
103
|
+
| --- | --- | --- |
|
|
104
|
+
| `querystring` | URL query parameter (`?lng=en`) | - |
|
|
105
|
+
| `cookie` | Cookie | - |
|
|
106
|
+
| `localStorage` | LocalStorage | Browser only |
|
|
107
|
+
| `sessionStorage` | SessionStorage | Browser only |
|
|
108
|
+
| `navigator` | Browser language settings | Browser only |
|
|
109
|
+
| `htmlTag` | `<html lang="...">` | Browser only |
|
|
110
|
+
| `header` | `Accept-Language` request header | - |
|
|
111
|
+
| `path` | URL path (requires `localePathRedirect`) | - |
|
|
112
|
+
| `subdomain` | Subdomain (`en.example.com`) | - |
|
|
231
113
|
|
|
232
|
-
|
|
233
|
-
detection: {
|
|
234
|
-
cookieMinutes: 60 * 24 * 7, // Expires in 7 days
|
|
235
|
-
// or
|
|
236
|
-
cookieExpirationDate: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000), // Expires in 7 days
|
|
237
|
-
}
|
|
238
|
-
```
|
|
114
|
+
## ignoreRedirectRoutes
|
|
239
115
|
|
|
240
|
-
|
|
116
|
+
Specify which paths should skip locale path redirects. This is useful for API routes, static assets, and other paths that do not need a locale prefix.
|
|
241
117
|
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
**Configuration**:
|
|
118
|
+
**Syntax 1: string array** (supports exact match and prefix match)
|
|
245
119
|
|
|
246
120
|
```ts
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
localePathRedirect: true,
|
|
250
|
-
languages: ['zh', 'en'],
|
|
251
|
-
fallbackLanguage: 'en',
|
|
252
|
-
// String array: supports exact match and prefix match
|
|
253
|
-
ignoreRedirectRoutes: ['/api', '/admin', '/static'],
|
|
254
|
-
// Or use function for more flexible judgment
|
|
255
|
-
ignoreRedirectRoutes: pathname => {
|
|
256
|
-
return pathname.startsWith('/api') || pathname.startsWith('/admin');
|
|
257
|
-
},
|
|
258
|
-
},
|
|
259
|
-
});
|
|
121
|
+
ignoreRedirectRoutes: ['/api', '/admin', '/static']
|
|
122
|
+
// '/api' matches both /api and /api/users.
|
|
260
123
|
```
|
|
261
124
|
|
|
262
|
-
**
|
|
263
|
-
|
|
264
|
-
- String array: Supports exact match (`'/api'`) and prefix match (`'/api'` will match `/api` and `/api/users`)
|
|
265
|
-
- Function: Receives pathname (with language prefix removed), returns `true` to indicate ignoring redirection
|
|
266
|
-
|
|
267
|
-
**Example**:
|
|
125
|
+
**Syntax 2: function** (more flexible custom logic)
|
|
268
126
|
|
|
269
127
|
```ts
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
// Use function to ignore all paths starting with /api
|
|
274
|
-
ignoreRedirectRoutes: pathname => pathname.startsWith('/api');
|
|
128
|
+
ignoreRedirectRoutes: pathname =>
|
|
129
|
+
pathname.startsWith('/api') || pathname.startsWith('/admin'),
|
|
275
130
|
```
|
|
276
|
-
|
|
@@ -4,11 +4,9 @@ title: Quick Start
|
|
|
4
4
|
|
|
5
5
|
# Quick Start
|
|
6
6
|
|
|
7
|
-
This guide
|
|
7
|
+
This guide helps you quickly integrate internationalization into a Modern.js project.
|
|
8
8
|
|
|
9
|
-
## Install Plugin
|
|
10
|
-
|
|
11
|
-
First, install the necessary dependencies:
|
|
9
|
+
## Install the Plugin
|
|
12
10
|
|
|
13
11
|
import InstallCommand from '@site-docs/components/international/install-command';
|
|
14
12
|
|
|
@@ -16,111 +14,99 @@ import InstallCommand from '@site-docs/components/international/install-command'
|
|
|
16
14
|
|
|
17
15
|
## Basic Configuration
|
|
18
16
|
|
|
19
|
-
###
|
|
17
|
+
### Step 1: Register the Plugin
|
|
18
|
+
|
|
19
|
+
Register the plugin in `modern.config.ts`:
|
|
20
20
|
|
|
21
21
|
```ts
|
|
22
22
|
import { appTools, defineConfig } from '@modern-js/app-tools';
|
|
23
23
|
import { i18nPlugin } from '@modern-js/plugin-i18n';
|
|
24
24
|
|
|
25
25
|
export default defineConfig({
|
|
26
|
-
server: {
|
|
27
|
-
publicDir: './locales', // Required: specify the resource file directory
|
|
28
|
-
},
|
|
29
26
|
plugins: [
|
|
30
27
|
appTools(),
|
|
31
28
|
i18nPlugin({
|
|
32
29
|
localeDetection: {
|
|
33
|
-
languages: ['zh', 'en'],
|
|
34
|
-
fallbackLanguage: 'en',
|
|
35
|
-
},
|
|
36
|
-
backend: {
|
|
37
|
-
enabled: true, // Enable backend resource loading
|
|
38
|
-
// loadPath defaults to '/locales/{{lng}}/{{ns}}.json', usually no need to modify
|
|
39
|
-
// Note: You can also omit 'enabled' and just configure 'loadPath' or 'addPath',
|
|
40
|
-
// or omit backend config entirely to let the plugin auto-detect locales directory
|
|
30
|
+
languages: ['zh', 'en'],
|
|
31
|
+
fallbackLanguage: 'en',
|
|
41
32
|
},
|
|
42
33
|
}),
|
|
43
34
|
],
|
|
44
35
|
});
|
|
45
36
|
```
|
|
46
37
|
|
|
47
|
-
|
|
48
|
-
`server.publicDir` is a required configuration used to specify the actual location of resource files. Even when using the default `loadPath`, this configuration is still required.
|
|
38
|
+
### Step 2: Create Translation Files
|
|
49
39
|
|
|
40
|
+
:::note Resource loading
|
|
41
|
+
By default, the plugin looks for translation resources in the specified local directory. More resource loading methods are covered in later chapters.
|
|
50
42
|
:::
|
|
51
43
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
import InstanceCode from '@site-docs/components/international/instance-code';
|
|
55
|
-
|
|
56
|
-
<InstanceCode />
|
|
57
|
-
|
|
58
|
-
### Create Language Resource Files
|
|
59
|
-
|
|
60
|
-
Create a `locales` folder in the project root and organize resource files by language:
|
|
44
|
+
Create the `config/public/locales` directory:
|
|
61
45
|
|
|
62
46
|
```
|
|
63
|
-
locales/
|
|
47
|
+
config/public/locales/
|
|
64
48
|
├── en/
|
|
65
49
|
│ └── translation.json
|
|
66
50
|
└── zh/
|
|
67
51
|
└── translation.json
|
|
68
52
|
```
|
|
69
53
|
|
|
70
|
-
**locales/en/translation.json**:
|
|
71
|
-
|
|
72
54
|
```json
|
|
55
|
+
// locales/en/translation.json
|
|
73
56
|
{
|
|
74
|
-
"hello": "Hello",
|
|
75
|
-
"world": "World",
|
|
76
57
|
"welcome": "Welcome to Modern.js"
|
|
77
58
|
}
|
|
78
59
|
```
|
|
79
60
|
|
|
80
|
-
**locales/zh/translation.json**:
|
|
81
|
-
|
|
82
61
|
```json
|
|
62
|
+
// locales/zh/translation.json
|
|
83
63
|
{
|
|
84
|
-
"hello": "你好",
|
|
85
|
-
"world": "世界",
|
|
86
64
|
"welcome": "欢迎使用 Modern.js"
|
|
87
65
|
}
|
|
88
66
|
```
|
|
89
67
|
|
|
90
|
-
###
|
|
68
|
+
### Step 3: Configure Runtime Options
|
|
69
|
+
|
|
70
|
+
import InstanceCode from '@site-docs/components/international/instance-code';
|
|
71
|
+
|
|
72
|
+
<InstanceCode />
|
|
73
|
+
|
|
74
|
+
### Step 4: Use It in Components
|
|
75
|
+
|
|
76
|
+
Text returned by the `t()` function updates automatically after `changeLanguage` is called. No manual refresh is required.
|
|
91
77
|
|
|
92
78
|
```tsx
|
|
93
|
-
import { useModernI18n } from '@modern-js/plugin-i18n/runtime';
|
|
94
79
|
import { useTranslation } from 'react-i18next';
|
|
80
|
+
import { useModernI18n } from '@modern-js/plugin-i18n/runtime';
|
|
95
81
|
|
|
96
82
|
function App() {
|
|
97
|
-
const { language, changeLanguage, supportedLanguages } = useModernI18n();
|
|
98
83
|
const { t } = useTranslation();
|
|
84
|
+
const { language, changeLanguage, supportedLanguages } = useModernI18n();
|
|
99
85
|
|
|
100
86
|
return (
|
|
101
87
|
<div>
|
|
102
88
|
<h1>{t('welcome')}</h1>
|
|
103
89
|
<p>Current language: {language}</p>
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
))}
|
|
114
|
-
</div>
|
|
90
|
+
{supportedLanguages.map(lang => (
|
|
91
|
+
<button
|
|
92
|
+
key={lang}
|
|
93
|
+
onClick={() => changeLanguage(lang)}
|
|
94
|
+
disabled={lang === language}
|
|
95
|
+
>
|
|
96
|
+
{lang}
|
|
97
|
+
</button>
|
|
98
|
+
))}
|
|
115
99
|
</div>
|
|
116
100
|
);
|
|
117
101
|
}
|
|
118
|
-
|
|
119
|
-
export default App;
|
|
120
102
|
```
|
|
121
103
|
|
|
122
104
|
## Next Steps
|
|
123
105
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
-
|
|
106
|
+
Choose what to read next based on your needs:
|
|
107
|
+
|
|
108
|
+
- **Want a URL structure like `/en/about`** -> [Routing Integration](./routing.md)
|
|
109
|
+
- **Need automatic language switching based on Cookie / Header** -> [Locale Detection](./locale-detection.md)
|
|
110
|
+
- **Translation resources come from a remote service** -> [Resource Loading -> Custom Backend](./resource-loading.md#custom-backend)
|
|
111
|
+
- **SSR scenarios** -> [Advanced Usage](./advanced.md)
|
|
112
|
+
- **View all configuration options** -> [Configuration](./configuration.md)
|