Vue+ElementUI多语言设置

介绍:Vue I18n 是 Vue.js 的国际化插件,可参考官网https://kazupon.github.io/vue-i18n/zh/

项目中使用到element组件,官网上有涉及到组件的国际化内容,同时兼容vue-i18n。可参考官网Element - The world's most popular Vue UI framework

以下为多语言设置基础步骤,详细代码见下方(可复制),本文以切中文,英文,日文三中语言的切换

步骤一、安装vue-i18n(vue2x的版本,一定要安装8.9.0以下的版本)

npm install vue-i18n@8.9.0 --save

步骤二、在src中,新建lang文件

index.js(用于配置i18n)

en_us.js(英文文件)

zh_cn.js(中文文件)

ja.js(日文文件)

1.index.js

2.zh_cn.js(中文文件)

3.en_us.js(英文文件)

4. ja.js(日文文件)

步骤三、在main.js文件中引入

步骤四、数据渲染模板的三种语法

// 语法1
<div>{{$t('message.zh')}}</div>
// 语法2
<input :placeholder="$t('message.zh')"></input>
// 语法3
data:{  msg:this.$t('message.zh')}

参考代码

// lang文件夹下index.js
import Vue from 'vue'
import VueI18n from 'vue-i18n'
import enLocale from 'element-ui/lib/locale/lang/en'
import zhLocale from 'element-ui/lib/locale/lang/zh-CN'
import jaLocale from 'element-ui/lib/locale/lang/ja'
import en from './en_us' //英文
import zh from './zh_cn' //中文
import ja from './ja'  //日文
Vue.use(VueI18n)

