@intlayer/docs 7.0.3 → 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.
Files changed (159) 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 +67 -103
  12. package/blog/en/intlayer_with_next-i18next.md +69 -294
  13. package/blog/en/intlayer_with_next-intl.md +48 -300
  14. package/blog/en/intlayer_with_react-i18next.md +61 -289
  15. package/blog/en/intlayer_with_react-intl.md +61 -284
  16. package/blog/en/next-i18next_vs_next-intl_vs_intlayer.md +1 -1
  17. package/blog/en-GB/intlayer_with_i18next.md +67 -103
  18. package/blog/en-GB/intlayer_with_next-i18next.md +71 -292
  19. package/blog/en-GB/intlayer_with_next-intl.md +58 -337
  20. package/blog/en-GB/intlayer_with_react-i18next.md +67 -289
  21. package/blog/en-GB/intlayer_with_react-intl.md +61 -264
  22. package/blog/es/intlayer_with_i18next.md +67 -103
  23. package/blog/es/intlayer_with_next-i18next.md +71 -296
  24. package/blog/es/intlayer_with_next-intl.md +57 -338
  25. package/blog/es/intlayer_with_react-i18next.md +68 -290
  26. package/blog/es/intlayer_with_react-intl.md +62 -265
  27. package/blog/fr/intlayer_with_i18next.md +66 -104
  28. package/blog/fr/intlayer_with_next-i18next.md +82 -285
  29. package/blog/fr/intlayer_with_next-intl.md +57 -338
  30. package/blog/fr/intlayer_with_react-i18next.md +67 -289
  31. package/blog/fr/intlayer_with_react-intl.md +61 -264
  32. package/blog/hi/intlayer_with_i18next.md +68 -104
  33. package/blog/hi/intlayer_with_next-i18next.md +74 -299
  34. package/blog/hi/intlayer_with_next-intl.md +57 -239
  35. package/blog/hi/intlayer_with_react-i18next.md +69 -291
  36. package/blog/hi/intlayer_with_react-intl.md +65 -268
  37. package/blog/id/intlayer_with_i18next.md +126 -0
  38. package/blog/id/intlayer_with_next-i18next.md +142 -0
  39. package/blog/id/intlayer_with_next-intl.md +113 -0
  40. package/blog/id/intlayer_with_react-i18next.md +124 -0
  41. package/blog/id/intlayer_with_react-intl.md +122 -0
  42. package/blog/it/intlayer_with_i18next.md +67 -103
  43. package/blog/it/intlayer_with_next-i18next.md +71 -296
  44. package/blog/it/intlayer_with_next-intl.md +57 -338
  45. package/blog/it/intlayer_with_react-i18next.md +68 -290
  46. package/blog/it/intlayer_with_react-intl.md +62 -265
  47. package/blog/ja/intlayer_with_i18next.md +68 -103
  48. package/blog/ja/intlayer_with_next-i18next.md +85 -283
  49. package/blog/ja/intlayer_with_next-intl.md +58 -336
  50. package/blog/ja/intlayer_with_react-i18next.md +68 -290
  51. package/blog/ja/intlayer_with_react-intl.md +62 -264
  52. package/blog/ko/intlayer_with_i18next.md +80 -96
  53. package/blog/ko/intlayer_with_next-i18next.md +85 -287
  54. package/blog/ko/intlayer_with_next-intl.md +68 -327
  55. package/blog/ko/intlayer_with_react-i18next.md +68 -290
  56. package/blog/ko/intlayer_with_react-intl.md +64 -266
  57. package/blog/pl/intlayer_with_i18next.md +126 -0
  58. package/blog/pl/intlayer_with_next-i18next.md +142 -0
  59. package/blog/pl/intlayer_with_next-intl.md +111 -0
  60. package/blog/pl/intlayer_with_react-i18next.md +124 -0
  61. package/blog/pl/intlayer_with_react-intl.md +122 -0
  62. package/blog/pt/intlayer_with_i18next.md +67 -103
  63. package/blog/pt/intlayer_with_next-i18next.md +72 -293
  64. package/blog/pt/intlayer_with_next-intl.md +57 -256
  65. package/blog/pt/intlayer_with_react-i18next.md +104 -78
  66. package/blog/pt/intlayer_with_react-intl.md +62 -266
  67. package/blog/ru/intlayer_with_i18next.md +66 -104
  68. package/blog/ru/intlayer_with_next-i18next.md +71 -296
  69. package/blog/ru/intlayer_with_next-intl.md +58 -337
  70. package/blog/ru/intlayer_with_react-i18next.md +68 -290
  71. package/blog/ru/intlayer_with_react-intl.md +62 -265
  72. package/blog/tr/intlayer_with_i18next.md +71 -107
  73. package/blog/tr/intlayer_with_next-i18next.md +72 -297
  74. package/blog/tr/intlayer_with_next-intl.md +58 -339
  75. package/blog/tr/intlayer_with_react-i18next.md +69 -291
  76. package/blog/tr/intlayer_with_react-intl.md +63 -285
  77. package/blog/vi/intlayer_with_i18next.md +126 -0
  78. package/blog/vi/intlayer_with_next-i18next.md +142 -0
  79. package/blog/vi/intlayer_with_next-intl.md +111 -0
  80. package/blog/vi/intlayer_with_react-i18next.md +124 -0
  81. package/blog/vi/intlayer_with_react-intl.md +122 -0
  82. package/blog/zh/intlayer_with_i18next.md +67 -102
  83. package/blog/zh/intlayer_with_next-i18next.md +72 -296
  84. package/blog/zh/intlayer_with_next-intl.md +58 -336
  85. package/blog/zh/intlayer_with_react-i18next.md +68 -290
  86. package/blog/zh/intlayer_with_react-intl.md +63 -106
  87. package/dist/cjs/generated/blog.entry.cjs +13 -1
  88. package/dist/cjs/generated/blog.entry.cjs.map +1 -1
  89. package/dist/cjs/generated/docs.entry.cjs +13 -1
  90. package/dist/cjs/generated/docs.entry.cjs.map +1 -1
  91. package/dist/cjs/generated/frequentQuestions.entry.cjs +13 -1
  92. package/dist/cjs/generated/frequentQuestions.entry.cjs.map +1 -1
  93. package/dist/cjs/generated/legal.entry.cjs +13 -1
  94. package/dist/cjs/generated/legal.entry.cjs.map +1 -1
  95. package/dist/esm/generated/blog.entry.mjs +13 -2
  96. package/dist/esm/generated/blog.entry.mjs.map +1 -1
  97. package/dist/esm/generated/docs.entry.mjs +13 -2
  98. package/dist/esm/generated/docs.entry.mjs.map +1 -1
  99. package/dist/esm/generated/frequentQuestions.entry.mjs +13 -2
  100. package/dist/esm/generated/frequentQuestions.entry.mjs.map +1 -1
  101. package/dist/esm/generated/legal.entry.mjs +13 -2
  102. package/dist/esm/generated/legal.entry.mjs.map +1 -1
  103. package/dist/types/generated/blog.entry.d.ts.map +1 -1
  104. package/dist/types/generated/docs.entry.d.ts.map +1 -1
  105. package/dist/types/generated/frequentQuestions.entry.d.ts.map +1 -1
  106. package/dist/types/generated/legal.entry.d.ts.map +1 -1
  107. package/docs/ar/plugins/sync-json.md +244 -0
  108. package/docs/de/plugins/sync-json.md +244 -0
  109. package/docs/de/releases/v7.md +1 -18
  110. package/docs/en/CI_CD.md +1 -1
  111. package/docs/en/configuration.md +1 -1
  112. package/docs/en/formatters.md +1 -1
  113. package/docs/en/how_works_intlayer.md +1 -1
  114. package/docs/en/intlayer_CMS.md +1 -1
  115. package/docs/en/intlayer_cli.md +26 -1
  116. package/docs/en/intlayer_with_nextjs_14.md +3 -1
  117. package/docs/en/intlayer_with_nextjs_15.md +3 -1
  118. package/docs/en/intlayer_with_nextjs_16.md +3 -1
  119. package/docs/en/intlayer_with_nextjs_page_router.md +1 -1
  120. package/docs/en/intlayer_with_nuxt.md +1 -1
  121. package/docs/en/intlayer_with_react_native+expo.md +1 -1
  122. package/docs/en/intlayer_with_react_router_v7.md +1 -1
  123. package/docs/en/intlayer_with_tanstack.md +1 -1
  124. package/docs/en/intlayer_with_vite+preact.md +1 -1
  125. package/docs/en/intlayer_with_vite+react.md +1 -1
  126. package/docs/en/intlayer_with_vite+solid.md +1 -1
  127. package/docs/en/intlayer_with_vite+svelte.md +1 -1
  128. package/docs/en/intlayer_with_vite+vue.md +1 -1
  129. package/docs/en/plugins/sync-json.md +1 -1
  130. package/docs/en/roadmap.md +1 -1
  131. package/docs/en-GB/plugins/sync-json.md +244 -0
  132. package/docs/es/plugins/sync-json.md +244 -0
  133. package/docs/es/releases/v7.md +1 -18
  134. package/docs/fr/intlayer_with_nextjs_16.md +2 -51
  135. package/docs/fr/plugins/sync-json.md +244 -0
  136. package/docs/fr/releases/v7.md +1 -18
  137. package/docs/hi/intlayer_with_nextjs_16.md +3 -2
  138. package/docs/hi/plugins/sync-json.md +244 -0
  139. package/docs/id/plugins/sync-json.md +244 -0
  140. package/docs/id/releases/v7.md +1 -18
  141. package/docs/it/plugins/sync-json.md +244 -0
  142. package/docs/it/releases/v7.md +1 -18
  143. package/docs/ja/intlayer_with_nextjs_16.md +44 -205
  144. package/docs/ja/plugins/sync-json.md +244 -0
  145. package/docs/ja/releases/v7.md +1 -18
  146. package/docs/ko/plugins/sync-json.md +244 -0
  147. package/docs/ko/releases/v7.md +1 -18
  148. package/docs/pl/plugins/sync-json.md +244 -0
  149. package/docs/pt/intlayer_with_nextjs_16.md +1 -52
  150. package/docs/pt/plugins/sync-json.md +244 -0
  151. package/docs/ru/plugins/sync-json.md +244 -0
  152. package/docs/tr/plugins/sync-json.md +245 -0
  153. package/docs/vi/plugins/sync-json.md +244 -0
  154. package/docs/zh/plugins/sync-json.md +244 -0
  155. package/package.json +14 -14
  156. package/src/generated/blog.entry.ts +26 -3
  157. package/src/generated/docs.entry.ts +26 -3
  158. package/src/generated/frequentQuestions.entry.ts +26 -3
  159. package/src/generated/legal.entry.ts +26 -3
