@intlayer/docs 7.5.9 → 7.5.10
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/README.md +9 -2
- package/dist/cjs/generated/docs.entry.cjs +19 -0
- package/dist/cjs/generated/docs.entry.cjs.map +1 -1
- package/dist/esm/generated/docs.entry.mjs +19 -0
- package/dist/esm/generated/docs.entry.mjs.map +1 -1
- package/dist/types/generated/docs.entry.d.ts +1 -0
- package/dist/types/generated/docs.entry.d.ts.map +1 -1
- package/docs/ar/cli/push.md +1 -1
- package/docs/ar/configuration.md +3 -3
- package/docs/ar/interest_of_intlayer.md +1 -1
- package/docs/ar/intlayer_CMS.md +5 -5
- package/docs/ar/intlayer_with_express.md +1 -0
- package/docs/ar/intlayer_with_fastify.md +516 -0
- package/docs/ar/readme.md +1 -1
- package/docs/de/cli/push.md +1 -1
- package/docs/de/configuration.md +3 -3
- package/docs/de/interest_of_intlayer.md +1 -1
- package/docs/de/intlayer_CMS.md +5 -5
- package/docs/de/intlayer_with_express.md +1 -0
- package/docs/de/intlayer_with_fastify.md +449 -0
- package/docs/de/readme.md +1 -1
- package/docs/en/cli/push.md +1 -1
- package/docs/en/configuration.md +3 -3
- package/docs/en/interest_of_intlayer.md +1 -1
- package/docs/en/intlayer_CMS.md +5 -5
- package/docs/en/intlayer_with_express.md +1 -0
- package/docs/en/intlayer_with_fastify.md +439 -0
- package/docs/en/readme.md +3 -1
- package/docs/en-GB/cli/push.md +1 -1
- package/docs/en-GB/configuration.md +3 -3
- package/docs/en-GB/interest_of_intlayer.md +1 -1
- package/docs/en-GB/intlayer_CMS.md +5 -5
- package/docs/en-GB/intlayer_with_express.md +1 -0
- package/docs/en-GB/intlayer_with_fastify.md +445 -0
- package/docs/en-GB/readme.md +3 -1
- package/docs/es/cli/push.md +1 -1
- package/docs/es/configuration.md +3 -3
- package/docs/es/interest_of_intlayer.md +1 -1
- package/docs/es/intlayer_CMS.md +5 -5
- package/docs/es/intlayer_with_express.md +1 -0
- package/docs/es/intlayer_with_fastify.md +480 -0
- package/docs/es/readme.md +1 -1
- package/docs/fr/cli/push.md +1 -1
- package/docs/fr/configuration.md +3 -3
- package/docs/fr/interest_of_intlayer.md +1 -1
- package/docs/fr/intlayer_CMS.md +5 -5
- package/docs/fr/intlayer_with_express.md +1 -0
- package/docs/fr/intlayer_with_fastify.md +439 -0
- package/docs/fr/readme.md +1 -1
- package/docs/hi/cli/push.md +1 -1
- package/docs/hi/configuration.md +3 -3
- package/docs/hi/interest_of_intlayer.md +1 -1
- package/docs/hi/intlayer_CMS.md +5 -5
- package/docs/hi/intlayer_with_express.md +1 -0
- package/docs/hi/intlayer_with_fastify.md +540 -0
- package/docs/hi/readme.md +1 -1
- package/docs/id/cli/push.md +1 -1
- package/docs/id/configuration.md +3 -3
- package/docs/id/interest_of_intlayer.md +1 -1
- package/docs/id/intlayer_CMS.md +5 -5
- package/docs/id/intlayer_with_express.md +1 -0
- package/docs/id/intlayer_with_fastify.md +470 -0
- package/docs/id/readme.md +1 -1
- package/docs/it/cli/push.md +1 -1
- package/docs/it/configuration.md +3 -3
- package/docs/it/interest_of_intlayer.md +1 -1
- package/docs/it/intlayer_CMS.md +5 -5
- package/docs/it/intlayer_with_express.md +1 -0
- package/docs/it/intlayer_with_fastify.md +445 -0
- package/docs/it/readme.md +1 -1
- package/docs/ja/cli/push.md +1 -1
- package/docs/ja/configuration.md +3 -3
- package/docs/ja/interest_of_intlayer.md +1 -1
- package/docs/ja/intlayer_CMS.md +5 -5
- package/docs/ja/intlayer_with_express.md +1 -0
- package/docs/ja/intlayer_with_fastify.md +516 -0
- package/docs/ja/readme.md +1 -1
- package/docs/ko/cli/push.md +1 -1
- package/docs/ko/configuration.md +3 -3
- package/docs/ko/interest_of_intlayer.md +1 -1
- package/docs/ko/intlayer_CMS.md +5 -5
- package/docs/ko/intlayer_with_express.md +1 -0
- package/docs/ko/intlayer_with_fastify.md +463 -0
- package/docs/ko/readme.md +1 -1
- package/docs/pl/cli/push.md +1 -1
- package/docs/pl/configuration.md +3 -3
- package/docs/pl/interest_of_intlayer.md +1 -1
- package/docs/pl/intlayer_CMS.md +5 -5
- package/docs/pl/intlayer_with_express.md +1 -0
- package/docs/pl/intlayer_with_fastify.md +457 -0
- package/docs/pl/readme.md +1 -1
- package/docs/pt/cli/push.md +1 -1
- package/docs/pt/configuration.md +3 -3
- package/docs/pt/interest_of_intlayer.md +1 -1
- package/docs/pt/intlayer_CMS.md +5 -5
- package/docs/pt/intlayer_with_express.md +1 -0
- package/docs/pt/intlayer_with_fastify.md +502 -0
- package/docs/pt/readme.md +1 -1
- package/docs/ru/cli/push.md +1 -1
- package/docs/ru/configuration.md +3 -3
- package/docs/ru/interest_of_intlayer.md +1 -1
- package/docs/ru/intlayer_CMS.md +5 -5
- package/docs/ru/intlayer_with_express.md +1 -0
- package/docs/ru/intlayer_with_fastify.md +468 -0
- package/docs/ru/readme.md +1 -1
- package/docs/tr/cli/push.md +1 -1
- package/docs/tr/configuration.md +3 -3
- package/docs/tr/interest_of_intlayer.md +1 -1
- package/docs/tr/intlayer_CMS.md +5 -5
- package/docs/tr/intlayer_with_express.md +1 -0
- package/docs/tr/intlayer_with_fastify.md +471 -0
- package/docs/tr/readme.md +1 -1
- package/docs/vi/cli/push.md +1 -1
- package/docs/vi/configuration.md +3 -3
- package/docs/vi/interest_of_intlayer.md +1 -1
- package/docs/vi/intlayer_CMS.md +5 -5
- package/docs/vi/intlayer_with_express.md +1 -0
- package/docs/vi/intlayer_with_fastify.md +490 -0
- package/docs/vi/readme.md +1 -1
- package/docs/zh/cli/push.md +1 -1
- package/docs/zh/configuration.md +3 -3
- package/docs/zh/interest_of_intlayer.md +1 -1
- package/docs/zh/intlayer_CMS.md +5 -5
- package/docs/zh/intlayer_with_express.md +1 -0
- package/docs/zh/intlayer_with_fastify.md +451 -0
- package/docs/zh/readme.md +1 -1
- package/package.json +6 -6
- package/src/generated/docs.entry.ts +19 -0
|
@@ -0,0 +1,451 @@
|
|
|
1
|
+
---
|
|
2
|
+
createdAt: 2025-12-30
|
|
3
|
+
updatedAt: 2025-12-30
|
|
4
|
+
title: 如何翻译你的 Fastify 后端 — 国际化 (i18n) 指南 2026
|
|
5
|
+
description: 了解如何使你的 Fastify 后端实现多语言。按照文档对其进行国际化 (i18n) 并翻译。
|
|
6
|
+
keywords:
|
|
7
|
+
- 国际化
|
|
8
|
+
- 文档
|
|
9
|
+
- Intlayer
|
|
10
|
+
- Fastify
|
|
11
|
+
- JavaScript
|
|
12
|
+
- 后端
|
|
13
|
+
slugs:
|
|
14
|
+
- doc
|
|
15
|
+
- environment
|
|
16
|
+
- fastify
|
|
17
|
+
applicationTemplate: https://github.com/aymericzip/intlayer-fastify-template
|
|
18
|
+
history:
|
|
19
|
+
- version: 7.6.0
|
|
20
|
+
date: 2025-12-31
|
|
21
|
+
changes: 添加 init 命令
|
|
22
|
+
- version: 7.6.0
|
|
23
|
+
date: 2025-12-31
|
|
24
|
+
changes: 初始化历史
|
|
25
|
+
---
|
|
26
|
+
|
|
27
|
+
# 使用 Intlayer 翻译你的 Fastify 后端网站 | 国际化 (i18n)
|
|
28
|
+
|
|
29
|
+
`fastify-intlayer` 是一个功能强大的 Fastify 应用国际化 (i18n) 插件,旨在通过根据客户端偏好提供本地化响应,使你的后端服务可在全球范围内访问。
|
|
30
|
+
|
|
31
|
+
### 实用用例
|
|
32
|
+
|
|
33
|
+
- **以用户语言显示后端错误**:当发生错误时,以用户的母语显示消息能提高理解并减少挫败感。这对于可能在前端组件(如 toast 或模态窗口)中显示的动态错误消息尤其有用。
|
|
34
|
+
|
|
35
|
+
`fastify-intlayer` 是一个强大的 Fastify 应用国际化 (i18n) 插件,旨在根据客户端的偏好提供本地化响应,使你的后端服务在全球范围内可访问。
|
|
36
|
+
|
|
37
|
+
### 实用场景
|
|
38
|
+
|
|
39
|
+
- **以用户语言显示后端错误**:当发生错误时,以用户的母语显示消息可以提高理解并减少挫折感。这对那些可能在前端组件(例如 toasts 或 modals)中显示的动态错误消息尤其有用。
|
|
40
|
+
- **检索多语言内容**:对于从数据库拉取内容的应用,国际化可确保你以多种语言提供这些内容。这对于需要以用户首选语言显示产品描述、文章和其他内容的平台(如电子商务网站或内容管理系统)至关重要。
|
|
41
|
+
- **发送多语言电子邮件**:无论是事务性邮件、营销活动还是通知,以收件人语言发送邮件都能显著提高参与度和效果。
|
|
42
|
+
- **多语言推送通知**:对于移动应用,在用户偏好的语言中发送推送通知可以提升互动和用户留存。这样的个性化处理会让通知显得更相关且更容易促使用户采取行动。
|
|
43
|
+
- **其他通讯**:来自后端的任何形式的通信,例如短信、系统告警或用户界面更新,在用户语言中呈现都能提升清晰度并增强整体用户体验。
|
|
44
|
+
|
|
45
|
+
通过对后端进行国际化,您的应用不仅尊重文化差异,而且更符合全球市场需求,这是一项将服务扩展到全球的重要步骤。
|
|
46
|
+
|
|
47
|
+
## 入门
|
|
48
|
+
|
|
49
|
+
### 安装
|
|
50
|
+
|
|
51
|
+
要开始使用 `fastify-intlayer`,请使用 npm 安装该包:
|
|
52
|
+
|
|
53
|
+
```bash packageManager="npm"
|
|
54
|
+
npm install intlayer fastify-intlayer
|
|
55
|
+
npx intlayer init
|
|
56
|
+
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
```bash packageManager="pnpm"
|
|
60
|
+
pnpm add intlayer fastify-intlayer
|
|
61
|
+
pnpm intlayer init
|
|
62
|
+
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
```bash packageManager="yarn"
|
|
66
|
+
yarn add intlayer fastify-intlayer
|
|
67
|
+
yarn intlayer init
|
|
68
|
+
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
```bash packageManager="bun"
|
|
72
|
+
bun add intlayer fastify-intlayer
|
|
73
|
+
bunx intlayer init
|
|
74
|
+
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### 设置
|
|
78
|
+
|
|
79
|
+
通过在项目根目录创建一个 `intlayer.config.ts` 来配置国际化设置:
|
|
80
|
+
|
|
81
|
+
```typescript fileName="intlayer.config.ts" codeFormat="typescript"
|
|
82
|
+
import { Locales, type IntlayerConfig } from "intlayer";
|
|
83
|
+
|
|
84
|
+
const config: IntlayerConfig = {
|
|
85
|
+
internationalization: {
|
|
86
|
+
locales: [
|
|
87
|
+
Locales.ENGLISH,
|
|
88
|
+
Locales.FRENCH,
|
|
89
|
+
Locales.SPANISH_MEXICO,
|
|
90
|
+
Locales.SPANISH_SPAIN,
|
|
91
|
+
],
|
|
92
|
+
defaultLocale: Locales.ENGLISH,
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
export default config;
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
```javascript fileName="intlayer.config.mjs" codeFormat="esm"
|
|
100
|
+
import { Locales } from "intlayer";
|
|
101
|
+
|
|
102
|
+
/** @type {import('intlayer').IntlayerConfig} */ // 类型注释:Intlayer 配置
|
|
103
|
+
const config = {
|
|
104
|
+
internationalization: {
|
|
105
|
+
locales: [
|
|
106
|
+
Locales.ENGLISH,
|
|
107
|
+
Locales.FRENCH,
|
|
108
|
+
Locales.SPANISH_MEXICO,
|
|
109
|
+
Locales.SPANISH_SPAIN,
|
|
110
|
+
],
|
|
111
|
+
defaultLocale: Locales.ENGLISH,
|
|
112
|
+
},
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
export default config;
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
```javascript fileName="intlayer.config.cjs" codeFormat="commonjs"
|
|
119
|
+
const { Locales } = require("intlayer");
|
|
120
|
+
|
|
121
|
+
/** @type {import('intlayer').IntlayerConfig} */ // 类型注释:Intlayer 配置
|
|
122
|
+
const config = {
|
|
123
|
+
internationalization: {
|
|
124
|
+
locales: [
|
|
125
|
+
Locales.ENGLISH,
|
|
126
|
+
Locales.FRENCH,
|
|
127
|
+
Locales.SPANISH_MEXICO,
|
|
128
|
+
Locales.SPANISH_SPAIN,
|
|
129
|
+
],
|
|
130
|
+
defaultLocale: Locales.ENGLISH,
|
|
131
|
+
},
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
module.exports = config;
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
### 声明您的内容
|
|
138
|
+
|
|
139
|
+
创建并管理您的内容声明以存储翻译:
|
|
140
|
+
|
|
141
|
+
```typescript fileName="src/index.content.ts" contentDeclarationFormat="typescript"
|
|
142
|
+
import { t, type Dictionary } from "intlayer";
|
|
143
|
+
|
|
144
|
+
const indexContent = {
|
|
145
|
+
key: "index",
|
|
146
|
+
content: {
|
|
147
|
+
exampleOfContent: t({
|
|
148
|
+
zh: "返回内容示例(英语)",
|
|
149
|
+
en: "Example of returned content in English",
|
|
150
|
+
fr: "Exemple de contenu renvoyé en français",
|
|
151
|
+
"es-ES": "Ejemplo de contenido devuelto en español (España)",
|
|
152
|
+
"es-MX": "Ejemplo de contenido devuelto en español (México)",
|
|
153
|
+
}),
|
|
154
|
+
},
|
|
155
|
+
} satisfies Dictionary;
|
|
156
|
+
|
|
157
|
+
export default indexContent;
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
```javascript fileName="src/index.content.mjs" contentDeclarationFormat="esm"
|
|
161
|
+
import { t } from "intlayer";
|
|
162
|
+
|
|
163
|
+
/** @type {import('intlayer').Dictionary} */ // 类型注解:import('intlayer').Dictionary
|
|
164
|
+
const indexContent = {
|
|
165
|
+
key: "index",
|
|
166
|
+
content: {
|
|
167
|
+
exampleOfContent: t({
|
|
168
|
+
zh: "示例:返回的内容(中文)",
|
|
169
|
+
en: "Example of returned content in English",
|
|
170
|
+
fr: "Exemple de contenu renvoyé en français",
|
|
171
|
+
"es-ES": "Ejemplo de contenido devuelto en español (España)",
|
|
172
|
+
"es-MX": "Ejemplo de contenido devuelto en español (México)",
|
|
173
|
+
}),
|
|
174
|
+
},
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
export default indexContent;
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
```javascript fileName="src/index.content.cjs" contentDeclarationFormat="commonjs"
|
|
181
|
+
const { t } = require("intlayer");
|
|
182
|
+
|
|
183
|
+
/** @type {import('intlayer').Dictionary} */ // 类型注解:import('intlayer').Dictionary
|
|
184
|
+
const indexContent = {
|
|
185
|
+
key: "index",
|
|
186
|
+
content: {
|
|
187
|
+
exampleOfContent: t({
|
|
188
|
+
zh: "示例:返回的内容(中文)",
|
|
189
|
+
en: "Example of returned content in English",
|
|
190
|
+
fr: "Exemple de contenu renvoyé en français",
|
|
191
|
+
"es-ES": "Ejemplo de contenido devuelto en español (España)",
|
|
192
|
+
"es-MX": "Ejemplo de contenido devuelto en español (México)",
|
|
193
|
+
}),
|
|
194
|
+
},
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
module.exports = indexContent;
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
```json fileName="src/index.content.json" contentDeclarationFormat="json"
|
|
201
|
+
{
|
|
202
|
+
"$schema": "https://intlayer.org/schema.json",
|
|
203
|
+
"key": "index",
|
|
204
|
+
"content": {
|
|
205
|
+
"exampleOfContent": {
|
|
206
|
+
"nodeType": "translation",
|
|
207
|
+
"translation": {
|
|
208
|
+
"zh": "返回内容示例(英语)",
|
|
209
|
+
"en": "Example of returned content in English",
|
|
210
|
+
"fr": "Exemple de contenu renvoyé en français",
|
|
211
|
+
"es-ES": "Ejemplo de contenido devuelto en español (España)",
|
|
212
|
+
"es-MX": "Ejemplo de contenido devuelto en español (México)"
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
> 一旦内容声明文件被包含到 `contentDir` 目录(默认 `./src`)中,你可以在应用的任何位置定义它们。并且文件扩展名应匹配内容声明文件扩展名(默认 `.content.{json,ts,tsx,js,jsx,mjs,mjx,cjs,cjx}`)。
|
|
220
|
+
|
|
221
|
+
> 更多详情,请参阅 [内容声明文档](https://github.com/aymericzip/intlayer/blob/main/docs/docs/zh/dictionary/content_file.md)。
|
|
222
|
+
|
|
223
|
+
### Fastify 应用设置
|
|
224
|
+
|
|
225
|
+
配置你的 Fastify 应用以使用 `fastify-intlayer`:
|
|
226
|
+
|
|
227
|
+
```typescript fileName="src/index.ts" codeFormat="typescript"
|
|
228
|
+
import Fastify from "fastify";
|
|
229
|
+
import { intlayer, t, getDictionary, getIntlayer } from "fastify-intlayer";
|
|
230
|
+
import dictionaryExample from "./index.content";
|
|
231
|
+
|
|
232
|
+
const fastify = Fastify({ logger: true });
|
|
233
|
+
|
|
234
|
+
// 加载国际化插件
|
|
235
|
+
await fastify.register(intlayer);
|
|
236
|
+
|
|
237
|
+
// 路由
|
|
238
|
+
fastify.get("/t_example", async (_req, reply) => {
|
|
239
|
+
return t({
|
|
240
|
+
zh: "返回的内容示例(中文)",
|
|
241
|
+
en: "Example of returned content in English",
|
|
242
|
+
fr: "Exemple de contenu renvoyé en français",
|
|
243
|
+
"es-ES": "Ejemplo de contenido devuelto en español (España)",
|
|
244
|
+
"es-MX": "Ejemplo de contenido devuelto en español (México)",
|
|
245
|
+
});
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
fastify.get("/getIntlayer_example", async (_req, reply) => {
|
|
249
|
+
return getIntlayer("index").exampleOfContent;
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
fastify.get("/getDictionary_example", async (_req, reply) => {
|
|
253
|
+
return getDictionary(dictionaryExample).exampleOfContent;
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
// 启动服务器
|
|
257
|
+
const start = async () => {
|
|
258
|
+
try {
|
|
259
|
+
await fastify.listen({ port: 3000 });
|
|
260
|
+
} catch (err) {
|
|
261
|
+
fastify.log.error(err);
|
|
262
|
+
process.exit(1);
|
|
263
|
+
}
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
start();
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
```javascript fileName="src/index.mjs" codeFormat="esm"
|
|
270
|
+
import Fastify from "fastify";
|
|
271
|
+
import { intlayer, t, getDictionary, getIntlayer } from "fastify-intlayer";
|
|
272
|
+
import dictionaryExample from "./index.content";
|
|
273
|
+
|
|
274
|
+
const fastify = Fastify({ logger: true });
|
|
275
|
+
|
|
276
|
+
// 加载国际化插件
|
|
277
|
+
await fastify.register(intlayer);
|
|
278
|
+
|
|
279
|
+
// 路由
|
|
280
|
+
fastify.get("/t_example", async (_req, reply) => {
|
|
281
|
+
return t({
|
|
282
|
+
zh: "示例:以中文返回的内容",
|
|
283
|
+
en: "Example of returned content in English",
|
|
284
|
+
fr: "Exemple de contenu renvoyé en français",
|
|
285
|
+
"es-ES": "Ejemplo de contenido devuelto en español (España)",
|
|
286
|
+
"es-MX": "Ejemplo de contenido devuelto en español (México)",
|
|
287
|
+
});
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
fastify.get("/getIntlayer_example", async (_req, reply) => {
|
|
291
|
+
return getIntlayer("index").exampleOfContent;
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
fastify.get("/getDictionary_example", async (_req, reply) => {
|
|
295
|
+
return getDictionary(dictionaryExample).exampleOfContent;
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
// 启动服务器
|
|
299
|
+
const start = async () => {
|
|
300
|
+
try {
|
|
301
|
+
await fastify.listen({ port: 3000 });
|
|
302
|
+
} catch (err) {
|
|
303
|
+
fastify.log.error(err);
|
|
304
|
+
process.exit(1);
|
|
305
|
+
}
|
|
306
|
+
};
|
|
307
|
+
|
|
308
|
+
start();
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
```javascript fileName="src/index.cjs" codeFormat="commonjs"
|
|
312
|
+
const Fastify = require("fastify");
|
|
313
|
+
const { intlayer, t, getDictionary, getIntlayer } = require("fastify-intlayer");
|
|
314
|
+
const dictionaryExample = require("./index.content");
|
|
315
|
+
|
|
316
|
+
const fastify = Fastify({ logger: true });
|
|
317
|
+
|
|
318
|
+
// 启动服务器的 async/await 包装器
|
|
319
|
+
const start = async () => {
|
|
320
|
+
try {
|
|
321
|
+
// 加载国际化插件
|
|
322
|
+
await fastify.register(intlayer);
|
|
323
|
+
|
|
324
|
+
// 路由
|
|
325
|
+
fastify.get("/t_example", async (_req, reply) => {
|
|
326
|
+
return t({
|
|
327
|
+
zh: "示例:以英文/法文/西班牙文返回的内容(参见各语言对应条目)",
|
|
328
|
+
en: "Example of returned content in English",
|
|
329
|
+
fr: "Exemple de contenu renvoyé en français",
|
|
330
|
+
"es-ES": "Ejemplo de contenido devuelto en español (España)",
|
|
331
|
+
"es-MX": "Ejemplo de contenido devuelto en español (México)",
|
|
332
|
+
});
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
fastify.get("/getIntlayer_example", async (_req, reply) => {
|
|
336
|
+
return getIntlayer("index").exampleOfContent;
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
fastify.get("/getDictionary_example", async (_req, reply) => {
|
|
340
|
+
return getDictionary(dictionaryExample).exampleOfContent;
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
await fastify.listen({ port: 3000 });
|
|
344
|
+
} catch (err) {
|
|
345
|
+
fastify.log.error(err);
|
|
346
|
+
process.exit(1);
|
|
347
|
+
}
|
|
348
|
+
};
|
|
349
|
+
|
|
350
|
+
start();
|
|
351
|
+
```
|
|
352
|
+
|
|
353
|
+
### 兼容性
|
|
354
|
+
|
|
355
|
+
`fastify-intlayer` 完全兼容:
|
|
356
|
+
|
|
357
|
+
- [`react-intlayer`](<https://www.google.com/search?q=%5Bhttps://github.com/aymericzip/intlayer/blob/main/docs/docs/zh/packages/react-intlayer/index.md%5D(https://github.com/aymericzip/intlayer/blob/main/docs/docs/zh/packages/react-intlayer/index.md)>) 用于 React 应用
|
|
358
|
+
- [`next-intlayer`](<https://www.google.com/search?q=%5Bhttps://github.com/aymericzip/intlayer/blob/main/docs/docs/zh/packages/next-intlayer/index.md%5D(https://github.com/aymericzip/intlayer/blob/main/docs/docs/zh/packages/next-intlayer/index.md)>) 用于 Next.js 应用
|
|
359
|
+
- [`vite-intlayer`](<https://www.google.com/search?q=%5Bhttps://github.com/aymericzip/intlayer/blob/main/docs/docs/zh/packages/vite-intlayer/index.md%5D(https://github.com/aymericzip/intlayer/blob/main/docs/docs/zh/packages/vite-intlayer/index.md)>) 适用于 Vite 应用程序
|
|
360
|
+
|
|
361
|
+
它还可与各种环境中的任何国际化解决方案无缝配合,包括浏览器和 API 请求。您可以自定义中间件,通过请求头或 Cookie 检测 locale:
|
|
362
|
+
|
|
363
|
+
```typescript fileName="intlayer.config.ts" codeFormat="typescript"
|
|
364
|
+
import { Locales, type IntlayerConfig } from "intlayer";
|
|
365
|
+
|
|
366
|
+
const config: IntlayerConfig = {
|
|
367
|
+
// ... 其他配置选项
|
|
368
|
+
middleware: {
|
|
369
|
+
headerName: "my-locale-header",
|
|
370
|
+
cookieName: "my-locale-cookie",
|
|
371
|
+
},
|
|
372
|
+
};
|
|
373
|
+
|
|
374
|
+
export default config;
|
|
375
|
+
```
|
|
376
|
+
|
|
377
|
+
```javascript fileName="intlayer.config.mjs" codeFormat="esm"
|
|
378
|
+
import { Locales } from "intlayer";
|
|
379
|
+
|
|
380
|
+
/** @type {import('intlayer').IntlayerConfig} */
|
|
381
|
+
const config = {
|
|
382
|
+
// ... 其他配置选项
|
|
383
|
+
middleware: {
|
|
384
|
+
headerName: "my-locale-header",
|
|
385
|
+
cookieName: "my-locale-cookie",
|
|
386
|
+
},
|
|
387
|
+
};
|
|
388
|
+
|
|
389
|
+
export default config;
|
|
390
|
+
```
|
|
391
|
+
|
|
392
|
+
```javascript fileName="intlayer.config.cjs" codeFormat="commonjs"
|
|
393
|
+
const { Locales } = require("intlayer");
|
|
394
|
+
|
|
395
|
+
/** @type {import('intlayer').IntlayerConfig} */
|
|
396
|
+
const config = {
|
|
397
|
+
// ... 其他配置选项
|
|
398
|
+
middleware: {
|
|
399
|
+
headerName: "my-locale-header",
|
|
400
|
+
cookieName: "my-locale-cookie",
|
|
401
|
+
},
|
|
402
|
+
};
|
|
403
|
+
|
|
404
|
+
module.exports = config;
|
|
405
|
+
```
|
|
406
|
+
|
|
407
|
+
默认情况下,`fastify-intlayer` 会解析 `Accept-Language` 头以确定客户端的首选语言。
|
|
408
|
+
|
|
409
|
+
> 欲了解有关配置和高级主题的更多信息,请访问我们的[文档](https://github.com/aymericzip/intlayer/blob/main/docs/docs/zh/configuration.md)。
|
|
410
|
+
|
|
411
|
+
### 配置 TypeScript
|
|
412
|
+
|
|
413
|
+
`fastify-intlayer` 利用 TypeScript 强大的能力来增强国际化流程。TypeScript 的静态类型确保每个翻译键都被涵盖,降低遗漏翻译的风险并提高可维护性。
|
|
414
|
+
|
|
415
|
+
确保自动生成的类型(默认位于 ./types/intlayer.d.ts)已包含在你的 tsconfig.json 文件中。
|
|
416
|
+
|
|
417
|
+
```json5 fileName="tsconfig.json"
|
|
418
|
+
{
|
|
419
|
+
// ... 你现有的 TypeScript 配置
|
|
420
|
+
"include": [
|
|
421
|
+
// ... 你现有的 TypeScript 配置
|
|
422
|
+
".intlayer/**/*.ts", // 包含自动生成的类型
|
|
423
|
+
],
|
|
424
|
+
}
|
|
425
|
+
```
|
|
426
|
+
|
|
427
|
+
### VS Code 扩展
|
|
428
|
+
|
|
429
|
+
为提升您使用 Intlayer 的开发体验,您可以安装官方的 **Intlayer VS Code Extension**。
|
|
430
|
+
|
|
431
|
+
[从 VS Code Marketplace 安装](https://marketplace.visualstudio.com/items?itemName=intlayer.intlayer-vs-code-extension)
|
|
432
|
+
|
|
433
|
+
该扩展提供:
|
|
434
|
+
|
|
435
|
+
- **自动完成(Autocompletion)** 翻译键。
|
|
436
|
+
- **实时错误检测**,用于发现缺失的翻译。
|
|
437
|
+
- **内联预览** 已翻译的内容。
|
|
438
|
+
- **快速操作**,便于快速创建和更新翻译。
|
|
439
|
+
|
|
440
|
+
有关如何使用该扩展的更多详细信息,请参阅 [Intlayer VS Code Extension 文档](https://intlayer.org/doc/vs-code-extension)。
|
|
441
|
+
|
|
442
|
+
### Git 配置
|
|
443
|
+
|
|
444
|
+
建议忽略 Intlayer 生成的文件,这样可以避免将它们提交到 Git 仓库。
|
|
445
|
+
|
|
446
|
+
为此,您可以将以下指令添加到 `.gitignore` 文件:
|
|
447
|
+
|
|
448
|
+
```plaintext fileName=".gitignore"
|
|
449
|
+
# 忽略 Intlayer 生成的文件
|
|
450
|
+
.intlayer
|
|
451
|
+
```
|
package/docs/zh/readme.md
CHANGED
|
@@ -50,7 +50,7 @@ Intlayer 是一个面向网页和移动应用的**现代国际化解决方案**
|
|
|
50
50
|
| 功能 | 描述 |
|
|
51
51
|
| --------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
52
52
|
| <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/frameworks.png?raw=true" alt="功能" width="700"> | **跨框架支持**<br><br>Intlayer 兼容所有主流框架和库,包括 Next.js、React、Vite、Vue.js、Nuxt、Preact、Express 等。 |
|
|
53
|
-
| <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/javascript_content_management.
|
|
53
|
+
| <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/javascript_content_management.jpg?raw=true" alt="Feature" width="700"> | **基于JavaScript的内容管理**<br><br>利用JavaScript的灵活性高效地定义和管理您的内容。<br><br> - [内容声明](https://intlayer.org/doc/concept/content) |
|
|
54
54
|
| <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/per_locale_content_declaration_file.png?raw=true" alt="Feature" width="700"> | **每语言内容声明文件**<br><br>通过在自动生成之前只声明一次内容,加快您的开发速度。<br><br> - [每语言内容声明文件](https://intlayer.org/doc/concept/per-locale-file) |
|
|
55
55
|
| <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/autocompletion.png?raw=true" alt="Feature" width="700"> | **类型安全环境**<br><br>利用 TypeScript 确保您的内容定义和代码无错误,同时享受 IDE 自动补全的便利。<br><br> - [TypeScript 配置](https://intlayer.org/doc/environment/vite-and-react#configure-typescript) |
|
|
56
56
|
| <img src="https://github.com/aymericzip/intlayer/blob/main/docs/assets/config_file.png?raw=true" alt="功能" width="700"> | **简化设置**<br><br>通过最少的配置快速启动。轻松调整国际化、路由、AI、构建和内容处理的设置。<br><br> - [探索 Next.js 集成](https://intlayer.org/doc/environment/nextjs) |
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@intlayer/docs",
|
|
3
|
-
"version": "7.5.
|
|
3
|
+
"version": "7.5.10",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Intlayer documentation",
|
|
6
6
|
"keywords": [
|
|
@@ -73,13 +73,13 @@
|
|
|
73
73
|
"watch": "webpack --config ./webpack.config.ts --watch"
|
|
74
74
|
},
|
|
75
75
|
"dependencies": {
|
|
76
|
-
"@intlayer/config": "7.5.
|
|
77
|
-
"@intlayer/core": "7.5.
|
|
78
|
-
"@intlayer/types": "7.5.
|
|
76
|
+
"@intlayer/config": "7.5.10",
|
|
77
|
+
"@intlayer/core": "7.5.10",
|
|
78
|
+
"@intlayer/types": "7.5.10"
|
|
79
79
|
},
|
|
80
80
|
"devDependencies": {
|
|
81
|
-
"@intlayer/api": "7.5.
|
|
82
|
-
"@intlayer/cli": "7.5.
|
|
81
|
+
"@intlayer/api": "7.5.10",
|
|
82
|
+
"@intlayer/cli": "7.5.10",
|
|
83
83
|
"@types/node": "25.0.3",
|
|
84
84
|
"@utils/ts-config": "1.0.4",
|
|
85
85
|
"@utils/ts-config-types": "1.0.4",
|
|
@@ -908,6 +908,25 @@ export const docsEntry = {
|
|
|
908
908
|
id: readLocale('intlayer_with_express.md', 'id'),
|
|
909
909
|
vi: readLocale('intlayer_with_express.md', 'vi'),
|
|
910
910
|
} as unknown as Record<LocalesValues, Promise<string>>,
|
|
911
|
+
'./docs/en/intlayer_with_fastify.md': {
|
|
912
|
+
en: readLocale('intlayer_with_fastify.md', 'en'),
|
|
913
|
+
ru: readLocale('intlayer_with_fastify.md', 'ru'),
|
|
914
|
+
ja: readLocale('intlayer_with_fastify.md', 'ja'),
|
|
915
|
+
fr: readLocale('intlayer_with_fastify.md', 'fr'),
|
|
916
|
+
ko: readLocale('intlayer_with_fastify.md', 'ko'),
|
|
917
|
+
zh: readLocale('intlayer_with_fastify.md', 'zh'),
|
|
918
|
+
es: readLocale('intlayer_with_fastify.md', 'es'),
|
|
919
|
+
de: readLocale('intlayer_with_fastify.md', 'de'),
|
|
920
|
+
ar: readLocale('intlayer_with_fastify.md', 'ar'),
|
|
921
|
+
it: readLocale('intlayer_with_fastify.md', 'it'),
|
|
922
|
+
'en-GB': readLocale('intlayer_with_fastify.md', 'en-GB'),
|
|
923
|
+
pt: readLocale('intlayer_with_fastify.md', 'pt'),
|
|
924
|
+
hi: readLocale('intlayer_with_fastify.md', 'hi'),
|
|
925
|
+
tr: readLocale('intlayer_with_fastify.md', 'tr'),
|
|
926
|
+
pl: readLocale('intlayer_with_fastify.md', 'pl'),
|
|
927
|
+
id: readLocale('intlayer_with_fastify.md', 'id'),
|
|
928
|
+
vi: readLocale('intlayer_with_fastify.md', 'vi'),
|
|
929
|
+
} as unknown as Record<LocalesValues, Promise<string>>,
|
|
911
930
|
'./docs/en/intlayer_with_lynx+react.md': {
|
|
912
931
|
en: readLocale('intlayer_with_lynx+react.md', 'en'),
|
|
913
932
|
ru: readLocale('intlayer_with_lynx+react.md', 'ru'),
|