const messages = {
    zh: {
        language: '简体中文',
        ...zh,
        ...zhLocale
    },
    en: {
        language: 'English',
        ...en,
        ...enLocale
    },
    ja: {
        language: 'ja',
        ...ja,
        ...jaLocale
    }
}
const i18n = new VueI18n({
    locale: localStorage.getItem('locale') || 'zh', // 语言类型存储到localstorage里
    messages
})
// 导出messages 给切换语言的时候用
export { i18n, messages }
// zh_cn.js中文文件
export default {
    test: 'Test',
    concent: {
        title: '天津天堰科技股份有限公司(以下简称:天堰公司)在此特别提醒用户认真阅读本《软件许可及服务协议》 (下称《协议》)中各条款,包括免除或者限制天堰公司责任的免责条款及对用户的权利限制条款。请用户审阅并接受或不接受本《协议》(未成年人应在法定监护人陪同下审阅)。除非用户接受本《协议》条款,否则用户无权下载、安装或使用本软件及其相关服务。用户的安装使用行为将视为对本《协议》全部条款的接受,并同意接受本《协议》各项条款的约束。\n' +
            '本《协议》是用户与天堰公司及其运营合作单位("合作单位")之间关于用户下载、安装、使用天堰公司软件所订立(以下简称"软件")。本《协议》描述天堰公司与用户之间关于"软件"许可使用及相关方面的权利义务。"用户"是指通过天堰公司提供的获取软件授权的途径获得软件授权许可和/或软件产品的自然人或组织。\n' +
            ' 本协议可视为《天堰公司网站服务条款》《免责声明》的补充条款,与其构成统一整体,您对本协议的接受即受全部条款的约束,包括接受天堰公司对服务条款随时所做的任何修改,这些条款可由天堰公司随时更新,且毋须另行通知。您在使用本软件的同时即是对所有条款的接受和遵守。'
    },
    xuanxiang: [
        { value: '选项1',label: '选项1'},
        { value: '选项2',label: '选项2'},
        { value: '选项3',label: '选项3'}
    ],
    table:{
      date:'日期',
      name:'姓名',
      address:'地址',
    },
    tableData:[{
        date: '2016-05-02',
        name: '王小虎',
        address: '上海市普陀区金沙江路'
    }, {
        date: '2016-05-04',
        name: '王小虎',
        address: '上海市普陀区金沙江路'
    }, {
        date: '2016-05-01',
        name: '王小虎',
        address: '上海市普陀区金沙江路'
    }, {
        date: '2016-05-03',
        name: '王小虎',
        address: '上海市普陀区金沙江路'
    }]
}
// en_us.js英文文件
export default {
    test: '测试',
    concent: {
        title: 'Tianjin Tianyan Technology Co., Ltd. (hereinafter referred to as Tianyan company) hereby specially reminds users to carefully read the terms of the software license and service agreement (hereinafter referred to as the agreement), including the exemption clauses for exempting or limiting the liability of Tianyan company and the rights restriction clauses for users. Users are invited to review and accept or not accept this Agreement (minors should be accompanied by legal guardians for review). Unless the user accepts the terms of this agreement, the user has no right to download, install or use the software and its related services. The user\'s installation and use will be deemed as acceptance of all the terms of this Agreement and agree to be bound by all the terms of this agreement.\n' +
            '\n' +
            'This agreement is concluded between the user and Tianyan company and its operation cooperation unit ("cooperation unit") regarding the downloading, installation and use of Tianyan company\'s software by the user (hereinafter referred to as "software"). This agreement describes the rights and obligations between Tianyan company and the user regarding the licensed use of the software and related aspects. "User" refers to the natural person or organization that obtains the software license and / or software products through the way of obtaining software license provided by Tianyan company.\n' +
            '\n' +
            'This agreement can be regarded as a supplement to the terms of service of Tianyan company\'s website and the disclaimer, which form a unified whole. Your acceptance of this agreement is bound by all terms, including any modification made by Tianyan company to the terms of service from time to time. These terms can be updated by Tianyan company from time to time without further notice. When you use this software, you accept and abide by all terms.'
    },
    xuanxiang: [
        {value: 'option1',label: 'option1'},
        {value: 'option2',label: 'option2'},
        {value: 'option3',label: 'option3'}
    ],
    table:{
        date:'date',
        name:'name',
        address:'address',
    },
    tableData:[{
        date: '2016-05-02',
        name: 'wangxiaohu',
        address: 'Jinshajiang Road, Putuo District, Shanghai'
    }, {
        date: '2016-05-04',
        name: 'wangxiaohu',
        address: 'Jinshajiang Road, Putuo District, Shanghai'
    }, {
        date: '2016-05-01',
        name: 'wangxiaohu',
        address: 'Jinshajiang Road, Putuo District, Shanghai'
    }, {
        date: '2016-05-03',
        name: 'wangxiaohu',
        address: 'Jinshajiang Road, Putuo District, Shanghai'
    }]
}
// ja.js日文文件
export default {
    test: 'Test',
    concent: {
        title: '天津天堰科技株式会社(以下:天堰公司)はここで、天堰公司の責任を免除または制限する免責条項とユーザーに対する権利制限条項を含む本「ソフトウェア許可およびサービス協定」(以下「協定」)の各条項を真剣に読むようユーザーに注意した。ユーザーに本契約書をレビューして承認してもらうか、受け取らないようにしてください(未成年者は法定保護者に付き添われて確認してください)。ユーザーは、本契約の条項を受け入れない限り、ソフトウェアとその関連サービスをダウンロード、インストール、または使用する権利はありません。ユーザーのインストール使用行為は、本契約のすべての条項の受け入れとみなされ、本契約の各条項の制約を受け入れることに同意します。\n' +

            '本契約書は、ユーザーと天堰会社及びその運営協力会社(「協力会社」)との間でユーザーが天堰会社のソフトウェアをダウンロード、インストール、使用することについて締結したもの(以下「ソフトウェア」と略称する)である。本契約書は、天堰会社とユーザーの間の「ソフトウェア」の使用許可及び関連する権利義務について説明する。「ユーザー」とは、天堰会社が提供するソフトウェアライセンス取得のルートを通じてソフトウェアライセンスおよび/またはソフトウェア製品を取得する自然人または組織を指す。\n' +

            '本契約は、天堰公司ウェブサイトサービス約款の「免責声明」の補足条項とみなされ、統一された全体を構成するものであり、天堰公司がサービス約款に対していつでも行っている変更を受け入れることを含む、本契約の受け入れは、天堰公司が随時更新することができ、別途通知する必要はありません。お客様は、本ソフトウェアを使用している間に、すべての条項を受け入れ、遵守しています'
    },
    xuanxiang: [
        {value: 'オプション1', label: 'オプション1'},
        { value: 'オプション2',label: 'オプション2'},
        {value: 'オプション3',label: 'オプション3'}
    ],
    table:{
        date:'日付',
        name:'名前',
        address:'アドレス',
    },
    tableData:[{
        date: '2016-05-02',
        name: '王小虎',
        address: '上海市普陀区金沙江路'
    }, {
        date: '2016-05-04',
        name: '王小虎',
        address: '上海市普陀区金沙江路'
    }, {
        date: '2016-05-01',
        name: '王小虎',
        address: '上海市普陀区金沙江路'
    }, {
        date: '2016-05-03',
        name: '王小虎',
        address: '上海市普陀区金沙江路'
    }]
}
// main.js引入
import Vue from 'vue'
import App from './App.vue'
import { i18n } from './lang'
import 'element-ui/lib/theme-chalk/index.css'
import Element from 'element-ui'
import router from './router'
Vue.config.productionTip = false
Vue.prototype.Base64 = require('js-base64').Base64
Vue.use(Element, {
  i18n: (key, value) => i18n.t(key, value), // 这里会根据选的语言切换Element-ui的语言
})
new Vue({
  el: '#app',
  router,
  i18n,
  render: h => h(App),
}).$mount('#app')
// 页面引入,查看含有$t的写法
<template>
  <div class="box">
