@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,8 +1,8 @@
1
1
  ---
2
2
  createdAt: 2024-12-24
3
- updatedAt: 2025-06-29
4
- title: Intlayer i18next
5
- description: Intlayer i18next 集成以实现最佳国际化。比较两个框架并了解如何配置它们。
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
- # 国际化与 Intlayer i18next
28
+ # 如何使用 Intlayer 自动化您的 i18next JSON 翻译
23
29
 
24
- i18next 是一个开源国际化 (i18n) 框架,旨在用于 JavaScript 应用程序。它广泛用于管理翻译、定位和软件项目中的语言切换。然而,它有一些限制,可能会使可扩展性和开发变得复杂。
30
+ ## 什么是 Intlayer?
25
31
 
26
- Intlayer 是另一个国际化框架,解决了这些限制,提供了一种更灵活的内容声明和管理方法。让我们探讨一下 i18next Intlayer 之间的主要区别,以及如何为最佳国际化配置两者。
32
+ **Intlayer** 是一个创新的开源国际化库,旨在解决传统 i18n 解决方案的不足。它为 JavaScript 应用程序中的内容管理提供了一种现代化的方法。
27
33
 
28
- ## Intlayer i18next:主要区别
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
- ### 1. 内容声明
36
+ ## 为什么要将 Intlayer 与 i18next 结合使用?
31
37
 
32
- 使用 i18next 时,翻译字典必须在特定文件夹中声明,这可能会使应用程序的可扩展性变得复杂。相比之下,Intlayer 允许内容在与组件相同的目录中声明。这有几个优点:
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
- - **自动适应**:如果组件更改位置或被删除,Intlayer 会自动检测并适应。
40
+ 1. **现有代码库**:您已经有一个成熟的 i18next 实现,并希望逐步迁移到 Intlayer 以获得更好的开发者体验。
41
+ 2. **遗留需求**:您的项目需要兼容现有的 i18next 插件或工作流程。
42
+ 3. **团队熟悉度**:您的团队熟悉 i18next,但希望获得更好的内容管理。
36
43
 
37
- ### 2. 配置复杂性
44
+ **为此,Intlayer 可以作为 i18next 的适配器来实现,帮助您在 CLI 或 CI/CD 流水线中自动化 JSON 翻译,测试翻译内容等。**
38
45
 
39
- 配置 i18next 可能很复杂,尤其是在与服务器组件集成或为 Next.js 等框架配置中间件时。Intlayer 简化了这一过程,提供了更直接的配置。
46
+ 本指南将向您展示如何利用 Intlayer 优越的内容声明系统,同时保持与 i18next 的兼容性。
40
47
 
41
- ### 3. 翻译字典的一致性
48
+ ## 目录
42
49
 
43
- 确保翻译字典在不同语言之间的一致性在使用 i18next 时可能会面临挑战。这种不一致性如果处理不当,可能导致应用程序崩溃。Intlayer 通过对翻译内容施加约束来解决这个问题,确保没有翻译遗漏,并且翻译内容准确。
50
+ <TOC/>
44
51
 
45
- ### 4. TypeScript 集成
52
+ ## 使用 i18next 设置 Intlayer 的分步指南
46
53
 
47
- Intlayer 与 TypeScript 的集成更好,允许在代码中自动建议内容,从而提高开发效率。
54
+ ### 第一步:安装依赖项
48
55
 
49
- ### 5. 跨应用共享内容
56
+ 安装必要的包:
50
57
 
51
- Intlayer 促进了跨多个应用程序和共享库的内容声明文件共享。这个功能使得在更大的代码库中维护一致的翻译变得更容易。
52
-
53
- ## 如何使用 Intlayer 生成 i18next 字典
58
+ ```bash packageManager="npm"
59
+ npm install intlayer @intlayer/sync-json-plugin
60
+ ```
54
61
 
