使用您最喜欢的AI助手总结文档,并引用此页面和AI提供商
版本历史
- "初始化历史记录"v9.0.02026/6/5
此页面的内容已使用 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
从 next-i18next 迁移到 Intlayer
为什么从 next-i18next 迁移到 Intlayer?
与其将大量 JSON 文件加载到页面中,不如只加载必要的内容。Intlayer 可帮助将 bundle 和页面大小减少最多 50%。
限定应用程序内容的范围有利于大规模应用程序的维护。你可以复制或删除单个功能文件夹,而无需费力审查整个内容 codebase。此外,Intlayer 完全类型化,以确保内容的准确性。
Intlayer 也是 i18n 生态系统中开发最活跃的解决方案 — 问题修复迅速,新的框架适配器定期发布,核心 API 根据真实的生产反馈不断改进。
内容共置减少了大型语言模型 (LLM) 所需的上下文。Intlayer 还附带一套工具,例如用于测试缺失翻译的 CLI、LSP、MCP 和 agent skills,使 AI agents 的开发者体验 (DX) 更加顺畅。
在你的 CI/CD pipeline 中使用自动化翻译,使用你选择的 LLM,成本由你的 AI 提供商承担。Intlayer 还提供编译器来自动化内容提取,以及一个 web platform 帮助后台翻译。
将大量 JSON 文件连接到组件可能会导致性能和响应性问题。Intlayer 在构建时优化了你的内容加载。
迁移策略
由于 next-i18next 在底层封装了 react-i18next 和 i18next,迁移到 Intlayer 有两种互补的策略:
兼容适配器(推荐用于现有应用) — 安装
@intlayer/next-i18next、@intlayer/react-i18next和@intlayer/i18next。这些包公开的 API 完全相同,但在底层将所有翻译工作委托给 Intlayer。您可以保持现有的useTranslation、appWithTranslation、serverSideTranslations调用和 Next.js Pages 路由不变 — 唯一的变化是初始化。完全迁移 — 逐步使用原生 Intlayer 钩子(
useIntlayer)替换next-i18nextAPI,并在组件旁边的.content.ts文件中并置内容。
本指南首先介绍策略 1(即插即用的兼容适配器),然后演示可选的完全迁移。
目录
快速迁移
以下步骤是让现有 Next.js Pages Router 应用在 Intlayer 上运行所需的最少要求,无需对页面和组件进行任何代码更改。
安装依赖
安装 Intlayer 核心包和兼容适配器:
bash复制代码复制代码到剪贴板
npx intlayer-cli init --interactive--interactive标志是可选的。如果您是 AI 代理,请使用intlayer-cli init。此命令将检测您的环境并安装所需的包。例如:
bash复制代码复制代码到剪贴板
npm install intlayer next-intlayer react-intlayer @intlayer/next-i18next @intlayer/react-i18next @intlayer/i18next @intlayer/sync-json-plugin在迁移期间,您可以安全地保留
next-i18next、react-i18next和i18next的安装,尽管在别名后您将删除它们。配置 Intlayer
intlayer init命令创建一个启动器intlayer.config.ts。更新它以匹配您现有的语言环境,并将syncJSON插件指向您的next-i18next消息文件(通常在public/locales内):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({ // 匹配 i18next 占位符语法: {{name}} format: "i18next", source: ({ key, locale }) => `./public/locales/${locale}/${key}.json`, location: "public/locales", }), ], }; export default config;source将语言环境和命名空间(key)映射到其 JSON 文件路径。location告诉 Intlayer 监听器监视哪个文件夹以获取更改。format: 'i18next'选项确保正确解析next-i18next的占位符。更新 Next.js 配置
使用来自
@intlayer/next-i18next/plugin的createNextI18nPlugin包装现有的next.config.ts(或.js)。此包装器组合withIntlayer并且 注入next-i18next/react-i18next/i18next→@intlayer/*别名,因此您现有的import { useTranslation } from 'next-i18next'调用在构建时透明地重定向。不需要更改源文件。next.config.ts复制代码复制代码到剪贴板
import type { NextConfig } from "next"; import { createNextI18nPlugin } from "@intlayer/next-i18next/plugin"; // 您可以删除从 next-i18next.config.js 导入的 i18n 配置 // import { i18n } from './next-i18next.config'; const withIntlayer = createNextI18nPlugin(); const nextConfig: NextConfig = { // Intlayer 在底层管理 Next.js i18n 路由, // 所以您不再需要在这里传递 i18n 对象。 }; export default withIntlayer(nextConfig);您不再需要
next-i18next.config.js。 Intlayer 在构建时编译所有字典,无缝处理语言环境检测、路由和字典加载。倾向于使用来自
next-intlayer/server的简单withIntlayer? 它编译您的字典但不添加next-i18next/react-i18next/i18next别名 — 您随后需要手动将导入重命名为@intlayer/*(请参阅第 4 步)。
快速迁移就到这里。您的 Next.js 应用现在在 Intlayer 上运行,同时保持每个 useTranslation、serverSideTranslations 和 appWithTranslation 调用完整。
有类型的翻译键 — 自动。 一旦 Intlayer 编译您的字典,
useTranslation和getFixedT就会针对您的实际内容进行类型化。键在您的 IDE 中自动完成,无效路径会在构建时导致 TypeScript 错误 — 无需额外设置。tsx复制代码复制代码到剪贴板
// Pages Router — 'about' 是一个已注册的字典键const { t } = useTranslation("about");t("counter.label"); // ✓ 自动完成t("does.not.exist"); // ✗ TypeScript 错误// getStaticProps / getServerSideProps (i18next 实例)const tAbout = i18n.getFixedT(null, "about");tAbout("counter.label"); // ✓ 有类型
完整迁移
下面的步骤是可选的,可以逐步完成。它们解锁完整的 Intlayer 功能集:可视化编辑器、CMS、类型化内容文件、AI 驱动的翻译等。
显式导入重命名(可选)
可选Intlayer 插件已经在 bundler 级别处理别名。如果您更希望在源文件中明确依赖关系,可以手动重命名导入:
显示表格的所有内容在弹窗中打开表格以清晰地查看所有数据
Before After import { serverSideTranslations } from 'next-i18next/serverSideTranslations'import { serverSideTranslations } from '@intlayer/next-i18next'import { appWithTranslation } from 'next-i18next'import { appWithTranslation } from '@intlayer/next-i18next'import { useTranslation } from 'next-i18next'import { useTranslation } from '@intlayer/next-i18next'import { useTranslation } from 'react-i18next'import { useTranslation } from '@intlayer/react-i18next'这些是即插即用替代品——无需更改调用签名、参数或返回类型。
启用 AI 驱动的翻译自动化
可选一旦 Intlayer 连接好,使用其 CLI 自动填充缺失的翻译:
bash复制代码复制代码到剪贴板
# 测试缺失的翻译(添加到 CI)npx intlayer test# 使用 AI 填充缺失的翻译npx intlayer fill将 AI 配置添加到
intlayer.config.ts: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({ format: "i18next", source: ({ key, locale }) => `./public/locales/${locale}/${key}.json`, location: "public/locales", }), ], ai: { apiKey: process.env.OPENAI_API_KEY, // provider: "openai", // 默认值 // model: "gpt-4o-mini", // 默认值 }, }; export default config;详见 Intlayer CLI 文档了解所有可用选项。
迁移后可以删除的内容
一旦 compat adapter 就位,以下 next-i18next 样板代码可以删除:
在弹窗中打开表格以清晰地查看所有数据
| 文件 / 模式 | 不再需要的原因 |
|---|---|
next-i18next.config.js | Intlayer 根据 intlayer.config.ts 在内部处理路由、字典加载和默认语言环境。 |
next-i18next from package.json | 完全被 @intlayer/next-i18next 和别名替代。 |
JSON language bundles (public/locales/*.json) | JSON bundles 仅在你仍然使用 syncJSON 插件时才需要。迁移到 .content.ts 文件后,你可以删除 JSON 文件夹。 |
当你准备进一步操作时,Intlayer 会自动发现整个 codebase 中的所有 .content.ts 和 .content.json 文件(默认情况下,在 ./src 内的任何位置)。你可以将 my-component.content.ts 文件放在 MyComponent.tsx 旁边,Intlayer 将在构建时自动识别它,无需任何额外配置 — 无需导入、无需注册、无需集中的索引文件。这使得将翻译与页面和组件并置完全无障碍。
配置 TypeScript
Intlayer 使用模块扩充来为你的翻译键提供完整的 TypeScript intellisense。确保你的 tsconfig.json 包含自动生成的类型:
复制代码到剪贴板
{ // ... 你现有的 TypeScript 配置 "include": [ // ... 你现有的 TypeScript 配置 ".intlayer/**/*.ts", // 包含自动生成的类型 ],}Git 配置
将 Intlayer 生成的目录添加到您的 .gitignore:
复制代码到剪贴板
# Ignore the files generated by Intlayer.intlayer进一步学习
- Visual Editor — 在浏览器中可视化管理翻译:Intlayer Visual Editor
- CMS — 外部化和远程管理内容:Intlayer CMS
- VS Code Extension — 获取自动完成和实时翻译错误检测:Intlayer VS Code Extension
- CLI Reference — CLI 命令完整列表:Intlayer CLI
- Intlayer with Next.js (Pages Router) — Next.js 完整设置指南:intlayerwithnextjspagerouter.md