@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.
Files changed (107) hide show
  1. package/blog/ar/intlayer_with_i18next.md +68 -106
  2. package/blog/ar/intlayer_with_next-i18next.md +84 -288
  3. package/blog/ar/intlayer_with_next-intl.md +58 -337
  4. package/blog/ar/intlayer_with_react-i18next.md +68 -290
  5. package/blog/ar/intlayer_with_react-intl.md +63 -266
  6. package/blog/de/intlayer_with_i18next.md +77 -97
  7. package/blog/de/intlayer_with_next-i18next.md +69 -296
  8. package/blog/de/intlayer_with_next-intl.md +59 -340
  9. package/blog/de/intlayer_with_react-i18next.md +68 -290
  10. package/blog/de/intlayer_with_react-intl.md +62 -264
  11. package/blog/en/intlayer_with_i18next.md +36 -1638
  12. package/blog/en/intlayer_with_next-i18next.md +22 -847
  13. package/blog/en/intlayer_with_next-intl.md +32 -1053
  14. package/blog/en/intlayer_with_react-i18next.md +38 -764
  15. package/blog/en/intlayer_with_react-intl.md +42 -1018
  16. package/blog/en-GB/intlayer_with_i18next.md +67 -103
  17. package/blog/en-GB/intlayer_with_next-i18next.md +71 -292
  18. package/blog/en-GB/intlayer_with_next-intl.md +58 -337
  19. package/blog/en-GB/intlayer_with_react-i18next.md +67 -289
  20. package/blog/en-GB/intlayer_with_react-intl.md +61 -264
  21. package/blog/es/intlayer_with_i18next.md +67 -103
  22. package/blog/es/intlayer_with_next-i18next.md +71 -296
  23. package/blog/es/intlayer_with_next-intl.md +57 -338
  24. package/blog/es/intlayer_with_react-i18next.md +68 -290
  25. package/blog/es/intlayer_with_react-intl.md +62 -265
  26. package/blog/fr/intlayer_with_i18next.md +66 -104
  27. package/blog/fr/intlayer_with_next-i18next.md +82 -285
  28. package/blog/fr/intlayer_with_next-intl.md +57 -338
  29. package/blog/fr/intlayer_with_react-i18next.md +67 -289
  30. package/blog/fr/intlayer_with_react-intl.md +61 -264
  31. package/blog/hi/intlayer_with_i18next.md +68 -104
  32. package/blog/hi/intlayer_with_next-i18next.md +74 -299
  33. package/blog/hi/intlayer_with_next-intl.md +57 -239
  34. package/blog/hi/intlayer_with_react-i18next.md +69 -291
  35. package/blog/hi/intlayer_with_react-intl.md +65 -268
  36. package/blog/id/intlayer_with_i18next.md +126 -0
  37. package/blog/id/intlayer_with_next-i18next.md +142 -0
  38. package/blog/id/intlayer_with_next-intl.md +113 -0
  39. package/blog/id/intlayer_with_react-i18next.md +124 -0
  40. package/blog/id/intlayer_with_react-intl.md +122 -0
  41. package/blog/it/intlayer_with_i18next.md +67 -103
  42. package/blog/it/intlayer_with_next-i18next.md +71 -296
  43. package/blog/it/intlayer_with_next-intl.md +57 -338
  44. package/blog/it/intlayer_with_react-i18next.md +68 -290
  45. package/blog/it/intlayer_with_react-intl.md +62 -265
  46. package/blog/ja/intlayer_with_i18next.md +68 -103
  47. package/blog/ja/intlayer_with_next-i18next.md +85 -283
  48. package/blog/ja/intlayer_with_next-intl.md +58 -336
  49. package/blog/ja/intlayer_with_react-i18next.md +68 -290
  50. package/blog/ja/intlayer_with_react-intl.md +62 -264
  51. package/blog/ko/intlayer_with_i18next.md +80 -96
  52. package/blog/ko/intlayer_with_next-i18next.md +85 -287
  53. package/blog/ko/intlayer_with_next-intl.md +68 -327
  54. package/blog/ko/intlayer_with_react-i18next.md +68 -290
  55. package/blog/ko/intlayer_with_react-intl.md +64 -266
  56. package/blog/pl/intlayer_with_i18next.md +126 -0
  57. package/blog/pl/intlayer_with_next-i18next.md +142 -0
  58. package/blog/pl/intlayer_with_next-intl.md +111 -0
  59. package/blog/pl/intlayer_with_react-i18next.md +124 -0
  60. package/blog/pl/intlayer_with_react-intl.md +122 -0
  61. package/blog/pt/intlayer_with_i18next.md +67 -103
  62. package/blog/pt/intlayer_with_next-i18next.md +72 -293
  63. package/blog/pt/intlayer_with_next-intl.md +57 -256
  64. package/blog/pt/intlayer_with_react-i18next.md +104 -78
  65. package/blog/pt/intlayer_with_react-intl.md +62 -266
  66. package/blog/ru/intlayer_with_i18next.md +66 -104
  67. package/blog/ru/intlayer_with_next-i18next.md +71 -296
  68. package/blog/ru/intlayer_with_next-intl.md +58 -337
  69. package/blog/ru/intlayer_with_react-i18next.md +68 -290
  70. package/blog/ru/intlayer_with_react-intl.md +62 -265
  71. package/blog/tr/intlayer_with_i18next.md +71 -107
  72. package/blog/tr/intlayer_with_next-i18next.md +72 -297
  73. package/blog/tr/intlayer_with_next-intl.md +58 -339
  74. package/blog/tr/intlayer_with_react-i18next.md +69 -291
  75. package/blog/tr/intlayer_with_react-intl.md +63 -285
  76. package/blog/vi/intlayer_with_i18next.md +126 -0
  77. package/blog/vi/intlayer_with_next-i18next.md +142 -0
  78. package/blog/vi/intlayer_with_next-intl.md +111 -0
  79. package/blog/vi/intlayer_with_react-i18next.md +124 -0
  80. package/blog/vi/intlayer_with_react-intl.md +122 -0
  81. package/blog/zh/intlayer_with_i18next.md +67 -102
  82. package/blog/zh/intlayer_with_next-i18next.md +72 -296
  83. package/blog/zh/intlayer_with_next-intl.md +58 -336
  84. package/blog/zh/intlayer_with_react-i18next.md +68 -290
  85. package/blog/zh/intlayer_with_react-intl.md +63 -106
  86. package/docs/ar/plugins/sync-json.md +244 -0
  87. package/docs/de/plugins/sync-json.md +244 -0
  88. package/docs/en/intlayer_cli.md +25 -0
  89. package/docs/en/intlayer_with_nextjs_14.md +2 -0
  90. package/docs/en/intlayer_with_nextjs_15.md +2 -0
  91. package/docs/en/intlayer_with_nextjs_16.md +2 -0
  92. package/docs/en/plugins/sync-json.md +1 -1
  93. package/docs/en-GB/plugins/sync-json.md +244 -0
  94. package/docs/es/plugins/sync-json.md +244 -0
  95. package/docs/fr/plugins/sync-json.md +244 -0
  96. package/docs/hi/plugins/sync-json.md +244 -0
  97. package/docs/id/plugins/sync-json.md +244 -0
  98. package/docs/it/plugins/sync-json.md +244 -0
  99. package/docs/ja/plugins/sync-json.md +244 -0
  100. package/docs/ko/plugins/sync-json.md +244 -0
  101. package/docs/pl/plugins/sync-json.md +244 -0
  102. package/docs/pt/plugins/sync-json.md +244 -0
  103. package/docs/ru/plugins/sync-json.md +244 -0
  104. package/docs/tr/plugins/sync-json.md +245 -0
  105. package/docs/vi/plugins/sync-json.md +244 -0
  106. package/docs/zh/plugins/sync-json.md +244 -0
  107. package/package.json +14 -14
