首页演练场案例展示应用文档博客
    • English英语
      EN
    • русский俄语
      RU
    • 日本語日语
      JA
    • français法语
      FR
    • 한국어韩语
      KO
    • 中文中文
      ZH
    • español西班牙语
      ES
    • Deutsch德语
      DE
    • العربية阿拉伯语
      AR
    • italiano意大利语
      IT
    • British English英国英语
      EN-GB
    • português葡萄牙语
      PT
    • हिन्दी印地语
      HI
    • Türkçe土耳其语
      TR
    • polski波兰语
      PL
    • Indonesia印度尼西亚语
      ID
    • Tiếng Việt越南语
      VI
    • українська乌克兰语
      UK
    /
    按框架筛选文档
    Alt+←
    为什么Intlayer?
    开始
    概念
    • Intlayer如何工作
    • 配置
    • TestFillBuildWatchExtractLoginPushPullConfigurationListVersionEditorLiveDebugDoc ReviewDoc TranslateSDK
    • 可视化编辑器
    • CMS
    • CI/CD集成
    • 翻译复数枚举条件性别插入文件嵌套MarkdownHTML函数获取
    • 每个语言环境的文件
    • 编译器
    • 自动填充
    • 测试
    • 打包优化
    环境
    • Next.js 14和应用路由器
      Next.js 15
      Next.js 无 locale URL
      Next.js和页面路由器
      编译器
    • Tanstack Start Solid
    • Astro和React
      Astro和Svelte
      Astro和Vue
      Astro和Solid
      Astro和Preact
      Astro和Lit
      Astro和Vanilla JS
    • React Router v7
      React Router v7 (fs-routes)
      Compiler
    • Nuxt和Vue
    • Vite和Solid
    • SvelteKit
    • Vite和Preact
    • Vite和Vanilla JS
    • Vite和Lit
    • Angular 19 (Webpack)
      Analog
    • React CRA
    • React Native和Expo
    • Express.js
      NestJS
      Fastify
      Hono
      Adonis
    • Lynx和React
    Plugins
    • JSON
    • gettext (.po)
    VS Code扩展
    代理
    • MCP服务器
    • 代理技能
    发布
    • v8
    • v7
    • v6
    基准测试
    • Next.js
    • TanStack
    • Vue
    • Solid
    • Svelte
    博客
    问问题
    1. Documentation
    2. 概念
    3. 每个语言环境的文件
    Creation:2025-04-18Last update:2025-06-29
    将此文档参考到您的 AI 助手
    ChatGPT
    Claude
    DeepSeek
    Google AI mode
    Gemini
    Perplexity
    Mistral
    Grok

    使用您最喜欢的AI助手总结文档,并引用此页面和AI提供商

    版本历史

    1. "初始化历史"
      v5.5.102025/6/29

    此页面的内容已使用 AI 翻译。

    查看英文原文的最新版本
    编辑此文档

    如果您有改善此文档的想法,请随时通过在GitHub上提交拉取请求来贡献。

    文档的 GitHub 链接
    Copy

    复制文档 Markdown 到剪贴板

    Intlayer 中的“每语言”内容声明

    Intlayer 支持两种声明多语言内容的方式:

    • 单文件包含所有翻译
    • 每个语言一个文件(每语言格式)

    这种灵活性使得:

    • 轻松迁移自其他国际化工具
    • 支持自动化翻译工作流程
    • 将翻译清晰地组织到独立的、特定语言的文件中

    单文件多语言翻译

    这种格式适用于:

    • 代码中的快速迭代。
    • 与内容管理系统(CMS)的无缝集成。

    这是大多数使用场景推荐的方法。它将翻译集中管理,便于迭代和与 CMS 集成。

    hello-world.content.ts
    复制代码

    复制代码到剪贴板

    import { t, type Dictionary } from "intlayer";
    
    const helloWorldContent = {
      key: "hello-world",
      content: {
        multilingualContent: t({
          en: "Title of my component",
          es: "Título de mi componente",
        }),
      },
    } satisfies Dictionary;
    
    export default helloWorldContent;
    推荐:当使用 Intlayer 的可视化编辑器或直接在代码中管理翻译时,此格式最佳。

    每个语言区域文件的全局配置

    您可以通过在 intlayer.config.ts 文件中添加以下内容来配置每个语言区域文件的全局配置:

    ts
    复制代码

    复制代码到剪贴板

    import { Locales, type IntlayerConfig } from "intlayer";const config: IntlayerConfig = {  dictionary: {    locale: Locales.ENGLISH,  },};export default config;

    使用此配置,所有按语言区域的文件都将以默认语言区域设置为英语的方式生成。它还包括使用 extract 命令和编译器生成的 .content 文件。(有关更多信息,请参阅 编译器 (Compiler) 或 提取 (Extract)。)

    按语言区域格式

    此格式适用于:

    • 您希望独立版本控制或覆盖翻译。
    • 您正在集成机器或人工翻译工作流。

    您还可以通过指定 locale 字段,将翻译拆分到单独的语言区域文件中:

    hello-world.es.content.ts
    复制代码

    复制代码到剪贴板

    import { t, Locales, type Dictionary } from "intlayer";
    
    const helloWorldContent = {
      key: "hello-world",
      locale: Locales.SPANISH, // 重要
      content: { multilingualContent: "Título de mi componente" },
    } satisfies Dictionary;
    
    export default helloWorldContent;
    重要提示:确保定义了 locale 字段。它告诉 Intlayer 该文件代表哪种语言。
    注意:在这两种情况下,内容声明文件必须遵循命名模式 *.content.{ts,tsx,js,jsx,mjs,cjs,json},以便被 Intlayer 识别。.[locale] 后缀是可选的,仅作为命名约定使用。

    混合格式

    您可以将两种声明方法结合使用于同一个内容键。例如:

    • 在像 index.content.ts 这样的文件中静态声明您的基础内容。
    • 在单独的文件中添加或覆盖特定的翻译,例如 index.fr.content.ts 或 index.content.json。

    这种设置特别适用于:

    • 您想在代码中定义初始内容结构。
    • 您计划稍后使用 CMS 或自动化工具丰富或完善翻译。
    bash
    复制代码

    复制代码到剪贴板

    .└── Components    └── MyComponent        ├── index.content.ts        ├── index.content.json        └── index.ts

    示例

    这是一个多语言内容声明文件:

    Components/MyComponent/index.content.ts
    复制代码

    复制代码到剪贴板

    import { t, type Dictionary } from "intlayer";const helloWorldContent = {  key: "hello-world",  locale: Locales.ENGLISH,  content: {    multilingualContent: "我的组件标题",    projectName: "我的项目",  },} satisfies Dictionary;export default helloWorldContent;
    Components/MyComponent/index.content.json
    复制代码

    复制代码到剪贴板

    {  "$schema": "https://intlayer.org/schema.json",  "key": "hello-world",  "content": {    "multilingualContent": {      "nodeType": "translation",      "translation": {        "fr": "Titre de mon composant",        "es": "Título de mi componente"      }    }  }}

    Intlayer 会自动合并多语言和每个语言环境的文件。

    Components/MyComponent/index.ts
    复制代码

    复制代码到剪贴板

    import { getIntlayer, Locales } from "intlayer";const intlayer = getIntlayer("hello-world"); // 默认语言是英语,因此它将返回英语内容console.log(JSON.stringify(intlayer, null, 2));// 结果:// {//  "multilingualContent": "我的组件标题",//  "projectName": "我的项目"// }const intlayer = getIntlayer("hello-world", Locales.SPANISH);console.log(JSON.stringify(intlayer, null, 2));// 结果:// {//  "multilingualContent": "Título de mi componente",//  "projectName": "我的项目"// }const intlayer = getIntlayer("hello-world", Locales.FRENCH);console.log(JSON.stringify(intlayer, null, 2));// 结果:// {//  "multilingualContent": "Titre de mon composant",//  "projectName": "我的项目"// }

    自动翻译生成

    使用 intlayer CLI 根据您偏好的服务自动填充缺失的翻译。

    函数获取
    编译器
    Alt+→

    在此页面

      讨论是匿名的,并会定期审查以解决常见问题。欢迎分享功能想法、对文档的反馈或任何与 Intlayer 相关的内容, 我们会利用这些意见来制定路线图并改进产品。

      import { Locales, type IntlayerConfig } from "intlayer";const config: IntlayerConfig = {  dictionary: {    locale: Locales.ENGLISH,  },};export default config;
      .└── Components    └── MyComponent        ├── index.content.ts        ├── index.content.json        └── index.ts
      import { t, type Dictionary } from "intlayer";const helloWorldContent = {  key: "hello-world",  locale: Locales.ENGLISH,  content: {    multilingualContent: "我的组件标题",    projectName: "我的项目",  },} satisfies Dictionary;export default helloWorldContent;
      {  "$schema": "https://intlayer.org/schema.json",  "key": "hello-world",  "content": {    "multilingualContent": {      "nodeType": "translation",      "translation": {        "fr": "Titre de mon composant",        "es": "Título de mi componente"      }    }  }}
      import { getIntlayer, Locales } from "intlayer";const intlayer = getIntlayer("hello-world"); // 默认语言是英语,因此它将返回英语内容console.log(JSON.stringify(intlayer, null, 2));// 结果:// {//  "multilingualContent": "我的组件标题",//  "projectName": "我的项目"// }const intlayer = getIntlayer("hello-world", Locales.SPANISH);console.log(JSON.stringify(intlayer, null, 2));// 结果:// {//  "multilingualContent": "Título de mi componente",//  "projectName": "我的项目"// }const intlayer = getIntlayer("hello-world", Locales.FRENCH);console.log(JSON.stringify(intlayer, null, 2));// 结果:// {//  "multilingualContent": "Titre de mon composant",//  "projectName": "我的项目"// }