55
- ### 配置 Intlayer 以导出 i18next 字典
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
- > i18next 字典的导出当前处于测试阶段,并不确保与其他框架的 1:1 兼容性。建议遵循基于 Intlayer 的配置以最小化问题。
70
+ **包描述:**
60
71
 
61
- 要导出 i18next 字典,您需要适当地配置 Intlayer。以下是如何设置 Intlayer 以导出 Intlayer 和 i18next 字典的示例。
72
+ - **intlayer**:用于国际化管理、内容声明和构建的核心库
73
+ - **@intlayer/sync-json-plugin**:用于将 Intlayer 内容声明导出为兼容 i18next 的 JSON 格式的插件
62
74
 
63
- ```typescript fileName="intlayer.config.ts" codeFormat="typescript"
64
- import { Locales, type IntlayerConfig } from "intlayer";
75
+ ### 步骤 2:实现 Intlayer 插件以包装 JSON
65
76
 
66
- const config: IntlayerConfig = {
67
- content: {
68
- // 指示 Intlayer 将导出 Intlayer 和 i18next 字典
69
- dictionaryOutput: ["intlayer", "i18next"],
70
- // 从项目根目录到导出 i18n 字典的目录的相对路径
71
- i18nextResourcesDir: "./i18next/dictionaries",
72
- },
73
- };
77
+ 创建一个 Intlayer 配置文件以定义支持的语言环境:
74
78
 
75
- export default config;
76
- ```
79
+ **如果你还想导出 i18next 的 JSON 词典**,请添加 `syncJSON` 插件:
77
80
 
78
- ```javascript fileName="intlayer.config.mjs" codeFormat="esm"
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
- /** @type {import('intlayer').IntlayerConfig} */
82
- const config = {
83
- content: {
84
- // 指示 Intlayer 将导出 Intlayer 和 i18next 字典
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
- ```javascript fileName="intlayer.config.cjs" codeFormat="commonjs"
95
- const { Locales } = require("intlayer");
100
+ `syncJSON` 插件会自动包装 JSON。它会读取和写入 JSON 文件,而不会改变内容结构。
96
101
 
97
- /** @type {import('intlayer').IntlayerConfig} */
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
- module.exports = config;
108
- ```
104
+ 1. 加载 JSON 和内容声明文件,并将它们转换为 intlayer 字典。
109
105
 
110
- 通过在配置中包含 'i18next',Intlayer 生成专用的 i18next 字典,除此之外,还有 Intlayer 字典。请注意,移除 'intlayer' 可能导致与 React-Intlayer 或 Next-Intlayer 的兼容性破坏。
106
+ 2. 如果 JSON 文件和内容声明文件之间存在冲突,Intlayer 将合并所有字典。合并的优先级取决于插件的优先级以及内容声明文件的优先级(所有这些都是可配置的)。
111
107
 
112
- ### 将字典导入到您的 i18next 配置中
108
+ 如果使用 CLI 翻译 JSON,或使用 CMS 进行更改,Intlayer 会使用新的翻译更新 JSON 文件。
113
109
 
114
- 要将生成的字典导入到您的 i18next 配置中,可以使用 'i18next-resources-to-backend'。以下是如何导入您的 i18next 字典的示例:
110
+ ## Git 配置
115
111
 
116
- ```typescript fileName="i18n/client.ts" codeFormat="typescript"
117
- // i18n/client.ts
112
+ 建议忽略自动生成的 Intlayer 文件:
118
113
 
119
- import i18next from "i18next";
120
- import resourcesToBackend from "i18next-resources-to-backend";
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
- ```javascript fileName="i18n/client.mjs" codeFormat="esm"
133
- // i18n/client.mjs
134
-
135
- import i18next from "i18next";
136
- import resourcesToBackend from "i18next-resources-to-backend";
119
+ 这些文件可以在构建过程中重新生成,无需提交到版本控制。
137
120
 
138
- i18next
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
- ```javascript fileName="i18n/client.cjs" codeFormat="commonjs"
149
- // i18n/client.cjs
123
+ 为了提升开发者体验,请安装官方的 **Intlayer VS Code 扩展**:
150
124
 
