惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Privacy & Cybersecurity Law Blog
Engineering at Meta
Engineering at Meta
Forbes - Security
Forbes - Security
MongoDB | Blog
MongoDB | Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
A
About on SuperTechFans
量子位
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
雷峰网
雷峰网
腾讯CDC
P
Proofpoint News Feed
S
Schneier on Security
S
Secure Thoughts
V
Visual Studio Blog
Help Net Security
Help Net Security
The Hacker News
The Hacker News
C
Cyber Attacks, Cyber Crime and Cyber Security
P
Privacy International News Feed
SecWiki News
SecWiki News
S
SegmentFault 最新的问题
T
Threatpost
小众软件
小众软件
MyScale Blog
MyScale Blog
F
Fortinet All Blogs
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Proofpoint News Feed
T
Tailwind CSS Blog
I
Intezer
C
CERT Recently Published Vulnerability Notes
U
Unit 42
V
V2EX
Cyberwarzone
Cyberwarzone
Recorded Future
Recorded Future
O
OpenAI News
Project Zero
Project Zero
有赞技术团队
有赞技术团队
Google DeepMind News
Google DeepMind News
Last Week in AI
Last Week in AI
Hugging Face - Blog
Hugging Face - Blog
Know Your Adversary
Know Your Adversary
C
Cybersecurity and Infrastructure Security Agency CISA
Scott Helme
Scott Helme
V2EX - 技术
V2EX - 技术
博客园 - 叶小钗
S
Securelist
A
Arctic Wolf
The Cloudflare Blog
W
WeLiveSecurity
T
Threat Research - Cisco Blogs
博客园 - Franky

博客园 - Mc525

antv3 x6 基本语法-流程图(二) element ui el-form 表单错误 滚动可视区域 vue2 element ui el-table大数据处理无分页解决方案-分片加载 vue2 地图热力图 -下钻省市县 三级及地图资源文件 vue2 组件封装 el-select vue2 组件封装 el-input vue2 组件封装 el-date-picker 日期 vue2 scss sass 基础安装包、安装依赖报错 !!! vue2 封装组件使用 v-mode【el-radio,el-input】 Mac Jenkins 环境部署 vue2 按钮权限(七) vue2 换肤(六) vue2 项目实例 动态路由菜单(四) vue2 项目实例 Mock数据模拟(三) vue2 项目实例 Layout布局(二) vue2 项目实例 项目初始化(一) vue2 项目架构--api.js(七) vue2 项目架构--vue.config.js(七) vue2 项目架构--main.js(六)
vue2 项目实例 国际化(五)
Mc525 · 2025-09-28 · via 博客园 - Mc525

首先安装适合 Vue2 的 vue-i18n 版本(注意:Vue2 需使用 vue-i18n@8.x,Vue3 则用 vue-i18n@9+

npm install vue-i18n@8 --save
# 或
yarn add vue-i18n@8

在项目中创建语言包目录(例如 src/lang),并添加不同语言的配置文件:

export default {
  message: {
    hello: 'Hello',
    welcome: 'Welcome to {name}', // 支持变量
    button: {
      submit: 'Submit',
      cancel: 'Cancel'
    }
  }
}
export default {
  message: {
    hello: '你好',
    welcome: '欢迎来到 {name}',
    button: {
      submit: '提交',
      cancel: '取消'
    }
  }
}

创建 src/lang/index.js 整合配置:

import Vue from 'vue'
import VueI18n from 'vue-i18n'
import en from './en'
import zh from './zh'

// 注册插件
Vue.use(VueI18n)

// 实例化
const i18n = new VueI18n({
  locale: 'zh', // 默认语言
  fallbackLocale: 'en', // 语言切换失败时的备用语言
  messages: {
    en,
    zh
  }
})

export default i18n

在 main.js 中挂载 i18n 实例:

import Vue from 'vue'
import App from './App.vue'
import i18n from './lang' // 引入配置

new Vue({
  el: '#app',
  i18n, // 挂载到实例
  render: h => h(App)
})
  • 基础用法:$t('键名')
  • 带变量:$t('键名', { 变量名: 值 })
<template>
  <div>
    <p>{{ $t('message.hello') }}</p>
    <p>{{ $t('message.welcome', { name: 'Vue' }) }}</p>
    <button>{{ $t('message.button.submit') }}</button>
  </div>
</template>

通过 this.$i18n.t() 调用

export default {
  mounted() {
    console.log(this.$i18n.t('message.hello')) // 输出当前语言的"你好"或"Hello"
  }
}

通过修改 this.$i18n.locale 实现语言切换:

<template>
  <div>
    <button @click="changeLang('zh')">中文</button>
    <button @click="changeLang('en')">English</button>
  </div>
</template>

<script>
export default {
  methods: {
    changeLang(lang) {
      this.$i18n.locale = lang // 切换语言
    }
  }
}
</script>
  • 持久化语言偏好:结合 localStorage 保存用户选择的语言,初始化时读取:

// 在 src/lang/index.js 中
const i18n = new VueI18n({
  locale: localStorage.getItem('lang') || 'zh', // 优先从本地存储读取
  // ...其他配置
})