在main.js同级建i18n文件夹,并里面建i18n.js、langs文件夹,langs文件夹下建en.js、cn.js
目录如下:

.
├── App.vue
├── assets
│   └── logo.png
├── components
│   └── HelloWorld.vue
├── i18n
│   ├── i18n.js
│   └── langs
│   ├── cn.js
│   ├── en.js
│   └── index.js
├── main.js
└── store.js
//i18n.js

import Vue from 'vue'
import VueI18n from 'vue-i18n'
import messages from './langs' Vue.use(VueI18n)
const i18n = new VueI18n({
locale: localStorage.lang || 'cn',
messages
}) export default i18n
//langs/index.js

import en from './en'
import cn from './cn'
export default {
en,
cn
}
//en.js
const en = {
message: {
'hello': 'hello, world',
}
} export default en
//cn.js
const cn = {
message: {
'hello': '你好,世界',
}
} export default cn
//main.js

import Vue from 'vue'
import App from './App'
import store from './store'
import i18n from './i18n/i18n'
Vue.config.productionTip = false window.app = new Vue({
store,
i18n,
render: h => h(App)
}).$mount('#app')

接下来是在页面中使用、切换语言。

//html:
<p>{{$t('message.hello')}}</p> // hello, world
//js切换语言
data() {
return {
lang: 'en'
}
},
methods: {
switchLang() {
this.$i18n.locale = this.lang
}
}

通过改变this.$i18n.locale的值就可以自动切换页面的语言了

接下来是将elementUI国际化,更改的地方不多,代码如下

//i18n.js
import Vue from 'vue'
import locale from 'element-ui/lib/locale'
import VueI18n from 'vue-i18n'
import messages from './langs' Vue.use(VueI18n)
const i18n = new VueI18n({
locale: localStorage.lang || 'cn',
messages
})
locale.i18n((key, value) => i18n.t(key, value)) //重点:为了实现element插件的多语言切换 export default i18n
//en.js

import enLocale from 'element-ui/lib/locale/lang/en'
const en = {
message: {
'hello': 'hello, world',
},
...enLocale
} export default en
//cn.js

import zhLocale from 'element-ui/lib/locale/lang/zh-CN'
const cn = {
message: {
'hello': '你好,世界',
},
...zhLocale
} export default cn

main.js保持不变,现在切换中英文,elementUI内部语言也会改变。

vue+elementUI+vue-i18n 实现国际化的更多相关文章

  1. 基于Vue+ElementUI架构的前端国际化解决方案

    1.项目目录结构 ├── build                      构建相关配置文件 |     |── index.js             webpack的基础配置入口 ├── m ...

  2. vue + element-ui 国际化实现

    1. 安装组件和插件 cnpm i element-ui -S // 安装elementcnpm i vue-i18n -S //安装i18n 2.将国际化资源放在assets目录下 3.在src下新 ...

  3. SpringBoot + Vue + ElementUI 实现后台管理系统模板 -- 后端篇(一): 搭建基本环境、整合 Swagger、MyBatisPlus、JSR303 以及国际化操作

    相关 (1) 相关博文地址: SpringBoot + Vue + ElementUI 实现后台管理系统模板 -- 前端篇(一):搭建基本环境:https://www.cnblogs.com/l-y- ...

  4. vue 项目的I18n国际化之路

    I18n (internationalization ) ---未完善 产品国际化是产品后期维护及推广中重要的一环,通过国际化操作使得产品能更好适应不同语言和地区的需求 国际化重点:1. 语言语言本地 ...

  5. TypeError: Cannot read property '_t' of undefined (VUE + ElementUI + i18n)

    在使用vue的ElementUI库,在多语言时报错: TypeError: Cannot read property '_t' of undefined 错误是在点菜单栏时随机抛出的,F12抓不到,只 ...

  6. 在Vue中使用i18n 国际化遇到 Uncaught TypeError: Cannot assign to read only property 'exports' of object '#<Object>'

    最近用Vue在搭建前端框架,在引用i18n时,运行的时候报错:Uncaught TypeError: Cannot assign to read only property 'exports' of ...

  7. vue + element-ui 制作下拉菜单(可配置路由、可根据路由高亮list、可刷新自动展开定位路由)

    本篇文章分享一篇关于 vue制作可路由切换组件.可刷新根据路由定位导航(自动展开).可根据路由高亮对应导航选项 一.实现的功能如下: 1.可折叠导航面板 2.点击导航路由不同组件           ...

  8. Vue+ElementUI的后台管理框架

    新开发的一个后台管理系统.在框架上,领导要用AdminLTE这套模板.这个其实很简单,把该引入的样式和js文件引入就可以了.这里就不多赘述了.有兴趣的可以参考:https://www.jianshu. ...

  9. vue elementui 切换语言

    1.安装插件:npm install vue-i18n  --save 2.src下新建i18n文件夹, i18n文件夹下创建langs文件夹和i18n.js文件 langs文件夹下创建cn.js; ...

随机推荐

  1. 程序人生丨听说程序员是相当就能当的?BAT大牛当场就不乐意了!

    有一种对软件开发者的偏见是:他们都是无趣的极客,是学校里的数学天才,每天都要在计算机屏幕前花费多个小时去写代码. 没错,开发者确实会在计算机屏幕前花费多个小时去写代码.但是,每天的工作中还有很多比写代 ...

  2. ABC 158 F - Removing Robots dp 单调栈

    LINK:Removing Robots 没想到 自闭. 考虑了一个容斥 发现不合法方案难以计算. 就算可以计算也几乎是n^2的做法. 考虑dp 左边会对右边产生影响 所以考虑先dp右边的再考虑左边的 ...

  3. 【HDU5869】 Different GCD Subarray Query 题解 (线段树维护区间GCD)

    题目大意:求区间$[L,R]$中所有子区间产生的最大公因数的个数. ------------------------- 对于$gcd$,我们知道$gcd(a,b,c)=gcd(gcd(a,b),c)$ ...

  4. vue-cli脚手架的搭建

    1.安装node.js 2.安装cnpm npm install -g cnpm --registry=https://registry.npm.taobao.org 3.安装vue-cli npm ...

  5. Python datetime 转 JSON

    Python datetime 转 JSON Python 中将 datetime 转换为 JSON 类型,在使用 Django 时遇到的问题. 环境: Python2.7 代码: import js ...

  6. C#LeetCode刷题-树状数组

    树状数组篇 # 题名 刷题 通过率 难度 218 天际线问题   32.7% 困难 307 区域和检索 - 数组可修改   42.3% 中等 315 计算右侧小于当前元素的个数   31.9% 困难 ...

  7. golang 中的struct理解

    golang实验代码 package main import("fmt") type Stu struct{ name string age int } func (stu *St ...

  8. 自动化特征工程—Featuretools

    Featuretools是一个可以自动进行特征工程的python库,主要原理是针对多个数据表以及它们之间的关系,通过转换(Transformation)和聚合(Aggregation)操作自动生成新的 ...

  9. Linux Centos 下安装npm 实测可用

    转载地址 https://blog.csdn.net/u012129607/article/details/60966045 1.root 登录linux 2.没有目录就自己创建一个 cd /usr/ ...

  10. PostgreSQL在没有备份情况下误删除Clog恢复

    创建实验表postgres# create table t (n_id int primary key,c_name varchar(300));CREATE TABLEpostgres# inser ...