接收有关即将发布的Intlayer的通知
    Creation:2025-01-02Last update:2025-10-29

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

    什么是 Intlayer?

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

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

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

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

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

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

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

    目录

    使用 Intlayer 配合 react-i18next 的逐步指南

    第一步:安装依赖

    安装所需的包:

    npm install intlayer @intlayer/sync-json-plugin

    包描述:

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

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

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

    如果您还想导出用于 react-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 市场安装