vue-i18n vs i18next-vue
Vue 应用国际化方案选型
vue-i18ni18next-vue类似的npm包:

Vue 应用国际化方案选型

i18next-vue 和 vue-i18n 都是用于在 Vue 应用中实现国际化的主流 npm 包,但它们的设计理念和适用场景有所不同。vue-i18n 是 Vue 官方推荐的国际化解决方案,专为 Vue 生态设计,提供简洁的 API 和深度集成。i18next-vue 则是通用国际化框架 i18next 的 Vue 适配器,继承了 i18next 强大的插件生态和高级功能(如远程资源加载、复杂复数规则),适合需要跨框架一致性或企业级多语言管理的场景。

npm下载趋势

3 年

GitHub Stars 排名

统计详情

npm包名称
下载量
Stars
大小
Issues
发布时间
License
vue-i18n4,996,2242,7111.7 MB802 天前MIT
i18next-vue112,58410111.8 kB01 年前MIT

Vue 国际化方案深度对比:i18next-vue 与 vue-i18n

在构建多语言 Vue 应用时,i18next-vue 和 vue-i18n 是两个主流选择。它们都提供翻译、格式化和语言切换能力,但底层架构、集成方式和生态支持存在显著差异。本文从工程实践角度,深入比较两者的核心机制与适用场景。

🌐 核心架构:适配器 vs 原生实现

vue-i18n 是专为 Vue 设计的国际化库,由 Vue 官方团队维护,深度集成 Vue 响应式系统。

  • 使用 $t() 等全局方法直接在模板中调用
  • 语言资源以 JavaScript 对象形式加载
  • 内置对 Vue 3 Composition API 的完整支持
// 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', ...) 监听变化
  • 插件内部使用 Vue 的 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 提供简洁的复数、日期/数字格式化。

  • 复数规则基于 CLDR 标准
  • 格式化器需预先注册
// 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 的强大插件生态。

  • 支持上下文(context)、复数(plural)、嵌套(nesting)
  • 可通过插件扩展(如 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>

⚙️ Composition API 支持

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:提取和管理翻译文件
  • VS Code 插件提供语法高亮和跳转
<!-- vue-i18n: SFC 支持 -->
<i18n>
{
  "zh-CN": { "title": "标题" },
  "en-US": { "title": "Title" }
}
</i18n>

i18next-vue 依赖 i18next 生态:

  • i18next-parser:从代码中提取翻译键
  • locize:商业级翻译管理平台
  • 社区插件丰富(如 React Native、Angular 适配器)

📊 何时选择哪个?

场景推荐方案
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 vs i18next-vue

  • vue-i18n:

    选择 vue-i18n 如果你追求与 Vue 深度集成的简洁开发体验,项目不需要复杂的远程资源加载或高级复数规则。它提供开箱即用的 Composition API 支持、官方工具链(如 SFC 的 块)和完善的文档,是大多数 Vue 项目的首选方案。

  • i18next-vue:

    选择 i18next-vue 如果你需要利用 i18next 成熟的插件生态(如远程加载、Locize 集成)、处理复杂的复数/上下文规则,或项目已使用 i18next 作为跨框架的国际化标准。它适合对国际化有高级定制需求的企业级应用,但需接受额外的配置复杂度。

vue-i18n的README

vue-i18n

Internationalization plugin for Vue.js

Which dist file to use?

From CDN or without a Bundler

  • vue-i18n(.runtime).global(.prod).js:

    • For direct use via <script src="..."> in the browser. Exposes the VueI18n global
    • Note that global builds are not UMD builds. They are built as IIFEs and is only meant for direct use via <script src="...">
    • In-browser locale messages compilation:
      • vue-i18n.global.js is the "full" build that includes both the compiler and the runtime so it supports compiling locale messages on the fly
      • vue-i18n.runtime.global.js contains only the runtime and requires locale messages to be pre-compiled during a build step
    • Inlines internal the bellow packages - i.e. it’s a single file with no dependencies on other files. This means you must import everything from this file and this file only to ensure you are getting the same instance of code
      • @intlify/shared
      • @intlify/message-compiler
      • @intlify/core
    • Contains hard-coded prod/dev branches, and the prod build is pre-minified. Use the *.prod.js files for production
  • vue-i18n(.runtime).esm-browser(.prod).js:

    • For usage via native ES modules imports (in browser via <script type="module">)
    • Shares the same runtime compilation, dependency inlining and hard-coded prod/dev behavior with the global build

With a Bundler

  • vue-i18n(.runtime).esm-bundler.js:
    • For use with bundlers like webpack, rollup and parcel
    • Leaves prod/dev branches with process.env.NODE_ENV guards (must be replaced by bundler)
    • Does not ship minified builds (to be done together with the rest of the code after bundling)
    • Imports dependencies (e.g. @intlify/core-base, @intlify/message-compiler)
      • Imported dependencies are also esm-bundler builds and will in turn import their dependencies (e.g. @intlify/message-compiler imports @intlify/shared)
      • This means you can install/import these deps individually without ending up with different instances of these dependencies, but you must make sure they all resolve to the same version
    • In-browser locale messages compilation:
      • 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)

For Node.js (Server-Side)

  • vue-i18n.cjs(.prod).js:

    • For CommonJS usage in Node.js
    • For use in Node.js via require()
    • If you bundle your app with webpack with target: 'node' and properly externalize vue-i18n, this is the build that will be loaded
    • The dev/prod files are pre-built, but the appropriate file is automatically required based on process.env.NODE_ENV
  • vue-i18n(.runtime).node.mjs:

    • For ES Moudles usage in Node.js
    • For use in Node.js via import
    • The dev/prod files are pre-built, but the appropriate file is automatically required based on process.env.NODE_ENV
    • This module is proxy module of vue-i18n(.runtime).mjs
      • vue-i18n.runtime.node.mjs: is runtime only
      • vue-i18n.node.mjs: includes the runtime compiler

NOTE: ES Modules will be the future of the Node.js module system. The vue-i18n.cjs(.prod).js will be deprecated in the future. We recommend you would use vue-i18n(.runtime).node.mjs. 9.3+

For Bundler feature flags

Build Feature Flags

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:

Note: the replacement value must be boolean literals and cannot be strings, otherwise the bundler/minifier will not be able to properly evaluate the conditions.

©️ License

MIT