首页演练场案例展示应用文档博客
    • 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+←
    什么是国际化?
    SEO和国际化
    指南
    • 使用next-i18next的i18n
    • 使用next-intl的i18n
    在你的方案中使用Intlayer
    • 自动化 next-i18next
    • 自动化 react-i18next
    • 自动化 next-intl
    • 自动化 react-intl
    • 自动化 vue-i18n
    比较
    • next-i18next vs next-intl vs Intlayer
    • react-i18next vs react-intl vs Intlayer
    文档
    1. Blog
    2. Intlayer with i18next
    Creation:2024-12-24Last update:2025-11-01
    Watch the video tutorial

    This page has a video tutorial available.

    将此文档参考到您的 AI 助手
    ChatGPT
    Claude
    DeepSeek
    Google AI mode
    Gemini
    Perplexity
    Mistral
    Grok

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

    版本历史

    1. 添加 loadJSON 插件
      v7.0.62025/11/1
    2. 更改为 syncJSON 插件
      v7.0.02025/10/29

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

    查看英文原文的最新版本
    Edit this doc

    If you have an idea for improving this documentation, please feel free to contribute by submitting a pull request on GitHub.

    GitHub link to the documentation
    Copy

    Copy doc Markdown to clipboard

    如何使用 Intlayer 自动化您的 i18next JSON 翻译

    www.youtube.com

    什么是 Intlayer?

    Intlayer 是一个创新的开源国际化库,旨在解决传统 i18n 解决方案的不足。它为 JavaScript 应用程序中的内容管理提供了一种现代化的方法。

    请参阅我们博客文章中的具体对比:next-i18next vs. next-intl vs. Intlayer。

    为什么将 Intlayer 与 i18next 结合使用?

    虽然 Intlayer 提供了一个出色的独立 i18n 解决方案(请参阅我们的Next.js 集成指南),但您可能出于以下几个原因想将其与 i18next 结合使用:

    1. 现有代码库:您已经有一个成熟的 i18next 实现,并希望逐步迁移到 Intlayer 以获得更好的开发者体验。
    2. 遗留需求:您的项目需要兼容现有的 i18next 插件或工作流程。
    3. 团队熟悉度:您的团队熟悉 i18next,但希望获得更好的内容管理。
    4. 使用 Intlayer 功能:您希望使用 Intlayer 的内容声明、翻译键管理、翻译状态等功能。

    为此,Intlayer 可以作为 i18next 的适配器实现,帮助您在 CLI 或 CI/CD 流水线中自动化 JSON 翻译、测试翻译等。

    本指南将向您展示如何利用 Intlayer 优越的内容声明系统,同时保持与 i18next 的兼容性。

    目录

    使用 i18next 设置 Intlayer 的分步指南

    第一步:安装依赖

    安装必要的软件包:

    bash
    复制代码

    复制代码到剪贴板

    npm install intlayer @intlayer/sync-json-plugin --save-devnpx intlayer init

    软件包说明:

    • intlayer:国际化管理、内容声明和构建的核心库
    • @intlayer/sync-json-plugin:用于将 Intlayer 内容声明导出为兼容 i18next 的 JSON 格式的插件

    第2步:实现 Intlayer 插件以封装 JSON

    创建一个 Intlayer 配置文件以定义支持的语言环境:

    如果您还想导出用于 i18next 的 JSON 字典,请添加 syncJSON 插件:

    intlayer.config.ts
    复制代码

    复制代码到剪贴板

    import { Locales, type IntlayerConfig } from "intlayer";import { syncJSON } from "@intlayer/sync-json-plugin";const config: IntlayerConfig = {  internationalization: {    locales: [Locales.ENGLISH, Locales.FRENCH, Locales.SPANISH],    defaultLocale: Locales.ENGLISH,  },  plugins: [    syncJSON({      source: ({ key, locale }) => `./locales/${locale}/${key}.json`,    }),  ],};export default config;

    syncJSON 插件会自动包装 JSON。它会读取和写入 JSON 文件,而不会改变内容结构。

    如果你想让 JSON 与 intlayer 内容声明文件(.content 文件)共存,Intlayer 会按以下方式处理:

    1. 加载 JSON 和内容声明文件,并将它们转换为 intlayer 字典。
    2. 如果 JSON 和内容声明文件之间存在冲突,Intlayer 会合并所有字典。具体合并方式取决于插件的优先级以及内容声明文件的优先级(所有这些都是可配置的)。

    如果通过 CLI 翻译 JSON 或使用 CMS 进行更改,Intlayer 会使用新的翻译更新 JSON 文件。

    要查看更多关于 syncJSON 插件的详细信息,请参阅 syncJSON 插件文档。

    (可选)步骤 3:实现按组件的 JSON 翻译

    默认情况下,Intlayer 会加载、合并并同步 JSON 文件和内容声明文件。更多详情请参阅 内容声明文档。但如果你愿意,也可以使用 Intlayer 插件,在代码库中的任意位置实现按组件管理本地化的 JSON。

    为此,你可以使用 loadJSON 插件。

    intlayer.config.ts
    复制代码

    复制代码到剪贴板

    import { Locales, type IntlayerConfig } from "intlayer";import { loadJSON, syncJSON } from "@intlayer/sync-json-plugin";const config: IntlayerConfig = {  internationalization: {    locales: [Locales.ENGLISH, Locales.FRENCH, Locales.SPANISH],    defaultLocale: Locales.ENGLISH,  },  // 保持当前的 JSON 文件与 Intlayer 字典同步  plugins: [    /**     * 将加载 src 目录中所有匹配模式 {key}.i18n.json 的 JSON 文件     */    loadJSON({      source: ({ key }) => `./src/**/${key}.i18n.json`,      locale: Locales.ENGLISH,      priority: 1, // 确保这些 JSON 文件优先于 `./locales/en/${key}.json` 中的文件    }),    /**     * 将加载并将输出和翻译写回到 locales 目录中的 JSON 文件     */    syncJSON({      source: ({ key, locale }) => `./locales/${locale}/${key}.json`,      priority: 0,    }),  ],};export default config;

    这将加载 src 目录中所有匹配 {key}.i18n.json 模式的 JSON 文件,并将它们作为 Intlayer 字典加载。


    Git 配置

    建议忽略自动生成的 Intlayer 文件:

    .gitignore
    复制代码

    复制代码到剪贴板

    # 忽略 Intlayer 生成的文件.intlayer

    这些文件可以在构建过程中重新生成,无需提交到版本控制。

    VS Code 扩展

    为了提升开发者体验,建议安装官方 Intlayer VS Code 扩展:

    从 VS Code 市场安装

    什么是国际化?
    Alt+→

    在此页面

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

      npm install intlayer @intlayer/sync-json-plugin --save-devnpx intlayer init
      import { Locales, type IntlayerConfig } from "intlayer";import { syncJSON } from "@intlayer/sync-json-plugin";const config: IntlayerConfig = {  internationalization: {    locales: [Locales.ENGLISH, Locales.FRENCH, Locales.SPANISH],    defaultLocale: Locales.ENGLISH,  },  plugins: [    syncJSON({      source: ({ key, locale }) => `./locales/${locale}/${key}.json`,    }),  ],};export default config;
      import { Locales, type IntlayerConfig } from "intlayer";import { loadJSON, syncJSON } from "@intlayer/sync-json-plugin";const config: IntlayerConfig = {  internationalization: {    locales: [Locales.ENGLISH, Locales.FRENCH, Locales.SPANISH],    defaultLocale: Locales.ENGLISH,  },  // 保持当前的 JSON 文件与 Intlayer 字典同步  plugins: [    /**     * 将加载 src 目录中所有匹配模式 {key}.i18n.json 的 JSON 文件     */    loadJSON({      source: ({ key }) => `./src/**/${key}.i18n.json`,      locale: Locales.ENGLISH,      priority: 1, // 确保这些 JSON 文件优先于 `./locales/en/${key}.json` 中的文件    }),    /**     * 将加载并将输出和翻译写回到 locales 目录中的 JSON 文件     */    syncJSON({      source: ({ key, locale }) => `./locales/${locale}/${key}.json`,      priority: 0,    }),  ],};export default config;
      # 忽略 Intlayer 生成的文件.intlayer