@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.
Files changed (47) hide show
  1. package/docs/en/components/international/init-options-desc.mdx +1 -1
  2. package/docs/en/components/international/install-command.mdx +4 -17
  3. package/docs/en/components/international/instance-code.mdx +4 -14
  4. package/docs/en/components/international/introduce.mdx +4 -1
  5. package/docs/en/configure/app/source/enable-async-pre-entry.mdx +30 -0
  6. package/docs/en/configure/app/tools/dev-server.mdx +0 -4
  7. package/docs/en/guides/advanced-features/international/_meta.json +0 -1
  8. package/docs/en/guides/advanced-features/international/advanced.mdx +48 -109
  9. package/docs/en/guides/advanced-features/international/api.mdx +125 -290
  10. package/docs/en/guides/advanced-features/international/best-practices.mdx +203 -48
  11. package/docs/en/guides/advanced-features/international/configuration.mdx +108 -315
  12. package/docs/en/guides/advanced-features/international/locale-detection.mdx +62 -208
  13. package/docs/en/guides/advanced-features/international/quick-start.mdx +41 -55
  14. package/docs/en/guides/advanced-features/international/resource-loading.mdx +63 -322
  15. package/docs/en/guides/advanced-features/international/routing.mdx +60 -138
  16. package/docs/en/guides/advanced-features/international.mdx +19 -27
  17. package/docs/en/guides/basic-features/alias.mdx +1 -1
  18. package/docs/en/guides/basic-features/html.mdx +2 -2
  19. package/docs/en/guides/basic-features/static-assets.mdx +1 -2
  20. package/docs/en/guides/concept/entries.mdx +2 -2
  21. package/docs/zh/components/international/init-options-desc.mdx +1 -1
  22. package/docs/zh/components/international/install-command.mdx +4 -16
  23. package/docs/zh/components/international/instance-code.mdx +4 -14
  24. package/docs/zh/components/international/introduce.mdx +5 -2
  25. package/docs/zh/configure/app/source/enable-async-pre-entry.mdx +77 -0
  26. package/docs/zh/configure/app/tools/dev-server.mdx +0 -4
  27. package/docs/zh/guides/advanced-features/bff/function.mdx +2 -2
  28. package/docs/zh/guides/advanced-features/international/_meta.json +0 -1
  29. package/docs/zh/guides/advanced-features/international/advanced.mdx +48 -109
  30. package/docs/zh/guides/advanced-features/international/api.mdx +126 -292
  31. package/docs/zh/guides/advanced-features/international/best-practices.mdx +204 -49
  32. package/docs/zh/guides/advanced-features/international/configuration.mdx +105 -318
  33. package/docs/zh/guides/advanced-features/international/locale-detection.mdx +62 -236
  34. package/docs/zh/guides/advanced-features/international/quick-start.mdx +40 -54
  35. package/docs/zh/guides/advanced-features/international/resource-loading.mdx +62 -324
  36. package/docs/zh/guides/advanced-features/international/routing.mdx +58 -136
  37. package/docs/zh/guides/advanced-features/international.mdx +19 -26
  38. package/docs/zh/guides/basic-features/alias.mdx +1 -1
  39. package/docs/zh/guides/basic-features/html.mdx +2 -2
  40. package/docs/zh/guides/basic-features/static-assets.mdx +1 -2
  41. package/docs/zh/guides/concept/entries.mdx +2 -2
  42. package/package.json +5 -4
  43. package/rspress.config.ts +45 -26
  44. package/docs/en/components/rspackPrecautions.mdx +0 -6
  45. package/docs/en/guides/advanced-features/international/basic.mdx +0 -417
  46. package/docs/zh/components/rspackPrecautions.mdx +0 -6
  47. package/docs/zh/guides/advanced-features/international/basic.mdx +0 -416
