vue 配置 i18n 多语言
1、安装 i18n 模块
vue-i18n与vue存在版本兼容性问题,vue2.x需要使用 8.9.0以下版本的vue-i18n,我们这里用的vue 2.6.10,配合vue-i18n 8.2.1版本使用
安装vue-i18n
npm install vue-i18n@8.2.1 --save
2、在src下创建lang目录及对应语言目录
3、配置src/lang/index.js
import Vue from 'vue'
import VueI18n from 'vue-i18n'
import elementEnLocale from 'element-ui/lib/locale/lang/en'
import elementZhLocale from 'element-ui/lib/locale/lang/zh-CN'
import elementJaLocale from 'element-ui/lib/locale/lang/ja'
import locale from 'element-ui/lib/locale'
import enLocale from './en'
import zhLocale from './zh'
import jaLocale from './ja'
Vue.use(VueI18n)
// 引入本地包
const messages = {
zh: {
...zhLocale,
...elementZhLocale
},
en: {
...enLocale,
...elementEnLocale
},
ja: {
...jaLocale,
...elementJaLocale
}
}
// 创建国际化实例
const i18n = new VueI18n({
locale: window.sessionStorage.getItem('lang') || 'zh',
messages
})
locale.i18n((key, value) => i18n.t(key, value))
export default i18n
zh.js文件
// module.exports = {
export default {
userManager: {
addBtn: '添加',
searchPlacholder: '请输入账号、姓名、工号',
searchBtn: '查询',
account: '账号',
username: '姓名',
departmentFullPath: '部门全称',
positionName: '岗位名称',
status: '状态',
operate: '操作'
}
}
en.js文件
// module.exports = {
export default {
userManager: {
addBtn: 'Add',
searchPlacholder: 'Please input your account, your name or your employee number to search',
searchBtn: 'Search',
account: 'Account',
username: 'Username',
departmentFullPath: 'DepartmentFullPath',
positionName: 'PositionName',
status: 'Status',
operate: 'Operate'
}
}
ja.js文件
// module.exports = {
export default {
userManager: {
addBtn: '新着',
searchPlacholder: 'アカウント番号、名前、ジョブ番号を入力してください',
searchBtn: 'お問い合わせ',
account: 'アカウント',
username: 'ユーザー名',
departmentFullPath: '部門の氏名',
positionName: '役職名',
status: '州',
operate: '操作する'
}
}
4、在main.js中引入i18n
main.js
import i18n from './lang'
new Vue({
el: '#app',
i18n,
router,
store,
render: h => h(App)
})
5、封装组件,用来切换语言
<template>
<el-dropdown class="lang-container hover-pointer" trigger="click">
<span>{{ language }} <i class="el-icon-caret-bottom el-icon--right" />
</span>
<el-dropdown-menu slot="dropdown">
<el-dropdown-item @click.native="changeLangauge('zh')">
<span style="display:block;">简体中文</span>
</el-dropdown-item>
<el-dropdown-item @click.native="changeLangauge('en')">
<span style="display:block;">English</span>
</el-dropdown-item>
<el-dropdown-item @click.native="changeLangauge('ja')">
<span style="display:block;">日本语</span>
</el-dropdown-item>
</el-dropdown-menu>
</el-dropdown>
</template>
<script>
export default {
data() {
return {
language: ''
}
},
created() {
this.showLangauge()
},
methods: {
showLangauge() {
const currentLang = window.sessionStorage.getItem('lang') || 'zh'
switch (currentLang) {
case 'zh':
this.language = '简体中文'
break
case 'en':
this.language = 'English'
break
case 'ja':
this.language = '日本语'
}
},
changeLangauge(lang) {
this.$i18n.locale = lang
window.sessionStorage.setItem('lang', lang)
window.location.reload()
}
}
}
</script>
<style lang="scss">
.hover-pointer {
span {
cursor: pointer
}
}
</style>
5、在vue文件中使用
vue代码使用$t获取国际化
普通文本使用方式: {{ $t('m.fm.components.fields.input') }}
标签内使用方式: :placeholder="$t('m.fm.components.fields.input')"
js内使用方式: this.$t('m.fm.components.fields.input')
<template>
<div class="login-container">
<language class="lanuage-component" />
<el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
<div class="title-container">
<h3 class="title">{{ $t('login.loginTitle') }}</h3>
</div>
<el-form-item prop="username">
<span class="svg-container">
<svg-icon icon-class="user" />
</span>
<el-input
ref="username"
v-model="loginForm.username"
:placeholder="$t('login.username')"
name="username"
type="text"
tabindex="1"
auto-complete="on"
/>
</el-form-item>
<el-form-item prop="password">
<span class="svg-container">
<svg-icon icon-class="password" />
</span>
<el-input
:key="passwordType"
ref="password"
v-model="loginForm.password"
:type="passwordType"
:placeholder="$t('login.password')"
name="password"
tabindex="2"
auto-complete="on"
@keyup.enter.native="handleLogin"
/>
<span class="show-pwd" @click="showPwd">
<svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
</span>
</el-form-item>
<el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">{{ $t('login.loginBtn') }}</el-button>
<div class="tips">
<span style="margin-right:20px;">username: admin</span>
<span> password: any</span>
</div>
</el-form>
</div>
</template>
<script>
export default {
name: 'Login',
data() {
const validateUsername = (rule, value, callback) => {
if (value.length < 2) {
callback(new Error(this.$t('login.usernameError')))
} else {
callback()
}
}
const validatePassword = (rule, value, callback) => {
if (value.length < 6) {
callback(new Error(this.$t('login.passwordError')))
} else {
callback()
}
}
return {
loginForm: {
username: '',
password: ''
},
loginRules: {
username: [{ required: true, trigger: 'blur', validator: validateUsername }],
password: [{ required: true, trigger: 'blur', validator: validatePassword }]
},
loading: false,
passwordType: 'password',
redirect: undefined
}
},
watch: {
$route: {
handler: function(route) {
this.redirect = route.query && route.query.redirect
},
immediate: true
}
},
methods: {
showPwd() {
if (this.passwordType === 'password') {
this.passwordType = ''
} else {
this.passwordType = 'password'
}
this.$nextTick(() => {
this.$refs.password.focus()
})
},
handleLogin() {
this.$refs.loginForm.validate(valid => {
if (valid) {
this.loading = true
this.$store.dispatch('user/login', this.loginForm).then(() => {
this.$router.push({ path: this.redirect || '/' })
this.loading = false
}).catch(() => {
this.loading = false
})
} else {
console.log('error submit!!')
return false
}
})
}
}
}
</script>
<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */
$bg:#283443;
$light_gray:#fff;
$cursor: #fff;
@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
.login-container .el-input input {
color: $cursor;
}
}
/* reset element-ui css */
.login-container {
.el-input {
display: inline-block;
height: 47px;
width: 85%;
input {
background: transparent;
border: 0px;
-webkit-appearance: none;
border-radius: 0px;
padding: 12px 5px 12px 15px;
color: $light_gray;
height: 47px;
caret-color: $cursor;
&:-webkit-autofill {
box-shadow: 0 0 0px 1000px $bg inset !important;
-webkit-text-fill-color: $cursor !important;
}
}
}
.el-form-item {
border: 1px solid rgba(255, 255, 255, 0.1);
background: rgba(0, 0, 0, 0.1);
border-radius: 5px;
color: #454545;
}
}
</style>
<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;
.login-container {
min-height: 100%;
width: 100%;
background-color: $bg;
overflow: hidden;
.lanuage-component {
position: fixed;
top: 20px;
right: 100px;
color: #fff;
}
.login-form {
position: relative;
width: 520px;
max-width: 100%;
padding: 160px 35px 0;
margin: 0 auto;
overflow: hidden;
}
.tips {
font-size: 14px;
color: #fff;
margin-bottom: 10px;
span {
&:first-of-type {
margin-right: 16px;
}
}
}
.svg-container {
padding: 6px 5px 6px 15px;
color: $dark_gray;
vertical-align: middle;
width: 30px;
display: inline-block;
}
.title-container {
position: relative;
.title {
font-size: 26px;
color: $light_gray;
margin: 0px auto 40px auto;
text-align: center;
font-weight: bold;
}
}
.show-pwd {
position: absolute;
right: 10px;
top: 7px;
font-size: 16px;
color: $dark_gray;
cursor: pointer;
user-select: none;
}
}
</style>
切换语言后,name字段的现实就根据切换的语言显示