使用您最喜欢的AI助手总结文档,并引用此页面和AI提供商
版本历史
- 添加 loadJSON 插件v7.0.62025/11/1
- 更改为 syncJSON 插件v7.0.02025/10/29
此页面的内容已使用 AI 翻译。
查看英文原文的最新版本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 documentationCopy doc Markdown to clipboard
如何使用 Intlayer 自动化您的 react-intl JSON 翻译
目录
什么是 Intlayer?
Intlayer 是一个创新的开源国际化库,旨在解决传统 i18n 解决方案的不足。它为 React 应用提供了一种现代化的内容管理方法。
请参阅我们博客文章中的具体对比:react-i18next vs. react-intl vs. Intlayer。
为什么要将 Intlayer 与 react-intl 结合使用?
虽然 Intlayer 提供了一个出色的独立 i18n 解决方案(请参阅我们的React 集成指南),但您可能出于以下几个原因希望将其与 react-intl 结合使用:
- 现有代码库:您已经有一个成熟的 react-intl 实现,并希望逐步迁移到 Intlayer 以获得更好的开发者体验。
- 遗留需求:您的项目需要兼容现有的 react-intl 插件或工作流程。
- 团队熟悉度:您的团队熟悉 react-intl,但希望获得更好的内容管理。
- 使用 Intlayer 功能:您想使用 Intlayer 的内容声明、翻译自动化、翻译测试等功能。
为此,Intlayer 可以作为 react-intl 的适配器实现,帮助您在 CLI 或 CI/CD 流水线中自动化 JSON 翻译、测试翻译等。
本指南将向您展示如何利用 Intlayer 优越的内容声明系统,同时保持与 react-intl 的兼容性。
使用 react-intl 设置 Intlayer 的分步指南
第一步:安装依赖
安装所需的包:
复制代码到剪贴板
npm install intlayer @intlayer/sync-json-plugin --save-devnpx intlayer init包说明:
- intlayer:用于国际化管理、内容声明和构建的核心库 /// @intlayer/sync-json-plugin:用于将 Intlayer 内容声明导出为兼容 react-intl 的 JSON 格式的插件
第 2 步:实现 Intlayer 插件以包装 JSON
创建一个 Intlayer 配置文件以定义您支持的语言环境:
如果您还想导出适用于 react-intl 的 JSON 字典,请添加 syncJSON 插件:
复制代码到剪贴板
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({ format: "icu", source: ({ key, locale }) => `./intl/messages/${locale}/${key}.json`, }), ],};export default config;syncJSON 插件会自动包装 JSON。它会读取和写入 JSON 文件,而不会改变内容结构。
如果你希望让 JSON 与 intlayer 内容声明文件(.content 文件)共存,Intlayer 会按以下方式处理:
- 加载 JSON 和内容声明文件,并将它们转换为 intlayer 字典。
- 如果 JSON 和内容声明文件之间存在冲突,Intlayer 会合并所有字典。具体合并方式取决于插件的优先级以及内容声明文件的优先级(所有优先级均可配置)。
如果通过 CLI 翻译 JSON 或使用 CMS 进行更改,Intlayer 会使用新的翻译更新 JSON 文件。
要查看更多关于 syncJSON 插件的详细信息,请参阅 syncJSON 插件文档。
实现按组件的 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({ format: "icu", source: ({ key, locale }) => `./messages/${locale}/${key}.json`, priority: 0, }), ],};export default config;这将加载
src目录中所有匹配{key}.i18n.json模式的 JSON 文件,并将它们作为 Intlayer 字典加载。
Git 配置
建议忽略自动生成的 Intlayer 文件:
复制代码到剪贴板
# 忽略 Intlayer 生成的文件.intlayer这些文件可以在构建过程中重新生成,无需提交到版本控制。
VS Code 扩展
为了提升开发者体验,请安装官方的 Intlayer VS Code 扩展:
评论
暂无评论。成为第一个分享您想法的人吧。