@modern-js/main-doc 3.9.3 → 3.10.1
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/apis/app/hooks/api/lambda.mdx +2 -1
- package/docs/en/apis/app/hooks/config/html.mdx +1 -0
- package/docs/en/apis/app/hooks/config/mock.mdx +1 -0
- package/docs/en/apis/app/hooks/config/public.mdx +1 -0
- package/docs/en/apis/app/hooks/config/upload.mdx +2 -3
- package/docs/en/apis/app/hooks/modern-config.mdx +1 -0
- package/docs/en/apis/app/hooks/shared.mdx +1 -0
- package/docs/en/apis/app/hooks/src/modern.runtime.mdx +1 -0
- package/docs/en/apis/app/hooks/src/routes.mdx +1 -0
- package/docs/en/apis/app/hooks/src/server.mdx +2 -1
- package/docs/en/apis/app/runtime/bff/use-hono-context.mdx +1 -0
- package/docs/en/apis/app/runtime/core/create-root.mdx +4 -1
- package/docs/en/apis/app/runtime/core/render.mdx +6 -2
- package/docs/en/apis/app/runtime/core/runtime-context.mdx +17 -17
- package/docs/en/apis/app/runtime/router/router.mdx +9 -6
- package/docs/en/apis/app/runtime/ssr/no-ssr.mdx +1 -0
- package/docs/en/apis/app/runtime/utility/head.mdx +1 -0
- package/docs/en/apis/app/runtime/utility/loadable.mdx +1 -0
- package/docs/en/community/blog/v3-release-note.mdx +36 -27
- package/docs/en/community/contributing-guide.mdx +15 -33
- package/docs/en/components/default-browserslist.mdx +0 -1
- package/docs/en/components/deploy-command.mdx +1 -1
- package/docs/en/components/deploy.mdx +1 -1
- package/docs/en/components/entry-scan-logic.mdx +1 -1
- package/docs/en/components/init-app.mdx +9 -3
- package/docs/en/components/international/custom-instance-code.mdx +0 -1
- package/docs/en/components/micro-runtime-config.mdx +12 -9
- package/docs/en/components/nodeVersion.mdx +2 -2
- package/docs/en/components/rsc-deploy-tip.mdx +0 -1
- package/docs/en/components/ssr-monitor.mdx +1 -1
- package/docs/en/configure/app/bff/cross-project.mdx +2 -2
- package/docs/en/configure/app/bff/prefix.mdx +1 -1
- package/docs/en/configure/app/dev/lazy-compilation.mdx +17 -1
- package/docs/en/configure/app/dev/start-url.mdx +0 -1
- package/docs/en/configure/app/output/css-modules.mdx +1 -5
- package/docs/en/configure/app/output/dist-path.mdx +0 -1
- package/docs/en/configure/app/output/inline-scripts.mdx +1 -2
- package/docs/en/configure/app/output/inline-styles.mdx +1 -2
- package/docs/en/configure/app/output/split-route-chunks.mdx +1 -1
- package/docs/en/configure/app/output/ssg.mdx +1 -1
- package/docs/en/configure/app/output/ssgByEntries.mdx +1 -0
- package/docs/en/configure/app/performance/chunk-split.mdx +1 -4
- package/docs/en/configure/app/runtime/plugins.mdx +6 -4
- package/docs/en/configure/app/server/rsc.mdx +1 -1
- package/docs/en/configure/app/server/ssr.mdx +7 -8
- package/docs/en/configure/app/server/tsconfig-path.mdx +2 -2
- package/docs/en/configure/app/tools/less.mdx +5 -6
- package/docs/en/configure/app/tools/sass.mdx +5 -6
- package/docs/en/configure/app/tools/svgr.mdx +7 -7
- package/docs/en/configure/app/usage.mdx +1 -5
- package/docs/en/guides/advanced-features/bff/cross-project.mdx +5 -4
- package/docs/en/guides/advanced-features/compatibility.mdx +0 -1
- package/docs/en/guides/advanced-features/international/advanced.mdx +1 -1
- package/docs/en/guides/advanced-features/international/api.mdx +34 -28
- package/docs/en/guides/advanced-features/international/best-practices.mdx +15 -15
- package/docs/en/guides/advanced-features/international/configuration.mdx +37 -37
- package/docs/en/guides/advanced-features/international/locale-detection.mdx +37 -28
- package/docs/en/guides/advanced-features/international/resource-loading.mdx +19 -17
- package/docs/en/guides/advanced-features/international/routing.mdx +8 -6
- package/docs/en/guides/advanced-features/international.mdx +8 -8
- package/docs/en/guides/advanced-features/server-monitor/logger.mdx +5 -5
- package/docs/en/guides/advanced-features/server-monitor/metrics.mdx +8 -8
- package/docs/en/guides/advanced-features/server-monitor/monitors.mdx +1 -3
- package/docs/en/guides/advanced-features/source-build.mdx +1 -1
- package/docs/en/guides/advanced-features/web-server.mdx +3 -3
- package/docs/en/guides/basic-features/data/data-fetch.mdx +1 -3
- package/docs/en/guides/basic-features/data/data-write.mdx +24 -28
- package/docs/en/guides/basic-features/debug/mock.mdx +2 -2
- package/docs/en/guides/basic-features/debug/using-storybook.mdx +5 -6
- package/docs/en/guides/basic-features/html.mdx +2 -3
- package/docs/en/guides/basic-features/output-files.mdx +1 -1
- package/docs/en/guides/basic-features/render/overview.mdx +12 -12
- package/docs/en/guides/basic-features/render/rsc.mdx +16 -9
- package/docs/en/guides/basic-features/render/ssr-cache.mdx +5 -5
- package/docs/en/guides/basic-features/render/ssr.mdx +8 -12
- package/docs/en/guides/basic-features/render/streaming-ssr.mdx +2 -1
- package/docs/en/guides/basic-features/routes/config-routes.mdx +60 -59
- package/docs/en/guides/basic-features/testing/playwright.mdx +14 -8
- package/docs/en/guides/basic-features/testing/rstest.mdx +6 -4
- package/docs/en/guides/get-started/ai-coding-agents.mdx +6 -6
- package/docs/en/guides/get-started/introduction.mdx +0 -1
- package/docs/en/guides/topic-detail/module-federation/deploy.mdx +2 -2
- package/docs/en/guides/topic-detail/module-federation/introduce.mdx +0 -1
- package/docs/en/guides/upgrade/config.mdx +12 -0
- package/docs/en/guides/upgrade/other.mdx +4 -4
- package/docs/en/guides/upgrade/tailwindcss.mdx +0 -1
- package/docs/en/guides/upgrade/web-server.md +34 -17
- package/docs/en/index.md +1 -0
- package/docs/en/plugin/cli-plugins/api.mdx +23 -23
- package/docs/en/plugin/cli-plugins/life-cycle.mdx +2 -0
- package/docs/en/plugin/introduction.mdx +6 -6
- package/docs/en/plugin/official/cli-plugins/plugin-bff.mdx +1 -1
- package/docs/en/plugin/official/cli-plugins/plugin-ssg.mdx +1 -1
- package/docs/en/plugin/plugin-system.mdx +19 -26
- package/docs/en/plugin/runtime-plugins/life-cycle.mdx +2 -0
- package/docs/en/plugin/server-plugins/api.mdx +11 -14
- package/docs/en/plugin/server-plugins/life-cycle.mdx +2 -0
- package/docs/zh/apis/app/hooks/api/lambda.mdx +1 -1
- package/docs/zh/apis/app/hooks/config/html.mdx +1 -0
- package/docs/zh/apis/app/hooks/config/mock.mdx +1 -0
- package/docs/zh/apis/app/hooks/config/public.mdx +1 -0
- package/docs/zh/apis/app/hooks/config/upload.mdx +2 -3
- package/docs/zh/apis/app/hooks/modern-config.mdx +1 -0
- package/docs/zh/apis/app/hooks/shared.mdx +1 -0
- package/docs/zh/apis/app/hooks/src/modern.runtime.mdx +1 -0
- package/docs/zh/apis/app/hooks/src/routes.mdx +1 -0
- package/docs/zh/apis/app/hooks/src/server.mdx +2 -1
- package/docs/zh/apis/app/runtime/bff/use-hono-context.mdx +1 -1
- package/docs/zh/apis/app/runtime/core/create-root.mdx +4 -1
- package/docs/zh/apis/app/runtime/core/render.mdx +6 -3
- package/docs/zh/apis/app/runtime/core/runtime-context.mdx +17 -17
- package/docs/zh/apis/app/runtime/router/router.mdx +10 -6
- package/docs/zh/apis/app/runtime/ssr/no-ssr.mdx +1 -0
- package/docs/zh/apis/app/runtime/utility/head.mdx +1 -0
- package/docs/zh/apis/app/runtime/utility/loadable.mdx +1 -0
- package/docs/zh/community/blog/v3-release-note.mdx +38 -29
- package/docs/zh/community/contributing-guide.mdx +16 -34
- package/docs/zh/components/default-browserslist.mdx +0 -1
- package/docs/zh/components/deploy-command.mdx +1 -1
- package/docs/zh/components/deploy.mdx +1 -1
- package/docs/zh/components/entry-scan-logic.mdx +1 -1
- package/docs/zh/components/init-app.mdx +9 -3
- package/docs/zh/components/international/custom-instance-code.mdx +0 -1
- package/docs/zh/components/international/install-command.mdx +0 -1
- package/docs/zh/components/micro-runtime-config.mdx +12 -9
- package/docs/zh/components/nodeVersion.mdx +2 -2
- package/docs/zh/components/rsc-deploy-tip.mdx +0 -1
- package/docs/zh/components/ssr-monitor.mdx +1 -1
- package/docs/zh/configure/app/bff/cross-project.mdx +2 -2
- package/docs/zh/configure/app/bff/prefix.mdx +1 -1
- package/docs/zh/configure/app/dev/lazy-compilation.mdx +17 -1
- package/docs/zh/configure/app/html/crossorigin.mdx +0 -1
- package/docs/zh/configure/app/html/favicon.mdx +0 -1
- package/docs/zh/configure/app/output/convert-to-rem.mdx +0 -1
- package/docs/zh/configure/app/output/css-modules.mdx +1 -5
- package/docs/zh/configure/app/output/inline-scripts.mdx +1 -2
- package/docs/zh/configure/app/output/inline-styles.mdx +1 -2
- package/docs/zh/configure/app/output/source-map.mdx +0 -2
- package/docs/zh/configure/app/output/split-route-chunks.mdx +1 -1
- package/docs/zh/configure/app/output/ssg.mdx +1 -0
- package/docs/zh/configure/app/output/ssgByEntries.mdx +1 -1
- package/docs/zh/configure/app/performance/chunk-split.mdx +1 -4
- package/docs/zh/configure/app/resolve/dedupe.mdx +0 -1
- package/docs/zh/configure/app/runtime/plugins.mdx +6 -4
- package/docs/zh/configure/app/server/rsc.mdx +1 -1
- package/docs/zh/configure/app/server/ssr.mdx +6 -7
- package/docs/zh/configure/app/server/tsconfig-path.mdx +2 -2
- package/docs/zh/configure/app/tools/less.mdx +6 -7
- package/docs/zh/configure/app/tools/sass.mdx +6 -7
- package/docs/zh/configure/app/tools/svgr.mdx +11 -11
- package/docs/zh/configure/app/usage.mdx +1 -5
- package/docs/zh/guides/advanced-features/bff.mdx +1 -0
- package/docs/zh/guides/advanced-features/international/advanced.mdx +1 -1
- package/docs/zh/guides/advanced-features/international/api.mdx +28 -27
- package/docs/zh/guides/advanced-features/international/best-practices.mdx +15 -15
- package/docs/zh/guides/advanced-features/international/configuration.mdx +38 -36
- package/docs/zh/guides/advanced-features/international/locale-detection.mdx +37 -28
- package/docs/zh/guides/advanced-features/international/resource-loading.mdx +20 -16
- package/docs/zh/guides/advanced-features/international/routing.mdx +6 -4
- package/docs/zh/guides/advanced-features/international.mdx +9 -9
- package/docs/zh/guides/advanced-features/server-monitor/logger.mdx +4 -6
- package/docs/zh/guides/advanced-features/server-monitor/metrics.mdx +9 -10
- package/docs/zh/guides/advanced-features/source-build.mdx +1 -1
- package/docs/zh/guides/advanced-features/web-server.mdx +3 -3
- package/docs/zh/guides/basic-features/data/data-fetch.mdx +1 -3
- package/docs/zh/guides/basic-features/data/data-write.mdx +25 -31
- package/docs/zh/guides/basic-features/debug/mock.mdx +2 -2
- package/docs/zh/guides/basic-features/debug/using-storybook.mdx +5 -5
- package/docs/zh/guides/basic-features/env-vars.mdx +0 -1
- package/docs/zh/guides/basic-features/html.mdx +2 -3
- package/docs/zh/guides/basic-features/render/overview.mdx +12 -13
- package/docs/zh/guides/basic-features/render/rsc.mdx +16 -9
- package/docs/zh/guides/basic-features/render/ssr.mdx +8 -17
- package/docs/zh/guides/basic-features/routes/config-routes.mdx +61 -59
- package/docs/zh/guides/basic-features/testing/playwright.mdx +15 -10
- package/docs/zh/guides/basic-features/testing/rstest.mdx +6 -4
- package/docs/zh/guides/get-started/ai-coding-agents.mdx +7 -7
- package/docs/zh/guides/topic-detail/module-federation/deploy.mdx +2 -2
- package/docs/zh/guides/upgrade/config.mdx +12 -0
- package/docs/zh/guides/upgrade/other.md +4 -11
- package/docs/zh/guides/upgrade/overview.mdx +1 -4
- package/docs/zh/guides/upgrade/tailwindcss.mdx +0 -1
- package/docs/zh/guides/upgrade/web-server.md +34 -19
- package/docs/zh/index.md +1 -0
- package/docs/zh/plugin/cli-plugins/api.mdx +24 -24
- package/docs/zh/plugin/cli-plugins/life-cycle.mdx +2 -0
- package/docs/zh/plugin/introduction.mdx +6 -6
- package/docs/zh/plugin/official/cli-plugins/plugin-bff.mdx +1 -1
- package/docs/zh/plugin/official/cli-plugins/plugin-ssg.mdx +1 -1
- package/docs/zh/plugin/plugin-system.mdx +19 -26
- package/docs/zh/plugin/runtime-plugins/life-cycle.mdx +2 -0
- package/docs/zh/plugin/server-plugins/api.mdx +11 -14
- package/docs/zh/plugin/server-plugins/life-cycle.mdx +2 -0
- package/package.json +11 -13
- package/{rspress.config.ts → rstack.config.mts} +7 -10
- package/src/components/Footer/styles.module.scss +1 -1
- package/src/components/SolutionCards/index.module.scss +3 -1
|
@@ -21,12 +21,12 @@ const defaultOptions = {
|
|
|
21
21
|
|
|
22
22
|
`tools.less` 用于修改 [@rsbuild/plugin-less](https://rsbuild.rs/plugins/list/plugin-less) 的选项,取值为该插件的完整选项:
|
|
23
23
|
|
|
24
|
-
| 选项
|
|
25
|
-
|
|
|
26
|
-
| `lessLoaderOptions`
|
|
27
|
-
| `include`
|
|
28
|
-
| `exclude`
|
|
29
|
-
| `parallel`
|
|
24
|
+
| 选项 | 说明 |
|
|
25
|
+
| ------------------- | ----------------------------------------------------------------------------------------- |
|
|
26
|
+
| `lessLoaderOptions` | 传给 [less-loader](https://github.com/webpack-contrib/less-loader) 的选项,支持对象或函数 |
|
|
27
|
+
| `include` | 指定哪些文件交给 less-loader 处理,默认 `/\.less$/` |
|
|
28
|
+
| `exclude` | 指定哪些文件不交给 less-loader 处理 |
|
|
29
|
+
| `parallel` | 是否使用 worker 线程并行编译 Less,默认 `false` |
|
|
30
30
|
|
|
31
31
|
### 修改 less-loader 选项
|
|
32
32
|
|
|
@@ -135,7 +135,6 @@ export default {
|
|
|
135
135
|
同一个对象里只要出现插件顶层键(`lessLoaderOptions`、`include`、`exclude` 等),整个对象就会按插件选项解析,其中的 loader 选项不会生效。例如 `tools.less: { parallel: true, lessOptions: { ... } }` 里的 `lessOptions` 会被忽略,必须移到 `lessLoaderOptions` 下;开发环境下会打印一条提示。
|
|
136
136
|
:::
|
|
137
137
|
|
|
138
|
-
|
|
139
138
|
在之前的版本中,`tools.less` 的取值直接是 less-loader 的选项,例如 `tools.less: { lessOptions: {} }` 或 `tools.less(config, { addExcludes }) {}`。这种写法目前仍然生效,Modern.js 会自动把它包装为 `lessLoaderOptions`,产出的配置与之前完全一致;在开发环境下会打印一条迁移提示,并将在下一个大版本中移除。
|
|
140
139
|
|
|
141
140
|
```js
|
|
@@ -18,12 +18,12 @@ const defaultOptions = {
|
|
|
18
18
|
|
|
19
19
|
`tools.sass` 用于修改 [@rsbuild/plugin-sass](https://rsbuild.rs/plugins/list/plugin-sass) 的选项,取值为该插件的完整选项:
|
|
20
20
|
|
|
21
|
-
| 选项
|
|
22
|
-
|
|
|
23
|
-
| `sassLoaderOptions`
|
|
24
|
-
| `include`
|
|
25
|
-
| `exclude`
|
|
26
|
-
| `rewriteUrls`
|
|
21
|
+
| 选项 | 说明 |
|
|
22
|
+
| ------------------- | ----------------------------------------------------------------------------------------- |
|
|
23
|
+
| `sassLoaderOptions` | 传给 [sass-loader](https://github.com/webpack-contrib/sass-loader) 的选项,支持对象或函数 |
|
|
24
|
+
| `include` | 指定哪些文件交给 sass-loader 处理,默认 `/\.s(?:a\|c)ss$/` |
|
|
25
|
+
| `exclude` | 指定哪些文件不交给 sass-loader 处理 |
|
|
26
|
+
| `rewriteUrls` | 是否使用 resolve-url-loader 重写 Sass 文件中的相对 URL,默认 `true` |
|
|
27
27
|
|
|
28
28
|
### 修改 sass-loader 选项
|
|
29
29
|
|
|
@@ -126,7 +126,6 @@ export default {
|
|
|
126
126
|
同一个对象里只要出现插件顶层键(`sassLoaderOptions`、`include`、`exclude` 等),整个对象就会按插件选项解析,其中的 loader 选项不会生效。例如 `tools.sass: { rewriteUrls: false, sassOptions: { ... } }` 里的 `sassOptions` 会被忽略,必须移到 `sassLoaderOptions` 下;开发环境下会打印一条提示。
|
|
127
127
|
:::
|
|
128
128
|
|
|
129
|
-
|
|
130
129
|
在之前的版本中,`tools.sass` 的取值直接是 sass-loader 的选项,例如 `tools.sass: { sassOptions: {} }` 或 `tools.sass(config, { addExcludes }) {}`。这种写法目前仍然生效,Modern.js 会自动把它包装为 `sassLoaderOptions`,产出的配置与之前完全一致;在开发环境下会打印一条迁移提示,并将在下一个大版本中移除。
|
|
131
130
|
|
|
132
131
|
```js
|
|
@@ -18,14 +18,14 @@ const defaultOptions = {
|
|
|
18
18
|
|
|
19
19
|
`tools.svgr` 用于修改 [@rsbuild/plugin-svgr](https://rsbuild.rs/plugins/list/plugin-svgr) 的选项,取值为该插件的完整选项。设置为 `false` 时不再注册 SVGR 插件,所有 `.svg` 文件按静态资源处理。
|
|
20
20
|
|
|
21
|
-
| 选项 | 说明
|
|
22
|
-
| ----------------- |
|
|
21
|
+
| 选项 | 说明 |
|
|
22
|
+
| ----------------- | ------------------------------------------------------------------------------------------------- |
|
|
23
23
|
| `svgrOptions` | 传给 [SVGR](https://react-svgr.com/docs/options/) 的参数,例如 `exportType`、`icon`、`svgoConfig` |
|
|
24
|
-
| `parallel` | 是否使用 worker 线程并行转换 SVG,默认 `false`
|
|
25
|
-
| `exclude` | 指定哪些 SVG 文件不交给 SVGR 处理
|
|
26
|
-
| `excludeImporter` | 指定从哪些文件导入 SVG 时不交给 SVGR 处理
|
|
27
|
-
| `query` | 指定触发 SVGR 转换的 query,默认 `/react/`
|
|
28
|
-
| `mixedImport` | 是否允许在同一个模块中同时导入 URL 与 React 组件,默认 `true`
|
|
24
|
+
| `parallel` | 是否使用 worker 线程并行转换 SVG,默认 `false` |
|
|
25
|
+
| `exclude` | 指定哪些 SVG 文件不交给 SVGR 处理 |
|
|
26
|
+
| `excludeImporter` | 指定从哪些文件导入 SVG 时不交给 SVGR 处理 |
|
|
27
|
+
| `query` | 指定触发 SVGR 转换的 query,默认 `/react/` |
|
|
28
|
+
| `mixedImport` | 是否允许在同一个模块中同时导入 URL 与 React 组件,默认 `true` |
|
|
29
29
|
|
|
30
30
|
### 合并规则
|
|
31
31
|
|
|
@@ -83,11 +83,11 @@ export default {
|
|
|
83
83
|
|
|
84
84
|
`tools.svgr` 取代了下面两个已废弃的配置项,二者目前仍然生效,并将在下一个大版本中移除:
|
|
85
85
|
|
|
86
|
-
| 旧配置
|
|
87
|
-
|
|
|
86
|
+
| 旧配置 | 新配置 |
|
|
87
|
+
| -------------------------------------- | -------------------------------------------------------- |
|
|
88
88
|
| `output.svgDefaultExport: 'component'` | `tools.svgr: { svgrOptions: { exportType: 'default' } }` |
|
|
89
89
|
| `output.svgDefaultExport: 'url'` | `tools.svgr: { svgrOptions: { exportType: 'named' } }` |
|
|
90
|
-
| `output.disableSvgr: true` | `tools.svgr: false`
|
|
91
|
-
| `output.disableSvgr: false` | 默认行为,直接删除即可
|
|
90
|
+
| `output.disableSvgr: true` | `tools.svgr: false` |
|
|
91
|
+
| `output.disableSvgr: false` | 默认行为,直接删除即可 |
|
|
92
92
|
|
|
93
93
|
同时设置时以 `tools.svgr` 为准:显式设置了 `tools.svgr` 后,`output.disableSvgr` 不再生效;`tools.svgr.svgrOptions.exportType` 会覆盖 `output.svgDefaultExport` 派生的值。
|
|
@@ -265,11 +265,7 @@ import { defineConfig } from '@modern-js/app-tools';
|
|
|
265
265
|
import { xxPlugin } from '@modern-js/plugin-xx';
|
|
266
266
|
|
|
267
267
|
export default defineConfig({
|
|
268
|
-
plugins: [
|
|
269
|
-
xxPlugin({
|
|
270
|
-
/** 在此处添加参数 */
|
|
271
|
-
}),
|
|
272
|
-
],
|
|
268
|
+
plugins: [xxPlugin({/** 在此处添加参数 */})],
|
|
273
269
|
});
|
|
274
270
|
```
|
|
275
271
|
|
|
@@ -10,14 +10,14 @@ title: API 参考
|
|
|
10
10
|
|
|
11
11
|
### 返回值
|
|
12
12
|
|
|
13
|
-
| 字段
|
|
14
|
-
|
|
|
15
|
-
| `language`
|
|
16
|
-
| `changeLanguage`
|
|
17
|
-
| `supportedLanguages`
|
|
18
|
-
| `isLanguageSupported` | `(lang: string) => boolean`
|
|
19
|
-
| `isResourcesReady`
|
|
20
|
-
| `i18nInstance`
|
|
13
|
+
| 字段 | 类型 | 说明 |
|
|
14
|
+
| --------------------- | --------------------------------- | -------------------------------------------------- |
|
|
15
|
+
| `language` | `string` | 当前语言代码 |
|
|
16
|
+
| `changeLanguage` | `(lang: string) => Promise<void>` | 切换语言 |
|
|
17
|
+
| `supportedLanguages` | `string[]` | 支持的语言列表(来自 `localeDetection.languages`) |
|
|
18
|
+
| `isLanguageSupported` | `(lang: string) => boolean` | 检查语言是否在支持列表中 |
|
|
19
|
+
| `isResourcesReady` | `boolean` | 当前语言的翻译资源是否已加载完成 |
|
|
20
|
+
| `i18nInstance` | `I18nInstance` | i18next 实例(用于高级场景) |
|
|
21
21
|
|
|
22
22
|
### 基本用法
|
|
23
23
|
|
|
@@ -85,13 +85,13 @@ function MyComponent() {
|
|
|
85
85
|
|
|
86
86
|
### Props
|
|
87
87
|
|
|
88
|
-
| Prop
|
|
89
|
-
|
|
|
90
|
-
| `to`
|
|
91
|
-
| `children`
|
|
92
|
-
| `replace`
|
|
93
|
-
| `state`
|
|
94
|
-
| 其他 Link props | —
|
|
88
|
+
| Prop | 类型 | 必填 | 说明 |
|
|
89
|
+
| --------------- | ----------------- | ---- | ------------------------------------------------- |
|
|
90
|
+
| `to` | `string` | 是 | 目标路径,不需要包含语言前缀 |
|
|
91
|
+
| `children` | `React.ReactNode` | 是 | 链接内容 |
|
|
92
|
+
| `replace` | `boolean` | 否 | 使用 `history.replace` 而非 `push` |
|
|
93
|
+
| `state` | `any` | 否 | 传递给目标路由的状态 |
|
|
94
|
+
| 其他 Link props | — | 否 | 继承自 `@modern-js/runtime/router` 的 `Link` 组件 |
|
|
95
95
|
|
|
96
96
|
### 用法
|
|
97
97
|
|
|
@@ -132,6 +132,7 @@ const myPlugin = (): RuntimePlugin => ({
|
|
|
132
132
|
```
|
|
133
133
|
|
|
134
134
|
**注意事项:**
|
|
135
|
+
|
|
135
136
|
- 确保 i18n 插件在使用 `context.changeLanguage` 的插件之前注册
|
|
136
137
|
- `changeLanguage` 在服务端不会更新 URL 路径,需要配合路由插件或手动处理
|
|
137
138
|
|
|
@@ -165,11 +166,11 @@ interface I18nInstance {
|
|
|
165
166
|
type I18nSdkLoader = (options: I18nSdkLoadOptions) => Promise<Resources>;
|
|
166
167
|
|
|
167
168
|
interface I18nSdkLoadOptions {
|
|
168
|
-
lng?: string;
|
|
169
|
-
ns?: string;
|
|
170
|
-
lngs?: string[];
|
|
171
|
-
nss?: string[];
|
|
172
|
-
all?: boolean;
|
|
169
|
+
lng?: string; // 单个语言代码
|
|
170
|
+
ns?: string; // 单个命名空间
|
|
171
|
+
lngs?: string[]; // 多个语言代码
|
|
172
|
+
nss?: string[]; // 多个命名空间
|
|
173
|
+
all?: boolean; // 加载所有资源
|
|
173
174
|
}
|
|
174
175
|
|
|
175
176
|
type Resources = {
|
|
@@ -184,14 +185,14 @@ type Resources = {
|
|
|
184
185
|
```ts
|
|
185
186
|
interface LanguageDetectorOptions {
|
|
186
187
|
order?: string[];
|
|
187
|
-
lookupQuerystring?: string;
|
|
188
|
-
lookupCookie?: string;
|
|
189
|
-
lookupLocalStorage?: string;
|
|
188
|
+
lookupQuerystring?: string; // 默认 'lng'
|
|
189
|
+
lookupCookie?: string; // 默认 'i18next'
|
|
190
|
+
lookupLocalStorage?: string; // 默认 'i18nextLng'
|
|
190
191
|
lookupSession?: string;
|
|
191
|
-
lookupHeader?: string;
|
|
192
|
+
lookupHeader?: string; // 默认 'accept-language'
|
|
192
193
|
lookupFromPathIndex?: number; // 默认 0
|
|
193
194
|
caches?: false | string[];
|
|
194
|
-
cookieMinutes?: number;
|
|
195
|
+
cookieMinutes?: number; // 默认 525600(1年)
|
|
195
196
|
cookieExpirationDate?: Date;
|
|
196
197
|
cookieDomain?: string;
|
|
197
198
|
}
|
|
@@ -211,8 +212,8 @@ import { useTranslation } from 'react-i18next';
|
|
|
211
212
|
import { useModernI18n } from '@modern-js/plugin-i18n/runtime';
|
|
212
213
|
|
|
213
214
|
function App() {
|
|
214
|
-
const { t } = useTranslation();
|
|
215
|
-
const { language, changeLanguage } = useModernI18n();
|
|
215
|
+
const { t } = useTranslation(); // 翻译文本
|
|
216
|
+
const { language, changeLanguage } = useModernI18n(); // 语言切换
|
|
216
217
|
|
|
217
218
|
return (
|
|
218
219
|
<div>
|
|
@@ -63,8 +63,8 @@ function ButtonGroup() {
|
|
|
63
63
|
const { t } = useTranslation('common', { keyPrefix: 'button' });
|
|
64
64
|
return (
|
|
65
65
|
<>
|
|
66
|
-
<button>{t('submit')}</button>
|
|
67
|
-
<button>{t('cancel')}</button>
|
|
66
|
+
<button>{t('submit')}</button> {/* → common:button.submit */}
|
|
67
|
+
<button>{t('cancel')}</button> {/* → common:button.cancel */}
|
|
68
68
|
</>
|
|
69
69
|
);
|
|
70
70
|
}
|
|
@@ -94,9 +94,9 @@ function ButtonGroup() {
|
|
|
94
94
|
```
|
|
95
95
|
|
|
96
96
|
```tsx
|
|
97
|
-
t('page.title')
|
|
98
|
-
t('form.username.label')
|
|
99
|
-
t('form.submit', { defaultValue: '保存' }) // defaultValue 防止键缺失时显示 key 字符串
|
|
97
|
+
t('page.title');
|
|
98
|
+
t('form.username.label');
|
|
99
|
+
t('form.submit', { defaultValue: '保存' }); // defaultValue 防止键缺失时显示 key 字符串
|
|
100
100
|
```
|
|
101
101
|
|
|
102
102
|
## 复数
|
|
@@ -121,9 +121,9 @@ i18next 根据 `count` 参数自动选择对应的复数形式。**注意:i18n
|
|
|
121
121
|
```
|
|
122
122
|
|
|
123
123
|
```tsx
|
|
124
|
-
t('item', { count: 0 })
|
|
125
|
-
t('item', { count: 1 })
|
|
126
|
-
t('item', { count: 5 })
|
|
124
|
+
t('item', { count: 0 }); // → "没有条目" / "No items"
|
|
125
|
+
t('item', { count: 1 }); // → "没有条目" / "1 item"(中文通常只有一种形式)
|
|
126
|
+
t('item', { count: 5 }); // → "5 个条目" / "5 items"
|
|
127
127
|
```
|
|
128
128
|
|
|
129
129
|
不同语言的复数规则不同(英语有单复数,俄语有 one/few/many 等多种形式),i18next 会根据语言代码自动匹配 CLDR 规则。
|
|
@@ -152,8 +152,8 @@ t('item', { count: 5 }) // → "5 个条目" / "5 items"
|
|
|
152
152
|
```
|
|
153
153
|
|
|
154
154
|
```tsx
|
|
155
|
-
t('modal.confirm.title')
|
|
156
|
-
t('modal.confirm.actions.ok')
|
|
155
|
+
t('modal.confirm.title');
|
|
156
|
+
t('modal.confirm.actions.ok');
|
|
157
157
|
```
|
|
158
158
|
|
|
159
159
|
嵌套层级不建议超过 4 层,过深的结构会让键名变得冗长难维护。
|
|
@@ -199,8 +199,8 @@ export default defineRuntimeConfig({
|
|
|
199
199
|
```
|
|
200
200
|
|
|
201
201
|
```tsx
|
|
202
|
-
t('price', { value: 99.5 })
|
|
203
|
-
t('expiry', { date: new Date() }) // → "到期日:2025年5月12日"(zh)
|
|
202
|
+
t('price', { value: 99.5 }); // → "当前价格:¥99.50"(zh)/ "$99.50"(en)
|
|
203
|
+
t('expiry', { date: new Date() }); // → "到期日:2025年5月12日"(zh)
|
|
204
204
|
```
|
|
205
205
|
|
|
206
206
|
## 错误处理
|
|
@@ -237,7 +237,7 @@ if (!i18n.isInitialized) return null;
|
|
|
237
237
|
通过 `defaultValue` 提供兜底文案,防止界面显示 key 字符串:
|
|
238
238
|
|
|
239
239
|
```tsx
|
|
240
|
-
t('missing.key', { defaultValue: '默认文案' })
|
|
240
|
+
t('missing.key', { defaultValue: '默认文案' });
|
|
241
241
|
```
|
|
242
242
|
|
|
243
243
|
开发环境可以开启 `saveMissing`,将缺失的键输出到控制台方便排查:
|
|
@@ -281,8 +281,8 @@ declare module 'react-i18next' {
|
|
|
281
281
|
|
|
282
282
|
```tsx
|
|
283
283
|
const { t } = useTranslation();
|
|
284
|
-
t('welcome');
|
|
285
|
-
t('nonExistent');
|
|
284
|
+
t('welcome'); // ✅ TypeScript 自动补全
|
|
285
|
+
t('nonExistent'); // ❌ TypeScript 报错
|
|
286
286
|
```
|
|
287
287
|
|
|
288
288
|
:::tip 大型项目
|
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
---
|
|
2
2
|
title: 配置说明
|
|
3
3
|
---
|
|
4
|
+
|
|
4
5
|
# 配置说明
|
|
5
6
|
|
|
6
7
|
插件配置分为两个文件,各有职责:
|
|
7
8
|
|
|
8
|
-
| 文件
|
|
9
|
-
|
|
|
10
|
-
| `modern.config.ts`
|
|
9
|
+
| 文件 | 用途 |
|
|
10
|
+
| ----------------------- | -------------------------------- |
|
|
11
|
+
| `modern.config.ts` | 插件基础设置,在构建/启动时读取 |
|
|
11
12
|
| `src/modern.runtime.ts` | i18next 初始化选项,在运行时读取 |
|
|
12
13
|
|
|
13
14
|
:::warning 函数类型只能在运行时配置中设置
|
|
@@ -16,13 +17,13 @@ title: 配置说明
|
|
|
16
17
|
|
|
17
18
|
## 配置归属一览
|
|
18
19
|
|
|
19
|
-
| 配置项
|
|
20
|
-
|
|
|
21
|
-
| `localeDetection`(语言检测、路径重定向等)
|
|
22
|
-
| `backend`(资源路径、sdk 声明等)
|
|
23
|
-
| `i18nInstance`
|
|
20
|
+
| 配置项 | 放在哪里 |
|
|
21
|
+
| ------------------------------------------------ | ------------------- |
|
|
22
|
+
| `localeDetection`(语言检测、路径重定向等) | `modern.config.ts` |
|
|
23
|
+
| `backend`(资源路径、sdk 声明等) | `modern.config.ts` |
|
|
24
|
+
| `i18nInstance` | `modern.runtime.ts` |
|
|
24
25
|
| `initOptions`(fallbackLng、ns 等 i18next 选项) | `modern.runtime.ts` |
|
|
25
|
-
| `backend.sdk` 的实际加载函数
|
|
26
|
+
| `backend.sdk` 的实际加载函数 | `modern.runtime.ts` |
|
|
26
27
|
|
|
27
28
|
## CLI 配置
|
|
28
29
|
|
|
@@ -43,15 +44,15 @@ export default defineConfig({
|
|
|
43
44
|
|
|
44
45
|
### localeDetection 选项
|
|
45
46
|
|
|
46
|
-
| 字段
|
|
47
|
-
|
|
|
48
|
-
| `languages`
|
|
49
|
-
| `fallbackLanguage`
|
|
50
|
-
| `localePathRedirect`
|
|
51
|
-
| `i18nextDetector`
|
|
52
|
-
| `detection`
|
|
53
|
-
| `ignoreRedirectRoutes`
|
|
54
|
-
| `localeDetectionByEntry` | `Record<string, ...>`
|
|
47
|
+
| 字段 | 类型 | 默认值 | 说明 |
|
|
48
|
+
| ------------------------ | ------------------------- | ------- | ------------------------------------------------------------------------------------ |
|
|
49
|
+
| `languages` | `string[]` | `[]` | 支持的语言列表 |
|
|
50
|
+
| `fallbackLanguage` | `string` | `''` | 语言检测失败时的兜底语言 |
|
|
51
|
+
| `localePathRedirect` | `boolean` | `false` | 接管 URL 语言前缀的识别、重定向和切换,详见[路由集成](./routing.md#启用语言路径前缀) |
|
|
52
|
+
| `i18nextDetector` | `boolean` | `false` | 启用 i18next 检测器(Cookie / Header 等) |
|
|
53
|
+
| `detection` | `LanguageDetectorOptions` | — | i18next 检测器详细配置,见[语言检测](./locale-detection.md) |
|
|
54
|
+
| `ignoreRedirectRoutes` | `string[] \| Function` | — | 跳过重定向的路由,见[语言检测](./locale-detection.md#ignoreredirectroutes) |
|
|
55
|
+
| `localeDetectionByEntry` | `Record<string, ...>` | — | 多入口按入口覆盖配置 |
|
|
55
56
|
|
|
56
57
|
**示例:**
|
|
57
58
|
|
|
@@ -104,23 +105,24 @@ interface BackendOptions {
|
|
|
104
105
|
|
|
105
106
|
**backend 自动启用规则:**
|
|
106
107
|
|
|
107
|
-
| 情况
|
|
108
|
-
|
|
|
109
|
-
| 配置了 `loadPath` 或 `addPath`
|
|
110
|
-
| 未配置 backend,但 `locales` 目录存在且有 JSON 文件 | 自动启用,使用默认 `loadPath`
|
|
111
|
-
| 显式设置 `enabled: false`
|
|
108
|
+
| 情况 | 结果 |
|
|
109
|
+
| --------------------------------------------------- | -------------------------------- |
|
|
110
|
+
| 配置了 `loadPath` 或 `addPath` | 自动启用,无需写 `enabled: true` |
|
|
111
|
+
| 未配置 backend,但 `locales` 目录存在且有 JSON 文件 | 自动启用,使用默认 `loadPath` |
|
|
112
|
+
| 显式设置 `enabled: false` | 禁用,不做自动检测 |
|
|
112
113
|
|
|
113
114
|
自动检测的目录位置(按优先级):
|
|
115
|
+
|
|
114
116
|
1. `{项目根目录}/config/public/locales`
|
|
115
117
|
2. `server.publicDir` 配置的目录下的 `locales` 子目录
|
|
116
118
|
|
|
117
119
|
**选择哪种后端?**
|
|
118
120
|
|
|
119
|
-
| 场景
|
|
120
|
-
|
|
|
121
|
-
| 翻译文件放在项目本地
|
|
122
|
-
| 翻译来自远程 API 或翻译平台 | 自定义后端(配置 `sdk: true`)
|
|
123
|
-
| 本地兜底 + 远程实时更新
|
|
121
|
+
| 场景 | 推荐方式 |
|
|
122
|
+
| --------------------------- | ------------------------------------------------- |
|
|
123
|
+
| 翻译文件放在项目本地 | 静态文件后端(配置 `loadPath`,CSR/SSR 自动切换) |
|
|
124
|
+
| 翻译来自远程 API 或翻译平台 | 自定义后端(配置 `sdk: true`) |
|
|
125
|
+
| 本地兜底 + 远程实时更新 | 链式后端(同时配置 `loadPath` 和 `sdk`) |
|
|
124
126
|
|
|
125
127
|
详细用法见[资源加载](./resource-loading.md)。
|
|
126
128
|
|
|
@@ -134,7 +136,7 @@ const i18nInstance = i18next.createInstance();
|
|
|
134
136
|
|
|
135
137
|
export default defineRuntimeConfig({
|
|
136
138
|
i18n: {
|
|
137
|
-
i18nInstance,
|
|
139
|
+
i18nInstance, // 可选,建议使用独立实例
|
|
138
140
|
initOptions: {
|
|
139
141
|
fallbackLng: 'en',
|
|
140
142
|
supportedLngs: ['zh', 'en'],
|
|
@@ -162,13 +164,13 @@ import Desc from '@site-docs/components/international/init-options-desc';
|
|
|
162
164
|
|
|
163
165
|
<Desc />
|
|
164
166
|
|
|
165
|
-
| 字段
|
|
166
|
-
|
|
|
167
|
-
| `fallbackLng`
|
|
168
|
-
| `supportedLngs` | i18next 支持的语言列表
|
|
169
|
-
| `ns`
|
|
170
|
-
| `defaultNS`
|
|
171
|
-
| `lng`
|
|
167
|
+
| 字段 | 说明 |
|
|
168
|
+
| --------------- | ---------------------------------------------------------------------------- |
|
|
169
|
+
| `fallbackLng` | i18next 找不到翻译键时回退到哪个语言的资源,支持字符串、数组或映射(回退链) |
|
|
170
|
+
| `supportedLngs` | i18next 支持的语言列表 |
|
|
171
|
+
| `ns` | 命名空间列表,默认 `['translation']` |
|
|
172
|
+
| `defaultNS` | 默认命名空间,默认 `'translation'` |
|
|
173
|
+
| `lng` | 强制指定初始语言(通常不需要,由检测器决定) |
|
|
172
174
|
|
|
173
175
|
`fallbackLng` 支持回退链写法,适合区域语言降级的场景:
|
|
174
176
|
|
|
@@ -64,8 +64,8 @@ i18nPlugin({
|
|
|
64
64
|
```ts
|
|
65
65
|
i18nPlugin({
|
|
66
66
|
localeDetection: {
|
|
67
|
-
localePathRedirect: true,
|
|
68
|
-
i18nextDetector: true,
|
|
67
|
+
localePathRedirect: true, // 路径前缀
|
|
68
|
+
i18nextDetector: true, // Cookie / Header 等
|
|
69
69
|
languages: ['zh', 'en'],
|
|
70
70
|
fallbackLanguage: 'en',
|
|
71
71
|
detection: {
|
|
@@ -77,39 +77,48 @@ i18nPlugin({
|
|
|
77
77
|
|
|
78
78
|
## 检测选项(detection)
|
|
79
79
|
|
|
80
|
-
| 选项
|
|
81
|
-
|
|
|
82
|
-
| `order`
|
|
83
|
-
| `lookupQuerystring`
|
|
84
|
-
| `lookupCookie`
|
|
85
|
-
| `lookupLocalStorage`
|
|
86
|
-
| `lookupSession`
|
|
87
|
-
| `lookupHeader`
|
|
88
|
-
| `lookupFromPathIndex`
|
|
89
|
-
| `caches`
|
|
90
|
-
| `cookieMinutes`
|
|
91
|
-
| `cookieExpirationDate` | `Date`
|
|
92
|
-
| `cookieDomain`
|
|
80
|
+
| 选项 | 类型 | 默认值 | 说明 |
|
|
81
|
+
| ---------------------- | ------------------- | ------------------- | ------------------------------------------- |
|
|
82
|
+
| `order` | `string[]` | 见下方 | i18next 检测器内部的检测顺序 |
|
|
83
|
+
| `lookupQuerystring` | `string` | `'lng'` | 查询参数键名,例如 `?lng=en` |
|
|
84
|
+
| `lookupCookie` | `string` | `'i18next'` | Cookie 键名 |
|
|
85
|
+
| `lookupLocalStorage` | `string` | `'i18nextLng'` | LocalStorage 键名(仅浏览器) |
|
|
86
|
+
| `lookupSession` | `string` | — | SessionStorage 键名(仅浏览器) |
|
|
87
|
+
| `lookupHeader` | `string` | `'accept-language'` | HTTP 请求头键名 |
|
|
88
|
+
| `lookupFromPathIndex` | `number` | `0` | 从 URL 路径的第几段开始识别语言 |
|
|
89
|
+
| `caches` | `false \| string[]` | — | 检测到的语言缓存到哪里,如 `['cookie']` |
|
|
90
|
+
| `cookieMinutes` | `number` | `525600`(1年) | Cookie 过期时间(分钟) |
|
|
91
|
+
| `cookieExpirationDate` | `Date` | — | Cookie 过期日期,优先级高于 `cookieMinutes` |
|
|
92
|
+
| `cookieDomain` | `string` | — | Cookie 域名 |
|
|
93
93
|
|
|
94
94
|
**默认检测顺序(未配置 `order` 时):**
|
|
95
95
|
|
|
96
96
|
```ts
|
|
97
|
-
[
|
|
97
|
+
[
|
|
98
|
+
'querystring',
|
|
99
|
+
'cookie',
|
|
100
|
+
'localStorage',
|
|
101
|
+
'header',
|
|
102
|
+
'navigator',
|
|
103
|
+
'htmlTag',
|
|
104
|
+
'path',
|
|
105
|
+
'subdomain',
|
|
106
|
+
];
|
|
98
107
|
```
|
|
99
108
|
|
|
100
109
|
**`order` 可用值:**
|
|
101
110
|
|
|
102
|
-
| 值
|
|
103
|
-
|
|
|
104
|
-
| `querystring`
|
|
105
|
-
| `cookie`
|
|
106
|
-
| `localStorage`
|
|
107
|
-
| `sessionStorage` | SessionStorage
|
|
108
|
-
| `navigator`
|
|
109
|
-
| `htmlTag`
|
|
110
|
-
| `header`
|
|
111
|
-
| `path`
|
|
112
|
-
| `subdomain`
|
|
111
|
+
| 值 | 检测来源 | 限制 |
|
|
112
|
+
| ---------------- | ------------------------------------------- | -------- |
|
|
113
|
+
| `querystring` | URL 查询参数(`?lng=en`) | — |
|
|
114
|
+
| `cookie` | Cookie | — |
|
|
115
|
+
| `localStorage` | LocalStorage | 仅浏览器 |
|
|
116
|
+
| `sessionStorage` | SessionStorage | 仅浏览器 |
|
|
117
|
+
| `navigator` | 浏览器语言设置 | 仅浏览器 |
|
|
118
|
+
| `htmlTag` | `<html lang="...">` | 仅浏览器 |
|
|
119
|
+
| `header` | `Accept-Language` 请求头 | — |
|
|
120
|
+
| `path` | URL 路径(需同时启用 `localePathRedirect`) | — |
|
|
121
|
+
| `subdomain` | 子域名(`en.example.com`) | — |
|
|
113
122
|
|
|
114
123
|
## ignoreRedirectRoutes
|
|
115
124
|
|
|
@@ -118,7 +127,7 @@ i18nPlugin({
|
|
|
118
127
|
**写法一:字符串数组**(支持精确匹配和前缀匹配)
|
|
119
128
|
|
|
120
129
|
```ts
|
|
121
|
-
ignoreRedirectRoutes: ['/api', '/admin', '/static']
|
|
130
|
+
ignoreRedirectRoutes: ['/api', '/admin', '/static'];
|
|
122
131
|
// '/api' 会匹配 /api 和 /api/users
|
|
123
132
|
```
|
|
124
133
|
|
|
@@ -1,21 +1,23 @@
|
|
|
1
1
|
---
|
|
2
2
|
title: 资源加载
|
|
3
3
|
---
|
|
4
|
+
|
|
4
5
|
# 资源加载
|
|
5
6
|
|
|
6
7
|
翻译文件的加载方式取决于你的翻译资源放在哪里:
|
|
7
8
|
|
|
8
|
-
| 场景
|
|
9
|
-
|
|
|
10
|
-
| 翻译文件放在项目本地
|
|
11
|
-
| 翻译来自远程 API、翻译平台
|
|
12
|
-
| 两者都要:本地兜底 + 远程实时更新 | 链式后端
|
|
9
|
+
| 场景 | 加载方式 |
|
|
10
|
+
| --------------------------------- | ---------------------------- |
|
|
11
|
+
| 翻译文件放在项目本地 | 静态文件后端(本篇默认方式) |
|
|
12
|
+
| 翻译来自远程 API、翻译平台 | 自定义后端 |
|
|
13
|
+
| 两者都要:本地兜底 + 远程实时更新 | 链式后端 |
|
|
13
14
|
|
|
14
15
|
## 静态文件后端
|
|
15
16
|
|
|
16
17
|
把翻译文件放在项目里,插件直接加载。这是最常见的方式。
|
|
17
18
|
|
|
18
19
|
**CSR 和 SSR 的加载机制不同,但配置完全一样:**
|
|
20
|
+
|
|
19
21
|
- CSR:浏览器通过 HTTP 请求拉取翻译文件(如 `GET /locales/zh/translation.json`)
|
|
20
22
|
- SSR:服务端直接从磁盘读取文件,不走 HTTP
|
|
21
23
|
|
|
@@ -100,11 +102,11 @@ export default defineRuntimeConfig({
|
|
|
100
102
|
|
|
101
103
|
```ts
|
|
102
104
|
interface I18nSdkLoadOptions {
|
|
103
|
-
lng?: string;
|
|
104
|
-
ns?: string;
|
|
105
|
-
lngs?: string[];
|
|
106
|
-
nss?: string[];
|
|
107
|
-
all?: boolean;
|
|
105
|
+
lng?: string; // 单个语言代码
|
|
106
|
+
ns?: string; // 单个命名空间
|
|
107
|
+
lngs?: string[]; // 多个语言代码
|
|
108
|
+
nss?: string[]; // 多个命名空间
|
|
109
|
+
all?: boolean; // 加载所有资源
|
|
108
110
|
}
|
|
109
111
|
```
|
|
110
112
|
|
|
@@ -122,7 +124,7 @@ interface I18nSdkLoadOptions {
|
|
|
122
124
|
i18nPlugin({
|
|
123
125
|
backend: {
|
|
124
126
|
loadPath: '/locales/{{lng}}/{{ns}}.json', // 本地文件,快速显示
|
|
125
|
-
sdk: true,
|
|
127
|
+
sdk: true, // 远程加载,异步更新
|
|
126
128
|
},
|
|
127
129
|
});
|
|
128
130
|
```
|
|
@@ -135,7 +137,9 @@ export default defineRuntimeConfig({
|
|
|
135
137
|
backend: {
|
|
136
138
|
sdk: async options => {
|
|
137
139
|
if (options.lng && options.ns) {
|
|
138
|
-
const res = await fetch(
|
|
140
|
+
const res = await fetch(
|
|
141
|
+
`https://api.example.com/i18n/${options.lng}/${options.ns}`,
|
|
142
|
+
);
|
|
139
143
|
return { [options.lng]: { [options.ns]: await res.json() } };
|
|
140
144
|
}
|
|
141
145
|
return {};
|
|
@@ -148,8 +152,8 @@ export default defineRuntimeConfig({
|
|
|
148
152
|
|
|
149
153
|
**`cacheHitMode` 选项**(控制两个后端之间的协作方式):
|
|
150
154
|
|
|
151
|
-
| 值
|
|
152
|
-
|
|
|
153
|
-
| `'none'`
|
|
154
|
-
| `'refresh'`
|
|
155
|
+
| 值 | 行为 |
|
|
156
|
+
| ------------------------- | ------------------------------------------------------ |
|
|
157
|
+
| `'none'` | 本地文件加载成功后直接使用,不再请求自定义后端 |
|
|
158
|
+
| `'refresh'` | 继续请求自定义后端并更新缓存,但不更新当前页面 |
|
|
155
159
|
| `'refreshAndUpdateStore'` | 继续请求自定义后端,更新缓存并同步刷新页面文案(默认) |
|