@@ -1,346 +1,124 @@
1
1
  ---
2
2
  createdAt: 2025-01-02
3
- updatedAt: 2025-06-29
4
- title: Intlayerreact-i18next
5
- description: 比较React应用程序的react-i18next与Intlayer
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
- - Next.js
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
- # React 国际化 (i18n) react-i18next Intlayer
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
- ```bash
86
- # 使用 yarn
87
- yarn add intlayer react-i18next i18next i18next-resources-to-backend
88
- ```
28
+ ## 什么是 Intlayer?
89
29
 
90
- ```bash
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
- - **intlayer** 用于管理 i18n 配置、内容声明和构建字典输出的 CLI 和核心库。
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
- ## 配置 Intlayer 以导出 i18next 字典
36
+ 虽然 Intlayer 提供了一个出色的独立 i18n 解决方案(请参阅我们的[React 集成指南](https://github.com/aymericzip/intlayer/blob/main/docs/docs/zh/intlayer_with_vite+react.md)),但您可能出于以下几个原因想将其与 react-i18next 结合使用:
104
37
 
105
- 在项目根目录中创建(或更新)`intlayer.config.ts`:
38
+ 1. **现有代码库**:您已经有一个成熟的 react-i18next 实现,并希望逐步迁移到 Intlayer 以获得更好的开发者体验。
39
+ 2. **遗留需求**:您的项目需要兼容现有的 react-i18next 插件或工作流程。
40
+ 3. **团队熟悉度**:您的团队熟悉 react-i18next,但希望获得更好的内容管理。
106
41
 
107
- ```typescript title="intlayer.config.ts"
108
- import { Locales, type IntlayerConfig } from "intlayer";
42
+ **为此,Intlayer 可以作为 react-i18next 的适配器来实现,帮助您在 CLI 或 CI/CD 流水线中自动化 JSON 翻译、测试翻译等功能。**
109
43
 
