@intlayer/docs 8.4.6 → 8.4.8

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 (33) hide show
  1. package/blog/en/next-i18next_vs_next-intl_vs_intlayer.md +1 -3
  2. package/blog/en-GB/next-i18next_vs_next-intl_vs_intlayer.md +1 -3
  3. package/blog/es/next-i18next_vs_next-intl_vs_intlayer.md +1 -3
  4. package/blog/id/next-i18next_vs_next-intl_vs_intlayer.md +1 -3
  5. package/blog/it/next-i18next_vs_next-intl_vs_intlayer.md +1 -3
  6. package/blog/ja/next-i18next_vs_next-intl_vs_intlayer.md +1 -3
  7. package/blog/ko/next-i18next_vs_next-intl_vs_intlayer.md +1 -3
  8. package/blog/uk/next-i18next_vs_next-intl_vs_intlayer.md +1 -3
  9. package/blog/vi/next-i18next_vs_next-intl_vs_intlayer.md +1 -3
  10. package/blog/zh/next-i18next_vs_next-intl_vs_intlayer.md +1 -3
  11. package/docs/ar/configuration.md +300 -264
  12. package/docs/cs/configuration.md +958 -0
  13. package/docs/de/configuration.md +285 -249
  14. package/docs/en/configuration.md +115 -137
  15. package/docs/en-GB/configuration.md +272 -237
  16. package/docs/es/configuration.md +161 -125
  17. package/docs/fr/configuration.md +137 -101
  18. package/docs/it/configuration.md +294 -259
  19. package/docs/ja/configuration.md +269 -233
  20. package/docs/nl/configuration.md +958 -0
  21. package/docs/pt/configuration.md +311 -275
  22. package/docs/ru/configuration.md +287 -272
  23. package/docs/tr/configuration.md +265 -229
  24. package/package.json +6 -6
  25. package/docs/bn/configuration.md +0 -922
  26. package/docs/hi/configuration.md +0 -922
  27. package/docs/id/configuration.md +0 -922
  28. package/docs/ko/configuration.md +0 -922
  29. package/docs/pl/configuration.md +0 -922
  30. package/docs/uk/configuration.md +0 -922
  31. package/docs/ur/configuration.md +0 -922
  32. package/docs/vi/configuration.md +0 -922
  33. package/docs/zh/configuration.md +0 -922