151
- const i18next = require("i18next");
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
- i18next
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-08-23
4
- title: Intlayernext-i18next
5
- description: 与Next.js应用程序的next-i18next集成Intlayer
3
+ updatedAt: 2025-10-29
4
+ title: Intlayernext-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
- # Next.js 国际化 (i18n) 与 next-i18next 和 Intlayer
24
+ # 使用 next-i18next 和 Intlayer 实现 Next.js 国际化(i18n)
21
25
 
22
- both next-i18next 和 Intlayer 是为 Next.js 应用程序设计的开源国际化 (i18n) 框架。它们广泛用于软件项目中的翻译、地方化和语言切换管理。
26
+ ## 目录
23
27
 
24
- 这两种解决方案包括三个主要概念:
28
+ <TOC/>
25
29
 
26
- 1. **内容声明**:定义应用程序可翻译内容的方法。
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
- 2. **工具**:构建和解释应用程序中的内容声明的工具,例如 `getI18n()`、`useCurrentLocale()` `useChangeLocale()` 对于 next-i18next,以及 `useIntlayer()` `useLocale()` 对于 Intlayer。
32
+ **next-i18next** Next.js 应用中最流行的国际化(i18n)框架之一。它构建于强大的 **i18next** 生态系统之上,为管理 Next.js 项目的翻译、本地化和语言切换提供了全面的解决方案。
31
33
 
32
- 3. **插件和中间件**:用于管理 URL 重定向、打包优化等功能,例如 next-i18next 的 `next-i18next/middleware` 或 Intlayer 的 `intlayerMiddleware`。
34
+ 然而,next-i18next 也存在一些挑战:
33
35
 
34
- ## Intlayer vs. i18next:主要区别
36
+ - **配置复杂**:设置 next-i18next 需要多个配置文件,并且需要仔细设置服务器端和客户端的 i18n 实例。
37
+ - **翻译分散**:翻译文件通常存储在与组件分开的目录中,导致维护一致性更加困难。
38
+ - **手动命名空间管理**:开发者需要手动管理命名空间,并确保正确加载翻译资源。
39
+ - **类型安全有限**:TypeScript 支持需要额外配置,并且不提供翻译的自动类型生成。
35
40
 
