* chore: update time picker doc * chore: update * chore: update * chore: update
6.4 KiB
Internationalization
The default locale of Element Plus is English. If you want to use another language, you'll need to do some i18n configuration. In your entry file, if you are importing Element Plus entirely:
import { createApp } from 'vue'
import ElementPlus from 'element-plus'
import locale from 'element-plus/lib/locale/lang/zh-cn'
import 'dayjs/locale/zh-cn'
createApp(App).use(ElementPlus, { locale })
Or if you are importing Element Plus on demand:
import Vue from 'vue'
import { ElButton, ElSelect } from 'element-plus'
import lang from 'element-plus/lib/locale/lang/zh-cn'
import 'dayjs/locale/zh-cn'
import locale from 'element-plus/lib/locale'
// configure language
locale.use(lang)
// import components
Vue.component(ElButton.name, ElButton)
Vue.component(ElSelect.name, ElSelect)
Set Day.js locale
Element Plus use date time locale (month name, first day of the week ...) from Day.js directlly. And will set the global Day.js locale automaticatlly after the locale file is loaded.
import locale from 'element-plus/lib/locale/lang/zh-cn'
import 'dayjs/locale/zh-cn'
// will auto set Day.js locale to 'zh-cn'
app.use(ElementPlus, { locale })
However, you can use another Day.js locale if needed.
import 'dayjs/locale/fr'
dayjs.locale('fr')
The English locale is imported by default, even if you're using another locale. But with NormalModuleReplacementPlugin
provided by webpack you can replace default locale:
webpack.config.js
{
plugins: [
new webpack.NormalModuleReplacementPlugin(/element-plus[\/\\]lib[\/\\]locale[\/\\]lang[\/\\]en/, 'element-plus/lib/locale/lang/zh-cn')
]
}
Compatible with vue-i18n@5.x
Element Plus is compatible with vue-i18n@5.x, which makes multilingual switching even easier.
import Vue from 'vue'
import VueI18n from 'vue-i18n'
import ElementPlus from 'element-plus'
import enLocale from 'element-plus/lib/locale/lang/en'
import zhLocale from 'element-plus/lib/locale/lang/zh-cn'
import App from './App.vue';
const app = createApp(App)
app.use(ElementPlus)
Vue.use(VueI18n)
Vue.config.lang = 'zh-cn'
Vue.locale('zh-cn', zhLocale)
Vue.locale('en', enLocale)
Compatible with other i18n plugins
Element Plus may not be compatible with i18n plugins other than vue-i18n, but you can customize how Element Plus processes i18n.
import Vue from 'vue'
import ElementPlus from 'element-plus'
import enLocale from 'element-plus/lib/locale/lang/en'
import zhLocale from 'element-plus/lib/locale/lang/zh-cn'
Vue.use(ElementPlus, {
i18n: function (path, options) {
// ...
}
})
Compatible with vue-i18n@6.x
You need to manually handle it for compatibility with 6.x
.
import Vue from 'vue'
import ElementPlus from 'element-plus'
import VueI18n from 'vue-i18n'
import enLocale from 'element-plus/lib/locale/lang/en'
import zhLocale from 'element-plus/lib/locale/lang/zh-cn'
Vue.use(VueI18n)
const messages = {
en: {
message: 'hello',
...enLocale // Or use `Object.assign({ message: 'hello' }, enLocale)`
},
zh: {
message: '你好',
...zhLocale // Or use `Object.assign({ message: '你好' }, zhLocale)`
}
}
// Create VueI18n instance with options
const i18n = new VueI18n({
locale: 'en', // set locale
messages, // set locale messages
})
Vue.use(ElementPlus, {
i18n: (key, value) => i18n.t(key, value)
})
new Vue({ i18n }).$mount('#app')
Custom i18n in on-demand components
import Vue from 'vue'
import DatePicker from 'element/lib/date-picker'
import VueI18n from 'vue-i18n'
import enLocale from 'element-plus/lib/locale/lang/en'
import zhLocale from 'element-plus/lib/locale/lang/zh-cn'
import ElementLocale from 'element-plus/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
})
ElementLocale.i18n((key, value) => i18n.t(key, value))
Import via CDN
<script src="//unpkg.com/vue"></script>
<script src="//unpkg.com/element-plus"></script>
<script src="//unpkg.com/element-plus/lib/umd/locale/zh-cn.js"></script>
<script src="//unpkg.com/dayjs/locale/zh-cn.js"></script>
<script>
ELEMENT.locale(ELEMENT.lang.zhCn)
</script>
Compatible with vue-i18n
<script src="//unpkg.com/vue"></script>
<script src="//unpkg.com/vue-i18n/dist/vue-i18n.js"></script>
<script src="//unpkg.com/element-plus"></script>
<script src="//unpkg.com/element-plus/lib/umd/locale/zh-cn.js"></script>
<script src="//unpkg.com/element-plus/lib/umd/locale/en.js"></script>
<script>
Vue.locale('en', ELEMENT.lang.en)
Vue.locale('zh-cn', ELEMENT.lang.zhCN)
</script>
Currently Element Plus ships with the following languages:
- Simplified Chinese (zh-cn)
- English (en)
- German (de)
- Portuguese (pt)
- Spanish (es)
- Danish (da)
- French (fr)
- Norwegian (nb-NO)
- Traditional Chinese (zh-tw)
- Italian (it)
- Korean (ko)
- Japanese (ja)
- Dutch (nl)
- Vietnamese (vi)
- Russian (ru)
- Turkish (tr)
- Brazilian Portuguese (pt-br)
- Farsi (fa)
- Thai (th)
- Indonesian (id)
- Bulgarian (bg)
- Polish (pl)
- Finnish (fi)
- Swedish (sv)
- Greek (el)
- Slovak (sk)
- Catalunya (ca)
- Czech (cs)
- Ukrainian (uk)
- Turkmen (tk)
- Tamil (ta)
- Latvian (lv)
- Afrikaans (af)
- Estonian (et)
- Slovenian (sl)
- Arabic (ar)
- Hebrew (he)
- Lithuanian (lt)
- Mongolian (mn)
- Kazakh (kk)
- Hungarian (hu)
- Romanian (ro)
- Kurdish (ku)
- Uighur (ug-cn)
- Khmer (km)
- Serbian (sr)
- Basque (eu)
- Kyrgyz (ky)
- Armenian (hy-am)
- Croatian (hr)
- Esperanto (eo)
If your target language is not included, you are more than welcome to contribute: just add another language config here and create a pull request.