@@ -1,416 +0,0 @@
1
- ---
2
- title: 基础概念
3
- ---
4
-
5
- # 基础概念
6
-
7
- 在项目集成国际化能力之前,需要了解国际化相关概念。理解核心概念可以帮助你快速建立稳定的翻译体系,帮助我们更好地解决使用过程中的各种问题。
8
-
9
- ## 核心概念
10
-
11
- ### i18n
12
-
13
- `i18n` 是 Internationalization 的缩写,指让应用在不同语言、地区和文化中都能良好运行,需要在设计阶段就考虑多语言资源、数字/日期/货币以及文化差异等因素。
14
-
15
- ### i18next
16
-
17
- i18next 是一个通用的国际化框架,提供语言检测、资源管理、插值、复数等能力。@modern-js/plugin-i18n 默认基于 i18next,请参考其[官方文档](https://www.i18next.com/)获取完整的配置说明。
18
-
19
- ### react-i18next
20
-
21
- react-i18next 是 i18next 的 React 绑定库,提供 `useTranslation`、`Trans` 等 Hook/组件,实现与 React 生命周期良好结合:
22
-
23
- ```tsx
24
- import { useTranslation } from 'react-i18next';
25
-
26
- function App() {
27
- const { t } = useTranslation();
28
- return <h1>{t('welcome')}</h1>;
29
- }
30
- ```
31
-
32
- ### i18n 实例
33
-
34
- i18next 默认导出一个实例,也支持通过 `createInstance` 生成多实例:
35
-
36
- ```ts
37
- import i18next, { createInstance } from 'i18next';
38
-
39
- i18next.init({
40
- /* ... */
41
- });
42
-
43
- const custom = createInstance();
44
- await custom.init({
45
- /* 独立配置 */
46
- });
47
- ```
48
-
49
- 实例负责翻译资源、当前语言、切换语言等功能,也可以在 Modern.js 的 `runtime` 中传入自定义实例。
50
-
51
- ### 初始化(init)
52
-
53
- i18next 通过 `init` 完成初始化,常用核心选项:
54
-
55
- - `lng`:初始语言
56
- - `ns` / `defaultNS`:命名空间列表与默认命名空间
57
- - `supportedLngs`:允许的语言集合
58
- - `fallbackLng`:缺失资源时的回退语言(可为数组或映射)
59
- - `interpolation`:插值设置,React 环境通常配置 `escapeValue: false`
60
-
61
- ```ts
62
- i18next.init({
63
- lng: 'zh',
64
- ns: ['translation', 'common'],
65
- defaultNS: 'translation',
66
- supportedLngs: ['zh', 'en'],
67
- fallbackLng: ['en'],
68
- interpolation: { escapeValue: false },
69
- });
70
- ```
71
-
72
- ### `t` 函数
73
-
74
- `t` 是获取翻译的核心 API,可直接从实例使用,也可以通过 react-i18next Hook 获得:
75
-
76
- ```ts
77
- i18next.t('welcome');
78
- ```
79
-
80
- ```tsx
81
- const { t } = useTranslation();
82
- t('welcome', { name: 'Modern.js', count: 3 });
83
- ```
84
-
85
- `t` 支持插值、复数、上下文等高级特性,后文会展开说明。
86
-
87
- import PlatformSupport from '@site-docs/components/international/platform-support';
88
-
89
- <PlatformSupport />
90
-
91
- ## 语言代码
92
-
93
- 语言代码用于标识当前界面语言,遵循 ISO 639-1 标准(`en`、`zh` 等),也可以携带地区信息(`en-US`、`zh-CN`)。
94
-
95
- - **支持语言列表**:通过插件配置声明,编译期即可获知需要生成的产物。
96
- - **默认语言**:当检测不到用户语言或资源缺失时使用。
97
- - **回退语言链**:`en-US → en → zh` 这类链决定缺失翻译时的查找顺序。
98
-
99
- ```ts
100
- // modern.config.ts
101
- import { defineConfig } from '@modern-js/app-tools';
102
- import { i18nPlugin } from '@modern-js/plugin-i18n';
103
-
104
- export default defineConfig({
105
- plugins: [
106
- i18nPlugin({
107
- localeDetection: {
108
- languages: ['zh', 'en', 'ja'],
109
- fallbackLanguage: ['zh', 'en'], // 支持回退链
110
- },
111
- }),
112
- ],
113
- });
114
- ```
115
-
116
- 💡 建议将 `supportedLanguages` 与 `fallbackLanguage` 同步维护,避免出现用户切换到未配置语言的情况。
117
-
118
- ## 命名空间
119
-
120
- 命名空间(Namespace)用于按照业务模块拆分翻译文件,便于代码分割与按需加载。未指定时使用默认命名空间 `translation`。
121
-
122
- ```ts
123
- // src/modern.runtime.ts
124
- import { defineRuntimeConfig } from '@modern-js/runtime';
125
-
126
- export default defineRuntimeConfig({
127
- i18n: {
128
- initOptions: {
129
- ns: ['translation', 'common', 'dashboard'],
130
- defaultNS: 'translation',
131
- },
132
- },
133
- });
134
- ```
135
-
136
- 在组件中使用不同命名空间:
137
-
138
- ```tsx
139
- import { useTranslation } from 'react-i18next';
140
-
141
- export function DashboardHeader() {
142
- const { t } = useTranslation(['dashboard', 'common']);
143
- return (
144
- <header>
145
- <h1>{t('dashboard:title')}</h1>
146
- <button>{t('common:button.refresh')}</button>
147
- </header>
148
- );
149
- }
150
- ```
151
-
152
- 命名空间还可以和动态加载结合,按需请求大体量文案。
153
-
154
- ## 资源文件结构
155
-
156
- 推荐的资源文件目录:
157
-
158
- ```
159
- locales/
160
- ├── en/
161
- │ ├── translation.json
162
- │ ├── common.json
163
- │ └── dashboard.json
164
- └── zh/
165
- ├── translation.json
166
- ├── common.json
167
- └── dashboard.json
168
- ```
169
-
170
- - **文件命名**:`locales/<language>/<namespace>.json`
171
- - **格式**:标准 JSON,键值对或嵌套对象
172
- - **组织**:嵌套对象用于表示 UI 层级,例如按钮、对话框等
173
-
174
- ```json
175
- {
176
- "header": {
177
- "title": "欢迎",
178
- "actions": {
179
- "save": "保存",
180
- "cancel": "取消"
181
- }
182
- }
183
- }
184
- ```
185
-
186
- 也可以通过 `resources` 选项在初始化时直接注入资源,或在运行时调用 `addResourceBundle`:
187
-
188
- ```ts
189
- i18next.init({
190
- resources: {
191
- en: {
192
- common: {
193
- welcome: 'Welcome',
194
- },
195
- },
196
- zh: {
197
- common: {
198
- welcome: '欢迎',
199
- },
200
- },
201
- },
202
- });
203
-
204
- i18next.addResourceBundle('en', 'home', { title: 'Home' });
205
- ```
206
-
207
- ## 翻译键
208
-
209
- 翻译键(Translation Key)是访问翻译的路径,通常使用点号表示层级:`common.button.submit`。
210
-
211
- 命名规范建议:
212
-
213
- - 使用语义化单词,避免缩写
214
- - 按模块划分前缀(`dashboard.table.*`)
215
- - 可使用 `:` 指定命名空间(`common:button.submit`)
216
- - 避免直接把完整中文文案当作键
217
-
218
- ```tsx
219
- const { t } = useTranslation();
220
-
221
- button.textContent = t('common.button.submit', {
222
- defaultValue: 'Submit',
223
- });
224
- ```
225
-
226
- ## 插值和变量
227
-
228
- 插值(Interpolation)允许在翻译文本中动态注入变量。
229
-
230
- **资源文件**:
231
-
232
- ```json
233
- {
234
- "welcome": "欢迎,{{name}}!",
235
- "invite": "{{name}} 邀请你加入 {{project}}",
236
- "formattedValue": "当前价格:{{value, currency}}"
237
- }
238
- ```
239
-
240
- **用法**:
241
-
242
- ```tsx
243
- const { t } = useTranslation();
244
-
245
- return (
246
- <>
247
- <p>{t('welcome', { name: 'John' })}</p>
248
- <p>{t('invite', { name: 'Alice', project: 'Modern.js' })}</p>
249
- </>
250
- );
251
- ```
252
-
253
- ### 嵌套插值
254
-
255
- 可以直接传递对象或多级变量:
256
-
257
- ```json
258
- {
259
- "greeting": "你好,{{user.name}},你有 {{user.notifications}} 条新消息"
260
- }
261
- ```
262
-
263
- ```tsx
264
- t('greeting', {
265
- user: { name: 'Jay', notifications: 3 },
266
- });
267
- ```
268
-
269
- ### 格式化插值
270
-
271
- 通过 `interpolation.format` 函数格式化数字、日期等:
272
-
273
- ```ts
274
- export default defineRuntimeConfig({
275
- i18n: {
276
- initOptions: {
277
- interpolation: {
278
- format(value, format, lng) {
279
- if (format === 'currency') {
280
- return new Intl.NumberFormat(lng, {
281
- style: 'currency',
282
- currency: lng === 'zh' ? 'CNY' : 'USD',
283
- }).format(Number(value));
284
- }
285
- if (value instanceof Date) {
286
- return new Intl.DateTimeFormat(lng, { dateStyle: 'medium' }).format(
287
- value,
288
- );
289
- }
290
- return value;
291
- },
292
- },
293
- },
294
- },
295
- });
296
- ```
297
-
298
- ```tsx
299
- t('formattedValue', { value: 99.5, format: 'currency' });
300
- ```
301
-
302
- ### 转义插值
303
-
304
- `react-i18next` 默认会对插值值进行转义以防止 XSS。如需渲染安全的 HTML,需显式开启 `interpolation.escapeValue = false` 并确保数据可信。
305
-
306
- ## 复数
307
-
308
- 复数处理根据语言自动选择合适的词形,依赖 `count` 参数。
309
-
310
- ```json
311
- {
312
- "item": "1 个条目",
313
- "item_plural": "{{count}} 个条目",
314
- "item_0": "没有条目"
315
- }
316
- ```
317
-
318
- ```tsx
319
- t('item', { count: 0 }); // 没有条目
320
- t('item', { count: 1 }); // 1 个条目
321
- t('item', { count: 5 }); // 5 个条目
322
- ```
323
-
324
- 不同语言具有不同的复数规则,例如:
325
-
326
- - **英语**:单数、复数
327
- - **俄语**:one、few、many 多种形式
328
- - **中文**:通常只有单一形式,可使用 `_0` 键覆盖特殊文案
329
-
330
- 💡 如果需要自定义复数规则,可通过 `i18next.services.pluralResolver` 扩展,详见高级用法。
331
-
332
- ## 嵌套翻译结构
333
-
334
- 嵌套结构可以直观反映 UI 层级。
335
-
336
- ```json
337
- {
338
- "common": {
339
- "button": {
340
- "submit": "提交",
341
- "cancel": "取消"
342
- }
343
- }
344
- }
345
- ```
346
-
347
- 在代码中使用点号访问:
348
-
349
- ```tsx
350
- const { t } = useTranslation();
351
- t('common.button.submit');
352
- ```
353
-
354
- 嵌套结构的优势:
355
-
356
- - 避免键名冗长
357
- - 便于在 JSON 中整体查看模块文案
358
- - 可搭配 `keyPrefix` 精简调用:`useTranslation('common', { keyPrefix: 'button' })`
359
-
360
- ## 回退语言
361
-
362
- 当当前语言缺少某个键时,会按回退语言链继续查找。
363
-
364
- ```ts
365
- export default defineRuntimeConfig({
366
- i18n: {
367
- initOptions: {
368
- lng: 'zh-CN',
369
- fallbackLng: {
370
- 'zh-CN': ['zh', 'en'],
371
- default: ['en'],
372
- },
373
- },
374
- },
375
- });
376
- ```
377
-
378
- :::tip
379
- 可以将地区语言(如 `zh-CN`)回退到通用语言(`zh`),最后再回退到默认语言(`en`),确保所有键都有可用文本。
380
- :::
381
-
382
- ## 语言检测
383
-
384
- i18next 通过语言检测插件自动识别用户语言,Modern.js 插件内置浏览器与服务端支持。
385
-
386
- ```ts
387
- import LanguageDetector from 'i18next-browser-languagedetector';
388
- import i18next from 'i18next';
389
-
390
- i18next.use(LanguageDetector).init({
391
- supportedLngs: ['zh', 'en', 'ja'],
392
- detection: {
393
- order: ['path', 'cookie', 'localStorage', 'navigator'],
394
- lookupCookie: 'i18next',
395
- lookupLocalStorage: 'i18nextLng',
396
- },
397
- });
398
- ```
399
-
400
- Modern.js 中可以直接在插件配置里开启内置检测:
401
-
402
- ```ts
403
- i18nPlugin({
404
- localeDetection: {
405
- i18nextDetector: true,
406
- languages: ['zh', 'en'],
407
- detection: {
408
- order: ['path', 'cookie', 'header'],
409
- },
410
- },
411
- });
412
- ```
413
-
414
- :::warning
415
- 启用检测后,无需在 `init` 中显式设置 `lng`。如果手动调用 `changeLanguage()` 未传入语言,也会根据检测配置自动推断。
416
- :::