@modern-js/main-doc 3.1.5 → 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 +4 -4
- 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,80 +4,57 @@ title: Configuration
|
|
|
4
4
|
|
|
5
5
|
# Configuration
|
|
6
6
|
|
|
7
|
-
Plugin configuration is
|
|
7
|
+
Plugin configuration is split between two files, each with a different responsibility:
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
| File | Purpose |
|
|
10
|
+
| --- | --- |
|
|
11
|
+
| `modern.config.ts` | Basic plugin settings, read during build/startup |
|
|
12
|
+
| `src/modern.runtime.ts` | i18next initialization options, read at runtime |
|
|
13
|
+
|
|
14
|
+
:::warning Function values can only be configured at runtime
|
|
15
|
+
`modern.config.ts` runs at build time and cannot serialize functions. SDK loaders, custom detection functions, and similar function values must be placed in `modern.runtime.ts`.
|
|
11
16
|
:::
|
|
12
17
|
|
|
13
|
-
##
|
|
18
|
+
## Configuration Ownership
|
|
14
19
|
|
|
15
|
-
|
|
20
|
+
| Configuration | Where to configure |
|
|
21
|
+
| --- | --- |
|
|
22
|
+
| `localeDetection` (language detection, path redirects, etc.) | `modern.config.ts` |
|
|
23
|
+
| `backend` (resource paths, SDK declaration, etc.) | `modern.config.ts` |
|
|
24
|
+
| `i18nInstance` | `modern.runtime.ts` |
|
|
25
|
+
| `initOptions` (`fallbackLng`, `ns`, and other i18next options) | `modern.runtime.ts` |
|
|
26
|
+
| The actual `backend.sdk` loader function | `modern.runtime.ts` |
|
|
16
27
|
|
|
17
|
-
|
|
28
|
+
## CLI Configuration
|
|
29
|
+
|
|
30
|
+
```ts title="modern.config.ts"
|
|
31
|
+
import { appTools, defineConfig } from '@modern-js/app-tools';
|
|
18
32
|
import { i18nPlugin } from '@modern-js/plugin-i18n';
|
|
19
33
|
|
|
20
34
|
export default defineConfig({
|
|
21
35
|
plugins: [
|
|
36
|
+
appTools(),
|
|
22
37
|
i18nPlugin({
|
|
23
|
-
localeDetection: {
|
|
24
|
-
|
|
25
|
-
},
|
|
26
|
-
backend: {
|
|
27
|
-
// Backend resource loading configuration
|
|
28
|
-
},
|
|
38
|
+
localeDetection: { ... },
|
|
39
|
+
backend: { ... },
|
|
29
40
|
}),
|
|
30
41
|
],
|
|
31
42
|
});
|
|
32
43
|
```
|
|
33
44
|
|
|
34
|
-
### localeDetection
|
|
45
|
+
### localeDetection Options
|
|
35
46
|
|
|
36
|
-
|
|
47
|
+
| Field | Type | Default | Description |
|
|
48
|
+
| --- | --- | --- | --- |
|
|
49
|
+
| `languages` | `string[]` | `[]` | Supported language list |
|
|
50
|
+
| `fallbackLanguage` | `string` | `''` | Fallback language when detection fails |
|
|
51
|
+
| `localePathRedirect` | `boolean` | `false` | Handles URL locale prefix recognition, redirects, and switching. See [Routing Integration](./routing.md#enable-locale-path-prefixes) |
|
|
52
|
+
| `i18nextDetector` | `boolean` | `false` | Enables the i18next detector (Cookie / Header, etc.) |
|
|
53
|
+
| `detection` | `LanguageDetectorOptions` | - | Detailed i18next detector configuration. See [Locale Detection](./locale-detection.md) |
|
|
54
|
+
| `ignoreRedirectRoutes` | `string[] \| Function` | - | Routes that skip redirects. See [Locale Detection](./locale-detection.md#ignoreredirectroutes) |
|
|
55
|
+
| `localeDetectionByEntry` | `Record<string, ...>` | - | Per-entry overrides for multiple entries |
|
|
37
56
|
|
|
38
|
-
|
|
39
|
-
If `localePathRedirect` is enabled, the `detection` configuration must be placed in CLI configuration (`modern.config.ts`), because the server-side plugin needs to read this configuration to get language information and perform path redirection.
|
|
40
|
-
:::
|
|
41
|
-
|
|
42
|
-
```ts
|
|
43
|
-
interface BaseLocaleDetectionOptions {
|
|
44
|
-
/** Whether to enable path redirection, adds language prefix to URL when enabled */
|
|
45
|
-
localePathRedirect?: boolean;
|
|
46
|
-
|
|
47
|
-
/** Whether to enable i18next language detector */
|
|
48
|
-
i18nextDetector?: boolean;
|
|
49
|
-
|
|
50
|
-
/** Supported language list */
|
|
51
|
-
languages?: string[];
|
|
52
|
-
|
|
53
|
-
/** Default fallback language */
|
|
54
|
-
fallbackLanguage?: string;
|
|
55
|
-
|
|
56
|
-
/** Custom detection configuration */
|
|
57
|
-
detection?: LanguageDetectorOptions;
|
|
58
|
-
|
|
59
|
-
/** Routes to ignore automatic redirection (array of path patterns or function)
|
|
60
|
-
*
|
|
61
|
-
* Can be a string array (path patterns) or a function to determine if redirection should be ignored.
|
|
62
|
-
* Supports exact match and prefix match (e.g., '/api' will match '/api' and '/api/users').
|
|
63
|
-
*
|
|
64
|
-
* @example
|
|
65
|
-
* // String array
|
|
66
|
-
* ignoreRedirectRoutes: ['/api', '/admin']
|
|
67
|
-
*
|
|
68
|
-
* // Function
|
|
69
|
-
* ignoreRedirectRoutes: (pathname) => pathname.startsWith('/api')
|
|
70
|
-
*/
|
|
71
|
-
ignoreRedirectRoutes?: string[] | ((pathname: string) => boolean);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
interface LocaleDetectionOptions extends BaseLocaleDetectionOptions {
|
|
75
|
-
/** Configure language detection by entry (multi-entry scenarios) */
|
|
76
|
-
localeDetectionByEntry?: Record<string, BaseLocaleDetectionOptions>;
|
|
77
|
-
}
|
|
78
|
-
```
|
|
79
|
-
|
|
80
|
-
**Example**:
|
|
57
|
+
**Example:**
|
|
81
58
|
|
|
82
59
|
```ts
|
|
83
60
|
i18nPlugin({
|
|
@@ -95,236 +72,79 @@ i18nPlugin({
|
|
|
95
72
|
});
|
|
96
73
|
```
|
|
97
74
|
|
|
98
|
-
### backend
|
|
99
|
-
|
|
100
|
-
`backend` is used to configure resource loading methods:
|
|
101
|
-
|
|
102
|
-
:::info
|
|
103
|
-
**Auto-detection**: The plugin automatically detects and enables backend in the following scenarios:
|
|
104
|
-
|
|
105
|
-
1. **If you configure `loadPath` or `addPath`**: The backend will be automatically enabled (`enabled: true`) without checking for locales directory, since you've already specified the resource path.
|
|
106
|
-
|
|
107
|
-
2. **If you don't configure backend**: The plugin will automatically detect if a `locales` directory exists in:
|
|
108
|
-
|
|
109
|
-
- Project root: `{projectRoot}/locales`
|
|
110
|
-
- Config public directory: `{projectRoot}/config/public/locales`
|
|
111
|
-
- Public directory configured via `server.publicDir`: `{projectRoot}/{publicDir}/locales`
|
|
112
|
-
|
|
113
|
-
If the directory exists and contains JSON files, the backend will be automatically enabled.
|
|
114
|
-
|
|
115
|
-
3. **If you explicitly set `enabled: false`**: No auto-detection will be performed, and the backend will remain disabled.
|
|
116
|
-
|
|
117
|
-
This automatic detection helps reduce unnecessary backend registration when locales directory doesn't exist, improving performance.
|
|
118
|
-
:::
|
|
75
|
+
### backend Options
|
|
119
76
|
|
|
120
77
|
```ts
|
|
121
|
-
interface
|
|
122
|
-
|
|
78
|
+
interface BackendOptions {
|
|
79
|
+
// Whether to enable backend resource loading.
|
|
80
|
+
// Automatically enabled when loadPath/addPath is configured, or when a locales directory exists.
|
|
123
81
|
enabled?: boolean;
|
|
124
82
|
|
|
125
|
-
|
|
83
|
+
// Decides which URL or local file to request for translations. Supports {{lng}} and {{ns}}.
|
|
84
|
+
// Default: '/locales/{{lng}}/{{ns}}.json'
|
|
126
85
|
loadPath?: string;
|
|
127
86
|
|
|
128
|
-
|
|
87
|
+
// Path for reporting missing translations (optional).
|
|
129
88
|
addPath?: string;
|
|
130
89
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
* - `'none'` (default, only when chained backend is not configured): If the first backend returns resources, stop and don't try the next backend
|
|
134
|
-
* - `'refresh'`: Try to refresh the cache by loading from the next backend and update the cache
|
|
135
|
-
* - `'refreshAndUpdateStore'` (default for chained backend): Try to refresh the cache by loading from the next backend,
|
|
136
|
-
* update the cache and also update the i18next resource store. This allows FS/HTTP resources to be displayed first,
|
|
137
|
-
* then SDK resources will update them asynchronously.
|
|
138
|
-
*
|
|
139
|
-
* @default 'refreshAndUpdateStore' when both loadPath and sdk are provided
|
|
140
|
-
*/
|
|
141
|
-
cacheHitMode?: 'none' | 'refresh' | 'refreshAndUpdateStore';
|
|
90
|
+
// Enables the custom backend SDK. The actual loader is provided in runtime config.
|
|
91
|
+
sdk?: boolean | string;
|
|
142
92
|
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
* When both `loadPath` (or FS backend) and `sdk` are provided, the plugin will automatically use `i18next-chained-backend`
|
|
149
|
-
* to chain multiple backends. The loading order will be:
|
|
150
|
-
* 1. HTTP/FS backend (primary) - loads from `loadPath` or file system first for quick initial display
|
|
151
|
-
* 2. SDK backend (update) - loads from the SDK function to update/refresh translations
|
|
152
|
-
*
|
|
153
|
-
* With `cacheHitMode: 'refreshAndUpdateStore'` (default), FS/HTTP resources will be displayed immediately,
|
|
154
|
-
* then SDK resources will be loaded asynchronously to update the translations.
|
|
155
|
-
*/
|
|
156
|
-
sdk?: I18nSdkLoader | boolean | string;
|
|
157
|
-
}
|
|
93
|
+
// Cache strategy for chained backend mode, effective when both loadPath and sdk are configured.
|
|
94
|
+
// 'none' - use local files and do not request the custom backend after a hit
|
|
95
|
+
// 'refresh' - continue requesting the custom backend and update cache, but not the page
|
|
96
|
+
// 'refreshAndUpdateStore' - continue requesting and update page text as well (default)
|
|
97
|
+
cacheHitMode?: 'none' | 'refresh' | 'refreshAndUpdateStore';
|
|
158
98
|
|
|
159
|
-
|
|
160
|
-
/** Configure backend by entry (multi-entry scenarios) */
|
|
99
|
+
// Per-entry overrides for multiple-entry scenarios.
|
|
161
100
|
backendOptionsByEntry?: Record<string, BaseBackendOptions>;
|
|
162
101
|
}
|
|
163
102
|
```
|
|
164
103
|
|
|
165
|
-
**
|
|
166
|
-
|
|
167
|
-
**1. HTTP/FS backend only**:
|
|
168
|
-
|
|
169
|
-
You can explicitly enable backend:
|
|
170
|
-
|
|
171
|
-
```ts
|
|
172
|
-
i18nPlugin({
|
|
173
|
-
backend: {
|
|
174
|
-
enabled: true,
|
|
175
|
-
loadPath: '/locales/{{lng}}/{{ns}}.json',
|
|
176
|
-
},
|
|
177
|
-
});
|
|
178
|
-
```
|
|
179
|
-
|
|
180
|
-
Or simply configure `loadPath` or `addPath`, and backend will be automatically enabled:
|
|
181
|
-
|
|
182
|
-
```ts
|
|
183
|
-
i18nPlugin({
|
|
184
|
-
backend: {
|
|
185
|
-
// enabled will be automatically set to true
|
|
186
|
-
loadPath: '/locales/{{lng}}/{{ns}}.json',
|
|
187
|
-
},
|
|
188
|
-
});
|
|
189
|
-
```
|
|
190
|
-
|
|
191
|
-
**Auto-detection without configuration**:
|
|
192
|
-
|
|
193
|
-
If you don't configure backend at all, the plugin will automatically detect locales directory:
|
|
194
|
-
|
|
195
|
-
```ts
|
|
196
|
-
i18nPlugin({
|
|
197
|
-
// No backend config - plugin will auto-detect locales directory
|
|
198
|
-
localeDetection: {
|
|
199
|
-
languages: ['zh', 'en'],
|
|
200
|
-
fallbackLanguage: 'en',
|
|
201
|
-
},
|
|
202
|
-
});
|
|
203
|
-
```
|
|
204
|
-
|
|
205
|
-
If `locales` directory exists with JSON files, backend will be automatically enabled with default `loadPath: '/locales/{{lng}}/{{ns}}.json'`.
|
|
206
|
-
|
|
207
|
-
**2. Chained backend (recommended)**: Use both HTTP/FS backend and SDK backend
|
|
208
|
-
|
|
209
|
-
When `backend.enabled = true` and `sdk` is configured, if `loadPath` is not explicitly configured, the default `loadPath` will be used automatically and chained backend will be enabled:
|
|
210
|
-
|
|
211
|
-
```ts
|
|
212
|
-
i18nPlugin({
|
|
213
|
-
backend: {
|
|
214
|
-
enabled: true,
|
|
215
|
-
// When loadPath is not configured, default '/locales/{{lng}}/{{ns}}.json' will be used
|
|
216
|
-
sdk: true, // SDK backend
|
|
217
|
-
// cacheHitMode: 'refreshAndUpdateStore', // Default value, can be omitted
|
|
218
|
-
},
|
|
219
|
-
});
|
|
220
|
-
```
|
|
221
|
-
|
|
222
|
-
You can also explicitly configure `loadPath`:
|
|
223
|
-
|
|
224
|
-
```ts
|
|
225
|
-
i18nPlugin({
|
|
226
|
-
backend: {
|
|
227
|
-
enabled: true,
|
|
228
|
-
loadPath: '/locales/{{lng}}/{{ns}}.json', // HTTP/FS backend
|
|
229
|
-
sdk: true, // SDK backend
|
|
230
|
-
},
|
|
231
|
-
});
|
|
232
|
-
```
|
|
233
|
-
|
|
234
|
-
Provide the SDK function in `modern.runtime.ts`:
|
|
235
|
-
|
|
236
|
-
```ts
|
|
237
|
-
export default defineRuntimeConfig({
|
|
238
|
-
i18n: {
|
|
239
|
-
initOptions: {
|
|
240
|
-
backend: {
|
|
241
|
-
sdk: async options => {
|
|
242
|
-
// SDK implementation
|
|
243
|
-
if (options.lng && options.ns) {
|
|
244
|
-
return await mySdk.getResource(options.lng, options.ns);
|
|
245
|
-
}
|
|
246
|
-
},
|
|
247
|
-
},
|
|
248
|
-
},
|
|
249
|
-
},
|
|
250
|
-
});
|
|
251
|
-
```
|
|
252
|
-
|
|
253
|
-
When using chained backend, the system will:
|
|
254
|
-
|
|
255
|
-
1. First load resources from `/locales/{{lng}}/{{ns}}.json` and display immediately (quick initial display of basic translations)
|
|
256
|
-
2. Then asynchronously load resources from SDK and update the i18next store (update/supplement translations)
|
|
257
|
-
|
|
258
|
-
This ensures users see page content quickly while the latest translation resources are loaded in the background.
|
|
259
|
-
|
|
260
|
-
**3. SDK backend only**:
|
|
104
|
+
**About the `sdk` field:** In `modern.config.ts`, set it to `true` to declare that the SDK backend is enabled. Provide the actual loader in `initOptions.backend.sdk` in `modern.runtime.ts`. See [Resource Loading -> Custom Backend](./resource-loading.md#custom-backend).
|
|
261
105
|
|
|
262
|
-
|
|
106
|
+
**backend auto-enable rules:**
|
|
263
107
|
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
sdk: true, // Use SDK backend only
|
|
270
|
-
},
|
|
271
|
-
});
|
|
272
|
-
```
|
|
108
|
+
| Situation | Result |
|
|
109
|
+
| --- | --- |
|
|
110
|
+
| `loadPath` or `addPath` is configured | Automatically enabled. No need to write `enabled: true` |
|
|
111
|
+
| No backend is configured, but a `locales` directory exists and contains JSON files | Automatically enabled with the default `loadPath` |
|
|
112
|
+
| `enabled: false` is set explicitly | Disabled, and auto-detection is skipped |
|
|
273
113
|
|
|
274
|
-
|
|
275
|
-
When using SDK backend only, you must provide the actual SDK function in `modern.runtime.ts`, otherwise it will fallback to HTTP/FS backend.
|
|
276
|
-
:::
|
|
114
|
+
Auto-detected directory locations, in priority order:
|
|
277
115
|
|
|
278
|
-
|
|
116
|
+
1. `{project root}/config/public/locales`
|
|
117
|
+
2. The `locales` subdirectory under the directory configured by `server.publicDir`
|
|
279
118
|
|
|
280
|
-
|
|
119
|
+
**Which backend should you choose?**
|
|
281
120
|
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
fallbackLanguage: 'en',
|
|
288
|
-
// Override configuration for specific entry
|
|
289
|
-
localeDetectionByEntry: {
|
|
290
|
-
admin: {
|
|
291
|
-
localePathRedirect: false, // admin entry does not use path redirection
|
|
292
|
-
},
|
|
293
|
-
},
|
|
294
|
-
},
|
|
295
|
-
backend: {
|
|
296
|
-
enabled: true,
|
|
297
|
-
// Override configuration for specific entry
|
|
298
|
-
backendOptionsByEntry: {
|
|
299
|
-
admin: {
|
|
300
|
-
loadPath: '/admin/locales/{{lng}}/{{ns}}.json',
|
|
301
|
-
},
|
|
302
|
-
},
|
|
303
|
-
},
|
|
304
|
-
});
|
|
305
|
-
```
|
|
121
|
+
| Scenario | Recommended approach |
|
|
122
|
+
| --- | --- |
|
|
123
|
+
| Translation files are local to the project | Static file backend (configure `loadPath`; CSR/SSR switch automatically) |
|
|
124
|
+
| Translations come from a remote API or translation platform | Custom backend (configure `sdk: true`) |
|
|
125
|
+
| Local fallback + remote real-time updates | Chained backend (configure both `loadPath` and `sdk`) |
|
|
306
126
|
|
|
307
|
-
|
|
127
|
+
See [Resource Loading](./resource-loading.md) for detailed usage.
|
|
308
128
|
|
|
309
|
-
|
|
129
|
+
## Runtime Configuration
|
|
310
130
|
|
|
311
|
-
```ts
|
|
131
|
+
```ts title="src/modern.runtime.ts"
|
|
312
132
|
import { defineRuntimeConfig } from '@modern-js/runtime';
|
|
313
133
|
import i18next from 'i18next';
|
|
314
134
|
|
|
315
|
-
// It's recommended to create a new i18next instance to avoid using the global default instance
|
|
316
135
|
const i18nInstance = i18next.createInstance();
|
|
317
136
|
|
|
318
137
|
export default defineRuntimeConfig({
|
|
319
138
|
i18n: {
|
|
320
|
-
//
|
|
321
|
-
i18nInstance: i18nInstance,
|
|
322
|
-
|
|
323
|
-
// i18next initialization options
|
|
139
|
+
i18nInstance, // Optional. An isolated instance is recommended.
|
|
324
140
|
initOptions: {
|
|
325
141
|
fallbackLng: 'en',
|
|
326
142
|
supportedLngs: ['zh', 'en'],
|
|
327
|
-
|
|
143
|
+
ns: ['translation'],
|
|
144
|
+
defaultNS: 'translation',
|
|
145
|
+
interpolation: {
|
|
146
|
+
escapeValue: false, // React already escapes text; disable this to avoid double escaping.
|
|
147
|
+
},
|
|
328
148
|
},
|
|
329
149
|
},
|
|
330
150
|
});
|
|
@@ -332,97 +152,70 @@ export default defineRuntimeConfig({
|
|
|
332
152
|
|
|
333
153
|
### i18nInstance Configuration
|
|
334
154
|
|
|
335
|
-
If you need to use a custom i18n instance,
|
|
155
|
+
If you need to use a custom i18n instance, provide it in the runtime configuration:
|
|
336
156
|
|
|
337
157
|
import CustomInstanceCode from '@site-docs/components/international/custom-instance-code';
|
|
338
158
|
|
|
339
159
|
<CustomInstanceCode />
|
|
340
160
|
|
|
341
|
-
### initOptions
|
|
161
|
+
### initOptions
|
|
342
162
|
|
|
343
|
-
import
|
|
163
|
+
import Desc from '@site-docs/components/international/init-options-desc';
|
|
344
164
|
|
|
345
|
-
<
|
|
165
|
+
<Desc />
|
|
346
166
|
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
167
|
+
| Field | Description |
|
|
168
|
+
| --- | --- |
|
|
169
|
+
| `fallbackLng` | Which language resources i18next falls back to when a translation key is missing. Supports strings, arrays, and maps (fallback chains) |
|
|
170
|
+
| `supportedLngs` | Language list supported by i18next |
|
|
171
|
+
| `ns` | Namespace list. Default is `['translation']` |
|
|
172
|
+
| `defaultNS` | Default namespace. Default is `'translation'` |
|
|
173
|
+
| `lng` | Forces the initial language. Usually not needed because the detector decides it |
|
|
350
174
|
|
|
351
|
-
|
|
352
|
-
export default defineRuntimeConfig({
|
|
353
|
-
i18n: {
|
|
354
|
-
initOptions: {
|
|
355
|
-
// Language related
|
|
356
|
-
lng: 'en',
|
|
357
|
-
fallbackLng: 'en',
|
|
358
|
-
supportedLngs: ['zh', 'en'],
|
|
359
|
-
|
|
360
|
-
// Namespace related
|
|
361
|
-
ns: ['translation', 'common'],
|
|
362
|
-
defaultNS: 'translation',
|
|
363
|
-
|
|
364
|
-
// React related
|
|
365
|
-
react: {
|
|
366
|
-
useSuspense: false,
|
|
367
|
-
},
|
|
175
|
+
`fallbackLng` supports fallback chains, which are useful for regional language degradation:
|
|
368
176
|
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
177
|
+
```ts
|
|
178
|
+
initOptions: {
|
|
179
|
+
lng: 'zh-CN',
|
|
180
|
+
fallbackLng: {
|
|
181
|
+
'zh-CN': ['zh', 'en'], // If zh-CN is missing, try zh first, then en.
|
|
182
|
+
default: ['en'],
|
|
374
183
|
},
|
|
375
|
-
}
|
|
184
|
+
},
|
|
376
185
|
```
|
|
377
186
|
|
|
378
|
-
###
|
|
379
|
-
|
|
380
|
-
If using SDK backend, you need to provide the actual SDK function in runtime configuration:
|
|
381
|
-
|
|
382
|
-
:::info
|
|
383
|
-
Function-type configurations can only be set in runtime configuration. In CLI configuration, `sdk` can only be set to `true` or a string identifier to enable SDK mode. The actual function implementation must be provided in `modern.runtime.ts`.
|
|
384
|
-
:::
|
|
187
|
+
### Custom Backend Function Configuration
|
|
385
188
|
|
|
386
|
-
|
|
189
|
+
The actual loader function for a custom backend must be provided in runtime configuration:
|
|
387
190
|
|
|
388
191
|
```ts
|
|
192
|
+
// modern.config.ts: only declare that it is enabled.
|
|
389
193
|
i18nPlugin({
|
|
390
|
-
backend: {
|
|
391
|
-
enabled: true,
|
|
392
|
-
sdk: true, // Enable SDK mode
|
|
393
|
-
},
|
|
194
|
+
backend: { sdk: true },
|
|
394
195
|
});
|
|
395
|
-
```
|
|
396
196
|
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
```ts
|
|
400
|
-
import { defineRuntimeConfig } from '@modern-js/runtime';
|
|
197
|
+
// modern.runtime.ts: provide the actual function.
|
|
401
198
|
import type { I18nSdkLoader } from '@modern-js/plugin-i18n/runtime';
|
|
402
199
|
|
|
403
|
-
const
|
|
404
|
-
if (options.all) {
|
|
405
|
-
// Load all resources
|
|
406
|
-
return await fetchAllResources();
|
|
407
|
-
}
|
|
408
|
-
|
|
200
|
+
const myLoader: I18nSdkLoader = async options => {
|
|
409
201
|
if (options.lng && options.ns) {
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
return response.json();
|
|
202
|
+
const res = await fetch(`/api/i18n/${options.lng}/${options.ns}`);
|
|
203
|
+
return res.json();
|
|
413
204
|
}
|
|
414
|
-
|
|
415
|
-
// Handle other cases
|
|
416
205
|
return {};
|
|
417
206
|
};
|
|
418
207
|
|
|
419
208
|
export default defineRuntimeConfig({
|
|
420
209
|
i18n: {
|
|
421
210
|
initOptions: {
|
|
422
|
-
backend: {
|
|
423
|
-
sdk: mySdkLoader,
|
|
424
|
-
},
|
|
211
|
+
backend: { sdk: myLoader },
|
|
425
212
|
},
|
|
426
213
|
},
|
|
427
214
|
});
|
|
428
215
|
```
|
|
216
|
+
|
|
217
|
+
For complete usage, see [Resource Loading -> Custom Backend](./resource-loading.md#custom-backend).
|
|
218
|
+
|
|
219
|
+
## Multiple Entries
|
|
220
|
+
|
|
221
|
+
Multiple-entry projects can override configuration per entry. See [Advanced Usage -> Multiple Entries](./advanced.md#multiple-entries).
|