@intlayer/docs 7.0.4-canary.0 → 7.0.5
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/blog/ar/intlayer_with_i18next.md +68 -106
- package/blog/ar/intlayer_with_next-i18next.md +84 -288
- package/blog/ar/intlayer_with_next-intl.md +58 -337
- package/blog/ar/intlayer_with_react-i18next.md +68 -290
- package/blog/ar/intlayer_with_react-intl.md +63 -266
- package/blog/de/intlayer_with_i18next.md +77 -97
- package/blog/de/intlayer_with_next-i18next.md +69 -296
- package/blog/de/intlayer_with_next-intl.md +59 -340
- package/blog/de/intlayer_with_react-i18next.md +68 -290
- package/blog/de/intlayer_with_react-intl.md +62 -264
- package/blog/en/intlayer_with_i18next.md +36 -1638
- package/blog/en/intlayer_with_next-i18next.md +22 -847
- package/blog/en/intlayer_with_next-intl.md +32 -1053
- package/blog/en/intlayer_with_react-i18next.md +38 -764
- package/blog/en/intlayer_with_react-intl.md +42 -1018
- package/blog/en-GB/intlayer_with_i18next.md +67 -103
- package/blog/en-GB/intlayer_with_next-i18next.md +71 -292
- package/blog/en-GB/intlayer_with_next-intl.md +58 -337
- package/blog/en-GB/intlayer_with_react-i18next.md +67 -289
- package/blog/en-GB/intlayer_with_react-intl.md +61 -264
- package/blog/es/intlayer_with_i18next.md +67 -103
- package/blog/es/intlayer_with_next-i18next.md +71 -296
- package/blog/es/intlayer_with_next-intl.md +57 -338
- package/blog/es/intlayer_with_react-i18next.md +68 -290
- package/blog/es/intlayer_with_react-intl.md +62 -265
- package/blog/fr/intlayer_with_i18next.md +66 -104
- package/blog/fr/intlayer_with_next-i18next.md +82 -285
- package/blog/fr/intlayer_with_next-intl.md +57 -338
- package/blog/fr/intlayer_with_react-i18next.md +67 -289
- package/blog/fr/intlayer_with_react-intl.md +61 -264
- package/blog/hi/intlayer_with_i18next.md +68 -104
- package/blog/hi/intlayer_with_next-i18next.md +74 -299
- package/blog/hi/intlayer_with_next-intl.md +57 -239
- package/blog/hi/intlayer_with_react-i18next.md +69 -291
- package/blog/hi/intlayer_with_react-intl.md +65 -268
- package/blog/id/intlayer_with_i18next.md +126 -0
- package/blog/id/intlayer_with_next-i18next.md +142 -0
- package/blog/id/intlayer_with_next-intl.md +113 -0
- package/blog/id/intlayer_with_react-i18next.md +124 -0
- package/blog/id/intlayer_with_react-intl.md +122 -0
- package/blog/it/intlayer_with_i18next.md +67 -103
- package/blog/it/intlayer_with_next-i18next.md +71 -296
- package/blog/it/intlayer_with_next-intl.md +57 -338
- package/blog/it/intlayer_with_react-i18next.md +68 -290
- package/blog/it/intlayer_with_react-intl.md +62 -265
- package/blog/ja/intlayer_with_i18next.md +68 -103
- package/blog/ja/intlayer_with_next-i18next.md +85 -283
- package/blog/ja/intlayer_with_next-intl.md +58 -336
- package/blog/ja/intlayer_with_react-i18next.md +68 -290
- package/blog/ja/intlayer_with_react-intl.md +62 -264
- package/blog/ko/intlayer_with_i18next.md +80 -96
- package/blog/ko/intlayer_with_next-i18next.md +85 -287
- package/blog/ko/intlayer_with_next-intl.md +68 -327
- package/blog/ko/intlayer_with_react-i18next.md +68 -290
- package/blog/ko/intlayer_with_react-intl.md +64 -266
- package/blog/pl/intlayer_with_i18next.md +126 -0
- package/blog/pl/intlayer_with_next-i18next.md +142 -0
- package/blog/pl/intlayer_with_next-intl.md +111 -0
- package/blog/pl/intlayer_with_react-i18next.md +124 -0
- package/blog/pl/intlayer_with_react-intl.md +122 -0
- package/blog/pt/intlayer_with_i18next.md +67 -103
- package/blog/pt/intlayer_with_next-i18next.md +72 -293
- package/blog/pt/intlayer_with_next-intl.md +57 -256
- package/blog/pt/intlayer_with_react-i18next.md +104 -78
- package/blog/pt/intlayer_with_react-intl.md +62 -266
- package/blog/ru/intlayer_with_i18next.md +66 -104
- package/blog/ru/intlayer_with_next-i18next.md +71 -296
- package/blog/ru/intlayer_with_next-intl.md +58 -337
- package/blog/ru/intlayer_with_react-i18next.md +68 -290
- package/blog/ru/intlayer_with_react-intl.md +62 -265
- package/blog/tr/intlayer_with_i18next.md +71 -107
- package/blog/tr/intlayer_with_next-i18next.md +72 -297
- package/blog/tr/intlayer_with_next-intl.md +58 -339
- package/blog/tr/intlayer_with_react-i18next.md +69 -291
- package/blog/tr/intlayer_with_react-intl.md +63 -285
- package/blog/vi/intlayer_with_i18next.md +126 -0
- package/blog/vi/intlayer_with_next-i18next.md +142 -0
- package/blog/vi/intlayer_with_next-intl.md +111 -0
- package/blog/vi/intlayer_with_react-i18next.md +124 -0
- package/blog/vi/intlayer_with_react-intl.md +122 -0
- package/blog/zh/intlayer_with_i18next.md +67 -102
- package/blog/zh/intlayer_with_next-i18next.md +72 -296
- package/blog/zh/intlayer_with_next-intl.md +58 -336
- package/blog/zh/intlayer_with_react-i18next.md +68 -290
- package/blog/zh/intlayer_with_react-intl.md +63 -106
- package/docs/ar/plugins/sync-json.md +244 -0
- package/docs/de/plugins/sync-json.md +244 -0
- package/docs/en/intlayer_cli.md +25 -0
- package/docs/en/intlayer_with_nextjs_14.md +2 -0
- package/docs/en/intlayer_with_nextjs_15.md +2 -0
- package/docs/en/intlayer_with_nextjs_16.md +2 -0
- package/docs/en/plugins/sync-json.md +1 -1
- package/docs/en-GB/plugins/sync-json.md +244 -0
- package/docs/es/plugins/sync-json.md +244 -0
- package/docs/fr/plugins/sync-json.md +244 -0
- package/docs/hi/plugins/sync-json.md +244 -0
- package/docs/id/plugins/sync-json.md +244 -0
- package/docs/it/plugins/sync-json.md +244 -0
- package/docs/ja/plugins/sync-json.md +244 -0
- package/docs/ko/plugins/sync-json.md +244 -0
- package/docs/pl/plugins/sync-json.md +244 -0
- package/docs/pt/plugins/sync-json.md +244 -0
- package/docs/ru/plugins/sync-json.md +244 -0
- package/docs/tr/plugins/sync-json.md +245 -0
- package/docs/vi/plugins/sync-json.md +244 -0
- package/docs/zh/plugins/sync-json.md +244 -0
- package/package.json +14 -14
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
---
|
|
2
2
|
createdAt: 2024-12-24
|
|
3
|
-
updatedAt: 2025-
|
|
4
|
-
title: Intlayer
|
|
5
|
-
description:
|
|
3
|
+
updatedAt: 2025-10-29
|
|
4
|
+
title: 如何使用 Intlayer 自动化您的 i18next JSON 翻译
|
|
5
|
+
description: 使用 Intlayer 和 i18next 自动化您的 JSON 翻译,提升 JavaScript 应用程序的国际化水平。
|
|
6
6
|
keywords:
|
|
7
7
|
- Intlayer
|
|
8
8
|
- i18next
|
|
@@ -14,149 +14,114 @@ keywords:
|
|
|
14
14
|
- Next.js
|
|
15
15
|
- JavaScript
|
|
16
16
|
- TypeScript
|
|
17
|
+
- 迁移
|
|
18
|
+
- 集成
|
|
17
19
|
slugs:
|
|
18
20
|
- blog
|
|
19
21
|
- intlayer-with-i18next
|
|
22
|
+
history:
|
|
23
|
+
- version: 7.0.0
|
|
24
|
+
date: 2025-10-29
|
|
25
|
+
changes: 更改为 syncJSON 插件
|
|
20
26
|
---
|
|
21
27
|
|
|
22
|
-
#
|
|
28
|
+
# 如何使用 Intlayer 自动化您的 i18next JSON 翻译
|
|
23
29
|
|
|
24
|
-
|
|
30
|
+
## 什么是 Intlayer?
|
|
25
31
|
|
|
26
|
-
Intlayer
|
|
32
|
+
**Intlayer** 是一个创新的开源国际化库,旨在解决传统 i18n 解决方案的不足。它为 JavaScript 应用程序中的内容管理提供了一种现代化的方法。
|
|
27
33
|
|
|
28
|
-
|
|
34
|
+
请参阅我们博客文章中的具体比较:[next-i18next vs. next-intl vs. Intlayer](https://github.com/aymericzip/intlayer/blob/main/docs/blog/zh/next-i18next_vs_next-intl_vs_intlayer.md)。
|
|
29
35
|
|
|
30
|
-
|
|
36
|
+
## 为什么要将 Intlayer 与 i18next 结合使用?
|
|
31
37
|
|
|
32
|
-
|
|
38
|
+
虽然 Intlayer 提供了一个出色的独立 i18n 解决方案(请参阅我们的[Next.js 集成指南](https://github.com/aymericzip/intlayer/blob/main/docs/docs/zh/intlayer_with_nextjs_16.md)),但您可能出于以下几个原因想将其与 i18next 结合使用:
|
|
33
39
|
|
|
34
|
-
|
|
35
|
-
|
|
40
|
+
1. **现有代码库**:您已经有一个成熟的 i18next 实现,并希望逐步迁移到 Intlayer 以获得更好的开发者体验。
|
|
41
|
+
2. **遗留需求**:您的项目需要兼容现有的 i18next 插件或工作流程。
|
|
42
|
+
3. **团队熟悉度**:您的团队熟悉 i18next,但希望获得更好的内容管理。
|
|
36
43
|
|
|
37
|
-
|
|
44
|
+
**为此,Intlayer 可以作为 i18next 的适配器来实现,帮助您在 CLI 或 CI/CD 流水线中自动化 JSON 翻译,测试翻译内容等。**
|
|
38
45
|
|
|
39
|
-
|
|
46
|
+
本指南将向您展示如何利用 Intlayer 优越的内容声明系统,同时保持与 i18next 的兼容性。
|
|
40
47
|
|
|
41
|
-
|
|
48
|
+
## 目录
|
|
42
49
|
|
|
43
|
-
|
|
50
|
+
<TOC/>
|
|
44
51
|
|
|
45
|
-
|
|
52
|
+
## 使用 i18next 设置 Intlayer 的分步指南
|
|
46
53
|
|
|
47
|
-
|
|
54
|
+
### 第一步:安装依赖项
|
|
48
55
|
|
|
49
|
-
|
|
56
|
+
安装必要的包:
|
|
50
57
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
58
|
+
```bash packageManager="npm"
|
|
59
|
+
npm install intlayer @intlayer/sync-json-plugin
|
|
60
|
+
```
|
|
54
61
|
|
|
55
|
-
|
|
62
|
+
```bash packageManager="pnpm"
|
|
63
|
+
pnpm add intlayer @intlayer/sync-json-plugin
|
|
64
|
+
```
|
|
56
65
|
|
|
57
|
-
|
|
66
|
+
```bash packageManager="yarn"
|
|
67
|
+
yarn add intlayer @intlayer/sync-json-plugin
|
|
68
|
+
```
|
|
58
69
|
|
|
59
|
-
|
|
70
|
+
**包描述:**
|
|
60
71
|
|
|
61
|
-
|
|
72
|
+
- **intlayer**:用于国际化管理、内容声明和构建的核心库
|
|
73
|
+
- **@intlayer/sync-json-plugin**:用于将 Intlayer 内容声明导出为兼容 i18next 的 JSON 格式的插件
|
|
62
74
|
|
|
63
|
-
|
|
64
|
-
import { Locales, type IntlayerConfig } from "intlayer";
|
|
75
|
+
### 步骤 2:实现 Intlayer 插件以包装 JSON
|
|
65
76
|
|
|
66
|
-
|
|
67
|
-
content: {
|
|
68
|
-
// 指示 Intlayer 将导出 Intlayer 和 i18next 字典
|
|
69
|
-
dictionaryOutput: ["intlayer", "i18next"],
|
|
70
|
-
// 从项目根目录到导出 i18n 字典的目录的相对路径
|
|
71
|
-
i18nextResourcesDir: "./i18next/dictionaries",
|
|
72
|
-
},
|
|
73
|
-
};
|
|
77
|
+
创建一个 Intlayer 配置文件以定义支持的语言环境:
|
|
74
78
|
|
|
75
|
-
|
|
76
|
-
```
|
|
79
|
+
**如果你还想导出 i18next 的 JSON 词典**,请添加 `syncJSON` 插件:
|
|
77
80
|
|
|
78
|
-
```
|
|
79
|
-
import { Locales } from "intlayer";
|
|
81
|
+
```typescript fileName="intlayer.config.ts"
|
|
82
|
+
import { Locales, type IntlayerConfig } from "intlayer";
|
|
83
|
+
import { syncJSON } from "@intlayer/sync-json-plugin";
|
|
80
84
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
dictionaryOutput: ["intlayer", "i18next"],
|
|
86
|
-
// 从项目根目录到导出 i18n 字典的目录的相对路径
|
|
87
|
-
i18nextResourcesDir: "./i18next/dictionaries",
|
|
85
|
+
const config: IntlayerConfig = {
|
|
86
|
+
internationalization: {
|
|
87
|
+
locales: [Locales.ENGLISH, Locales.FRENCH, Locales.SPANISH],
|
|
88
|
+
defaultLocale: Locales.ENGLISH,
|
|
88
89
|
},
|
|
90
|
+
plugins: [
|
|
91
|
+
syncJSON({
|
|
92
|
+
source: ({ key, locale }) => `./intl/messages/${locale}/${key}.json`,
|
|
93
|
+
}),
|
|
94
|
+
],
|
|
89
95
|
};
|
|
90
96
|
|
|
91
97
|
export default config;
|
|
92
98
|
```
|
|
93
99
|
|
|
94
|
-
|
|
95
|
-
const { Locales } = require("intlayer");
|
|
100
|
+
`syncJSON` 插件会自动包装 JSON。它会读取和写入 JSON 文件,而不会改变内容结构。
|
|
96
101
|
|
|
97
|
-
|
|
98
|
-
const config = {
|
|
99
|
-
content: {
|
|
100
|
-
// 指示 Intlayer 将导出 Intlayer 和 i18next 字典
|
|
101
|
-
dictionaryOutput: ["intlayer", "i18next"],
|
|
102
|
-
// 从项目根目录到导出 i18n 字典的目录的相对路径
|
|
103
|
-
i18nextResourcesDir: "./i18next/dictionaries",
|
|
104
|
-
},
|
|
105
|
-
};
|
|
102
|
+
如果你想让 JSON 与 intlayer 内容声明文件(`.content` 文件)共存,Intlayer 会按以下方式处理:
|
|
106
103
|
|
|
107
|
-
|
|
108
|
-
```
|
|
104
|
+
1. 加载 JSON 和内容声明文件,并将它们转换为 intlayer 字典。
|
|
109
105
|
|
|
110
|
-
|
|
106
|
+
2. 如果 JSON 文件和内容声明文件之间存在冲突,Intlayer 将合并所有字典。合并的优先级取决于插件的优先级以及内容声明文件的优先级(所有这些都是可配置的)。
|
|
111
107
|
|
|
112
|
-
|
|
108
|
+
如果使用 CLI 翻译 JSON,或使用 CMS 进行更改,Intlayer 会使用新的翻译更新 JSON 文件。
|
|
113
109
|
|
|
114
|
-
|
|
110
|
+
## Git 配置
|
|
115
111
|
|
|
116
|
-
|
|
117
|
-
// i18n/client.ts
|
|
112
|
+
建议忽略自动生成的 Intlayer 文件:
|
|
118
113
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
i18next
|
|
123
|
-
// 您的 i18next 配置
|
|
124
|
-
.use(
|
|
125
|
-
resourcesToBackend(
|
|
126
|
-
(language: string, namespace: string) =>
|
|
127
|
-
import(`../i18next/dictionaries/${language}/${namespace}.json`)
|
|
128
|
-
)
|
|
129
|
-
);
|
|
114
|
+
```plaintext fileName=".gitignore"
|
|
115
|
+
# 忽略 Intlayer 生成的文件
|
|
116
|
+
.intlayer
|
|
130
117
|
```
|
|
131
118
|
|
|
132
|
-
|
|
133
|
-
// i18n/client.mjs
|
|
134
|
-
|
|
135
|
-
import i18next from "i18next";
|
|
136
|
-
import resourcesToBackend from "i18next-resources-to-backend";
|
|
119
|
+
这些文件可以在构建过程中重新生成,无需提交到版本控制。
|
|
137
120
|
|
|
138
|
-
|
|
139
|
-
// 您的 i18next 配置
|
|
140
|
-
.use(
|
|
141
|
-
resourcesToBackend(
|
|
142
|
-
(language, namespace) =>
|
|
143
|
-
import(`../i18next/dictionaries/${language}/${namespace}.json`)
|
|
144
|
-
)
|
|
145
|
-
);
|
|
146
|
-
```
|
|
121
|
+
### VS Code 扩展
|
|
147
122
|
|
|
148
|
-
|
|
149
|
-
// i18n/client.cjs
|
|
123
|
+
为了提升开发者体验,请安装官方的 **Intlayer VS Code 扩展**:
|
|
150
124
|
|
|
151
|
-
|
|
152
|
-
const resourcesToBackend = require("i18next-resources-to-backend");
|
|
125
|
+
[从 VS Code 市场安装](https://marketplace.visualstudio.com/items?itemName=intlayer.intlayer-vs-code-extension)
|
|
153
126
|
|
|
154
|
-
|
|
155
|
-
// 您的 i18next 配置
|
|
156
|
-
.use(
|
|
157
|
-
resourcesToBackend(
|
|
158
|
-
(language, namespace) =>
|
|
159
|
-
import(`../i18next/dictionaries/${language}/${namespace}.json`)
|
|
160
|
-
)
|
|
161
|
-
);
|
|
162
|
-
```
|
|
127
|
+
[从 VS Code 市场安装](https://marketplace.visualstudio.com/items?itemName=intlayer.intlayer-vs-code-extension)
|
|
@@ -1,367 +1,143 @@
|
|
|
1
1
|
---
|
|
2
2
|
createdAt: 2025-08-23
|
|
3
|
-
updatedAt: 2025-
|
|
4
|
-
title: Intlayer
|
|
5
|
-
description: 与Next.js
|
|
3
|
+
updatedAt: 2025-10-29
|
|
4
|
+
title: Intlayer 与 next-i18next
|
|
5
|
+
description: 将 Intlayer 与 next-i18next 集成,实现全面的 Next.js 国际化解决方案
|
|
6
6
|
keywords:
|
|
7
7
|
- i18next
|
|
8
8
|
- next-i18next
|
|
9
9
|
- Intlayer
|
|
10
10
|
- 国际化
|
|
11
|
-
-
|
|
11
|
+
- 博客
|
|
12
12
|
- Next.js
|
|
13
13
|
- JavaScript
|
|
14
14
|
- React
|
|
15
15
|
slugs:
|
|
16
16
|
- blog
|
|
17
17
|
- intlayer-with-next-i18next
|
|
18
|
+
history:
|
|
19
|
+
- version: 7.0.0
|
|
20
|
+
date: 2025-10-29
|
|
21
|
+
changes: 更改为 syncJSON 插件并全面重写
|
|
18
22
|
---
|
|
19
23
|
|
|
20
|
-
#
|
|
24
|
+
# 使用 next-i18next 和 Intlayer 实现 Next.js 国际化(i18n)
|
|
21
25
|
|
|
22
|
-
|
|
26
|
+
## 目录
|
|
23
27
|
|
|
24
|
-
|
|
28
|
+
<TOC/>
|
|
25
29
|
|
|
26
|
-
|
|
27
|
-
- 在 `i18next` 的情况下称为 `resource`,内容声明是一个结构化的 JSON 对象,包含一种或多种语言翻译的键值对。有关更多信息,请参阅 [i18next 文档](https://www.i18next.com/translation-function/essentials)。
|
|
28
|
-
- 在 `Intlayer` 的情况下称为 `content declaration file`,内容声明可以是导出结构化数据的 JSON、JS 或 TS 文件。有关更多信息,请参阅 [Intlayer 文档](https://intlayer.org/fr/doc/concept/content)。
|
|
30
|
+
## 什么是 next-i18next?
|
|
29
31
|
|
|
30
|
-
|
|
32
|
+
**next-i18next** 是 Next.js 应用中最流行的国际化(i18n)框架之一。它构建于强大的 **i18next** 生态系统之上,为管理 Next.js 项目的翻译、本地化和语言切换提供了全面的解决方案。
|
|
31
33
|
|
|
32
|
-
|
|
34
|
+
然而,next-i18next 也存在一些挑战:
|
|
33
35
|
|
|
34
|
-
|
|
36
|
+
- **配置复杂**:设置 next-i18next 需要多个配置文件,并且需要仔细设置服务器端和客户端的 i18n 实例。
|
|
37
|
+
- **翻译分散**:翻译文件通常存储在与组件分开的目录中,导致维护一致性更加困难。
|
|
38
|
+
- **手动命名空间管理**:开发者需要手动管理命名空间,并确保正确加载翻译资源。
|
|
39
|
+
- **类型安全有限**:TypeScript 支持需要额外配置,并且不提供翻译的自动类型生成。
|
|
35
40
|
|
|
36
|
-
|
|
41
|
+
## 什么是 Intlayer?
|
|
37
42
|
|
|
38
|
-
|
|
43
|
+
**Intlayer** 是一个创新的开源国际化库,旨在解决传统 i18n 解决方案的不足。它为 Next.js 应用提供了一种现代化的内容管理方法。
|
|
39
44
|
|
|
40
|
-
|
|
45
|
+
请参阅我们博客文章中的具体对比:[next-i18next vs. next-intl vs. Intlayer](https://github.com/aymericzip/intlayer/blob/main/docs/blog/zh/next-i18next_vs_next-intl_vs_intlayer.md)。
|
|
41
46
|
|
|
42
|
-
Intlayer
|
|
47
|
+
## 为什么将 Intlayer 与 next-i18next 结合使用?
|
|
43
48
|
|
|
44
|
-
|
|
49
|
+
虽然 Intlayer 提供了一个出色的独立 i18n 解决方案(请参阅我们的[Next.js 集成指南](https://github.com/aymericzip/intlayer/blob/main/docs/docs/zh/intlayer_with_nextjs_16.md)),但您可能出于以下几个原因希望将其与 next-i18next 结合使用:
|
|
45
50
|
|
|
46
|
-
|
|
51
|
+
1. **现有代码库**:您已经有一个成熟的 next-i18next 实现,并希望逐步迁移到 Intlayer 以获得更好的开发者体验。
|
|
52
|
+
2. **遗留需求**:您的项目需要兼容现有的 i18next 插件或工作流程。
|
|
53
|
+
3. **团队熟悉度**:您的团队对 next-i18next 很熟悉,但希望获得更好的内容管理。
|
|
47
54
|
|
|
48
|
-
|
|
49
|
-
.
|
|
50
|
-
└── src
|
|
51
|
-
└── components
|
|
52
|
-
└── MyComponent
|
|
53
|
-
├── index.content.ts # 内容声明文件
|
|
54
|
-
└── index.tsx
|
|
55
|
-
```
|
|
55
|
+
**为此,Intlayer 可以作为 next-i18next 的适配器来实现,帮助在 CLI 或 CI/CD 流水线中自动化您的 JSON 翻译,测试您的翻译内容等。**
|
|
56
56
|
|
|
57
|
-
|
|
58
|
-
.
|
|
59
|
-
└── src
|
|
60
|
-
└── components
|
|
61
|
-
└── MyComponent
|
|
62
|
-
├── index.content.mjs # 内容声明文件
|
|
63
|
-
└── index.mjx
|
|
64
|
-
```
|
|
57
|
+
本指南将向您展示如何利用 Intlayer 优越的内容声明系统,同时保持与 next-i18next 的兼容性。
|
|
65
58
|
|
|
66
|
-
|
|
67
|
-
.
|
|
68
|
-
└── src
|
|
69
|
-
└── components
|
|
70
|
-
└── MyComponent
|
|
71
|
-
├── index.content.cjs # 内容声明文件
|
|
72
|
-
└── index.cjx
|
|
73
|
-
```
|
|
59
|
+
---
|
|
74
60
|
|
|
75
|
-
|
|
76
|
-
.
|
|
77
|
-
└── src
|
|
78
|
-
└── components
|
|
79
|
-
└── MyComponent
|
|
80
|
-
├── index.content.json # 内容声明文件
|
|
81
|
-
└── index.jsx
|
|
82
|
-
```
|
|
61
|
+
## 使用 next-i18next 设置 Intlayer 的分步指南
|
|
83
62
|
|
|
84
|
-
|
|
63
|
+
### 第一步:安装依赖
|
|
85
64
|
|
|
86
|
-
|
|
65
|
+
使用您喜欢的包管理器安装所需的包:
|
|
87
66
|
|
|
88
67
|
```bash packageManager="npm"
|
|
89
|
-
npm install intlayer
|
|
68
|
+
npm install intlayer @intlayer/sync-json-plugin
|
|
90
69
|
```
|
|
91
70
|
|
|
92
|
-
```bash packageManager="
|
|
93
|
-
|
|
71
|
+
```bash packageManager="pnpm"
|
|
72
|
+
pnpm add intlayer @intlayer/sync-json-plugin
|
|
94
73
|
```
|
|
95
74
|
|
|
96
|
-
```bash packageManager="
|
|
97
|
-
|
|
75
|
+
```bash packageManager="yarn"
|
|
76
|
+
yarn add intlayer @intlayer/sync-json-plugin
|
|
98
77
|
```
|
|
99
78
|
|
|
100
|
-
|
|
79
|
+
**包说明:**
|
|
80
|
+
|
|
81
|
+
- **intlayer**:内容声明和管理的核心库
|
|
82
|
+
- **next-intlayer**:带有构建插件的 Next.js 集成层
|
|
83
|
+
- **i18next**:核心国际化框架
|
|
84
|
+
- **next-i18next**:i18next 的 Next.js 封装器
|
|
85
|
+
- **i18next-resources-to-backend**:i18next 的动态资源加载
|
|
86
|
+
- **@intlayer/sync-json-plugin**:将 Intlayer 内容声明同步为 i18next JSON 格式的插件
|
|
101
87
|
|
|
102
|
-
|
|
88
|
+
### 第2步:实现 Intlayer 插件以封装 JSON
|
|
103
89
|
|
|
104
|
-
|
|
90
|
+
创建一个 Intlayer 配置文件以定义支持的语言环境:
|
|
105
91
|
|
|
106
|
-
|
|
92
|
+
**如果你还想为 i18next 导出 JSON 词典**,请添加 `syncJSON` 插件:
|
|
93
|
+
|
|
94
|
+
```typescript fileName="intlayer.config.ts"
|
|
107
95
|
import { Locales, type IntlayerConfig } from "intlayer";
|
|
96
|
+
import { syncJSON } from "@intlayer/sync-json-plugin";
|
|
108
97
|
|
|
109
98
|
const config: IntlayerConfig = {
|
|
110
99
|
internationalization: {
|
|
111
100
|
locales: [Locales.ENGLISH, Locales.FRENCH, Locales.SPANISH],
|
|
112
101
|
defaultLocale: Locales.ENGLISH,
|
|
113
102
|
},
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
103
|
+
plugins: [
|
|
104
|
+
syncJSON({
|
|
105
|
+
source: ({ key, locale }) => `./messages/${locale}/${key}.json`,
|
|
106
|
+
}),
|
|
107
|
+
],
|
|
118
108
|
};
|
|
119
109
|
|
|
120
110
|
export default config;
|
|
121
111
|
```
|
|
122
112
|
|
|
123
|
-
|
|
124
|
-
import { Locales } from "intlayer";
|
|
125
|
-
|
|
126
|
-
/** @type {import('intlayer').IntlayerConfig} */
|
|
127
|
-
const config = {
|
|
128
|
-
internationalization: {
|
|
129
|
-
locales: [Locales.ENGLISH, Locales.FRENCH, Locales.SPANISH],
|
|
130
|
-
defaultLocale: Locales.ENGLISH,
|
|
131
|
-
},
|
|
132
|
-
content: {
|
|
133
|
-
dictionaryOutput: ["i18next"],
|
|
134
|
-
i18nextResourcesDir: "./i18next/resources",
|
|
135
|
-
},
|
|
136
|
-
};
|
|
137
|
-
|
|
138
|
-
export default config;
|
|
139
|
-
```
|
|
113
|
+
`syncJSON` 插件会自动包装 JSON。它会读取和写入 JSON 文件,而不会改变内容结构。
|
|
140
114
|
|
|
141
|
-
|
|
142
|
-
const { Locales } = require("intlayer");
|
|
115
|
+
如果你想让 JSON 与 intlayer 内容声明文件(`.content` 文件)共存,Intlayer 会按以下方式处理:
|
|
143
116
|
|
|
144
|
-
|
|
145
|
-
const config = {
|
|
146
|
-
internationalization: {
|
|
147
|
-
locales: [Locales.ENGLISH, Locales.FRENCH, Locales.SPANISH],
|
|
148
|
-
defaultLocale: Locales.ENGLISH,
|
|
149
|
-
},
|
|
150
|
-
content: {
|
|
151
|
-
dictionaryOutput: ["i18next"],
|
|
152
|
-
i18nextResourcesDir: "./i18next/resources",
|
|
153
|
-
},
|
|
154
|
-
};
|
|
117
|
+
1. 加载 JSON 和内容声明文件,并将它们转换为 intlayer 字典。
|
|
155
118
|
|
|
156
|
-
|
|
157
|
-
```
|
|
119
|
+
2. 如果 JSON 文件和内容声明文件之间存在冲突,Intlayer 将合并所有字典。合并的优先级取决于插件的优先级以及内容声明文件的优先级(所有这些都是可配置的)。
|
|
158
120
|
|
|
159
|
-
|
|
121
|
+
如果使用 CLI 翻译 JSON,或使用 CMS 进行更改,Intlayer 将使用新的翻译更新 JSON 文件。
|
|
160
122
|
|
|
161
123
|
---
|
|
162
124
|
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
要将生成的资源导入到您的 i18next 配置中,请使用 `i18next-resources-to-backend`。以下是示例:
|
|
166
|
-
|
|
167
|
-
```typescript fileName="i18n/client.ts" codeFormat="typescript"
|
|
168
|
-
import i18next from "i18next";
|
|
169
|
-
import resourcesToBackend from "i18next-resources-to-backend";
|
|
170
|
-
|
|
171
|
-
i18next.use(
|
|
172
|
-
resourcesToBackend(
|
|
173
|
-
(language: string, namespace: string) =>
|
|
174
|
-
import(`../i18next/resources/${language}/${namespace}.json`)
|
|
175
|
-
)
|
|
176
|
-
);
|
|
177
|
-
```
|
|
178
|
-
|
|
179
|
-
```javascript fileName="i18n/client.mjs" codeFormat="esm"
|
|
180
|
-
import i18next from "i18next";
|
|
181
|
-
import resourcesToBackend from "i18next-resources-to-backend";
|
|
182
|
-
|
|
183
|
-
i18next.use(
|
|
184
|
-
resourcesToBackend(
|
|
185
|
-
(language, namespace) =>
|
|
186
|
-
import(`../i18next/resources/${language}/${namespace}.json`)
|
|
187
|
-
)
|
|
188
|
-
);
|
|
189
|
-
```
|
|
125
|
+
## Git 配置
|
|
190
126
|
|
|
191
|
-
|
|
192
|
-
const i18next = require("i18next");
|
|
193
|
-
const resourcesToBackend = require("i18next-resources-to-backend");
|
|
127
|
+
将生成的文件排除在版本控制之外:
|
|
194
128
|
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
)
|
|
200
|
-
);
|
|
129
|
+
```plaintext fileName=".gitignore"
|
|
130
|
+
# 忽略 Intlayer 生成的文件
|
|
131
|
+
.intlayer
|
|
132
|
+
intl
|
|
201
133
|
```
|
|
202
134
|
|
|
203
|
-
|
|
135
|
+
这些文件会在构建过程中自动重新生成,无需提交到您的代码仓库。
|
|
204
136
|
|
|
205
|
-
|
|
137
|
+
### VS Code 扩展
|
|
206
138
|
|
|
207
|
-
|
|
208
|
-
import { t, type Dictionary } from "intlayer";
|
|
139
|
+
为了提升开发者体验,请安装官方的 **Intlayer VS Code 扩展**:
|
|
209
140
|
|
|
210
|
-
|
|
211
|
-
key: "my-content",
|
|
212
|
-
content: {
|
|
213
|
-
myTranslatedContent: t({
|
|
214
|
-
en: "Hello World",
|
|
215
|
-
es: "Hola Mundo",
|
|
216
|
-
fr: "Bonjour le monde",
|
|
217
|
-
}),
|
|
218
|
-
},
|
|
219
|
-
} satisfies Dictionary;
|
|
220
|
-
|
|
221
|
-
export default content;
|
|
222
|
-
```
|
|
141
|
+
[从 VS Code 市场安装](https://marketplace.visualstudio.com/items?itemName=intlayer.intlayer-vs-code-extension)
|
|
223
142
|
|
|
224
|
-
|
|
225
|
-
import { t } from "intlayer";
|
|
226
|
-
|
|
227
|
-
/** @type {import('intlayer').Dictionary} */
|
|
228
|
-
const content = {
|
|
229
|
-
key: "my-content",
|
|
230
|
-
content: {
|
|
231
|
-
myTranslatedContent: t({
|
|
232
|
-
en: "Hello World",
|
|
233
|
-
es: "Hola Mundo",
|
|
234
|
-
fr: "Bonjour le monde",
|
|
235
|
-
}),
|
|
236
|
-
},
|
|
237
|
-
};
|
|
238
|
-
```
|
|
239
|
-
|
|
240
|
-
```javascript fileName="**/*.content.cjs" contentDeclarationFormat="commonjs"
|
|
241
|
-
const { t } = require("intlayer");
|
|
242
|
-
|
|
243
|
-
module.exports = {
|
|
244
|
-
key: "my-content",
|
|
245
|
-
content: {
|
|
246
|
-
myTranslatedContent: t({
|
|
247
|
-
en: "Hello World",
|
|
248
|
-
es: "Hola Mundo",
|
|
249
|
-
fr: "Bonjour le monde",
|
|
250
|
-
}),
|
|
251
|
-
},
|
|
252
|
-
};
|
|
253
|
-
```
|
|
254
|
-
|
|
255
|
-
```json fileName="**/*.content.json" contentDeclarationFormat="json"
|
|
256
|
-
{
|
|
257
|
-
"$schema": "https://intlayer.org/schema.json",
|
|
258
|
-
"key": "my-content",
|
|
259
|
-
"content": {
|
|
260
|
-
"myTranslatedContent": {
|
|
261
|
-
"nodeType": "translation",
|
|
262
|
-
"translation": {
|
|
263
|
-
"en": "Hello World",
|
|
264
|
-
"fr": "Bonjour le monde",
|
|
265
|
-
"es": "Hola Mundo"
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
```
|
|
271
|
-
|
|
272
|
-
### 构建 next-i18next 资源
|
|
273
|
-
|
|
274
|
-
要构建 next-i18next 资源,请运行以下命令:
|
|
275
|
-
|
|
276
|
-
```bash packageManager="npm"
|
|
277
|
-
npx run intlayer build
|
|
278
|
-
```
|
|
279
|
-
|
|
280
|
-
```bash packageManager="yarn"
|
|
281
|
-
yarn intlayer build
|
|
282
|
-
```
|
|
283
|
-
|
|
284
|
-
```bash packageManager="pnpm"
|
|
285
|
-
pnpm intlayer build
|
|
286
|
-
```
|
|
287
|
-
|
|
288
|
-
这将生成资源在 `./i18next/resources` 目录中。预期的输出:
|
|
289
|
-
|
|
290
|
-
```bash
|
|
291
|
-
.
|
|
292
|
-
└── i18next
|
|
293
|
-
└── resources
|
|
294
|
-
└── en
|
|
295
|
-
└── my-content.json
|
|
296
|
-
└── fr
|
|
297
|
-
└── my-content.json
|
|
298
|
-
└── es
|
|
299
|
-
└── my-content.json
|
|
300
|
-
```
|
|
301
|
-
|
|
302
|
-
注意:i18next 命名空间对应 Intlayer 声明键。
|
|
303
|
-
|
|
304
|
-
### 实现 Next.js 插件
|
|
305
|
-
|
|
306
|
-
配置完成后,实施 Next.js 插件,以便在 Intlayer 内容声明文件更新时重新构建 i18next 资源。
|
|
307
|
-
|
|
308
|
-
```typescript fileName="next.config.mjs"
|
|
309
|
-
import { withIntlayer } from "next-intlayer/server";
|
|
310
|
-
|
|
311
|
-
/** @type {import('next').NextConfig} */
|
|
312
|
-
const nextConfig = {};
|
|
313
|
-
|
|
314
|
-
export default withIntlayer(nextConfig);
|
|
315
|
-
```
|
|
316
|
-
|
|
317
|
-
### 在 Next.js 组件中使用内容
|
|
318
|
-
|
|
319
|
-
实施 Next.js 插件后,您可以在组件中使用内容:
|
|
320
|
-
|
|
321
|
-
```typescript fileName="src/components/myComponent/index.tsx" codeFormat="typescript"
|
|
322
|
-
import type { FC } from "react";
|
|
323
|
-
import { useTranslation } from "react-i18next";
|
|
324
|
-
|
|
325
|
-
const IndexPage: FC = () => {
|
|
326
|
-
const { t } = useTranslation();
|
|
327
|
-
|
|
328
|
-
return (
|
|
329
|
-
<div>
|
|
330
|
-
<h1>{t("my-content.title")}</h1>
|
|
331
|
-
<p>{t("my-content.description")}</p>
|
|
332
|
-
</div>
|
|
333
|
-
);
|
|
334
|
-
};
|
|
335
|
-
|
|
336
|
-
export default IndexPage;
|
|
337
|
-
```
|
|
338
|
-
|
|
339
|
-
```jsx fileName="src/components/myComponent/index.mjx" codeFormat="esm"
|
|
340
|
-
import { useTranslation } from "react-i18next";
|
|
341
|
-
|
|
342
|
-
const IndexPage = () => {
|
|
343
|
-
const { t } = useTranslation();
|
|
344
|
-
|
|
345
|
-
return (
|
|
346
|
-
<div>
|
|
347
|
-
<h1>{t("my-content.title")}</h1>
|
|
348
|
-
<p>{t("my-content.description")}</p>
|
|
349
|
-
</div>
|
|
350
|
-
);
|
|
351
|
-
};
|
|
352
|
-
```
|
|
353
|
-
|
|
354
|
-
```jsx fileName="src/components/myComponent/index.cjx" codeFormat="commonjs"
|
|
355
|
-
const { useTranslation } = require("react-i18next");
|
|
356
|
-
|
|
357
|
-
const IndexPage = () => {
|
|
358
|
-
const { t } = useTranslation();
|
|
359
|
-
|
|
360
|
-
return (
|
|
361
|
-
<div>
|
|
362
|
-
<h1>{t("my-content.title")}</h1>
|
|
363
|
-
<p>{t("my-content.description")}</p>
|
|
364
|
-
</div>
|
|
365
|
-
);
|
|
366
|
-
};
|
|
367
|
-
```
|
|
143
|
+
[从 VS Code 市场安装](https://marketplace.visualstudio.com/items?itemName=intlayer.intlayer-vs-code-extension)
|