Vue.js ElementUI i18n settings

Usage

Settings

zh.js
1
2
3
module.exports = {
name: '用户名'
}
en.js
1
2
3
module.exports = {
name: 'User Name'
}
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// import element ui language package
import enLocale from 'element-ui/lib/locale/lang/en'
import zhLocale from 'element-ui/lib/locale/lang/zh-CN'
import VueI18n from 'vue-i18n'
Vue.use(VueI18n)

// set language messages
const messages = {
'zh': Object.assign(require('./i18n/zh'), zhLocale),
'en': Object.assign(require('./i18n/en'), enLocale)
}

// set date format
const dateTimeFormats = {
'zh': {
short: {
year: 'numeric', month: 'short', day: 'numeric', hour12: false
},
long: {
year: 'numeric', month: 'short', day: 'numeric', hour: 'numeric', minute: 'numeric', hour12: false
}
},
'en': {
short: {
year: 'numeric', month: 'short', day: 'numeric', hour12: false
},
long: {
year: 'numeric', month: 'short', day: 'numeric', hour: 'numeric', minute: 'numeric', hour12: false
}
}
}

const i18n = new VueI18n({
locale: Vue.config.lang,
fallbackLocale: 'en',
messages,
dateTimeFormats
})

Useage

1
2
3
4
<template>
<span>{{ $t('name') }}</span>
<span>{{ $d(new Date(), 'short') }}</span>
</template>
;