<!--选择切换语言-->
    <el-select v-model="value" placeholder="请选择" @change="select">
      <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value">
      </el-option>
    </el-select>
    <div>{{$t('concent.title')}}</div>
    <el-date-picker
        type="date"
        placeholder="选择日期">
    </el-date-picker>
    <el-select placeholder="请选择" v-model="selectvue">
      <el-option
          v-for="item in $t('xuanxiang')"
          :key="item.value"
          :label="item.label"
          :value="item.value">
      </el-option>
    </el-select>
    <el-table
        :data="$t('tableData')"
        style="width: 100%">
      <el-table-column
          prop="date"
          :label="$t('table.date')"
          width="180">
      </el-table-column>
      <el-table-column
          prop="name"
          :label="$t('table.name')"
          width="180">
      </el-table-column>
      <el-table-column
          prop="address"
          :label="$t('table.address')">
      </el-table-column>
    </el-table>
  </div>
</template>

如果想使用下拉选择框切换语言,要将设置的语言存储在localstorage中,关闭页面再进入会保留上次设置的语言。设置@change="select"方法如下

methods:{
    select(val){
      console.log(val)
      window.localStorage.setItem('locale',val)
      this.$i18n.locale = val
      window.location.reload()
    }
  }

实现结果如图

 

 通过此方法组件内的内容也是可以自动实现多语言的切换

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Vue ElementUI是一个基于Vue.js前端UI框架,提供丰富的组件和模板,用于快速构建Web应用程序的用户界面。 在使用Vue ElementUI前端模板时,我们首先需要安装Vue.jsElementUI的依赖包,并在项目中引入相应的样式和组件。使用Vue Cli搭建项目可以方便地集成ElementUI,并使用Vue Router进行路由管理。 Vue ElementUI提供了许多常用的UI组件,如按钮、表单、表格、弹窗等,这些组件都经过封装和优化,可以在项目中快速使用。同时,ElementUI还提供了丰富的主题样式,可以根据项目需求进行自定义,并且支持国际化的多语言功能。 在使用Vue ElementUI前端模板时,我们可以方便地根据需求进行快速开发,减少重复工作。可以通过组件的配置参数和事件绑定来实现与后端的数据交互和动态的页面效果。同时,ElementUI还提供了一些常用的工具类和函数,帮助开发者更方便地进行业务开发。 总之,Vue ElementUI前端模板是一种快速构建Web应用程序界面的工具,它提供了丰富的组件和样式,帮助我们快速开发高效的前端页面。无论是初学者还是有经验的开发者,都能够通过学习和使用Vue ElementUI前端模板来提高开发效率,并且能够满足不同项目的需求。 ### 回答2: Vue element-ui前端模板是一个基于Vue.js的开源UI库,它提供了一系列的可复用的组件,用于快速构建现代化的Web应用程序。 使用Vue element-ui前端模板可以帮助开发者快速搭建出具有一致性和可扩展性的界面。它提供了丰富的组件库,包括但不限于按钮、表格、表单、弹窗等,这些组件提供了丰富的样式和交互效果,可以满足各种需求。 除了基础的组件,Vue element-ui还提供了丰富的工具和辅助类,帮助开发者更方便地实现页面布局和功能实现。例如,它提供了拖拽排序、图片上传、日期选择等功能,极大地提升了开发效率。 另外,Vue element-ui还支持自定义主题,开发者可以根据自己的需求来修改组件的样式,满足项目的视觉要求。 在使用Vue element-ui前端模板时,我们只需要在Vue项目中引入相应的模块,然后按照文档给出的示例代码编写即可。它的文档非常详细,并且提供了示例代码和在线演示,方便开发者学习和使用。 总之,Vue element-ui前端模板是一个功能强大、易用且高度可定制的开源UI库,适用于构建各种类型的Web应用程序。无论是初学者还是有经验的开发者,都可以通过使用它来提高开发效率,实现优质的用户界面。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值