@@ -1,392 +1,114 @@
1
1
  ---
2
2
  createdAt: 2025-01-02
3
- updatedAt: 2025-06-29
4
- title: Intlayernext-intl
5
- description: 与React应用程序的next-intl集成Intlayer
6
- keywords:
7
- - next-intl
8
- - Intlayer
9
- - 国际化
10
- - 文档
11
- - Next.js
12
- - JavaScript
13
- - React
3
+ updatedAt: 2025-10-29
4
+ title: 如何使用 Intlayer 自动化您的 next-intl JSON 翻译
5
+ description: 使用 Intlayer 和 next-intl 自动化您的 JSON 翻译,提升 Next.js 应用的国际化体验。
14
6
  slugs:
15
7
  - blog
16
8
  - intlayer-with-next-intl
9
+ history:
10
+ - version: 7.0.0
11
+ date: 2025-10-29
12
+ changes: 更改为 syncJSON 插件
17
13
  ---
18
14
 
19
- # Next.js 国际化 (i18n) next-intl Intlayer
15
+ # 如何使用 Intlayer 自动化您的 next-intl JSON 翻译
20
16
 
21
- next-intl Intlayer 是为 Next.js 应用程序设计的开源国际化 (i18n) 框架。它们广泛用于管理软件项目中的翻译、本地化和语言切换。
17
+ ## 什么是 Intlayer
22
18
 
