@intlayer/docs 7.0.4-canary.0 → 7.0.4
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,346 +1,124 @@
|
|
|
1
1
|
---
|
|
2
2
|
createdAt: 2025-01-02
|
|
3
|
-
updatedAt: 2025-
|
|
4
|
-
title: Intlayer
|
|
5
|
-
description:
|
|
3
|
+
updatedAt: 2025-10-29
|
|
4
|
+
title: 如何使用 Intlayer 自动化您的 react-i18next JSON 翻译
|
|
5
|
+
description: 使用 Intlayer 和 react-i18next 自动化您的 JSON 翻译,提升 React 应用的国际化体验。
|
|
6
6
|
keywords:
|
|
7
7
|
- react-i18next
|
|
8
8
|
- i18next
|
|
9
9
|
- Intlayer
|
|
10
10
|
- 国际化
|
|
11
|
-
-
|
|
12
|
-
-
|
|
13
|
-
- JavaScript
|
|
11
|
+
- i18n
|
|
12
|
+
- 博客
|
|
14
13
|
- React
|
|
14
|
+
- JavaScript
|
|
15
|
+
- TypeScript
|
|
16
|
+
- 内容管理
|
|
15
17
|
slugs:
|
|
16
18
|
- blog
|
|
17
19
|
- intlayer-with-react-i18next
|
|
20
|
+
history:
|
|
21
|
+
- version: 7.0.0
|
|
22
|
+
date: 2025-10-29
|
|
23
|
+
changes: 更改为 syncJSON 插件
|
|
18
24
|
---
|
|
19
25
|
|
|
20
|
-
#
|
|
21
|
-
|
|
22
|
-
## 概述
|
|
23
|
-
|
|
24
|
-
- **Intlayer** 帮助您通过 **组件级别** 内容声明文件管理翻译。
|
|
25
|
-
- **react-i18next** 是一个流行的 React 集成 **i18next** 的库,提供 `useTranslation` 等钩子以在组件中获取本地化字符串。
|
|
26
|
-
|
|
27
|
-
结合使用时,Intlayer 可以 **导出** 与 **i18next 兼容的 JSON**,以便 react-i18next 可以在运行时 **使用** 它们。
|
|
28
|
-
|
|
29
|
-
## 为什么将 Intlayer 与 react-i18next 结合使用?
|
|
30
|
-
|
|
31
|
-
**Intlayer** 内容声明文件提供了更好的开发者体验,因为它们是:
|
|
32
|
-
|
|
33
|
-
1. **灵活的文件位置**
|
|
34
|
-
将每个内容声明文件放在需要它的组件旁边。这种结构允许您保持翻译的共存,防止组件移动或删除时出现孤立翻译。
|
|
35
|
-
|
|
36
|
-
```bash codeFormat="typescript"
|
|
37
|
-
.
|
|
38
|
-
└── src
|
|
39
|
-
└── components
|
|
40
|
-
└── MyComponent
|
|
41
|
-
├── index.content.ts # 内容声明文件
|
|
42
|
-
└── index.tsx
|
|
43
|
-
```
|
|
44
|
-
|
|
45
|
-
```bash codeFormat="esm"
|
|
46
|
-
.
|
|
47
|
-
└── src
|
|
48
|
-
└── components
|
|
49
|
-
└── MyComponent
|
|
50
|
-
├── index.content.mjs # 内容声明文件
|
|
51
|
-
└── index.mjx
|
|
52
|
-
```
|
|
53
|
-
|
|
54
|
-
```bash codeFormat="cjs"
|
|
55
|
-
.
|
|
56
|
-
└── src
|
|
57
|
-
└── components
|
|
58
|
-
└── MyComponent
|
|
59
|
-
├── index.content.cjs # 内容声明文件
|
|
60
|
-
└── index.cjx
|
|
61
|
-
```
|
|
62
|
-
|
|
63
|
-
```bash codeFormat="json"
|
|
64
|
-
.
|
|
65
|
-
└── src
|
|
66
|
-
└── components
|
|
67
|
-
└── MyComponent
|
|
68
|
-
├── index.content.json # 内容声明文件
|
|
69
|
-
└── index.jsx
|
|
70
|
-
```
|
|
71
|
-
|
|
72
|
-
2. **集中翻译**
|
|
73
|
-
单个内容声明文件收集一个组件所需的所有翻译,使缺失翻译更易于捕捉。
|
|
74
|
-
使用 TypeScript 时,如果缺少翻译,您甚至会在编译时收到错误。
|
|
75
|
-
|
|
76
|
-
## 安装
|
|
77
|
-
|
|
78
|
-
在 Create React App 项目中,安装这些依赖:
|
|
79
|
-
|
|
80
|
-
```bash
|
|
81
|
-
# 使用 npm
|
|
82
|
-
npm install intlayer react-i18next i18next i18next-resources-to-backend
|
|
83
|
-
```
|
|
26
|
+
# 如何使用 Intlayer 自动化您的 react-i18next JSON 翻译
|
|
84
27
|
|
|
85
|
-
|
|
86
|
-
# 使用 yarn
|
|
87
|
-
yarn add intlayer react-i18next i18next i18next-resources-to-backend
|
|
88
|
-
```
|
|
28
|
+
## 什么是 Intlayer?
|
|
89
29
|
|
|
90
|
-
|
|
91
|
-
# 使用 pnpm
|
|
92
|
-
pnpm add intlayer react-i18next i18next i18next-resources-to-backend
|
|
93
|
-
```
|
|
30
|
+
**Intlayer** 是一个创新的开源国际化库,旨在解决传统 i18n 解决方案的不足。它为 React 应用提供了一种现代化的内容管理方法。
|
|
94
31
|
|
|
95
|
-
|
|
32
|
+
请参阅我们博客文章中的具体对比:[react-i18next vs. react-intl vs. Intlayer](https://github.com/aymericzip/intlayer/blob/main/docs/blog/zh/react-i18next_vs_react-intl_vs_intlayer.md)。
|
|
96
33
|
|
|
97
|
-
|
|
98
|
-
- **react-intlayer** – 针对 Intlayer 的 React 特定集成,提供自动化构建字典的一些脚本。
|
|
99
|
-
- **react-i18next** – i18next 的 React 特定集成库,包括 `useTranslation` 钩子。
|
|
100
|
-
- **i18next** – 用于处理翻译的底层框架。
|
|
101
|
-
- **i18next-resources-to-backend** – 一个动态导入 JSON 资源的 i18next 后端。
|
|
34
|
+
## 为什么要将 Intlayer 与 react-i18next 结合使用?
|
|
102
35
|
|
|
103
|
-
|
|
36
|
+
虽然 Intlayer 提供了一个出色的独立 i18n 解决方案(请参阅我们的[React 集成指南](https://github.com/aymericzip/intlayer/blob/main/docs/docs/zh/intlayer_with_vite+react.md)),但您可能出于以下几个原因想将其与 react-i18next 结合使用:
|
|
104
37
|
|
|
105
|
-
|
|
38
|
+
1. **现有代码库**:您已经有一个成熟的 react-i18next 实现,并希望逐步迁移到 Intlayer 以获得更好的开发者体验。
|
|
39
|
+
2. **遗留需求**:您的项目需要兼容现有的 react-i18next 插件或工作流程。
|
|
40
|
+
3. **团队熟悉度**:您的团队熟悉 react-i18next,但希望获得更好的内容管理。
|
|
106
41
|
|
|
107
|
-
|
|
108
|
-
import { Locales, type IntlayerConfig } from "intlayer";
|
|
42
|
+
**为此,Intlayer 可以作为 react-i18next 的适配器来实现,帮助您在 CLI 或 CI/CD 流水线中自动化 JSON 翻译、测试翻译等功能。**
|
|
109
43
|
|
|
110
|
-
|
|
111
|
-
internationalization: {
|
|
112
|
-
// 添加您想要的任意多的语言环境
|
|
113
|
-
locales: [Locales.ENGLISH, Locales.FRENCH, Locales.SPANISH],
|
|
114
|
-
defaultLocale: Locales.ENGLISH,
|
|
115
|
-
},
|
|
116
|
-
content: {
|
|
117
|
-
// 告诉 Intlayer 创建与 i18next 兼容的 JSON
|
|
118
|
-
dictionaryOutput: ["i18next"],
|
|
44
|
+
本指南将向您展示如何利用 Intlayer 优秀的内容声明系统,同时保持与 react-i18next 的兼容性。
|
|
119
45
|
|
|
120
|
-
|
|
121
|
-
// 如果该文件夹尚不存在,将自动创建。
|
|
122
|
-
i18nextResourcesDir: "./i18next/resources",
|
|
123
|
-
},
|
|
124
|
-
};
|
|
46
|
+
## 目录
|
|
125
47
|
|
|
126
|
-
|
|
127
|
-
```
|
|
48
|
+
<TOC/>
|
|
128
49
|
|
|
129
|
-
|
|
50
|
+
## 使用 react-i18next 设置 Intlayer 的分步指南
|
|
130
51
|
|
|
131
|
-
|
|
52
|
+
### 第一步:安装依赖
|
|
132
53
|
|
|
133
|
-
|
|
54
|
+
安装必要的包:
|
|
134
55
|
|
|
135
|
-
```bash
|
|
136
|
-
|
|
137
|
-
npx run intlayer build
|
|
56
|
+
```bash packageManager="npm"
|
|
57
|
+
npm install intlayer @intlayer/sync-json-plugin
|
|
138
58
|
```
|
|
139
59
|
|
|
140
|
-
```bash
|
|
141
|
-
|
|
142
|
-
yarn intlayer build
|
|
60
|
+
```bash packageManager="pnpm"
|
|
61
|
+
pnpm add intlayer @intlayer/sync-json-plugin
|
|
143
62
|
```
|
|
144
63
|
|
|
145
|
-
```bash
|
|
146
|
-
|
|
147
|
-
pnpm intlayer build
|
|
64
|
+
```bash packageManager="yarn"
|
|
65
|
+
yarn add intlayer @intlayer/sync-json-plugin
|
|
148
66
|
```
|
|
149
67
|
|
|
150
|
-
|
|
68
|
+
**包描述:**
|
|
151
69
|
|
|
152
|
-
|
|
70
|
+
- **intlayer**:用于国际化管理、内容声明和构建的核心库
|
|
71
|
+
- **@intlayer/sync-json-plugin**:将 Intlayer 内容声明导出为兼容 react-i18next 的 JSON 格式的插件
|
|
153
72
|
|
|
154
|
-
|
|
155
|
-
.
|
|
156
|
-
└── i18next
|
|
157
|
-
└── resources
|
|
158
|
-
├── zh
|
|
159
|
-
│ └── my-content.json
|
|
160
|
-
├── fr
|
|
161
|
-
│ └── my-content.json
|
|
162
|
-
└── es
|
|
163
|
-
└── my-content.json
|
|
164
|
-
```
|
|
73
|
+
### 第二步:实现 Intlayer 插件以包装 JSON
|
|
165
74
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
## 将字典导入到您的 react-i18next 配置中
|
|
169
|
-
|
|
170
|
-
要在运行时动态加载这些资源,请使用 [`i18next-resources-to-backend`](https://www.npmjs.com/package/i18next-resources-to-backend)。例如,在您的项目中创建一个 `i18n.ts`(或 `.js`)文件:
|
|
171
|
-
|
|
172
|
-
```typescript title="i18n.ts"
|
|
173
|
-
import i18next from "i18next";
|
|
174
|
-
import { initReactI18next } from "react-i18next";
|
|
175
|
-
import resourcesToBackend from "i18next-resources-to-backend";
|
|
176
|
-
|
|
177
|
-
i18next
|
|
178
|
-
// react-i18next 插件
|
|
179
|
-
.use(initReactI18next)
|
|
180
|
-
// 动态加载资源
|
|
181
|
-
.use(
|
|
182
|
-
resourcesToBackend((language: string, namespace: string) => {
|
|
183
|
-
// 调整您资源目录的导入路径
|
|
184
|
-
return import(`../i18next/resources/${language}/${namespace}.json`);
|
|
185
|
-
})
|
|
186
|
-
)
|
|
187
|
-
// 初始化 i18next
|
|
188
|
-
.init({
|
|
189
|
-
// 回退语言
|
|
190
|
-
fallbackLng: "zh",
|
|
191
|
-
|
|
192
|
-
// 您可以在此处添加其他 i18next 配置选项,见:
|
|
193
|
-
// https://www.i18next.com/overview/configuration-options
|
|
194
|
-
});
|
|
195
|
-
|
|
196
|
-
export default i18next;
|
|
197
|
-
```
|
|
75
|
+
创建一个 Intlayer 配置文件来定义你支持的语言环境:
|
|
198
76
|
|
|
199
|
-
|
|
200
|
-
import i18next from "i18next";
|
|
201
|
-
import { initReactI18next } from "react-i18next";
|
|
202
|
-
import resourcesToBackend from "i18next-resources-to-backend";
|
|
203
|
-
|
|
204
|
-
i18next
|
|
205
|
-
.use(initReactI18next)
|
|
206
|
-
.use(
|
|
207
|
-
resourcesToBackend(
|
|
208
|
-
(language, namespace) =>
|
|
209
|
-
import(`../i18next/resources/${language}/${namespace}.json`)
|
|
210
|
-
)
|
|
211
|
-
)
|
|
212
|
-
.init({
|
|
213
|
-
fallbackLng: "zh",
|
|
214
|
-
});
|
|
215
|
-
|
|
216
|
-
export default i18next;
|
|
217
|
-
```
|
|
77
|
+
**如果你还想导出适用于 react-i18next 的 JSON 字典**,请添加 `syncJSON` 插件:
|
|
218
78
|
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
import React from "react";
|
|
223
|
-
import ReactDOM from "react-dom/client";
|
|
224
|
-
// 在任何其他事情之前初始化 i18n
|
|
225
|
-
import "./i18n";
|
|
226
|
-
import App from "./App";
|
|
227
|
-
|
|
228
|
-
ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(
|
|
229
|
-
<React.StrictMode>
|
|
230
|
-
<App />
|
|
231
|
-
</React.StrictMode>
|
|
232
|
-
);
|
|
233
|
-
```
|
|
234
|
-
|
|
235
|
-
## 创建和管理您的内容声明
|
|
236
|
-
|
|
237
|
-
Intlayer 从位于 `./src` 下的“内容声明文件”中提取翻译(默认为此)。
|
|
238
|
-
以下是 TypeScript 中的一个最小示例:
|
|
239
|
-
|
|
240
|
-
```typescript title="src/components/MyComponent/MyComponent.content.ts"
|
|
241
|
-
import { t, type Dictionary } from "intlayer";
|
|
79
|
+
```typescript fileName="intlayer.config.ts"
|
|
80
|
+
import { Locales, type IntlayerConfig } from "intlayer";
|
|
81
|
+
import { syncJSON } from "@intlayer/sync-json-plugin";
|
|
242
82
|
|
|
243
|
-
const
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
// 每个“t”调用是一个单独的翻译节点
|
|
248
|
-
heading: t({
|
|
249
|
-
en: "Hello World",
|
|
250
|
-
es: "Hola Mundo",
|
|
251
|
-
fr: "Bonjour le monde",
|
|
252
|
-
}),
|
|
253
|
-
description: t({
|
|
254
|
-
en: "My i18n description text...",
|
|
255
|
-
fr: "Ma description en i18n...",
|
|
256
|
-
es: "Mi descripción en i18n...",
|
|
257
|
-
}),
|
|
83
|
+
const config: IntlayerConfig = {
|
|
84
|
+
internationalization: {
|
|
85
|
+
locales: [Locales.ENGLISH, Locales.FRENCH, Locales.SPANISH],
|
|
86
|
+
defaultLocale: Locales.ENGLISH,
|
|
258
87
|
},
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
如果您更喜欢 JSON、`.cjs` 或 `.mjs`,请参考 [Intlayer 文档](https://intlayer.org/zh/doc/concept/content)。
|
|
265
|
-
|
|
266
|
-
> 默认情况下,有效的内容声明匹配文件扩展名模式:
|
|
267
|
-
|
|
268
|
-
> `*.content.{ts,tsx,js,jsx,mjs,mjx,cjs,cjx,json}`
|
|
269
|
-
|
|
270
|
-
## 在 React 组件中使用翻译
|
|
271
|
-
|
|
272
|
-
在您 **构建** 了 Intlayer 资源并配置了 react-i18next 后,您可以直接使用 **react-i18next** 的 `useTranslation` 钩子。
|
|
273
|
-
例如:
|
|
274
|
-
|
|
275
|
-
```tsx title="src/components/MyComponent/MyComponent.tsx"
|
|
276
|
-
import type { FC } from "react";
|
|
277
|
-
import { useTranslation } from "react-i18next";
|
|
278
|
-
|
|
279
|
-
/**
|
|
280
|
-
* i18next 的“命名空间”是来自“MyComponent.content.ts”的 Intlayer `key`
|
|
281
|
-
* 因此我们将“my-component”传递给 useTranslation()。
|
|
282
|
-
*/
|
|
283
|
-
const MyComponent: FC = () => {
|
|
284
|
-
const { t } = useTranslation("my-component");
|
|
285
|
-
|
|
286
|
-
return (
|
|
287
|
-
<div>
|
|
288
|
-
<h1>{t("heading")}</h1>
|
|
289
|
-
<p>{t("description")}</p>
|
|
290
|
-
</div>
|
|
291
|
-
);
|
|
88
|
+
plugins: [
|
|
89
|
+
syncJSON({
|
|
90
|
+
source: ({ key, locale }) => `./messages/${locale}/${key}.json`,
|
|
91
|
+
}),
|
|
92
|
+
],
|
|
292
93
|
};
|
|
293
94
|
|
|
294
|
-
export default
|
|
95
|
+
export default config;
|
|
295
96
|
```
|
|
296
97
|
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
## 可选:与 Create React App 脚本集成 (CRACO)
|
|
300
|
-
|
|
301
|
-
**react-intlayer** 提供了一种基于 CRACO 的自定义构建和开发服务器配置方法。如果您希望 Intlayer 的构建步骤无缝集成,可以:
|
|
98
|
+
`syncJSON` 插件会自动包装 JSON。它会读取和写入 JSON 文件,而不改变内容结构。
|
|
302
99
|
|
|
303
|
-
|
|
304
|
-
```bash
|
|
305
|
-
npm install react-intlayer --save-dev
|
|
306
|
-
```
|
|
307
|
-
2. **调整您的 `package.json` 脚本**以使用 `react-intlayer` 脚本:
|
|
100
|
+
如果你想让 JSON 与 intlayer 内容声明文件(`.content` 文件)共存,Intlayer 会按以下方式处理:
|
|
308
101
|
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
"start": "react-intlayer start",
|
|
312
|
-
"build": "react-intlayer build",
|
|
313
|
-
"transpile": "intlayer build"
|
|
314
|
-
}
|
|
315
|
-
```
|
|
102
|
+
1. 加载 JSON 和内容声明文件,并将它们转换为 Intlayer 字典。
|
|
103
|
+
2. 如果 JSON 和内容声明文件之间存在冲突,Intlayer 将合并所有字典。合并的优先级取决于插件的优先级以及内容声明文件的优先级(所有均可配置)。
|
|
316
104
|
|
|
317
|
-
|
|
105
|
+
如果使用 CLI 翻译 JSON,或使用 CMS 进行更改,Intlayer 将使用新的翻译更新 JSON 文件。
|
|
318
106
|
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
## TypeScript 配置
|
|
322
|
-
|
|
323
|
-
**Intlayer** 提供了 **自动生成的类型定义** 来支持您的内容。为确保 TypeScript 能够捕获这些类型,请在 `tsconfig.json` 的 **include** 数组中添加 **`types`**(如果您另有配置则为 `types`):
|
|
324
|
-
|
|
325
|
-
```json5 title="tsconfig.json"
|
|
326
|
-
{
|
|
327
|
-
"compilerOptions": {
|
|
328
|
-
// ...
|
|
329
|
-
},
|
|
330
|
-
"include": ["src", "types"],
|
|
331
|
-
}
|
|
332
|
-
```
|
|
333
|
-
|
|
334
|
-
> 这将使 TypeScript 推断您的翻译的形状,以获得更好的自动补全和错误检测。
|
|
107
|
+
要查看更多关于 `syncJSON` 插件的详细信息,请参阅 [syncJSON 插件文档](https://github.com/aymericzip/intlayer/blob/main/docs/docs/zh/plugins/sync-json.md)。
|
|
335
108
|
|
|
336
109
|
## Git 配置
|
|
337
110
|
|
|
338
|
-
|
|
111
|
+
建议忽略自动生成的 Intlayer 文件:
|
|
339
112
|
|
|
340
|
-
```plaintext
|
|
113
|
+
```plaintext fileName=".gitignore"
|
|
341
114
|
# 忽略 Intlayer 生成的文件
|
|
342
115
|
.intlayer
|
|
343
|
-
i18next
|
|
344
116
|
```
|
|
345
117
|
|
|
346
|
-
|
|
118
|
+
这些文件可以在构建过程中重新生成,无需提交到版本控制。
|
|
119
|
+
|
|
120
|
+
### VS Code 扩展
|
|
121
|
+
|
|
122
|
+
为了提升开发者体验,请安装官方的 **Intlayer VS Code 扩展**:
|
|
123
|
+
|
|
124
|
+
[从 VS Code 市场安装](https://marketplace.visualstudio.com/items?itemName=intlayer.intlayer-vs-code-extension)
|
|
@@ -1,166 +1,123 @@
|
|
|
1
1
|
---
|
|
2
2
|
createdAt: 2025-01-02
|
|
3
|
-
updatedAt: 2025-
|
|
4
|
-
title: Intlayer
|
|
5
|
-
description:
|
|
3
|
+
updatedAt: 2025-10-29
|
|
4
|
+
title: 如何使用 Intlayer 自动化您的 react-intl JSON 翻译
|
|
5
|
+
description: 使用 Intlayer 和 react-intl 自动化您的 JSON 翻译,提升 React 应用的国际化体验。
|
|
6
6
|
keywords:
|
|
7
7
|
- react-intl
|
|
8
8
|
- Intlayer
|
|
9
9
|
- 国际化
|
|
10
|
-
-
|
|
11
|
-
-
|
|
10
|
+
- 博客
|
|
11
|
+
- i18n
|
|
12
12
|
- JavaScript
|
|
13
13
|
- React
|
|
14
|
+
- FormatJS
|
|
14
15
|
slugs:
|
|
15
16
|
- blog
|
|
16
17
|
- intlayer-with-react-intl
|
|
18
|
+
history:
|
|
19
|
+
- version: 7.0.0
|
|
20
|
+
date: 2025-10-29
|
|
21
|
+
changes: 更改为 syncJSON 插件
|
|
17
22
|
---
|
|
18
23
|
|
|
19
|
-
#
|
|
24
|
+
# 如何使用 Intlayer 自动化您的 react-intl JSON 翻译
|
|
20
25
|
|
|
21
|
-
|
|
26
|
+
## 什么是 Intlayer?
|
|
22
27
|
|
|
23
|
-
|
|
28
|
+
**Intlayer** 是一个创新的开源国际化库,旨在解决传统 i18n 解决方案的不足。它为 React 应用中的内容管理提供了一种现代化的方法。
|
|
24
29
|
|
|
25
|
-
-
|
|
26
|
-
- **react-intl** 提供 React 组件和钩子(如 `<FormattedMessage>` 和 `useIntl()`)来显示本地化字符串。
|
|
30
|
+
请参阅我们博客文章中的具体对比:[react-i18next vs. react-intl vs. Intlayer](https://github.com/aymericzip/intlayer/blob/main/docs/blog/zh/react-i18next_vs_react-intl_vs_intlayer.md)。
|
|
27
31
|
|
|
28
|
-
|
|
32
|
+
## 为什么要将 Intlayer 与 react-intl 结合使用?
|
|
29
33
|
|
|
30
|
-
|
|
34
|
+
虽然 Intlayer 提供了一个出色的独立 i18n 解决方案(请参阅我们的[React 集成指南](https://github.com/aymericzip/intlayer/blob/main/docs/docs/zh/intlayer_with_vite+react.md)),但您可能出于以下几个原因想将其与 react-intl 结合使用:
|
|
31
35
|
|
|
32
|
-
|
|
36
|
+
1. **现有代码库**:您已经有了成熟的 react-intl 实现,并希望逐步迁移到 Intlayer 以获得更好的开发者体验。
|
|
37
|
+
2. **遗留需求**:您的项目需要兼容现有的 react-intl 插件或工作流程。
|
|
38
|
+
3. **团队熟悉度**:您的团队对 react-intl 很熟悉,但希望获得更好的内容管理。
|
|
33
39
|
|
|
34
|
-
|
|
35
|
-
Intlayer 内容声明文件可以与您的 React 组件并存,如果组件被移动或删除,可以防止“孤立”翻译。例如:
|
|
40
|
+
**为此,Intlayer 可以作为 react-intl 的适配器实现,帮助您在 CLI 或 CI/CD 流水线中自动化 JSON 翻译,测试翻译内容等。**
|
|
36
41
|
|
|
37
|
-
|
|
38
|
-
.
|
|
39
|
-
└── src
|
|
40
|
-
└── components
|
|
41
|
-
└── MyComponent
|
|
42
|
-
├── index.content.ts # Intlayer 内容声明
|
|
43
|
-
└── index.tsx # React 组件
|
|
44
|
-
```
|
|
42
|
+
本指南将向您展示如何利用 Intlayer 优越的内容声明系统,同时保持与 react-intl 的兼容性。
|
|
45
43
|
|
|
46
|
-
|
|
47
|
-
每个内容声明文件收集组件所需的所有翻译。这在 TypeScript 项目中特别有帮助:缺少翻译可以在 **编译时** 被捕获。
|
|
44
|
+
## 目录
|
|
48
45
|
|
|
49
|
-
|
|
50
|
-
每当您添加或更新翻译时,Intlayer 会重新生成消息 JSON 文件。然后,您可以将这些文件传入 react-intl 的 `<IntlProvider>`。
|
|
46
|
+
<TOC/>
|
|
51
47
|
|
|
52
|
-
|
|
48
|
+
## 使用 react-intl 设置 Intlayer 的分步指南
|
|
53
49
|
|
|
54
|
-
|
|
50
|
+
### 第一步:安装依赖项
|
|
55
51
|
|
|
56
|
-
|
|
52
|
+
安装必要的包:
|
|
57
53
|
|
|
58
|
-
```bash
|
|
59
|
-
|
|
60
|
-
|
|
54
|
+
```bash packageManager="npm"
|
|
55
|
+
npm install intlayer @intlayer/sync-json-plugin
|
|
56
|
+
```
|
|
61
57
|
|
|
62
|
-
|
|
63
|
-
|
|
58
|
+
```bash packageManager="pnpm"
|
|
59
|
+
pnpm add intlayer @intlayer/sync-json-plugin
|
|
60
|
+
```
|
|
64
61
|
|
|
65
|
-
|
|
66
|
-
|
|
62
|
+
```bash packageManager="yarn"
|
|
63
|
+
yarn add intlayer @intlayer/sync-json-plugin
|
|
67
64
|
```
|
|
68
65
|
|
|
69
|
-
|
|
66
|
+
**包描述:**
|
|
70
67
|
|
|
71
|
-
- **intlayer
|
|
72
|
-
-
|
|
68
|
+
- **intlayer**:用于国际化管理、内容声明和构建的核心库
|
|
69
|
+
- **@intlayer/sync-json-plugin**:将 Intlayer 内容声明导出为兼容 react-intl 的 JSON 格式的插件
|
|
73
70
|
|
|
74
|
-
|
|
71
|
+
### 步骤 2:实现 Intlayer 插件以包装 JSON
|
|
75
72
|
|
|
76
|
-
|
|
73
|
+
创建一个 Intlayer 配置文件以定义支持的语言环境:
|
|
77
74
|
|
|
78
|
-
|
|
75
|
+
**如果您还想导出适用于 react-intl 的 JSON 字典**,请添加 `syncJSON` 插件:
|
|
79
76
|
|
|
80
|
-
```typescript
|
|
77
|
+
```typescript fileName="intlayer.config.ts"
|
|
81
78
|
import { Locales, type IntlayerConfig } from "intlayer";
|
|
79
|
+
import { syncJSON } from "@intlayer/sync-json-plugin";
|
|
82
80
|
|
|
83
81
|
const config: IntlayerConfig = {
|
|
84
82
|
internationalization: {
|
|
85
|
-
// 可以添加任意数量的语言环境
|
|
86
83
|
locales: [Locales.ENGLISH, Locales.FRENCH, Locales.SPANISH],
|
|
87
84
|
defaultLocale: Locales.ENGLISH,
|
|
88
85
|
},
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
reactIntlMessagesDir: "./react-intl/messages",
|
|
95
|
-
},
|
|
86
|
+
plugins: [
|
|
87
|
+
syncJSON({
|
|
88
|
+
source: ({ key, locale }) => `./intl/messages/${locale}/${key}.json`,
|
|
89
|
+
}),
|
|
90
|
+
],
|
|
96
91
|
};
|
|
97
92
|
|
|
98
93
|
export default config;
|
|
99
94
|
```
|
|
100
95
|
|
|
101
|
-
|
|
96
|
+
`syncJSON` 插件会自动包装 JSON。它会读取和写入 JSON 文件,而不会改变内容结构。
|
|
102
97
|
|
|
103
|
-
|
|
98
|
+
如果你想让 JSON 与 intlayer 内容声明文件(`.content` 文件)共存,Intlayer 会按以下方式处理:
|
|
104
99
|
|
|
105
|
-
|
|
100
|
+
1. 加载 JSON 和内容声明文件,并将它们转换为 intlayer 字典。
|
|
106
101
|
|
|
107
|
-
|
|
108
|
-
这里是一个 **TypeScript** 示例:
|
|
102
|
+
2. 如果 JSON 文件和内容声明文件之间存在冲突,Intlayer 将合并所有字典。合并的优先级取决于插件的优先级以及内容声明文件的优先级(所有这些都是可配置的)。
|
|
109
103
|
|
|
110
|
-
|
|
111
|
-
import { t, type Dictionary } from "intlayer";
|
|
104
|
+
如果使用 CLI 翻译 JSON,或使用 CMS 进行更改,Intlayer 会使用新的翻译更新 JSON 文件。
|
|
112
105
|
|
|
113
|
-
|
|
114
|
-
// "key" 变成您的 react-intl JSON 文件中的顶级消息键
|
|
115
|
-
key: "my-component",
|
|
106
|
+
有关 `syncJSON` 插件的更多详细信息,请参阅 [syncJSON 插件文档](https://github.com/aymericzip/intlayer/blob/main/docs/docs/zh/plugins/sync-json.md)。
|
|
116
107
|
|
|
117
|
-
|
|
118
|
-
// 每次调用 t() 声明一个可翻译的字段
|
|
119
|
-
helloWorld: t({
|
|
120
|
-
en: "Hello World",
|
|
121
|
-
es: "Hola Mundo",
|
|
122
|
-
fr: "Bonjour le monde",
|
|
123
|
-
}),
|
|
124
|
-
description: t({
|
|
125
|
-
en: "This is a description",
|
|
126
|
-
fr: "Ceci est une description",
|
|
127
|
-
es: "Esta es una descripción",
|
|
128
|
-
}),
|
|
129
|
-
},
|
|
130
|
-
} satisfies Dictionary;
|
|
108
|
+
## Git 配置
|
|
131
109
|
|
|
132
|
-
|
|
133
|
-
```
|
|
134
|
-
|
|
135
|
-
如果您更喜欢 JSON 或不同的 JS 语法(`.cjs`、`.mjs`),结构大致相同, 请参阅 [Intlayer 文档关于内容声明](https://intlayer.org/en/doc/concept/content)。
|
|
136
|
-
|
|
137
|
-
---
|
|
138
|
-
|
|
139
|
-
## 构建 react-intl 消息
|
|
110
|
+
建议忽略自动生成的 Intlayer 文件:
|
|
140
111
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
# with npm
|
|
145
|
-
npx intlayer dictionaries build
|
|
146
|
-
|
|
147
|
-
# with yarn
|
|
148
|
-
yarn intlayer build
|
|
149
|
-
|
|
150
|
-
# with pnpm
|
|
151
|
-
pnpm intlayer build
|
|
112
|
+
```plaintext fileName=".gitignore"
|
|
113
|
+
# 忽略 Intlayer 生成的文件
|
|
114
|
+
.intlayer
|
|
152
115
|
```
|
|
153
116
|
|
|
154
|
-
|
|
155
|
-
典型输出可能如下所示:
|
|
117
|
+
这些文件可以在构建过程中重新生成,无需提交到版本控制。
|
|
156
118
|
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
└── messages
|
|
161
|
-
├── en.json
|
|
162
|
-
├── fr.json
|
|
163
|
-
└── es.json
|
|
164
|
-
```
|
|
119
|
+
### VS Code 扩展
|
|
120
|
+
|
|
121
|
+
为了提升开发者体验,建议安装官方的 **Intlayer VS Code 扩展**:
|
|
165
122
|
|
|
166
|
-
|
|
123
|
+
[从 VS Code 市场安装](https://marketplace.visualstudio.com/items?itemName=intlayer.intlayer-vs-code-extension)
|