从i18next、next-intl到Intlayer:6大i18n库迁移路线图的完整迁移清单
从i18next、next-intl到Intlayer:6大i18n库迁移路线图的完整迁移清单
Intlayer 是一款面向 JS 应用的「按组件拆分」国际化(i18n)解决方案,主打类型安全、AI 翻译与可视化编辑器。如果你正使用 i18next、next-intl、react-i18next、next-i18next、vue-i18n 或 @nuxtjs/i18n 中的任意一种 i18n 库,本文给出一条完整的 i18n 迁移路线图:借助 Intlayer 官方兼容适配器(compat adapter),以几乎零代码改动完成切换,并附上一份可逐项打勾的迁移清单。
为什么考虑从传统 i18n 库迁移?
传统 i18n 库普遍存在四类痛点,而 Intlayer 在迁移后直接改善这些方面:
- 包体积(Bundle size):传统方案常把整份 JSON 语言包塞进页面,Intlayer 按组件加载字典,官方称可减少最多 50% 的 bundle 与页面体积。
- 可维护性:内容散落在巨大的 JSON 文件中。Intlayer 采用「1 个组件 = 1 个字典文件」的同目录组织方式,整个内容体系由 TypeScript 全量类型保障。
- AI 工作流:内容随组件就近存放,降低了 LLM 所需上下文;配套 CLI、LSP、MCP 与 agent skills,让 AI 辅助翻译更顺滑。
- 性能:巨型 JSON 的运行时加载会带来响应式问题,Intlayer 在构建期完成字典编译,无运行时加载步骤。
核心思路:不需要重写应用。Intlayer 为每个流行 i18n 库提供了 API 完全一致的兼容包(如 @intlayer/i18next),底层翻译全部委托给 Intlayer 引擎,构建插件再把旧 import 自动别名为新包——useTranslation、t()、useTranslations 等调用原样保留。
6 大 i18n 库迁移路线速览
Intlayer 仓库内置了 11 个兼容适配器源码(compat/ 目录),其中 6 条官方迁移路线如下表:
| 原 i18n 库 | 兼容适配器包 | 别名注入插件 | syncJSON 占位符格式 |
官方迁移文档 |
|---|---|---|---|---|
i18next |
@intlayer/i18next |
i18nextVitePlugin() |
i18next |
migration_from_i18next_to_intlayer.md |
react-i18next |
@intlayer/react-i18next |
reactI18nextVitePlugin() |
i18next |
migration_from_react-i18next_to_intlayer.md |
next-i18next |
@intlayer/next-i18next |
createNextI18nPlugin() |
i18next |
migration_from_next-i18next_to_intlayer.md |
next-intl |
@intlayer/next-intl |
createNextIntlPlugin() |
icu |
migration_from_next-intl_to_intlayer.md |
vue-i18n |
@intlayer/vue-i18n |
vueI18nVitePlugin() |
vue-i18n |
migration_from_vue-i18n_to_intlayer.md |
@nuxtjs/i18n |
@intlayer/vue-i18n + nuxt-intlayer |
Nuxt 模块 | vue-i18n |
migration_from_nuxtjs_i18n_to_intlayer.md |
💡 迁移期间可以保留原 i18n 库不卸载:兼容包仅将其作为类型依赖(peer/devDependency),不会引发版本冲突。完整适配器列表见 compat/index.md。
通用三步迁移法(所有路线通用)
第一步:一键安装依赖
npx intlayer init --interactive
该命令自动检测环境并安装所需包,例如 i18next 路线对应:
npm install intlayer @intlayer/i18next @intlayer/sync-json-plugin
React 路线会追加 react-intlayer 与 @intlayer/react-i18next;Nuxt 路线追加 nuxt-intlayer。
第二步:配置 intlayer.config.ts
intlayer init 会生成 intlayer.config.ts 骨架,你需要补两件事:声明语言列表,并用 sync-json-plugin 指回现有 JSON 语言包,让旧数据继续可用:
const config: IntlayerConfig = {
internationalization: {
locales: [Locales.ENGLISH, Locales.FRENCH, Locales.SPANISH],
defaultLocale: Locales.ENGLISH,
},
plugins: [
syncJSON({
format: "i18next", // next-intl 用 "icu",vue-i18n 用 "vue-i18n"
source: ({ locale }) => `./src/locales/${locale}.json`,
location: "src/locales",
}),
],
};
format 决定了旧库占位符({{name}}、{count, plural…})如何被正确解析,是三库路线中最容易配错的一项。
第三步:在构建工具中注入别名插件
以 Vite 为例(Next.js 则在 next.config.ts 中用对应插件包裹配置):
// vite.config.ts
import { reactI18nextVitePlugin } from "@intlayer/react-i18next/plugin";
export default defineConfig({
plugins: [react(), reactI18nextVitePlugin()],
});
插件会同时完成两件事:编译 Intlayer 字典 + 把 react-i18next / i18next 的 import 自动重定向到 @intlayer/*。至此,业务代码一行未改,应用已运行在 Intlayer 之上。
各库专属快速要点
- i18next / react-i18next:删掉
i18next.init()与initReactI18next引导即可,Provider 由插件自动注入。 - next-i18next(Pages Router):
next-i18next.config.js可整体删除,serverSideTranslations、appWithTranslation调用保持不变。 - next-intl:
getRequestConfig的运行时加载可删除(构建期已编译);⚠️ URL 路由相关 API(createNavigation、createMiddleware、Link)需继续从真实next-intl包引入,兼容层不接管路由。 - vue-i18n:
createI18n()与app.use(i18n)注册步骤可删除,$t、useI18n()调用原样保留。 - @nuxtjs/i18n:在
nuxt.config.ts的modules中把@nuxtjs/i18n换成nuxt-intlayer即可,Nuxt 多语言路由不受影响。
迁移后清理清单 ✅
逐项打勾,越删越干净:
| 可删除项 | 原因 |
|---|---|
i18next.init() / createI18n() 等初始化调用 |
Intlayer 构建期完成初始化,无运行时加载 |
i18next.use(backend/ detector) 插件链 |
兼容层不使用 i18next 插件体系 |
next-i18next.config.js、getRequestConfig(src/i18n.ts) |
语言检测、路由与字典加载由 Intlayer 接管 |
I18nextProvider / initReactI18next 手动引导 |
插件自动注入 |
JSON 语言包(locales/*.json) |
仅在仍用 syncJSON 过渡期需要;迁完 .content.ts 后可整体删除 |
同时做两件事收尾:把 .intlayer 加入 .gitignore;在 tsconfig.json 的 include 中加入 ".intlayer/**/*.ts",即可获得翻译键的 IDE 自动补全。
迁移后开启 AI 自动翻译
迁移完成后,用 Intlayer CLI 把翻译维护自动化:
npx intlayer test # 检测缺失翻译(建议加入 CI)
npx intlayer fill # 用 AI 自动补齐缺失翻译
在 intlayer.config.ts 中配置 ai: { apiKey: ... } 后,即可在 CI/CD 中用任意 AI 提供商批量翻译,配合自带的可视化编辑器与多语言 CMS,让非开发人员也能实时维护文案。
迁移效果验证:更小的包、更硬的类型
上图是官方在 Next.js 环境下的 bundle 体积基准(数据来自 benchmark/nextjs.md):next-intl、next-i18next 等方案体积显著更高,而 next-intlayer 处于第一梯队,接近基准应用。
类型安全是另一项「迁移收益」:字典编译后,useTranslations("about")、useTranslation("about") 等调用会针对真实内容做类型检查——t("counter.label") 自动补全,t("does.not.exist") 直接构建报错,键名拼写错误从此在编译期暴露。
小结
Intlayer 的迁移策略可以概括为一句话:先用兼容适配器平滑换引擎,再逐步把内容迁移到 .content.ts 解锁全部能力。整条路线分四段:
npx intlayer init安装对应兼容包(零代码改动跑起来);syncJSON指向旧 JSON,配置别名插件;- 按清理清单删除旧库样板代码;
intlayer test/intlayer fill开启 AI 翻译自动化。
更多参考:完整兼容层设计见 compat/index.md,可运行的迁移示例位于 examples/compat-next-intl/ 与 examples/compat-next-i18next/ 等目录。



