使用您最喜欢的AI助手总结文档,并引用此页面和AI提供商
通过将 Intlayer MCP 服务器集成到您的 AI 助手,您可以直接从 ChatGPT、DeepSeek、Cursor、VSCode 等获取所有文档。
查看 MCP 服务器文档此页面的内容已使用 AI 翻译。
查看英文原文的最新版本如果您有改善此文档的想法,请随时通过在GitHub上提交拉取请求来贡献。
文档的 GitHub 链接复制文档 Markdown 到剪贴板
自动填充内容声明文件翻译
自动填充内容声明文件 是加快开发工作流程的一种方式。
自动填充机制通过内容声明文件之间的“主从”关系来实现。当主(master)文件被更新时,Intlayer 会自动将这些更改应用到派生(自动填充)的声明文件中。
复制代码到剪贴板
import { Locales, type Dictionary } from "intlayer";// 定义示例内容字典const exampleContent = { key: "example", locale: Locales.ENGLISH, autoFill: "./example.content.json", content: { contentExample: "This is an example of content", // 这是内容示例 },} satisfies Dictionary;export default exampleContent;
这是一个使用 autoFill 指令的按语言环境划分的内容声明文件。
然后,当你运行以下命令时:
复制代码到剪贴板
npx intlayer fill --file 'src/components/example/example.content.ts'
Intlayer 将自动生成派生的声明文件,路径为 src/components/example/example.content.json,填充所有主文件中尚未声明的语言环境。
复制代码到剪贴板
{ "key": "example", "content": { "contentExample": { "nodeType": "translation", "translation": { "fr": "Ceci est un exemple de contenu", "es": "Este es un ejemplo de contenido", }, }, },}
之后,两个声明文件将合并为一个字典,可以通过标准的 useIntlayer("example") 钩子(React)/ 组合函数(Vue)访问。
自动填充文件格式
自动填充声明文件推荐的格式是 JSON,这有助于避免格式限制。然而,Intlayer 也支持 .ts、.js、.mjs、.cjs 以及其他格式。
复制代码到剪贴板
const exampleContent = { key: "example", autoFill: "./example.filled.content.ts", content: { // 你的内容 },};
这将生成文件于:
src/components/example/example.filled.content.ts.js、.ts 及类似文件的生成方式如下:
- 如果文件已存在,Intlayer 将使用 AST(抽象语法树)解析它,以定位每个字段并插入任何缺失的翻译。
- 如果文件不存在,Intlayer 将使用默认内容声明文件模板生成该文件。
绝对路径
autoFill 字段也支持绝对路径。
复制代码到剪贴板
const exampleContent = { key: "example", autoFill: "/messages/example.content.json", content: { // 你的内容 },};
这将生成文件于:
/messages/example.content.json自动生成每个语言环境的内容声明文件
autoFill 字段还支持生成每个语言环境的内容声明文件。
复制代码到剪贴板
const exampleContent = { key: "example", autoFill: { fr: "./example.fr.content.json", es: "./example.es.content.json", }, content: { // 你的内容 },};
这将生成两个独立的文件:
- src/components/example/example.fr.content.json
- src/components/example/example.es.content.json
过滤特定语言自动填充
使用对象作为 autoFill 字段允许你应用过滤器,只生成特定语言的文件。
复制代码到剪贴板
const exampleContent = { key: "example", autoFill: { fr: "./example.fr.content.json", }, content: { // 你的内容 },};
这将只生成法语翻译文件。
路径变量
你可以在 autoFill 路径中使用变量,动态解析生成文件的目标路径。
可用变量:
- {{locale}} – 语言代码(例如 fr,es)
- {{key}} – 字典键(例如 example)
复制代码到剪贴板
const exampleContent = { key: "example", autoFill: "/messages/{{locale}}/{{key}}.content.json", content: { // 您的内容 },};
这将生成:
- /messages/fr/example.content.json
- /messages/es/example.content.json
文档历史
- 5.5.10 - 2025-06-29: 初始化历史记录