36
- 要探索 i18next Intlayer 之间的区别,请查看我们的 [next-i18next vs. next-intl vs. Intlayer](https://github.com/aymericzip/intlayer/blob/main/docs/blog/zh/i18next_vs_next-intl_vs_intlayer.md) 博客文章。
41
+ ## 什么是 Intlayer
37
42
 
38
- ## 如何使用 Intlayer 生成 next-i18next 字典
43
+ **Intlayer** 是一个创新的开源国际化库,旨在解决传统 i18n 解决方案的不足。它为 Next.js 应用提供了一种现代化的内容管理方法。
39
44
 
40
- ### 为什么在 next-i18next 中使用 Intlayer
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
- 1. **灵活的放置**:Intlayer 内容声明文件可以放置在应用程序的文件树中的任何位置,简化了重复或删除组件管理,而无需留下未使用的内容声明。
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
- ```bash codeFormat="typescript"
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
- ```bash codeFormat="esm"
58
- .
59
- └── src
60
- └── components
61
- └── MyComponent
62
- ├── index.content.mjs # 内容声明文件
63
- └── index.mjx
64
- ```
57
+ 本指南将向您展示如何利用 Intlayer 优越的内容声明系统,同时保持与 next-i18next 的兼容性。
65
58
 
66
- ```bash codeFormat="cjs"
67
- .
68
- └── src
69
- └── components
70
- └── MyComponent
71
- ├── index.content.cjs # 内容声明文件
72
- └── index.cjx
73
- ```
59
+ ---
74
60
 
75
- ```bash codeFormat="json"
76
- .
77
- └── src
78
- └── components
79
- └── MyComponent
80
- ├── index.content.json # 内容声明文件
81
- └── index.jsx
82
- ```
61
+ ## 使用 next-i18next 设置 Intlayer 的分步指南
83
62
 
84
- 2. **集中翻译**:Intlayer 将所有翻译存储在一个文件中,确保没有翻译缺失。当使用 TypeScript 时,缺失的翻译会被自动检测并报告为错误。
63
+ ### 第一步:安装依赖
85
64
 
86
- ### 安装
65
+ 使用您喜欢的包管理器安装所需的包:
87
66
 
88
67
  ```bash packageManager="npm"
89
- npm install intlayer i18next next-i18next i18next-resources-to-backend
68
+ npm install intlayer @intlayer/sync-json-plugin
90
69
  ```
91
70
 
92
- ```bash packageManager="yarn"
93
- yarn add intlayer i18next next-i18next i18next-resources-to-backend
71
+ ```bash packageManager="pnpm"
72
+ pnpm add intlayer @intlayer/sync-json-plugin
94
73
  ```
95
74
 
96
- ```bash packageManager="pnpm"
97
- pnpm add intlayer i18next next-i18next i18next-resources-to-backend
75
+ ```bash packageManager="yarn"
76
+ yarn add intlayer @intlayer/sync-json-plugin
98
77
  ```
99
78
 
100
- ### 配置 Intlayer 导出 i18next 字典
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
- > 导出 i18next 资源并不能确保与其他框架的 1:1 兼容。建议坚持使用基于 Intlayer 的配置以减少问题。
88
+ ### 第2步:实现 Intlayer 插件以封装 JSON
103
89
 
104
- 要导出 i18next 资源,请在 `intlayer.config.ts` 文件中配置 Intlayer。示例配置:
90
+ 创建一个 Intlayer 配置文件以定义支持的语言环境:
105
91
 
106
- ```typescript fileName="intlayer.config.ts" codeFormat="typescript"
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
- content: {
115
- dictionaryOutput: ["i18next"],
116
- i18nextResourcesDir: "./i18next/resources",
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
- ```javascript fileName="intlayer.config.mjs" codeFormat="esm"
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
- ```javascript fileName="intlayer.config.cjs" codeFormat="commonjs"
142
- const { Locales } = require("intlayer");
115
+ 如果你想让 JSON 与 intlayer 内容声明文件(`.content` 文件)共存,Intlayer 会按以下方式处理:
143
116
 
144
- /** @type {import('intlayer').IntlayerConfig} */
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
- module.exports = config;
157
- ```
119
+ 2. 如果 JSON 文件和内容声明文件之间存在冲突,Intlayer 将合并所有字典。合并的优先级取决于插件的优先级以及内容声明文件的优先级(所有这些都是可配置的)。
158
120
 
159
- 以下是文档其余部分的续写和更正:
121
+ 如果使用 CLI 翻译 JSON,或使用 CMS 进行更改,Intlayer 将使用新的翻译更新 JSON 文件。
160
122
 
161
123
  ---
162
124
 
163
- ### 将字典导入到您的 i18next 配置中
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
- ```javascript fileName="i18n/client.cjs" codeFormat="commonjs"
192
- const i18next = require("i18next");
193
- const resourcesToBackend = require("i18next-resources-to-backend");
127
+ 将生成的文件排除在版本控制之外:
194
128
 
195
- i18next.use(
196
- resourcesToBackend(
197
- (language, namespace) =>
198
- import(`../i18next/resources/${language}/${namespace}.json`)
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
- ```typescript fileName="**/*.content.ts" contentDeclarationFormat="typescript"
208
- import { t, type Dictionary } from "intlayer";
139
+ 为了提升开发者体验,请安装官方的 **Intlayer VS Code 扩展**:
209
140
 
210
- const content = {
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
- ```javascript fileName="**/*.content.mjs" contentDeclarationFormat="esm"
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)