110
- const config: IntlayerConfig = {
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
- export default config;
127
- ```
48
+ <TOC/>
128
49
 
129
- > **注意**:如果您不使用 TypeScript,可以将此配置文件创建为 `.cjs`、`.mjs` 或 `.js`(请参阅 [Intlayer 文档](https://intlayer.org/zh/doc/concept/configuration) 以获取详细信息)。
50
+ ## 使用 react-i18next 设置 Intlayer 的分步指南
130
51
 
131
- ## 构建 i18next 资源
52
+ ### 第一步:安装依赖
132
53
 
133
- 一旦您的内容声明到位(下一部分),运行 **Intlayer 构建命令**:
54
+ 安装必要的包:
134
55
 
135
- ```bash
136
- # 使用 npm
137
- npx run intlayer build
56
+ ```bash packageManager="npm"
57
+ npm install intlayer @intlayer/sync-json-plugin
138
58
  ```
139
59
 
140
- ```bash
141
- # 使用 yarn
142
- yarn intlayer build
60
+ ```bash packageManager="pnpm"
61
+ pnpm add intlayer @intlayer/sync-json-plugin
143
62
  ```
144
63
 
145
- ```bash
146
- # 使用 pnpm
147
- pnpm intlayer build
64
+ ```bash packageManager="yarn"
65
+ yarn add intlayer @intlayer/sync-json-plugin
148
66
  ```
149
67
 
150
- > 这将在默认情况下在 `./i18next/resources` 目录中生成您的 i18next 资源。
68
+ **包描述:**
151
69
 
152
- 一个典型的输出可能如下所示:
70
+ - **intlayer**:用于国际化管理、内容声明和构建的核心库
71
+ - **@intlayer/sync-json-plugin**:将 Intlayer 内容声明导出为兼容 react-i18next 的 JSON 格式的插件
153
72
 
154
- ```bash
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
- 其中每个 **Intlayer** 声明键用作 **i18next 命名空间**(例如,`my-content.json`)。
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
- ```javascript title="i18n.js"
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
- 然后,在您的 **根** 或 **索引** 文件(例如,`src/index.tsx`)中,导入此 `i18n` 设置 **在** 渲染 `App` 之前:
220
-
221
- ```typescript
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 content = {
244
- // “key” 将是您的 i18next 命名空间(例如,“my-component”)
245
- key: "my-component",
246
- content: {
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
- } satisfies Dictionary;
260
-
261
- export default content;
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 MyComponent;
95
+ export default config;
295
96
  ```
296
97
 
297
- > 请注意,`t` 函数引用您生成的 JSON 中的 **键**。对于名为 `heading` 的 Intlayer 内容条目,您将使用 `t("heading")`。
298
-
299
- ## 可选:与 Create React App 脚本集成 (CRACO)
300
-
301
- **react-intlayer** 提供了一种基于 CRACO 的自定义构建和开发服务器配置方法。如果您希望 Intlayer 的构建步骤无缝集成,可以:
98
+ `syncJSON` 插件会自动包装 JSON。它会读取和写入 JSON 文件,而不改变内容结构。
302
99
 
303
- 1. **安装 react-intlayer**(如果您还没有):
304
- ```bash
305
- npm install react-intlayer --save-dev
306
- ```
307
- 2. **调整您的 `package.json` 脚本**以使用 `react-intlayer` 脚本:
100
+ 如果你想让 JSON intlayer 内容声明文件(`.content` 文件)共存,Intlayer 会按以下方式处理:
308
101
 
309
- ```jsonc
310
- "scripts": {
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
- > `react-intlayer` 脚本基于 [CRACO](https://craco.js.org/)。您还可以根据 intlayer craco 插件实现自己的设置。[在此处查看示例](https://github.com/aymericzip/intlayer/blob/main/examples/react-app/craco.config.js)。
105
+ 如果使用 CLI 翻译 JSON,或使用 CMS 进行更改,Intlayer 将使用新的翻译更新 JSON 文件。
318
106
 
319
- 现在,运行 `npm run build`、`yarn build` `pnpm build` 将同时触发 Intlayer 和 CRA 的构建。
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
- 建议 **忽略** Intlayer 生成的自动生成文件和文件夹。将以下行添加到您的 `.gitignore`:
111
+ 建议忽略自动生成的 Intlayer 文件:
339
112
 
340
- ```plaintext
113
+ ```plaintext fileName=".gitignore"
341
114
  # 忽略 Intlayer 生成的文件
342
115
  .intlayer
343
- i18next
344
116
  ```
345
117
 
346
- 您通常不应提交这些资源或 `.intlayer` 内部构建工件,因为它们可以在每次构建时重新生成。
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-06-29
4
- title: Intlayerreact-intl
5
- description: 与React应用程序一起使用react-intl和Intlayer
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
- - Next.js
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
- # React Internationalization (i18n) with **react-intl** and Intlayer
24
+ # 如何使用 Intlayer 自动化您的 react-intl JSON 翻译
20
25
 
21
- 本指南展示了如何将 **Intlayer** 与 **react-intl** 集成,以管理 React 应用程序中的翻译。您将使用 Intlayer 声明可翻译的内容,然后使用 **react-intl** 这个来自 [FormatJS](https://formatjs.io/docs/react-intl) 生态系统的流行库来消费这些消息。
26
+ ## 什么是 Intlayer
22
27
 
23
- ## 概述
28
+ **Intlayer** 是一个创新的开源国际化库,旨在解决传统 i18n 解决方案的不足。它为 React 应用中的内容管理提供了一种现代化的方法。
24
29
 
25
- - **Intlayer** 允许您在项目中存储翻译,位于 **组件级** 内容声明文件(如 JSON、JS、TS 等)中。
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
- 通过配置 Intlayer **导出** 以 **react-intl 兼容** 格式的翻译,您可以自动 **生成** 和 **更新** `<IntlProvider>`(来自 react-intl)所需的消息文件。
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
- ## 为什么使用 Intlayer 和 react-intl
36
+ 1. **现有代码库**:您已经有了成熟的 react-intl 实现,并希望逐步迁移到 Intlayer 以获得更好的开发者体验。
37
+ 2. **遗留需求**:您的项目需要兼容现有的 react-intl 插件或工作流程。
38
+ 3. **团队熟悉度**:您的团队对 react-intl 很熟悉,但希望获得更好的内容管理。
33
39
 
34
- 1. **按组件内容声明**
35
- Intlayer 内容声明文件可以与您的 React 组件并存,如果组件被移动或删除,可以防止“孤立”翻译。例如:
40
+ **为此,Intlayer 可以作为 react-intl 的适配器实现,帮助您在 CLI 或 CI/CD 流水线中自动化 JSON 翻译,测试翻译内容等。**
36
41
 
37
- ```bash
38
- .
39
- └── src
40
- └── components
41
- └── MyComponent
42
- ├── index.content.ts # Intlayer 内容声明
43
- └── index.tsx # React 组件
44
- ```
42
+ 本指南将向您展示如何利用 Intlayer 优越的内容声明系统,同时保持与 react-intl 的兼容性。
45
43
 
46
- 2. **集中翻译**
47
- 每个内容声明文件收集组件所需的所有翻译。这在 TypeScript 项目中特别有帮助:缺少翻译可以在 **编译时** 被捕获。
44
+ ## 目录
48
45
 
49
- 3. **自动构建和再生**
50
- 每当您添加或更新翻译时,Intlayer 会重新生成消息 JSON 文件。然后,您可以将这些文件传入 react-intl 的 `<IntlProvider>`。
46
+ <TOC/>
51
47
 
52
- ---
48
+ ## 使用 react-intl 设置 Intlayer 的分步指南
53
49
 
54
- ## 安装
50
+ ### 第一步:安装依赖项
55
51
 
56
- 在典型的 React 项目中,安装以下内容:
52
+ 安装必要的包:
57
53
 
58
- ```bash
59
- # with npm
60
- npm install intlayer react-intl
54
+ ```bash packageManager="npm"
55
+ npm install intlayer @intlayer/sync-json-plugin
56
+ ```
61
57
 
62
- # with yarn
63
- yarn add intlayer react-intl
58
+ ```bash packageManager="pnpm"
59
+ pnpm add intlayer @intlayer/sync-json-plugin
60
+ ```
64
61
 
65
- # with pnpm
66
- pnpm add intlayer react-intl
62
+ ```bash packageManager="yarn"
63
+ yarn add intlayer @intlayer/sync-json-plugin
67
64
  ```
68
65
 
69
- ### 为什么选择这些包?
66
+ **包描述:**
70
67
 
71
- - **intlayer**:核心 CLI 和库,扫描内容声明,合并它们,并构建字典输出。
72
- - **react-intl**:来自 FormatJS 的主要库,提供 `<IntlProvider>`、`<FormattedMessage>`、`useIntl()` 和其他国际化原语。
68
+ - **intlayer**:用于国际化管理、内容声明和构建的核心库
69
+ - **@intlayer/sync-json-plugin**:将 Intlayer 内容声明导出为兼容 react-intl JSON 格式的插件
73
70
 
74
- > 如果您还没有安装 React,您也需要安装它(`react` `react-dom`)。
71
+ ### 步骤 2:实现 Intlayer 插件以包装 JSON
75
72
 
76
- ## 配置 Intlayer 以导出 react-intl 消息
73
+ 创建一个 Intlayer 配置文件以定义支持的语言环境:
77
74
 
78
- 在项目根目录下,创建 **`intlayer.config.ts`**(或 `.js`、`.mjs`、`.cjs`),如下所示:
75
+ **如果您还想导出适用于 react-intl 的 JSON 字典**,请添加 `syncJSON` 插件:
79
76
 
80
- ```typescript title="intlayer.config.ts"
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
- content: {
90
- // 告诉 Intlayer 为 react-intl 生成消息文件
91
- dictionaryOutput: ["react-intl"],
92
-
93
- // Intlayer 将在此目录中写入您的消息 JSON 文件
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
- > **注意**:有关其他文件扩展名(`.mjs`、`.cjs`、`.js`),请参阅 [Intlayer 文档](https://intlayer.org/en/doc/concept/configuration) 以获取使用细节。
96
+ `syncJSON` 插件会自动包装 JSON。它会读取和写入 JSON 文件,而不会改变内容结构。
102
97
 
103
- ---
98
+ 如果你想让 JSON 与 intlayer 内容声明文件(`.content` 文件)共存,Intlayer 会按以下方式处理:
104
99
 
105
- ## 创建您的 Intlayer 内容声明
100
+ 1. 加载 JSON 和内容声明文件,并将它们转换为 intlayer 字典。
106
101
 
107
- Intlayer 默认扫描您的代码库(在 `./src` 下),查找匹配 `*.content.{ts,tsx,js,jsx,mjs,mjx,cjs,cjx,json}` 的文件。
108
- 这里是一个 **TypeScript** 示例:
102
+ 2. 如果 JSON 文件和内容声明文件之间存在冲突,Intlayer 将合并所有字典。合并的优先级取决于插件的优先级以及内容声明文件的优先级(所有这些都是可配置的)。
109
103
 
110
- ```typescript title="src/components/MyComponent/index.content.ts"
111
- import { t, type Dictionary } from "intlayer";
104
+ 如果使用 CLI 翻译 JSON,或使用 CMS 进行更改,Intlayer 会使用新的翻译更新 JSON 文件。
112
105
 
113
- const content = {
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
- content: {
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
- export default content;
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
- 要生成 **react-intl** 的实际消息 JSON 文件,请运行:
142
-
143
- ```bash
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
- 这会扫描所有 `*.content.*` 文件,编译它们,并将结果写入您在 **`intlayer.config.ts`** 中指定的目录, 在这个例子中是 `./react-intl/messages`。
155
- 典型输出可能如下所示:
117
+ 这些文件可以在构建过程中重新生成,无需提交到版本控制。
156
118
 
157
- ```bash
158
- .
159
- └── react-intl
160
- └── messages
161
- ├── en.json
162
- ├── fr.json
163
- └── es.json
164
- ```
119
+ ### VS Code 扩展
120
+
121
+ 为了提升开发者体验,建议安装官方的 **Intlayer VS Code 扩展**:
165
122
 
166
- 每个文件都是一个 JSON 对象,其 **顶级键** 对应于每个
123
+ [从 VS Code 市场安装](https://marketplace.visualstudio.com/items?itemName=intlayer.intlayer-vs-code-extension)