Creation:2026-06-13Last update:2026-06-13
将此文档参考到您的 AI 助手ChatGPTClaudeDeepSeekGoogle AI modeGeminiPerplexityMistralGrok
使用您最喜欢的AI助手总结文档,并引用此页面和AI提供商
版本历史
- "Init history"v9.0.02026/6/13
此页面的内容已使用 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 documentationCopy
Copy doc Markdown to clipboard
从 react-i18next 迁移到 Intlayer
有关完整详细的分步教程,请参阅我们完整的 react-i18next 迁移指南。
使用 Intlayer 的兼容适配器,您可以从 react-i18next 迁移,而无需对源代码导入进行任何更改。
操作步骤
要初始化项目,运行:
bash
复制代码
复制代码到剪贴板
npx intlayer init在初始化期间,Intlayer 将安装 @intlayer/react-i18next 并创建 intlayer.config.ts。在您的 bundler(如 Vite)中,应用 Intlayer 插件:
vite.config.ts
复制代码
复制代码到剪贴板
import { defineConfig } from "vite";import react from "@vitejs/plugin-react";import { reactI18nextVitePlugin } from "@intlayer/react-i18next/plugin";export default defineConfig({ plugins: [react(), reactI18nextVitePlugin()],});底层原理
reactI18nextVitePlugin 包装了核心 vite-intlayer 插件,并为 react-i18next 和 i18next 注入解析别名,将它们重定向到 @intlayer/react-i18next 和 @intlayer/i18next。
底层实现:
useTranslation和withTranslation: 重写为使用 Intlayer 的原生 hook,为您的字典键提供自动 TypeScript 补全。无缝支持命名空间(例如t('namespace:key'))。- 复数与上下文: 使用原生
Intl.PluralRules和上下文后缀(key_male)处理 i18next 基于后缀的复数化(key_one、key_other)。 <Trans>组件: 重新实现以支持componentsprop、对象和数组形式,以及编号标签<1>...</1>直接映射到您的 React 节点。i18n实例: 直接从 Intlayer 解析键值,无需获取大型 JSON 文件,从而显著降低 bundle 体积。