i18next-vue 和 vue-i18n 都是用于在 Vue 应用中实现国际化的主流 npm 包,但它们的设计理念和适用场景有所不同。vue-i18n 是 Vue 官方推荐的国际化解决方案,专为 Vue 生态设计,提供简洁的 API 和深度集成。i18next-vue 则是通用国际化框架 i18next 的 Vue 适配器,继承了 i18next 强大的插件生态和高级功能(如远程资源加载、复杂复数规则),适合需要跨框架一致性或企业级多语言管理的场景。
在构建多语言 Vue 应用时,i18next-vue 和 vue-i18n 是两个主流选择。它们都提供翻译、格式化和语言切换能力,但底层架构、集成方式和生态支持存在显著差异。本文从工程实践角度,深入比较两者的核心机制与适用场景。
vue-i18n 是专为 Vue 设计的国际化库,由 Vue 官方团队维护,深度集成 Vue 响应式系统。
$t() 等全局方法直接在模板中调用// vue-i18n: 创建实例
import { createI18n } from 'vue-i18n';
const i18n = createI18n({
locale: 'zh-CN',
messages: {
'zh-CN': { hello: '你好' },
'en-US': { hello: 'Hello' }
}
});
// 在组件中使用
<template>
<p>{{ $t('hello') }}</p>
</template>
i18next-vue 是通用国际化框架 i18next 的 Vue 适配器。
i18next 核心库,通过插件提供 Vue 集成i18next 实例// i18next-vue: 初始化
import i18next from 'i18next';
import I18NextVue from 'i18next-vue';
i18next.init({
lng: 'zh-CN',
resources: {
'zh-CN': { translation: { hello: '你好' } },
'en-US': { translation: { hello: 'Hello' } }
}
});
// 在组件中使用
<template>
<p>{{ $t('hello') }}</p>
</template>
vue-i18n 利用 Vue 响应式系统自动更新界面。
i18n.global.locale 会触发所有 $t() 调用的重新计算// vue-i18n: 切换语言
const changeLocale = (locale) => {
i18n.global.locale = locale; // 所有文本自动刷新
};
i18next-vue 依赖 i18next 的事件系统。
i18next.on('languageChanged', ...) 监听变化reactive 包装当前语言,确保模板响应式// i18next-vue: 切换语言
const changeLanguage = (lng) => {
i18next.changeLanguage(lng); // 触发响应式更新
};
vue-i18n 推荐静态导入或动态 import() 加载资源。
// vue-i18n: 动态加载
const loadLocaleMessages = async (locale) => {
const messages = await import(`./locales/${locale}.json`);
i18n.global.setLocaleMessage(locale, messages.default);
};
i18next-vue 天然支持 i18next 的后端插件(如 i18next-http-backend)。
// i18next-vue: 远程加载
import HttpApi from 'i18next-http-backend';
i18next.use(HttpApi).init({
backend: {
loadPath: '/locales/{{lng}}/{{ns}}.json'
}
});
vue-i18n 提供简洁的复数、日期/数字格式化。
// vue-i18n: 复数
messages: {
'en-US': {
apple: 'no apples | one apple | {count} apples'
}
}
<template>
<p>{{ $t('apple', { count: 3 }) }}</p> <!-- "3 apples" -->
</template>
i18next-vue 继承 i18next 的强大插件生态。
i18next-intervalplural-postprocessor)// i18next-vue: 上下文
resources: {
'en-US': {
translation: {
friend_context: 'A friend',
friend_context_male: 'A boyfriend',
friend_context_female: 'A girlfriend'
}
}
}
<template>
<p>{{ $t('friend', { context: user.gender }) }}</p>
</template>
vue-i18n 提供 useI18n() 组合式函数。
{ t, locale, ... } 响应式引用<script setup> 无缝集成// vue-i18n: Composition API
<script setup>
import { useI18n } from 'vue-i18n';
const { t, locale } = useI18n();
</script>
<template>
<p>{{ t('hello') }}</p>
</template>
i18next-vue 通过 $t 全局属性和 useTranslation() 钩子提供支持。
useTranslation() 返回 t 函数和当前语言// i18next-vue: Composition API
<script setup>
import { useTranslation } from 'i18next-vue';
const { t, i18n } = useTranslation();
</script>
<template>
<p>{{ t('hello') }}</p>
</template>
vue-i18n 拥有官方配套工具:
@intlify/vue-i18n-loader:SFC 中直接写 <i18n> 块@intlify/cli:提取和管理翻译文件<!-- vue-i18n: SFC 支持 -->
<i18n>
{
"zh-CN": { "title": "标题" },
"en-US": { "title": "Title" }
}
</i18n>
i18next-vue 依赖 i18next 生态:
i18next-parser:从代码中提取翻译键locize:商业级翻译管理平台| 场景 | 推荐方案 |
|---|---|
| Vue 专属项目,追求简洁集成 | ✅ vue-i18n |
| 需要远程加载、复杂复数/上下文规则 | ✅ i18next-vue |
| 已有 i18next 后端或跨框架需求 | ✅ i18next-vue |
| 重度使用 Vue 3 Composition API | ✅ vue-i18n |
| 企业级多语言管理平台集成 | ✅ i18next-vue(配合 locize) |
vue-i18n 是 Vue 项目的“原生选择”——API 简洁、文档完善、工具链成熟,适合大多数中大型应用。i18next-vue 是“能力优先”的方案——当你需要 i18next 强大的插件体系、后端集成或跨技术栈一致性时,它是更灵活的选择。两者均活跃维护,无弃用风险。选择的关键在于:你的项目是否需要 i18next 生态的高级能力,还是更看重与 Vue 深度集成的开发体验。
选择 vue-i18n 如果你追求与 Vue 深度集成的简洁开发体验,项目不需要复杂的远程资源加载或高级复数规则。它提供开箱即用的 Composition API 支持、官方工具链(如 SFC 的
选择 i18next-vue 如果你需要利用 i18next 成熟的插件生态(如远程加载、Locize 集成)、处理复杂的复数/上下文规则,或项目已使用 i18next 作为跨框架的国际化标准。它适合对国际化有高级定制需求的企业级应用,但需接受额外的配置复杂度。
Internationalization plugin for Vue.js
vue-i18n(.runtime).global(.prod).js:
<script src="..."> in the browser. Exposes the VueI18n global<script src="...">vue-i18n.global.js is the "full" build that includes both the compiler and the runtime so it supports compiling locale messages on the flyvue-i18n.runtime.global.js contains only the runtime and requires locale messages to be pre-compiled during a build step@intlify/shared@intlify/message-compiler@intlify/core*.prod.js files for productionvue-i18n(.runtime).esm-browser(.prod).js:
<script type="module">)vue-i18n(.runtime).esm-bundler.js:
webpack, rollup and parcelprocess.env.NODE_ENV guards (must be replaced by bundler)@intlify/core-base, @intlify/message-compiler)
esm-bundler builds and will in turn import their dependencies (e.g. @intlify/message-compiler imports @intlify/shared)vue-i18n.runtime.esm-bundler.js (default) is runtime only, and requires all locale messages to be pre-compiled. This is the default entry for bundlers (via module field in package.json) because when using a bundler templates are typically pre-compiled (e.g. in *.json files)vue-i18n.esm-bundler.js: includes the runtime compiler. Use this if you are using a bundler but still want locale messages compilation (e.g. templates via inline JavaScript strings)vue-i18n.cjs(.prod).js:
require()target: 'node' and properly externalize vue-i18n, this is the build that will be loadedprocess.env.NODE_ENVvue-i18n(.runtime).node.mjs:
importprocess.env.NODE_ENVvue-i18n(.runtime).mjs
vue-i18n.runtime.node.mjs: is runtime onlyvue-i18n.node.mjs: includes the runtime compilerNOTE: ES Modules will be the future of the Node.js module system. The
vue-i18n.cjs(.prod).jswill be deprecated in the future. We recommend you would usevue-i18n(.runtime).node.mjs. 9.3+
The esm-bundler builds now exposes global feature flags that can be overwritten at compile time:
__VUE_I18N_FULL_INSTALL__ (enable/disable, in addition to vue-i18n APIs, components and directives all fully support installation: true)__VUE_I18N_LEGACY_API__ (enable/disable vue-i18n legacy style APIs support, default: true)__INTLIFY_DROP_MESSAGE_COMPILER__ (enable/disable whether to tree-shake message compiler when we will be bundling)The build will work without configuring these flags, however it is strongly recommended to properly configure them in order to get proper tree shaking in the final bundle. To configure these flags:
define optionNote: the replacement value must be boolean literals and cannot be strings, otherwise the bundler/minifier will not be able to properly evaluate the conditions.