@@ -1,922 +0,0 @@
1
- ---
2
- createdAt: 2024-08-13
3
- updatedAt: 2026-03-20
4
- title: 配置 (Configuration)
5
- description: 了解如何为您的应用程序配置 Intlayer。理解各种设置和选项,以根据您的需求自定义 Intlayer。
6
- keywords:
7
- - 配置
8
- - 设置
9
- - 自定义
10
- - Intlayer
11
- - 选项
12
- slugs:
13
- - doc
14
- - concept
15
- - configuration
16
- history:
17
- - version: 8.4.0
18
- date: 2026-03-20
19
- changes: 为 'compiler.output' 和 'dictionary.fill' 添加了按语言环境的对象表示法
20
- - version: 8.3.0
21
- date: 2026-03-11
22
- changes: 将 'baseDir' 从 'content' 配置移动到 'system' 配置
23
- - version: 8.2.0
24
- date: 2026-03-09
25
- changes: 更新了编译器 (compiler) 选项,添加了对 'output' 和 'noMetadata' 的支持
26
- - version: 8.1.7
27
- date: 2026-02-25
28
- changes: 更新了编译器选项
29
- - version: 8.1.5
30
- date: 2026-02-23
31
- changes: 添加了编译器选项 'build-only' 和字典前缀
32
- - version: 8.0.6
33
- date: 2026-02-12
34
- changes: 添加了对 Open Router, Alibaba, Amazon, Google Vertex Bedrock, Fireworks, Groq, Hugging Face 和 Together.ai 提供商的支持
35
- - version: 8.0.5
36
- date: 2026-02-06
37
- changes: 在 AI 配置中添加了 `dataSerialization`
38
- - version: 8.0.0
39
- date: 2026-01-24
40
- changes: 将导入模式 `live` 重命名为 `fetch`,以更好地描述底层机制。
41
- - version: 8.0.0
42
- date: 2026-01-22
43
- changes: 将构建配置 `importMode` 移动到 `dictionary` 配置。
44
- - version: 8.0.0
45
- date: 2026-01-22
46
- changes: 在路由配置中添加了 `rewrite` 选项
47
- - version: 8.0.0
48
- date: 2026-01-18
49
- changes: 将系统配置与内容配置分离。将内部路径移动到 `system` 属性。添加了 `codeDir` 以分离内容文件和代码转换。
50
- - version: 8.0.0
51
- date: 2026-01-18
52
- changes: 添加了字典选项 `location` 和 `schema`
53
- - version: 7.5.1
54
- date: 2026-01-10
55
- changes: 添加了对 JSON5 和 JSONC 文件格式的支持
56
- - version: 7.5.0
57
- date: 2025-12-17
58
- changes: 添加了 `buildMode` 选项
59
- - version: 7.0.0
60
- date: 2025-10-25
61
- changes: 添加了 `dictionary` 配置
62
- - version: 7.0.0
63
- date: 2025-10-21
64
- changes: 使用路由配置 `routing` 替换了 `middleware`
65
- - version: 7.0.0
66
- date: 2025-10-12
67
- changes: 添加了 `formatCommand` 选项
68
- - version: 6.2.0
69
- date: 2025-10-12
70
- changes: 更新了 `excludedPath` 选项
71
- - version: 6.0.2
72
- date: 2025-09-23
73
- changes: 添加了 `outputFormat` 选项
74
- - version: 6.0.0
75
- date: 2025-09-21
76
- changes: 删除了 `dictionaryOutput` 字段和 `i18nextResourcesDir` 字段
77
- - version: 6.0.0
78
- date: 2025-09-16
79
- changes: 添加了 `live` 导入模式
80
- - version: 6.0.0
81
- date: 2025-09-04
82
- changes: 使用 `liveSync` 替换了 `hotReload` 字段,并添加了 `liveSyncPort` 和 `liveSyncURL` 字段
83
- - version: 5.6.1
84
- date: 2025-07-25
85
- changes: 使用 `importMode` 选项替换了 `activateDynamicImport`
86
- - version: 5.6.0
87
- date: 2025-07-13
88
- changes: 将默认的 contentDir 从 `['src']` 更改为 `['.']`
89
- - version: 5.5.11
90
- date: 2025-06-29
91
- changes: 添加了 `docs` 命令
92
- ---
93
-
94
- # Intlayer 配置文档
95
-
96
- ## 概述
97
-
98
- Intlayer 配置文件允许您自定义插件的各个方面,如国际化 (internationalization)、中间件和内容处理。本文档提供了配置中每个属性的深度说明。
99
-
100
- ---
101
-
102
- ## 目录
103
-
104
- <TOC/>
105
-
106
- ---
107
-
108
- ## 支持的配置文件格式
109
-
110
- Intlayer 接受 JSON, JS, MJS, TS 等配置文件格式:
111
-
112
- - `intlayer.config.ts`
113
- - `intlayer.config.js`
114
- - `intlayer.config.json`
115
- - `intlayer.config.json5`
116
- - `intlayer.config.jsonc`
117
- - `intlayer.config.cjs`
118
- - `intlayer.config.mjs`
119
- - `.intlayerrc`
120
-
121
- ---
122
-
123
- ## 配置文件示例
124
-
125
- ````typescript fileName="intlayer.config.ts" codeFormat="typescript"
126
- import { Locales, type IntlayerConfig } from "intlayer";
127
- import { nextjsRewrite } from "intlayer/routing";
128
- import { z } from "zod";
129
-
130
- /**
131
- * 显示所有可用选项的 Intlayer 配置文件示例。
132
- */
133
- const config: IntlayerConfig = {
134
- /**
135
- * 国际化设置配置。
136
- */
137
- internationalization: {
138
- /**
139
- * 应用程序支持的语言环境 (locales) 列表。
140
- * 默认值: [Locales.ENGLISH]
141
- */
142
- locales: [Locales.ENGLISH, Locales.FRENCH, Locales.SPANISH],
143
-
144
- /**
145
- * 每个字典中必须定义的必需语言环境列表。
146
- * 如果为空,在 `strict` 模式下所有语言环境都是必需的。
147
- * 默认值: []
148
- */
149
- requiredLocales: [Locales.ENGLISH],
150
-
151
- /**
152
- * 国际化内容的严格程度。
153
- * - "strict": 如果任何声明的语言环境缺失或未声明,则报错。
154
- * - "inclusive": 如果声明的语言环境缺失,则警告。
155
- * - "loose": 接受任何现有的语言环境。
156
- * 默认值: "inclusive"
157
- */
158
- strictMode: "inclusive",
159
-
160
- /**
161
- * 当请求的语言环境未找到时使用的默认语言环境。
162
- * 默认值: Locales.ENGLISH
163
- */
164
- defaultLocale: Locales.ENGLISH,
165
- },
166
-
167
- /**
168
- * 控制字典操作和回退行为的设置。
169
- */
170
- dictionary: {
171
- /**
172
- * 控制字典的导入方式。
173
- * - "static": 在构建时静态导入。
174
- * - "dynamic": 使用 Suspense 动态导入。
175
- * - "fetch": 通过 Live Sync API 动态获取。
176
- * 默认值: "static"
177
- */
178
- importMode: "static",
179
-
180
- /**
181
- * 使用 AI 自动填充缺失翻译的策略。
182
- * 可以是布尔值或用于保存填充内容的路径模式。
183
- * 默认值: true
184
- */
185
- fill: true,
186
-
187
- /**
188
- * 字典文件的物理位置。
189
- * - "local": 存储在本地文件系统中。
190
- * - "remote": 存储在 Intlayer CMS 中。
191
- * - "hybrid": 同时存储在本地和 Intlayer CMS 中。
192
- * - "plugin" (或任何自定义字符串): 由插件或自定义源提供。
193
- * 默认值: "local"
194
- */
195
- location: "local",
196
-
197
- /**
198
- * 内容是否应自动转换 (例如:Markdown 到 HTML)。
199
- * 默认值: false
200
- */
201
- contentAutoTransformation: false,
202
- },
203
-
204
- /**
205
- * 路由和中间件配置。
206
- */
207
- routing: {
208
- /**
209
- * 语言环境路由策略。
210
- * - "prefix-no-default": 为除默认语言环境以外的所有语言环境添加前缀 (例如:/dashboard, /fr/dashboard)。
211
- * - "prefix-all": 为所有语言环境添加前缀 (例如:/en/dashboard, /fr/dashboard)。
212
- * - "no-prefix": URL 中没有语言环境。
213
- * - "search-params": 使用 ?locale=...
214
- * 默认值: "prefix-no-default"
215
- */
216
- mode: "prefix-no-default",
217
-
218
- /**
219
- * 存储用户选择的语言环境的位置。
220
- * 选项: 'cookie', 'localStorage', 'sessionStorage', 'header' 或它们的数组。
221
- * 默认值: ['cookie', 'header']
222
- */
223
- storage: ["cookie", "header"],
224
-
225
- /**
226
- * 应用程序 URL 的基础路径。
227
- * 默认值: ""
228
- */
229
- basePath: "",
230
-
231
- /**
232
- * 针对各语言环境特定路径的自定义 URL 重写规则。
233
- */
234
- rewrite: nextjsRewrite({
235
- "/[locale]/about": {
236
- en: "/[locale]/about",
237
- fr: "/[locale]/a-propos",
238
- },
239
- }),
240
- },
241
-
242
- /**
243
- * 与内容文件查找和处理相关的设置。
244
- */
245
- content: {
246
- /**
247
- * 扫描字典的文件扩展名。
248
- * 默认值: ['.content.ts', '.content.js', '.content.json' 等]
249
- */
250
- fileExtensions: [".content.ts", ".content.js", ".content.json"],
251
-
252
- /**
253
- * .content 文件所在的目录。
254
- * 默认值: ["."]
255
- */
256
- contentDir: ["src"],
257
-
258
- /**
259
- * 源代码所在位置。
260
- * 用于构建优化和代码转换。
261
- * 默认值: ["."]
262
- */
263
- codeDir: ["src"],
264
-
265
- /**
266
- * 扫描中排除的模式。
267
- * 默认值: ['node_modules', '.intlayer' 等]
268
- */
269
- excludedPath: ["node_modules"],
270
-
271
- /**
272
- * 开发过程中是否监视更改并重新构建字典。
273
- * 默认值: 开发环境下为 true
274
- */
275
- watch: true,
276
-
277
- /**
278
- * 用于格式化新创建/更新的 .content 文件的命令。
279
- */
280
- formatCommand: 'npx prettier --write "{{file}}"',
281
- },
282
-
283
- /**
284
- * 可视化编辑器 (Visual Editor) 配置。
285
- */
286
- editor: {
287
- /**
288
- * 是否启用可视化编辑器。
289
- * 默认值: false
290
- */
291
- enabled: true,
292
-
293
- /**
294
- * 应用程序的 URL,用于源验证 (origin validation)。
295
- * 默认值: ""
296
- */
297
- applicationURL: "http://localhost:3000",
298
-
299
- /**
300
- * 本地编辑器服务器的端口。
301
- * 默认值: 8000
302
- */
303
- port: 8000,
304
-
305
- /**
306
- * 编辑器的公开 URL。
307
- * 默认值: "http://localhost:8000"
308
- */
309
- editorURL: "http://localhost:8000",
310
-
311
- /**
312
- * Intlayer CMS URL。
313
- * 默认值: "https://app.intlayer.org"
314
- */
315
- cmsURL: "https://app.intlayer.org",
316
-
317
- /**
318
- * 后端 API URL。
319
- * 默认值: "https://back.intlayer.org"
320
- */
321
- backendURL: "https://back.intlayer.org",
322
-
323
- /**
324
- * 是否启用实时内容同步。
325
- * 默认值: false
326
- */
327
- liveSync: true,
328
- },
329
-
330
- /**
331
- * 基于 AI 的翻译和构建设置。
332
- */
333
- ai: {
334
- /**
335
- * 要使用的 AI 提供商。
336
- * 选项: 'openai', 'anthropic', 'mistral', 'deepseek', 'gemini', 'ollama', 'openrouter', 'alibaba', 'fireworks', 'groq', 'huggingface', 'bedrock', 'googlevertex', 'togetherai'
337
- * 默认值: 'openai'
338
- */
339
- provider: "openai",
340
-
341
- /**
342
- * 要使用的所选提供商的模型。
343
- */
344
- model: "gpt-4o",
345
-
346
- /**
347
- * 提供商 API 密钥。
348
- */
349
- apiKey: process.env.OPENAI_API_KEY,
350
-
351
- /**
352
- * 全局上下文,用于指导 AI 生成翻译。
353
- */
354
- applicationContext: "这是一个旅游预订应用程序。",
355
-
356
- /**
357
- * AI API 的基础路径 URL。
358
- */
359
- baseURL: "http://localhost:3000",
360
-
361
- /**
362
- * 数据序列化 (Data Serialization)
363
- *
364
- * 选项:
365
- * - "json": 默认设置,稳健;消耗更多 Token。
366
- * - "toon": 消耗 Token 较少,可能不如 JSON 一致。
367
- *
368
- * 默认值: "json"
369
- */
370
- dataSerialization: "json",
371
- },
372
-
373
- /**
374
- * 构建和优化设置。
375
- */
376
- build: {
377
- /**
378
- * 构建执行模式。
379
- * - "auto": 在应用程序构建期间自动构建。
380
- * - "manual": 需要明确的构建命令。
381
- * 默认值: "auto"
382
- */
383
- mode: "auto",
384
-
385
- /**
386
- * 是否通过删除未使用的字典来优化最终包。
387
- * 默认值: 生产环境下为 true
388
- */
389
- optimize: true,
390
-
391
- /**
392
- * 生成的字典文件的输出格式。
393
- * 默认值: ['cjs', 'esm']
394
- */
395
- outputFormat: ["cjs", "esm"],
396
-
397
- /**
398
- * 指示构建是否应检查 TypeScript 类型。
399
- * 默认值: false
400
- */
401
- checkTypes: false,
402
- },
403
-
404
- /**
405
- * 日志记录器 (Logger) 配置。
406
- */
407
- log: {
408
- /**
409
- * 日志级别。
410
- * - "default": 标准日志记录。
411
- * - "verbose": 深入调试日志记录。
412
- * - "disabled": 禁用日志记录。
413
- * 默认值: "default"
414
- */
415
- mode: "default",
416
-
417
- /**
418
- * 所有日志消息的前缀。
419
- * 默认值: "[intlayer]"
420
- */
421
- prefix: "[intlayer]",
422
- },
423
-
424
- /**
425
- * 系统配置 (高级用途)
426
- */
427
- system: {
428
- /**
429
- * 存储本地化字典的目录。
430
- */
431
- dictionariesDir: ".intlayer/dictionary",
432
-
433
- /**
434
- * TypeScript 模块扩充 (module augmentation) 目录。
435
- */
436
- moduleAugmentationDir: ".intlayer/types",
437
-
438
- /**
439
- * 存储未合并字典的目录。
440
- */
441
- unmergedDictionariesDir: ".intlayer/unmerged_dictionary",
442
-
443
- /**
444
- * 存储字典类型的目录。
445
- */
446
- typesDir: ".intlayer/types",
447
-
448
- /**
449
- * 存储主要应用程序文件的目录。
450
- */
451
- mainDir: ".intlayer/main",
452
-
453
- /**
454
- * 存储配置文件的目录。
455
- */
456
- configDir: ".intlayer/config",
457
-
458
- /**
459
- * 存储缓存文件的目录。
460
- */
461
- cacheDir: ".intlayer/cache",
462
- },
463
-
464
- /**
465
- * 编译器配置 (高级用途)
466
- */
467
- compiler: {
468
- /**
469
- * 指示是否启用编译器。
470
- *
471
- * - false: 禁用编译器。
472
- * - true: 启用编译器。
473
- * - "build-only": 在开发期间跳过编译器,加速启动时间。
474
- *
475
- * 默认值: false
476
- */
477
- enabled: true,
478
-
479
- /**
480
- * 定义输出文件的路径。替换 `outputDir`。
481
- *
482
- * - `./` 路径相对于组件目录解析。
483
- * - `/` 路径相对于项目根目录 (`baseDir`) 解析。
484
- *
485
- * - 在路径中包含 `{{locale}}` 变量将为每个语言创建单独的字典。
486
- *
487
- * 示例:
488
- * ```ts
489
- * {
490
- * // 在组件旁边创建多语言 .content.ts 文件
491
- * output: ({ fileName, extension }) => `./${fileName}${extension}`,
492
- *
493
- * // output: './{{fileName}}{{extension}}', // 使用模板字符串的等效方式
494
- * }
495
- * ```
496
- *
497
- * ```ts
498
- * {
499
- * // 在项目根目录为每个语言创建集中的 JSON
500
- * output: ({ key, locale }) => `/locales/${locale}/${key}.content.json`,
501
- *
502
- * // output: '/locales/{{locale}}/{{key}}.content.json', // 使用模板字符串的等效方式
503
- * }
504
- * ```
505
- *
506
- * 变量列表:
507
- * - `fileName`: 文件名。
508
- * - `key`: 内容键。
509
- * - `locale`: 内容语言环境。
510
- * - `extension`: 文件扩展名。
511
- * - `componentFileName`: 组件文件名。
512
- * - `componentExtension`: 组件文件扩展名。
513
- * - `format`: 字典格式。
514
- * - `componentFormat`: 组件字典格式。
515
- * - `componentDirPath`: 组件目录路径。
516
- */
517
- output: ({ locale, key }) => `compiler/${locale}/${key}.json`,
518
-
519
- /**
520
- * 指示组件在转换后是否应保存。
521
- * 这样,编译器可以只运行一次以转换应用程序,然后删除。
522
- */
523
- saveComponents: false,
524
-
525
- /**
526
- * 仅在生成的文件中插入内容。对于 i18next 或 ICU MessageFormat 格式的每种语言 JSON 输出非常有用。
527
- */
528
- noMetadata: false,
529
-
530
- /**
531
- * 字典键前缀
532
- */
533
- dictionaryKeyPrefix: "", // 为提取的字典键添加可选前缀
534
- },
535
-
536
- /**
537
- * 验证字典内容的自定义架构 (Schemas)。
538
- */
539
- schemas: {
540
- "my-schema": z.object({
541
- title: z.string(),
542
- }),
543
- },
544
-
545
- /**
546
- * 插件配置。
547
- */
548
- plugins: [],
549
- };
550
-
551
- export default config;
552
- ````
553
-
554
- ---
555
-
556
- ## 配置参考 (Configuration Reference)
557
-
558
- 以下部分描述了 Intlayer 中可用的各种配置项。
559
-
560
- ---
561
-
562
- ### 国际化配置 (Internationalization Configuration)
563
-
564
- 定义与国际化相关的设置,包括可用的语言环境和应用程序的默认语言环境。
565
-
566
- | 字段 | 类型 | 说明 | 示例 | 备注 |
567
- | ----------------- | ---------- | --------------------------------------------------------------------- | -------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
568
- | `locales` | `string[]` | 应用程序支持的语言环境列表。默认值: `[Locales.ENGLISH]` | `['en', 'fr', 'es']` | |
569
- | `requiredLocales` | `string[]` | 应用程序中必需的语言环境列表。默认值: `[]` | `[]` | 如果为空,在 `strict` 模式下所有语言环境都是必需的。请确保必需的语言环境也在 `locales` 字段中定义。 |
570
- | `strictMode` | `string` | 确保使用 TypeScript 稳健地实现国际化内容。默认值: `inclusive` | | 如果为 `"strict"`: `t` 函数要求定义每个声明的语言环境 — 如果有任何缺失或未声明,则抛出错误。如果为 `"inclusive"`: 对缺失的语言环境进行警告,但接受现有的未声明语言环境。如果为 `"loose"`: 接受任何现有的语言环境。 |
571
- | `defaultLocale` | `string` | 当请求的语言环境未找到时使用的默认语言环境。默认值: `Locales.ENGLISH` | `'en'` | 当 URL、Cookie 或 Header 中未指定语言环境时,用于确定语言环境。 |
572
-
573
- ---
574
-
575
- ### 编辑器配置 (Editor Configuration)
576
-
577
- 定义与集成编辑器相关的设置,包括服务器端口和活动状态。
578
-
579
- | 字段 | 类型 | 说明 | 示例 | 备注 |
580
- | ---------------------------- | ------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------- |
581
- | `applicationURL` | `string` | 应用程序的 URL。默认值: `''` | `'http://localhost:3000'`, `'https://example.com'`, `process.env.INTLAYER_EDITOR_URL` | 用于出于安全原因限制编辑器的源 (origin)。如果设置为 `'*'`,则可以从任何源访问编辑器。 |
582
- | `port` | `number` | 可视化编辑器服务器使用的端口。默认值: `8000` | | |
583
- | `editorURL` | `string` | 编辑器服务器的 URL。默认值: `'http://localhost:8000'` | `'http://localhost:3000'`, `'https://example.com'`, `process.env.INTLAYER_EDITOR_URL` | 用于限制可以与应用程序交互的源。如果设置为 `'*'`,则可从任何源访问。在更改端口或编辑器托管在其他域名时必须进行设置。 |
584
- | `cmsURL` | `string` | Intlayer CMS URL。默认值: `'https://intlayer.org'` | `'https://intlayer.org'` | |
585
- | `backendURL` | `string` | 后端服务器 URL。默认值: `https://back.intlayer.org` | `http://localhost:4000` | |
586
- | `enabled` | `boolean` | 指示应用是否将与可视化编辑器交互。默认值: `true` | `process.env.NODE_ENV !== 'production'` | 如果为 `false`,则编辑器无法与应用交互。针对特定环境禁用它可以增强安全性。 |
587
- | `clientId` | `string &#124; undefined` | 允许 intlayer 包使用 oAuth2 在后端进行身份验证。要获取访问令牌,请访问 [intlayer.org/project](https://app.intlayer.org/project)。默认值: `undefined` | | 请保密;存储在环境变量中。 |
588
- | `clientSecret` | `string &#124; undefined` | 允许 intlayer 包使用 oAuth2 在后端进行身份验证。要获取访问令牌,请访问 [intlayer.org/project](https://app.intlayer.org/project)。默认值: `undefined` | | 请保密;存储在环境变量中。 |
589
- | `dictionaryPriorityStrategy` | `string` | 当本地字典和远程字典同时存在时字典优先级策略。默认值: `'local_first'` | `'distant_first'` | `'distant_first'`: 远程优先于本地。 `'local_first'`: 本地优先于远程。 |
590
- | `liveSync` | `boolean` | 指示当在 CMS / 可视化编辑器 / 后端检测到更改时,应用服务器是否应热重载内容。默认值: `true` | `true` | 当添加/更新字典时,应用会更新页面内容。实时同步将内容外包给另一个服务器,这可能会对性能产生轻微影响。建议将两者托管在同一台机器上。 |
591
- | `liveSyncPort` | `number` | 实时同步服务器端口。默认值: `4000` | `4000` | |
592
- | `liveSyncURL` | `string` | 实时同步服务器 URL。默认值: `'http://localhost:{liveSyncPort}'` | `'https://example.com'` | 默认指向 localhost;可以更改为远程实时同步服务器。 |
593
-
594
- ### 路由配置 (Routing Configuration)
595
-
596
- 控制路由行为的设置,包括 URL 结构、语言环境存储和中间件处理。
597
-
598
- | 字段 | 类型 | 说明 | 示例 | 备注 |
599
- | ---------- | ---------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
600
- | `mode` | `'prefix-no-default' &#124; 'prefix-all' &#124; 'no-prefix' &#124; 'search-params'` | 处理语言环境的 URL 路由模式。默认值: `'prefix-no-default'` | `'prefix-no-default'`: `/dashboard` (en) 或 `/fr/dashboard` (fr)。 `'prefix-all'`: `/en/dashboard`。 `'no-prefix'`: 语言环境通过其他方式处理。 `'search-params'`: `/dashboard?locale=fr` | 不影响 Cookie 或语言环境存储管理。 |
601
- | `storage` | `false &#124; 'cookie' &#124; 'localStorage' &#124; 'sessionStorage' &#124; 'header' &#124; CookiesAttributes &#124; StorageAttributes &#124; Array` | 在客户端存储语言环境的配置。默认值: `['cookie', 'header']` | `'localStorage'`, `[{ type: 'cookie', name: 'custom-locale', secure: true }]` | 请参阅下方的“存储选项”表。 |
602
- | `basePath` | `string` | 应用程序 URL 的基础路径。默认值: `''` | `'/my-app'` | 如果应用程序位于 `https://example.com/my-app`,则 basePath 为 `'/my-app'`,URL 变为 `https://example.com/my-app/en`。 |
603
- | `rewrite` | `Record<string, StrictModeLocaleMap<string>>` | 自定义 URL 重写规则,针对特定路径覆盖默认路由模式。支持动态参数 `[param]`。默认值: `undefined` | 请参阅下方示例 | 重写规则优先于 `mode`。适用于 Next.js 和 Vite。`getLocalizedUrl()` 会自动应用匹配的规则。请参阅 [自定义 URL 重写](https://github.com/aymericzip/intlayer/blob/main/docs/docs/zh/custom_url_rewrites.md)。 |
604
-
605
- **`rewrite` 示例**:
606
-
607
- ```typescript
608
- routing: {
609
- mode: "prefix-no-default", // 回退策略
610
- rewrite: nextjsRewrite({
611
- "/about": {
612
- en: "/about",
613
- fr: "/a-propos",
614
- },
615
- "/product/[slug]": {
616
- en: "/product/[slug]",
617
- fr: "/produit/[slug]",
618
- },
619
- "/blog/[category]/[id]": {
620
- en: "/blog/[category]/[id]",
621
- fr: "/journal/[category]/[id]",
622
- },
623
- }),
624
- }
625
- ```
626
-
627
- #### 存储选项 (Storage Options)
628
-
629
- | 值 | 说明 | 备注 |
630
- | ------------------ | ------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- |
631
- | `'cookie'` | 将语言环境存储在 Cookie 中 — 客户端和服务器端均可访问。 | 为了符合 GDPR,请确保已获得适当的用户同意。可通过 `CookiesAttributes` 自定义 (`{ type: 'cookie', name: 'custom-locale', secure: true, httpOnly: false }`)。 |
632
- | `'localStorage'` | 将语言环境存储在浏览器中,没有过期日期 — 仅限客户端。 | 除非明确清除,否则不会过期。Intlayer 代理无法访问。可通过 `StorageAttributes` 自定义 (`{ type: 'localStorage', name: 'custom-locale' }`)。 |
633
- | `'sessionStorage'` | 在页面会话期间存储语言环境 — 仅限客户端。 | 关闭标签页/窗口时将被清除。Intlayer 代理无法访问。可通过 `StorageAttributes` 自定义 (`{ type: 'sessionStorage', name: 'custom-locale' }`)。 |
634
- | `'header'` | 通过 HTTP 头存储或传输语言环境 — 仅限服务器端。 | 对于 API 调用非常有用。客户端无法访问。可通过 `StorageAttributes` 自定义 (`{ type: 'header', name: 'custom-locale' }`)。 |
635
-
636
- #### Cookie 属性 (Cookie Attributes)
637
-
638
- 使用 Cookie 存储时,您可以配置额外的 Cookie 属性:
639
-
640
- | 字段 | 类型 | 说明 |
641
- | ---------- | ------------------------------------- | ---------------------------------------- |
642
- | `name` | `string` | Cookie 名称。默认值: `'INTLAYER_LOCALE'` |
643
- | `domain` | `string` | Cookie 域名。默认值: `undefined` |
644
- | `path` | `string` | Cookie 路径。默认值: `undefined` |
645
- | `secure` | `boolean` | 要求 HTTPS。默认值: `undefined` |
646
- | `httpOnly` | `boolean` | HTTP-only 标志。默认值: `undefined` |
647
- | `sameSite` | `'strict' &#124; 'lax' &#124; 'none'` | SameSite 策略。 |
648
- | `expires` | `Date &#124; number` | 过期日期或天数。默认值: `undefined` |
649
-
650
- #### 语言环境存储属性 (Locale Storage Attributes)
651
-
652
- 使用 localStorage 或 sessionStorage 时:
653
-
654
- | 字段 | 类型 | 说明 |
655
- | ------ | ---------------------------------------- | ----------------------------------------- |
656
- | `type` | `'localStorage' &#124; 'sessionStorage'` | 存储类型。 |
657
- | `name` | `string` | 存储键的名称。默认值: `'INTLAYER_LOCALE'` |
658
-
659
- #### 配置示例
660
-
661
- 以下是新 v7 路由结构的一些常见配置示例:
662
-
663
- **基础配置 (默认)**:
664
-
665
- ```typescript
666
- import { Locales, type IntlayerConfig } from "intlayer";
667
- // intlayer.config.ts
668
- const config: IntlayerConfig = {
669
- internationalization: {
670
- locales: ["en", "fr", "es"],
671
- defaultLocale: "en",
672
- },
673
- routing: {
674
- mode: "prefix-no-default",
675
- storage: "localStorage",
676
- basePath: "",
677
- },
678
- };
679
-
680
- export default config;
681
- ```
682
-
683
- **符合 GDPR 的配置**:
684
-
685
- ```typescript
686
- import { Locales, type IntlayerConfig } from "intlayer";
687
- // intlayer.config.ts
688
- const config: IntlayerConfig = {
689
- internationalization: {
690
- locales: ["en", "fr", "es"],
691
- defaultLocale: "en",
692
- },
693
- routing: {
694
- mode: "prefix-no-default",
695
- storage: [
696
- {
697
- type: "localStorage",
698
- name: "user-locale",
699
- },
700
- {
701
- type: "cookie",
702
- name: "user-locale",
703
- secure: true,
704
- sameSite: "strict",
705
- httpOnly: false,
706
- },
707
- ],
708
- basePath: "",
709
- },
710
- };
711
-
712
- export default config;
713
- ```
714
-
715
- **搜索参数模式 (Search Parameters Mode)**:
716
-
717
- ```typescript
718
- import { Locales, type IntlayerConfig } from "intlayer";
719
- // intlayer.config.ts
720
- const config: IntlayerConfig = {
721
- internationalization: {
722
- locales: ["en", "fr", "es"],
723
- defaultLocale: "en",
724
- },
725
- routing: {
726
- mode: "search-params",
727
- storage: "localStorage",
728
- basePath: "",
729
- },
730
- };
731
-
732
- export default config;
733
- ```
734
-
735
- **带自定义存储的无前缀模式 (No Prefix Mode)**:
736
-
737
- ```typescript
738
- import { Locales, type IntlayerConfig } from "intlayer";
739
- // intlayer.config.ts
740
- const config: IntlayerConfig = {
741
- internationalization: {
742
- locales: ["en", "fr", "es"],
743
- defaultLocale: "en",
744
- },
745
- routing: {
746
- mode: "no-prefix",
747
- storage: {
748
- type: "sessionStorage",
749
- name: "app-locale",
750
- },
751
- basePath: "/my-app",
752
- },
753
- };
754
-
755
- export default config;
756
- ```
757
-
758
- **带有动态路径的自定义 URL 重写**:
759
-
760
- ```typescript
761
- // intlayer.config.ts
762
- import { nextjsRewrite } from "intlayer/routing";
763
-
764
- const config: IntlayerConfig = {
765
- internationalization: {
766
- locales: ["en", "fr"],
767
- defaultLocale: "en",
768
- },
769
- routing: {
770
- mode: "prefix-no-default", // 针对未重写路径的回退策略
771
- storage: "cookie",
772
- rewrite: nextjsRewrite({
773
- "/about": {
774
- en: "/about",
775
- fr: "/a-propos",
776
- },
777
- "/product/[slug]": {
778
- en: "/product/[slug]",
779
- fr: "/produit/[slug]",
780
- },
781
- "/blog/[category]/[id]": {
782
- en: "/blog/[category]/[id]",
783
- fr: "/journal/[category]/[id]",
784
- },
785
- }),
786
- },
787
- };
788
-
789
- export default config;
790
- ```
791
-
792
- ---
793
-
794
- ### 内容配置 (Content Configuration)
795
-
796
- 与应用程序中的内容处理相关的设置 (目录名、文件扩展名和衍生配置)。
797
-
798
- | 字段 | 类型 | 说明 | 示例 | 备注 |
799
- | ---------------- | ---------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------- | -------------------------------------------------------- |
800
- | `watch` | `boolean` | 指示 Intlayer 是否应监视内容声明文件的更改以重新构建字典。默认值: `process.env.NODE_ENV === 'development'` | | |
801
- | `fileExtensions` | `string[]` | 用于扫描内容声明文件的文件扩展名。默认值: `['.content.ts', '.content.js', '.content.mjs', '.content.cjs', '.content.json', '.content.json5', '.content.jsonc']` | `['.content.ts', '.content.js']` | |
802
- | `contentDir` | `string[]` | 内容声明文件所在目录的路径。默认值: `['.']` | `['src/content']` | |
803
- | `codeDir` | `string[]` | 应用程序源代码文件所在目录的路径。默认值: `['.']` | `['src']` | 用于优化构建,并确保代码转换和热重载仅应用于必要的文件。 |
804
- | `excludedPath` | `string[]` | 内容扫描中排除的路径。默认值: `['node_modules', '.intlayer', '.next', 'dist', 'build']` | `['src/styles']` | |
805
- | `formatCommand` | `string` | 将为格式化新创建或更新的内容文件而运行的命令。默认值: `undefined` | `'npx prettier --write "{{file}}"'` | 在内容提取期间或通过可视化编辑器使用。 |
806
-
807
- ---
808
-
809
- ### 字典配置 (Dictionary Configuration)
810
-
811
- 控制字典操作,包括自动填充行为和内容生成。
812
-
813
- 此字典配置有两个主要目的:
814
-
815
- 1. **默认值**:在创建内容声明文件时定义默认值
816
- 2. **回退行为**:当未定义特定字段时提供回退值,允许您全局定义字典操作行为
817
-
818
- 有关内容声明文件以及配置值如何应用的更多信息,请参阅 [内容文件文档](https://github.com/aymericzip/intlayer/blob/main/docs/docs/zh/dictionary/content_file.md)。
819
-
820
- | 字段 | 类型 | 说明 | 示例 | 备注 |
821
- | --------------------------- | ----------------------------------------------------------------------------------------------- | ------------------------------------------------------------------- | -------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
822
- | `fill` | `boolean &#124; FilePathPattern &#124; Partial<Record<Locale, boolean &#124; FilePathPattern>>` | 控制自动填充(AI 翻译)输出文件的生成方式。默认值: `true` | 请参阅下方示例 | `true`: 默认路径(与源文件相同)。`false`: 禁用。字符串/函数模板为每个语言环境生成文件。每个语言环境的对象:每个语言环境映射到其自己的模式;`false` 跳过该语言环境。包含 `{{locale}}` 会触发每个语言环境的生成。字典级别的 `fill` 始终优先于此全局配置。 |
823
- | `importMode` | `'static' &#124; 'dynamic' &#124; 'fetch'` | 控制字典的导入方式。默认值: `'static'` | `'dynamic'` | `'static'`: 静态导入。`'dynamic'`: 通过 Suspense 动态导入。`'fetch'`: 通过实时同步 API 动态获取。不影响 `getIntlayer`, `getDictionary`, `useDictionary` 等。 |
824
- | `location` | `'local' &#124; 'remote' &#124; 'hybrid' &#124; string` | 字典存储的位置。默认值: `'local'` | `'remote'` | `'local'`: 文件系统。`'remote'`: Intlayer CMS。`'hybrid'`: 两者。 |
825
- | `contentAutoTransformation` | `boolean` | 内容文件是否应自动转换 (例如:从 Markdown 到 HTML)。默认值: `false` | `true` | 有助于通过 @intlayer/markdown 处理 Markdown 字段。 |
826
-
827
- **`fill` 示例**:
828
-
829
- ```ts
830
- dictionary: {
831
- fill: {
832
- en: '/locales/en/{{key}}.content.json',
833
- fr: ({ key }) => `/locales/fr/${key}.content.json`,
834
- es: false,
835
- }
836
- }
837
- ```
838
-
839
- ---
840
-
841
- ### AI 配置 (AI Configuration)
842
-
843
- 定义 Intlayer AI 驱动功能 (如翻译构建) 的设置。
844
-
845
- | 字段 | 类型 | 说明 | 示例 | 备注 |
846
- | -------------------- | ---------------------- | ---------------------------------------------------- | ------------------------------------------- | ------------------------------------------------------------------ |
847
- | `provider` | `string` | 要使用的 AI 提供商。 | `'openai'`, `'anthropic'`, `'googlevertex'` | |
848
- | `model` | `string` | 要使用的 AI 模型。 | `'gpt-4o'`, `'claude-3-5-sonnet-20240620'` | |
849
- | `apiKey` | `string` | 所选提供商的 API 密钥。 | `process.env.OPENAI_API_KEY` | |
850
- | `applicationContext` | `string` | 有关您应用程序的额外上下文,以提高 AI 翻译的准确性。 | `'面向儿童的学习平台。'` | |
851
- | `baseURL` | `string` | API 调用的可选基础 URL。 | | 如果您使用代理或本地 AI 部署,这很有用。 |
852
- | `dataSerialization` | `'json' &#124; 'toon'` | 定义如何向 AI 发送数据。默认值: `'json'` | `'json'` | `'json'`: 更稳健精确。 `'toon'`: 消耗 Token 较少,但可能不太稳定。 |
853
-
854
- ---
855
-
856
- ### 构建配置 (Build Configuration)
857
-
858
- Intlayer 构建流程和优化设置。
859
-
860
- | 字段 | 类型 | 说明 | 示例 | 备注 |
861
- | -------------- | ------------------------ | --------------------------------------------------------------------- | ---- | ---- |
862
- | `mode` | `'auto' &#124; 'manual'` | 指示 Intlayer 是否应在应用预构建步骤期间自动运行。默认值: `'auto'` | | |
863
- | `optimize` | `boolean` | 指示编译后的字典是否应针对运行时进行优化。默认值: 生产环境下为 `true` | | |
864
- | `outputFormat` | `('cjs' &#124; 'esm')[]` | 生成的字典文件的输出格式。默认值: `['cjs', 'esm']` | | |
865
- | `checkTypes` | `boolean` | 指示 Intlayer 是否应检查生成文件中的类型。默认值: `false` | | |
866
-
867
- ---
868
-
869
- ### 系统配置 (System Configuration)
870
-
871
- 这些设置面向高级用例和 Intlayer 内部配置。
872
-
873
- | 字段 | 类型 | 说明 | 默认值 |
874
- | ------------------------- | -------- | ------------------------- | --------------------------------- |
875
- | `dictionariesDir` | `string` | 编译后字典的目录。 | `'.intlayer/dictionary'` |
876
- | `moduleAugmentationDir` | `string` | TypeScript 模块扩充目录。 | `'.intlayer/types'` |
877
- | `unmergedDictionariesDir` | `string` | 未合并字典的目录。 | `'.intlayer/unmerged_dictionary'` |
878
- | `typesDir` | `string` | 生成类型的目录。 | `'.intlayer/types'` |
879
- | `mainDir` | `string` | 主要 Intlayer 文件目录。 | `'.intlayer/main'` |
880
- | `configDir` | `string` | 编译后的配置文件目录。 | `'.intlayer/config'` |
881
- | `cacheDir` | `string` | 缓存文件目录。 | `'.intlayer/cache'` |
882
-
883
- ---
884
-
885
- ### 编译器配置 (Compiler Configuration)
886
-
887
- Intlayer 编译器 (`intlayer compiler`) 的设置。
888
-
889
- | 字段 | 类型 | 说明 | 默认值 |
890
- | --------------------- | ------------------------ | ----------------------------------------------------- | ------- |
891
- | `enabled` | `boolean` | 指示编译器是否处于活动状态。 | `false` |
892
- | `output` | `string &#124; Function` | 提取的字典的输出路径。 | |
893
- | `saveComponents` | `boolean` | 指示是否应使用转换后的版本覆盖原始源文件。 | `false` |
894
- | `noMetadata` | `boolean` | 如果为 `true`,编译器将不会在生成的文件中包含元数据。 | `false` |
895
- | `dictionaryKeyPrefix` | `string` | 可选的字典键前缀。 | `''` |
896
-
897
- ---
898
-
899
- ### 日志记录器配置 (Logger Configuration)
900
-
901
- 自定义 Intlayer 日志输出的设置。
902
-
903
- | 字段 | 类型 | 说明 | 默认值 |
904
- | -------- | ---------------------------------------------- | -------------- | -------------- |
905
- | `mode` | `'default' &#124; 'verbose' &#124; 'disabled'` | 日志模式。 | `'default'` |
906
- | `prefix` | `string` | 日志消息前缀。 | `'[intlayer]'` |
907
-
908
- ---
909
-
910
- ### 自定义架构 (Custom Schemas)
911
-
912
- | 字段 | 类型 | 说明 |
913
- | --------- | --------------------------- | ------------------------------------- |
914
- | `schemas` | `Record<string, ZodSchema>` | 允许您定义 Zod 架构来验证字典的结构。 |
915
-
916
- ---
917
-
918
- ### 插件 (Plugins)
919
-
920
- | 字段 | 类型 | 说明 |
921
- | --------- | ------------------ | ---------------------------- |
922
- | `plugins` | `IntlayerPlugin[]` | 要激活的 Intlayer 插件列表。 |