2016-11-10 21:46:55 +08:00
|
|
|
|
## 国际化
|
|
|
|
|
|
|
|
|
|
Element 组件内部默认使用中文,若希望使用其他语言,则需要进行多语言设置。以英文为例,在 main.js 中:
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
// 完整引入 Element
|
|
|
|
|
import Vue from 'vue'
|
|
|
|
|
import ElementUI from 'element-ui'
|
|
|
|
|
import locale from 'element-ui/lib/locale/lang/en'
|
|
|
|
|
|
|
|
|
|
Vue.use(ElementUI, { locale })
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
或
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
// 按需引入 Element
|
|
|
|
|
import Vue from 'vue'
|
|
|
|
|
import { Button, Select } from 'element-ui'
|
|
|
|
|
import lang from 'element-ui/lib/locale/lang/en'
|
|
|
|
|
import locale from 'element-ui/lib/locale'
|
|
|
|
|
|
|
|
|
|
// 设置语言
|
|
|
|
|
locale.use(lang)
|
|
|
|
|
|
|
|
|
|
// 引入组件
|
|
|
|
|
Vue.component(Button.name, Button)
|
|
|
|
|
Vue.component(Select.name, Select)
|
|
|
|
|
```
|
|
|
|
|
|
2016-12-06 17:19:40 +08:00
|
|
|
|
如果使用其它语言,默认情况下中文语言包依旧是被引入的,可以使用 webpack 的 NormalModuleReplacementPlugin 替换默认语言包。
|
2016-11-10 21:46:55 +08:00
|
|
|
|
|
|
|
|
|
**webpack.config.js**
|
|
|
|
|
```javascript
|
|
|
|
|
{
|
|
|
|
|
plugins: [
|
2016-12-06 17:19:40 +08:00
|
|
|
|
new webpack.NormalModuleReplacementPlugin(/element-ui[\/\\]lib[\/\\]locale[\/\\]lang[\/\\]zh-CN/, 'element-ui/lib/locale/lang/en')
|
2016-11-10 21:46:55 +08:00
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2017-04-28 08:35:12 +08:00
|
|
|
|
## 兼容 `vue-i18n@5.x`
|
2016-12-29 17:50:09 +08:00
|
|
|
|
|
2017-04-28 08:35:12 +08:00
|
|
|
|
Element 兼容 [vue-i18n@5.x](https://github.com/kazupon/vue-i18n),搭配使用能更方便地实现多语言切换。
|
2016-12-29 17:50:09 +08:00
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
import Vue from 'vue'
|
|
|
|
|
import VueI18n from 'vue-i18n'
|
|
|
|
|
import Element from 'element-ui'
|
|
|
|
|
import enLocale from 'element-ui/lib/locale/lang/en'
|
|
|
|
|
import zhLocale from 'element-ui/lib/locale/lang/zh-CN'
|
|
|
|
|
|
|
|
|
|
Vue.use(VueI18n)
|
|
|
|
|
Vue.use(Element)
|
|
|
|
|
|
|
|
|
|
Vue.config.lang = 'zh-cn'
|
|
|
|
|
Vue.locale('zh-cn', zhLocale)
|
|
|
|
|
Vue.locale('en', enLocale)
|
|
|
|
|
```
|
|
|
|
|
|
2017-01-04 12:22:27 +08:00
|
|
|
|
## 兼容其他 i18n 插件
|
2017-04-28 08:35:12 +08:00
|
|
|
|
如果不使用 `vue-i18n@5.x`,而是用其他的 i18n 插件,Element 将无法兼容,但是可以自定义 Element 的 i18n 的处理方法。
|
2017-01-04 12:22:27 +08:00
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
import Vue from 'vue'
|
|
|
|
|
import Element from 'element-ui'
|
|
|
|
|
import enLocale from 'element-ui/lib/locale/lang/en'
|
|
|
|
|
import zhLocale from 'element-ui/lib/locale/lang/zh-CN'
|
|
|
|
|
|
|
|
|
|
Vue.use(Element, {
|
|
|
|
|
i18n: function (path, options) {
|
|
|
|
|
// ...
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
```
|
|
|
|
|
|
2017-04-28 08:35:12 +08:00
|
|
|
|
## 兼容 `vue-i18n@6.x`
|
|
|
|
|
|
|
|
|
|
默认不支持 6.x 的 `vue-i18n`,你需要手动处理。
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
import Vue from 'vue'
|
|
|
|
|
import Element from 'element-ui'
|
|
|
|
|
import VueI18n from 'vue-i18n'
|
|
|
|
|
import enLocale from 'element-ui/lib/locale/lang/en'
|
|
|
|
|
import zhLocale from 'element-ui/lib/locale/lang/zh-CN'
|
|
|
|
|
|
|
|
|
|
Vue.use(VueI18n)
|
|
|
|
|
|
|
|
|
|
const messages = {
|
|
|
|
|
en: {
|
|
|
|
|
message: 'hello',
|
|
|
|
|
...enLocale // 或者用 Object.assign({ message: 'hello' }, enLocale)
|
|
|
|
|
},
|
|
|
|
|
zh: {
|
|
|
|
|
message: '你好',
|
|
|
|
|
...zhLocale // 或者用 Object.assign({ message: '你好' }, zhLocale)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Create VueI18n instance with options
|
|
|
|
|
const i18n = new VueI18n({
|
|
|
|
|
locale: 'en', // set locale
|
|
|
|
|
messages, // set locale messages
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
Vue.use(Element, {
|
2017-12-13 15:21:00 +08:00
|
|
|
|
i18n: (key, value) => i18n.t(key, value)
|
2017-04-28 08:35:12 +08:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
new Vue({ i18n }).$mount('#app')
|
|
|
|
|
```
|
|
|
|
|
|
2017-05-02 10:55:19 +08:00
|
|
|
|
## 按需加载里定制 i18n
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
import Vue from 'vue'
|
|
|
|
|
import DatePicker from 'element/lib/date-picker'
|
|
|
|
|
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 ElementLocale from 'element-ui/lib/locale'
|
|
|
|
|
|
|
|
|
|
Vue.use(VueI18n)
|
|
|
|
|
Vue.use(DatePicker)
|
|
|
|
|
|
|
|
|
|
const messages = {
|
|
|
|
|
en: {
|
|
|
|
|
message: 'hello',
|
|
|
|
|
...enLocale
|
|
|
|
|
},
|
|
|
|
|
zh: {
|
|
|
|
|
message: '你好',
|
|
|
|
|
...zhLocale
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Create VueI18n instance with options
|
|
|
|
|
const i18n = new VueI18n({
|
|
|
|
|
locale: 'en', // set locale
|
|
|
|
|
messages, // set locale messages
|
|
|
|
|
})
|
|
|
|
|
|
2017-07-11 11:39:58 +08:00
|
|
|
|
ElementLocale.i18n((key, value) => i18n.t(key, value))
|
2017-05-02 10:55:19 +08:00
|
|
|
|
```
|
|
|
|
|
|
2017-02-07 14:13:29 +08:00
|
|
|
|
## 通过 CDN 的方式加载语言文件
|
|
|
|
|
|
|
|
|
|
```html
|
2022-04-11 16:06:36 +08:00
|
|
|
|
<script src="//unpkg.com/vue@2"></script>
|
2017-02-07 14:13:29 +08:00
|
|
|
|
<script src="//unpkg.com/element-ui"></script>
|
|
|
|
|
<script src="//unpkg.com/element-ui/lib/umd/locale/en.js"></script>
|
|
|
|
|
|
|
|
|
|
<script>
|
|
|
|
|
ELEMENT.locale(ELEMENT.lang.en)
|
|
|
|
|
</script>
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
搭配 `vue-i18n` 使用
|
|
|
|
|
|
|
|
|
|
```html
|
2022-04-11 16:06:36 +08:00
|
|
|
|
<script src="//unpkg.com/vue@2"></script>
|
2017-02-07 14:13:29 +08:00
|
|
|
|
<script src="//unpkg.com/vue-i18n/dist/vue-i18n.js"></script>
|
|
|
|
|
<script src="//unpkg.com/element-ui"></script>
|
|
|
|
|
<script src="//unpkg.com/element-ui/lib/umd/locale/zh-CN.js"></script>
|
|
|
|
|
<script src="//unpkg.com/element-ui/lib/umd/locale/en.js"></script>
|
|
|
|
|
|
|
|
|
|
<script>
|
|
|
|
|
Vue.locale('en', ELEMENT.lang.en)
|
|
|
|
|
Vue.locale('zh-cn', ELEMENT.lang.zhCN)
|
|
|
|
|
</script>
|
|
|
|
|
```
|
|
|
|
|
|
2016-11-13 11:58:45 +08:00
|
|
|
|
目前 Element 内置了以下语言:
|
|
|
|
|
<ul class="language-list">
|
2016-11-18 15:04:19 +08:00
|
|
|
|
<li>简体中文(zh-CN)</li>
|
2016-11-13 11:58:45 +08:00
|
|
|
|
<li>英语(en)</li>
|
|
|
|
|
<li>德语(de)</li>
|
|
|
|
|
<li>葡萄牙语(pt)</li>
|
2016-11-18 15:04:19 +08:00
|
|
|
|
<li>西班牙语(es)</li>
|
|
|
|
|
<li>丹麦语(da)</li>
|
|
|
|
|
<li>法语(fr)</li>
|
|
|
|
|
<li>挪威语(nb-NO)</li>
|
|
|
|
|
<li>繁体中文(zh-TW)</li>
|
|
|
|
|
<li>意大利语(it)</li>
|
|
|
|
|
<li>韩语(ko)</li>
|
|
|
|
|
<li>日语(ja)</li>
|
2016-12-06 16:05:38 +08:00
|
|
|
|
<li>荷兰语(nl)</li>
|
|
|
|
|
<li>越南语(vi)</li>
|
|
|
|
|
<li>俄语(ru-RU)</li>
|
|
|
|
|
<li>土耳其语(tr-TR)</li>
|
|
|
|
|
<li>巴西葡萄牙语(pt-br)</li>
|
2016-12-09 17:59:51 +08:00
|
|
|
|
<li>波斯语(fa)</li>
|
|
|
|
|
<li>泰语(th)</li>
|
2016-12-20 18:21:30 +08:00
|
|
|
|
<li>印尼语(id)</li>
|
2017-01-09 16:14:07 +08:00
|
|
|
|
<li>保加利亚语(bg)</li>
|
2017-01-16 16:01:58 +08:00
|
|
|
|
<li>波兰语(pl)</li>
|
2017-01-23 16:09:29 +08:00
|
|
|
|
<li>芬兰语(fi)</li>
|
2017-02-22 15:02:34 +08:00
|
|
|
|
<li>瑞典语(sv-SE)</li>
|
|
|
|
|
<li>希腊语(el)</li>
|
|
|
|
|
<li>斯洛伐克语(sk)</li>
|
2017-04-19 20:01:02 +08:00
|
|
|
|
<li>加泰罗尼亚语(ca)</li>
|
2018-01-08 10:39:42 +08:00
|
|
|
|
<li>捷克语(cs-CZ)</li>
|
2017-04-19 20:01:02 +08:00
|
|
|
|
<li>乌克兰语(ua)</li>
|
|
|
|
|
<li>土库曼语(tk)</li>
|
2017-06-03 20:55:17 +08:00
|
|
|
|
<li>泰米尔语(ta)</li>
|
|
|
|
|
<li>拉脱维亚语(lv)</li>
|
2017-06-09 20:13:37 +08:00
|
|
|
|
<li>南非荷兰语(af-ZA)</li>
|
|
|
|
|
<li>爱沙尼亚语(ee)</li>
|
2017-06-18 20:54:47 +08:00
|
|
|
|
<li>斯洛文尼亚语(sl)</li>
|
2017-08-09 12:18:28 +08:00
|
|
|
|
<li>阿拉伯语(ar)</li>
|
2017-09-17 20:47:27 +08:00
|
|
|
|
<li>希伯来语(he)</li>
|
2017-11-26 16:07:44 +08:00
|
|
|
|
<li>立陶宛语(lt)</li>
|
2017-12-12 16:45:26 +08:00
|
|
|
|
<li>蒙古语(mn)</li>
|
2017-12-12 12:45:09 +08:00
|
|
|
|
<li>哈萨克斯坦语(kz)</li>
|
|
|
|
|
<li>匈牙利语(hu)</li>
|
2017-12-28 17:47:37 +08:00
|
|
|
|
<li>罗马尼亚语(ro)</li>
|
2018-02-11 18:18:49 +08:00
|
|
|
|
<li>库尔德语(ku)</li>
|
2018-04-12 21:21:22 +08:00
|
|
|
|
<li>维吾尔语(ug-CN)</li>
|
2018-05-21 10:37:00 +08:00
|
|
|
|
<li>高棉语(km)</li>
|
2018-09-25 12:09:01 +08:00
|
|
|
|
<li>塞尔维亚语(sr)</li>
|
2018-10-31 16:41:35 +08:00
|
|
|
|
<li>巴斯克语(eu)</li>
|
2019-01-23 11:32:49 +08:00
|
|
|
|
<li>吉尔吉斯语(kg)</li>
|
2019-02-01 17:15:47 +08:00
|
|
|
|
<li>亚美尼亚语 (hy)</li>
|
2019-03-05 15:29:14 +08:00
|
|
|
|
<li>克罗地亚 (hr)</li>
|
2019-08-12 10:26:54 +08:00
|
|
|
|
<li>世界语 (eo)</li>
|
2016-11-13 11:58:45 +08:00
|
|
|
|
</ul>
|
2016-12-06 17:19:40 +08:00
|
|
|
|
|
2017-12-15 13:50:54 +08:00
|
|
|
|
如果你需要使用其他的语言,欢迎贡献 PR:只需在 [这里](https://github.com/ElemeFE/element/tree/dev/src/locale/lang) 添加一个语言配置文件即可。
|