在Vue中加入国际化(i18n)中英文功能

1.npm安装方法

npm install vue-i18n --save

2.在src资源文件下创建文件夹i18n,i18n下面创建index.js文件,引入VueI18n和导入语言包(按开发需求可添加多种语言)

具体代码:

复制代码
import Vue from 'vue'
import VueI18n from 'vue-i18n'

Vue.use(VueI18n)
 
// 注册i18n实例并引入语言文件
const i18n = new VueI18n({
  locale: 'zh_cn',
  messages: {
    'zh_cn': require('@/assets/languages/zh_cn.json'),
    'en_us': require('@/assets/languages/en_us.json')
  }
})

export default i18n
复制代码

3.在mian.js文件中:

复制代码
import i18n from './i18n';

new Vue({
  el: '#app',
  i18n,
  router,
  store,
  components: { App },
  template: '<App/>'
})
复制代码

4.接在assets文件下新建languages包:

语言JSON包:

复制代码
//zh_cn.json
{
  "SYS": {
    "confirmButtonText": "确认",
    "cancelButtonText": "取消"
  },
  "user": {
    "title": "个人中心",
    "Exit": "退出系统",
    "Processing": "代办工作",
    "initiated": "我发起的",
    "transferring": "办结工作",
    "phoneService": "电话客服",
    "Accounts": "账号中心",
    "modifyPassword": "修改密码",
    "Language": "切换中英文",
    "about": "关于",
    "confirmExit": "确定退出?",
    "confirmChangeLanguage": "是否切换语言?",
    "ScExit": "退出成功",
    "loadingtext": "退出账号中..."
  }
}
复制代码
复制代码
//en_us.json

{
"SYS": { "confirmButtonText": "Comfirm", "cancelButtonText": "Cancel" }, "user": { "title": "Personal center", "Exit": "Exit system", "Processing": "to-do list", "initiated": "I initiated", "transferring": "transferring work", "phoneService": "Telephone customer service", "Accounts": "Account center", "modifyPassword": "change Password", "Language": "CH/EN", "about": "about", "confirmExit":"Confirm Exit ?", "confirmChangeLanguage":"Whether to switch languages", "ScExit":"exit successfully", "loadingtext":"Exiting..." } }
复制代码

5.组件使用(语言切换)

        <van-cell
          icon="setting-o"
          @click="changeLanguage"
          :title="$t('user.Language')"
          :value="Languagevalue"
          is-link
        />

   展示语言切换弹窗:(注意一定要使用$t())

复制代码
    //选择语言
    changeLanguage() {
      this.$dialog
        .confirm({
          message: this.$t("user.confirmChangeLanguage"),
          confirmButtonText: this.$t("SYS.confirmButtonText"),
          cancelButtonText: this.$t("SYS.cancelButtonText")
        })
        .then(() => {
          this.Language = !this.Language;
          if (this.Language == false) {
            this.CHLanguage("zh_cn");
            this.Languagevalue = "English";
          } else {
            this.CHLanguage("en_us");
            this.Languagevalue = "Chinese";
          }
        });
    },
    // 语言切换
    CHLanguage(lang) {
      lang === "" ? "zh_cn" : lang;
      this.$i18n.locale = lang;
    },
复制代码

 

效果:

 

 

 

posted @   lwming  阅读(4205)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具
点击右上角即可分享
微信分享提示