23
- 它们共享三个主要概念:
19
+ **Intlayer** 是一个创新的开源国际化库,旨在解决传统 i18n 解决方案的不足。它为 Next.js 应用提供了一种现代化的内容管理方法。
24
20
 
25
- 1. **内容声明**: 定义应用程序可翻译内容的方法。
26
- - 在 Intlayer 中称为 `content declaration file`,可以是导出结构化数据的 JSON、JS 或 TS 文件。有关更多信息,请参见 [Intlayer 文档](https://github.com/aymericzip/intlayer/blob/main/docs/docs/zh/concept/content.md)。
27
- - 在 next-intl 中称为 `messages` 或 `locale messages`,通常在 JSON 文件中。有关更多信息,请参见 [next-intl 文档](https://github.com/amannn/next-intl/blob/main/docs/zh/introduction.md)。
21
+ 请参阅我们博客文章中的具体比较:[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)。
28
22
 
29
- 2. **工具**: 用于构建和解释应用程序中的内容声明的工具,例如 Intlayer `useIntlayer()` 或 `useLocale()`,以及 next-intl 的 `useTranslations()`。
23
+ ## 为什么要将 Intlayer next-intl 结合使用?
30
24
 
31
- 3. **插件和中间件**: 用于管理 URL 重定向、打包优化等功能, , 例如,Intlayer `intlayerMiddleware` next-intl 的 [`createMiddleware`](https://github.com/amannn/next-intl/blob/main/docs/zh/api/middleware.md)
25
+ 虽然 Intlayer 提供了一个出色的独立 i18n 解决方案(请参阅我们的[Next.js 集成指南](https://github.com/aymericzip/intlayer/blob/main/docs/docs/zh/intlayer_with_nextjs_16.md)),但您可能出于以下几个原因想将其与 next-intl 结合使用:
32
26
 
33
- ## Intlayer next-intl: 关键区别
27
+ 1. **现有代码库**:您已经有了成熟的 next-intl 实现,并希望逐步迁移到 Intlayer 以获得更好的开发者体验。
28
+ 2. **遗留需求**:您的项目需要兼容现有的 next-intl 插件或工作流程。
29
+ 3. **团队熟悉度**:您的团队对 next-intl 很熟悉,但希望获得更好的内容管理。
34
30
 
35
- 要深入分析 Intlayer 如何与其他 i18n 库(如 next-intl)进行比较,请查看 [next-i18next vs. next-intl vs. Intlayer 博客文章](https://github.com/aymericzip/intlayer/blob/main/docs/blog/zh/i18next_vs_next-intl_vs_intlayer.md)。
31
+ **为此,Intlayer 可以作为 next-intl 的适配器来实现,帮助您在 CLI CI/CD 流水线中自动化 JSON 翻译,测试翻译内容等。**
36
32
 
37
- ## 如何生成 next-intl 消息与 Intlayer
33
+ 本指南将向您展示如何利用 Intlayer 优越的内容声明系统,同时保持与 next-intl 的兼容性。
38
34
 
39
- ### 为什么使用 Intlayer 与 next-intl?
35
+ ## 目录
40
36
 
41
- Intlayer 内容声明文件通常提供更好的开发者体验。由于两个主要优点,它们更加灵活和易于维护:
37
+ <TOC/>
42
38
 
43
- 1. **灵活的位置**: 你可以将 Intlayer 内容声明文件放置在应用程序文件树的任何位置。这使得重命名或删除组件变得简单,而不会留下未使用或悬挂的消息文件。
39
+ ## 使用 next-intl 设置 Intlayer 的逐步指南
44
40
 
45
- 示例文件结构:
41
+ ### 第一步:安装依赖
46
42
 
47
- ```bash codeFormat="typescript"
48
- .
49
- └── src
50
- └── components
51
- └── MyComponent
52
- ├── index.content.ts # 内容声明文件
53
- └── index.tsx
54
- ```
55
-
56
- ```bash codeFormat="esm"
57
- .
58
- └── src
59
- └── components
60
- └── MyComponent
61
- ├── index.content.mjs # 内容声明文件
62
- └── index.mjx
63
- ```
64
-
65
- ```bash codeFormat="cjs"
66
- .
67
- └── src
68
- └── components
69
- └── MyComponent
70
- ├── index.content.cjs # 内容声明文件
71
- └── index.cjx
72
- ```
73
-
74
- ```bash codeFormat="json"
75
- .
76
- └── src
77
- └── components
78
- └── MyComponent
79
- ├── index.content.json # 内容声明文件
80
- └── index.jsx
81
- ```
82
-
83
- 2. **集中翻译**: Intlayer 将所有翻译存储在一个内容声明中,确保没有翻译被遗漏。在 TypeScript 项目中,缺失翻译会自动被标记为类型错误,为开发者提供即时反馈。
84
-
85
- ### 安装
86
-
87
- 要一起使用 Intlayer 和 next-intl,请安装这两个库:
43
+ 安装必要的包:
88
44
 
89
45
  ```bash packageManager="npm"
90
- npm install intlayer next-intl
46
+ npm install intlayer @intlayer/sync-json-plugin
91
47
  ```
92
48
 
93
- ```bash packageManager="yarn"
94
- yarn add intlayer next-intl
49
+ ```bash packageManager="pnpm"
50
+ pnpm add intlayer @intlayer/sync-json-plugin
95
51
  ```
96
52
 
97
- ```bash packageManager="pnpm"
98
- pnpm add intlayer next-intl
53
+ ```bash packageManager="yarn"
54
+ yarn add intlayer @intlayer/sync-json-plugin
99
55
  ```
100
56
 
101
- ### 配置 Intlayer 导出 next-intl 消息
57
+ **软件包说明:**
102
58
 
103
- > **注意:** 从 Intlayer 导出消息到 next-intl 可能会引入结构上的细微差异。如果可能,保持 Intlayer 或 next-intl 单独的流程以简化集成。如果确实需要从 Intlayer 生成 next-intl 消息,请遵循以下步骤。
59
+ - **intlayer**:国际化管理、内容声明和构建的核心库
60
+ - **@intlayer/sync-json-plugin**:用于将 Intlayer 内容声明导出为 next-intl 兼容的 JSON 格式的插件
104
61
 
105
- 在项目根目录中创建或更新一个 `intlayer.config.ts` 文件(或 `.mjs` / `.cjs`):
62
+ ### 第2步:实现 Intlayer 插件以包装 JSON
106
63
 
107
- ```typescript fileName="intlayer.config.ts" codeFormat="typescript"
108
- import { Locales, type IntlayerConfig } from "intlayer";
64
+ 创建一个 Intlayer 配置文件以定义支持的语言环境:
109
65
 
110
- const config: IntlayerConfig = {
111
- internationalization: {
112
- locales: [Locales.ENGLISH, Locales.FRENCH, Locales.SPANISH],
113
- defaultLocale: Locales.ENGLISH,
114
- },
115
- content: {
116
- dictionaryOutput: ["next-intl"], // 使用 next-intl 输出
117
- nextIntlMessagesDir: "./intl/messages", // 保存 next-intl 消息的位置
118
- },
119
- };
120
-
121
- export default config;
122
- ```
123
-
124
- ```javascript fileName="intlayer.config.mjs" codeFormat="esm"
125
- import { Locales } from "intlayer";
126
-
127
- /** @type {import('intlayer').IntlayerConfig} */
128
- const config = {
129
- internationalization: {
130
- locales: [Locales.ENGLISH, Locales.FRENCH, Locales.SPANISH],
131
- defaultLocale: Locales.ENGLISH,
132
- },
133
- content: {
134
- dictionaryOutput: ["react-intl"],
135
- nextIntlMessagesDir: "./intl/messages",
136
- },
137
- };
66
+ **如果您还想导出适用于 next-intl JSON 字典**,请添加 `syncJSON` 插件:
138
67
 
139
- export default config;
140
- ```
141
-
142
- ```javascript fileName="intlayer.config.cjs" codeFormat="commonjs"
143
- const { Locales } = require("intlayer");
68
+ ```typescript fileName="intlayer.config.ts"
69
+ import { Locales, type IntlayerConfig } from "intlayer";
70
+ import { syncJSON } from "@intlayer/sync-json-plugin";
144
71
 
145
- /** @type {import('intlayer').IntlayerConfig} */
146
- const config = {
72
+ const config: IntlayerConfig = {
147
73
  internationalization: {
148
74
  locales: [Locales.ENGLISH, Locales.FRENCH, Locales.SPANISH],
149
75
  defaultLocale: Locales.ENGLISH,
150
76
  },
151
- content: {
152
- dictionaryOutput: ["next-intl"],
153
- nextIntlMessagesDir: "./intl/messages",
154
- },
155
- };
156
-
157
- module.exports = config;
158
- ```
159
-
160
- ### 内容声明
161
-
162
- 以下是多个格式的内容声明文件示例。Intlayer 将把这些编译成 next-intl 可以使用的消息文件。
163
-
164
- ```typescript fileName="**/*.content.ts" contentDeclarationFormat="typescript"
165
- import { t, type Dictionary } from "intlayer";
166
-
167
- const content = {
168
- key: "my-component",
169
- content: {
170
- helloWorld: t({
171
- en: "Hello World",
172
- es: "Hola Mundo",
173
- fr: "Bonjour le monde",
174
- }),
175
- },
176
- } satisfies Dictionary;
177
-
178
- export default content;
179
- ```
180
-
181
- ```javascript fileName="**/*.content.mjs" contentDeclarationFormat="esm"
182
- import { t } from "intlayer";
183
-
184
- /** @type {import('intlayer').Dictionary} */
185
- const content = {
186
- key: "my-component",
187
- content: {
188
- helloWorld: t({
189
- en: "Hello World",
190
- es: "Hola Mundo",
191
- fr: "Bonjour le monde",
77
+ plugins: [
78
+ syncJSON({
79
+ source: ({ key, locale }) => `./messages/${locale}/${key}.json`,
192
80
  }),
193
- },
81
+ ],
194
82
  };
195
83
 
196
- export default content;
84
+ export default config;
197
85
  ```
198
86
 
199
- ```javascript fileName="**/*.content.cjs" contentDeclarationFormat="commonjs"
200
- const { t } = require("intlayer");
87
+ `syncJSON` 插件会自动包装 JSON。它会读取和写入 JSON 文件,而不会改变内容结构。
201
88
 
202
- module.exports = {
203
- key: "my-component",
204
- content: {
205
- helloWorld: t({
206
- en: "Hello World",
207
- es: "Hola Mundo",
208
- fr: "Bonjour le monde",
209
- }),
210
- },
211
- };
212
- ```
89
+ 如果你想让 JSON 与 intlayer 内容声明文件(`.content` 文件)共存,Intlayer 会按以下方式处理:
213
90
 
214
- ```json fileName="**/*.content.json" contentDeclarationFormat="json"
215
- {
216
- "$schema": "https://intlayer.org/schema.json",
217
- "key": "my-component",
218
- "content": {
219
- "helloWorld": {
220
- "nodeType": "translation",
221
- "translation": {
222
- "en": "Hello World",
223
- "fr": "Bonjour le monde",
224
- "es": "Hola Mundo"
225
- }
226
- }
227
- }
228
- }
229
- ```
230
-
231
- ### 构建 next-intl 消息
91
+ 1. 加载 JSON 和内容声明文件,并将它们转换为 intlayer 字典。
232
92
 
233
- 要构建 next-intl 的消息文件,请运行:
93
+ 2. 如果 JSON 文件和内容声明文件之间存在冲突,Intlayer 将合并所有字典。合并的优先级取决于插件的优先级以及内容声明文件的优先级(所有均可配置)。
234
94
 
235
- ```bash packageManager="npm"
236
- npx intlayer dictionaries build
237
- ```
95
+ 如果使用 CLI 翻译 JSON 或使用 CMS 进行更改,Intlayer 会使用新的翻译更新 JSON 文件。
238
96
 
239
- ```bash packageManager="yarn"
240
- yarn intlayer build
241
- ```
97
+ ## Git 配置
242
98
 
243
- ```bash packageManager="pnpm"
244
- pnpm intlayer build
245
- ```
99
+ 建议忽略自动生成的 Intlayer 文件:
246
100
 
247
- 这将在 `./intl/messages` 目录中生成资源(如 `intlayer.config.*` 中配置的)。预期输出:
248
-
249
- ```bash
250
- .
251
- └── intl
252
- └── messages
253
- └── zh
254
- └── my-content.json
255
- └── fr
256
- └── my-content.json
257
- └── es
258
- └── my-content.json
101
+ ```plaintext fileName=".gitignore"
102
+ # 忽略 Intlayer 生成的文件
103
+ .intlayer
259
104
  ```
260
105
 
261
- 每个文件包含来自所有 Intlayer 内容声明的编译消息。顶级键通常与 `content.key` 字段匹配。
262
-
263
- ### 在你的 Next.js 应用中使用 next-intl
264
-
265
- > 欲了解更多详细信息,请参阅官方 [next-intl 使用文档](https://github.com/amannn/next-intl/blob/main/docs/zh/getting-started.md)。
266
-
267
- 1. **创建中间件 (可选):**
268
- 如果你想管理自动语言检测或重定向,请使用 next-intl 的 [createMiddleware](https://github.com/amannn/next-intl/blob/main/docs/zh/api/middleware.md)。
269
-
270
- ```typescript fileName="middleware.ts"
271
- import createMiddleware from "next-intl/middleware";
272
- import { NextResponse } from "next/server";
273
-
274
- export default createMiddleware({
275
- locales: ["en", "fr", "es"],
276
- defaultLocale: "en",
277
- });
278
-
279
- export const config = {
280
- matcher: ["/((?!api|_next|.*\\..*).*)"],
281
- };
282
- ```
283
-
284
- 2. **创建一个 `layout.tsx` 或 `_app.tsx` 来加载消息:**
285
- 如果你正在使用 App Router(Next.js 13+),请创建一个布局:
286
-
287
- ```typescript fileName="app/[locale]/layout.tsx"
288
- import { NextIntlClientProvider } from 'next-intl';
289
- import { notFound } from 'next/navigation';
290
- import React, { ReactNode } from 'react';
291
-
292
-
293
- export default async function RootLayout({
294
- children,
295
- params
296
- }: {
297
- children: ReactNode;
298
- params: { locale: string };
299
- }) {
300
- let messages;
301
- try {
302
- messages = (await import(`../../intl/messages/${params.locale}.json`)).default;
303
- } catch (error) {
304
- notFound();
305
- }
306
-
307
- return (
308
- <html lang={params.locale}>
309
- <body>
310
- <NextIntlClientProvider locale={params.locale} messages={messages}>
311
- {children}
312
- </NextIntlClientProvider>
313
- </body>
314
- </html>
315
- );
316
- }
317
- ```
318
-
319
- 如果你正在使用 Pages Router(Next.js 12 或更早),请在 `_app.tsx` 中加载消息:
320
-
321
- ```typescript fileName="pages/_app.tsx"
322
- import type { AppProps } from 'next/app';
323
- import { NextIntlProvider } from 'next-intl';
324
-
325
- function MyApp({ Component, pageProps }: AppProps) {
326
- return (
327
- <NextIntlProvider locale={pageProps.locale} messages={pageProps.messages}>
328
- <Component {...pageProps} />
329
- </NextIntlProvider>
330
- );
331
- }
332
-
333
- export default MyApp;
334
- ```
335
-
336
- 3. **在服务器端获取消息 (Pages Router 示例):**
337
-
338
- ```typescript fileName="pages/index.tsx"
339
- import { GetServerSideProps } from "next";
340
- import HomePage from "../components/HomePage";
341
-
342
- export default HomePage;
343
-
344
- export const getServerSideProps: GetServerSideProps = async ({ locale }) => {
345
- const messages = (await import(`../intl/messages/${locale}.json`)).default;
346
-
347
- return {
348
- props: {
349
- locale,
350
- messages,
351
- },
352
- };
353
- };
354
- ```
355
-
356
- ### 在 Next.js 组件中使用内容
357
-
358
- 一旦消息被加载到 next-intl 中,你可以通过 `useTranslations()` 钩子在组件中使用它们:
359
-
360
- ```typescript fileName="src/components/MyComponent/index.tsx" codeFormat="typescript"
361
- import type { FC } from "react";
362
- import { useTranslations } from 'next-intl';
363
-
364
- const MyComponent: FC = () => {
365
- const t = useTranslations('my-component');
366
- // 'my-component' 对应于 Intlayer 中的内容键
367
-
368
- return (
369
- <div>
370
- <h1>{t('helloWorld')}</h1>
371
- </div>
372
- );
373
- };
374
-
375
- export default MyComponent;
376
- ```
106
+ 这些文件可以在构建过程中重新生成,无需提交到版本控制。
377
107
 
378
- ```jsx fileName="src/components/MyComponent/index.jsx" codeFormat="esm"
379
- import { useTranslations } from "next-intl";
108
+ ### VS Code 扩展
380
109
 
381
- export default function MyComponent() {
382
- const t = useTranslations("my-component");
110
+ 为了提升开发者体验,请安装官方的 **Intlayer VS Code 扩展**:
383
111
 
384
- return (
385
- <div>
386
- <h1>{t("helloWorld")}</h1>
387
- </div>
388
- );
389
- }
390
- ```
112
+ [从 VS Code 市场安装](https://marketplace.visualstudio.com/items?itemName=intlayer.intlayer-vs-code-extension)
391
113
 
392
- **就这些!** 每当你更新或添加新的 Intlayer 内容声明文件时,重新运行 `intlayer build` 命令以重新生成你的 next-intl JSON 消息。next-intl 将自动获取更新的内容。
114
+ [从 VS Code 市场安装](https://marketplace.visualstudio.com/items?itemName=intlayer.intlayer-vs-code-extension)