2020-11-04 10:30:14 +08:00
## 国際化
2021-01-19 23:49:07 +08:00
Element Plus のデフォルト言語は英語です。他の言語を使用したい場合は、i18n の設定を行う必要があります。Element Plus を完全にインポートする場合のエントリーファイル:
2020-11-04 10:30:14 +08:00
2021-01-19 23:49:07 +08:00
```typescript
2020-11-04 11:15:05 +08:00
import { createApp } from 'vue'
2020-11-04 10:30:14 +08:00
import ElementPlus from 'element-plus'
2020-12-23 16:38:06 +08:00
import 'dayjs/locale/ja'
2020-11-09 14:53:43 +08:00
import locale from 'element-plus/lib/locale/lang/ja'
2020-11-04 10:30:14 +08:00
2020-11-20 23:02:15 +08:00
createApp(App).use(ElementPlus, { locale })
2020-11-04 10:30:14 +08:00
```
2021-01-25 16:23:15 +08:00
if you are importing Element Plus on demand via **es modules** :
2020-11-04 10:30:14 +08:00
2021-01-19 23:49:07 +08:00
```typescript
2021-01-25 16:23:15 +08:00
import { createApp } from 'vue'
import { ElButton, locale } from 'element-plus'
import lang from 'element-plus/lib/locale/lang/ja'
import 'dayjs/locale/ja'
locale(lang)
createApp(App).component(ElButton.name, ElButton)
```
Or if you are importing Element Plus on demand via [babel-plugin-component ](#/jp/component/quickstart#on-demand ):
```typescript
import { createApp } from 'vue'
2020-11-23 16:28:06 +08:00
import { ElButton, ElSelect } from 'element-plus'
2020-11-09 14:53:43 +08:00
import lang from 'element-plus/lib/locale/lang/ja'
2020-12-23 16:38:06 +08:00
import 'dayjs/locale/ja'
2020-11-04 11:15:05 +08:00
import locale from 'element-plus/lib/locale'
2020-11-04 10:30:14 +08:00
// configure language
locale.use(lang)
// import components
2021-01-25 16:23:15 +08:00
createApp(App).component(ElButton.name, ElButton)
createApp(App).component(ElSelect.name, ElSelect)
2020-11-04 10:30:14 +08:00
```
2021-01-25 16:23:15 +08:00
2020-12-23 16:38:06 +08:00
### Set Day.js locale
Element Plus use date time locale (month name, first day of the week ...) from [Day.js ](https://day.js.org/ ) directlly. And will set the global Day.js locale automaticatlly after the locale file is loaded.
2021-01-19 23:49:07 +08:00
```typescript
2020-12-23 16:38:06 +08:00
import locale from 'element-plus/lib/locale/lang/ja'
import 'dayjs/locale/ja'
// will auto set Day.js locale to 'ja'
app.use(ElementPlus, { locale })
```
However, you can use another Day.js locale if needed.
2021-01-19 23:49:07 +08:00
```typescript
2020-12-23 16:38:06 +08:00
import 'dayjs/locale/fr'
dayjs.locale('fr')
```
2021-01-19 23:49:07 +08:00
たとえ、別の言語を使っていても、英語パックはデフォルトでインポートされます。 しかしながら、webpack で提供されている `NormalModuleReplacementPlugin` を使えばデフォルト locale を差し替えることが出来ます:
2020-11-04 10:30:14 +08:00
webpack.config.js
2021-01-19 23:49:07 +08:00
```typescript
2020-11-04 10:30:14 +08:00
{
plugins: [
2021-01-19 23:49:07 +08:00
new webpack.NormalModuleReplacementPlugin(
/element-plus[\/\\]lib[\/\\]locale[\/\\]lang[\/\\]en/,
'element-plus/lib/locale/lang/ja',
),
2020-11-04 10:30:14 +08:00
]
}
```
2021-01-19 23:49:07 +08:00
### `vue-i18n@9.x` との互換性
2020-11-04 10:30:14 +08:00
2021-01-19 23:49:07 +08:00
Element [vue-i18n@9.x ](https://vue-i18n-next.intlify.dev/guide/#html ) は互換性があり、 多言語切り替えをより簡単に出来ます。
2020-11-04 10:30:14 +08:00
2021-01-19 23:49:07 +08:00
Need translation
If you need to check out [VueI18n documentation ](https://vue-i18n-next.intlify.dev/guide/#html ), please click this link to check it out.
```typescript
import { createApp } from 'vue'
import { createI18n } from 'vue-i18n'
2020-11-04 10:30:14 +08:00
import ElementPlus from 'element-plus'
import enLocale from 'element-plus/lib/locale/lang/en'
2020-11-14 01:39:03 +08:00
import zhLocale from 'element-plus/lib/locale/lang/zh-cn'
2021-01-19 23:49:07 +08:00
import App from './App.vue'
2020-11-04 10:30:14 +08:00
2021-01-19 23:49:07 +08:00
const messages = {
[enLocale.name]: {
// el property is critical, set this in order for ElementPlus translate strings correctly.
el: enLocale.el,
// Define your own dictionary with your own namespace, but DO NOT use the namespace `el` ,
// Because that makes the ElementPlus internal translation invalid.
message: {
hello: 'hello world',
},
},
[zhLocale.name]: {
el: zhLocale.el,
// Define your own dictionary with your own namespace, but DO NOT use the namespace `el` ,
// Because that makes the ElementPlus internal translation invalid.
message: {
hello: '你好,世界',
},
},
testLocale: {
el: {},
// No message translations, it will fallback to en lang, the definition of fallbackLocale is below 👇
},
}
2020-11-04 10:30:14 +08:00
2021-01-19 23:49:07 +08:00
const i18n = createI18n({
locale: zhLocale.name,
fallbackLocale: enLocale.name,
messages,
})
2020-11-04 10:30:14 +08:00
2021-01-19 23:49:07 +08:00
const app = createApp(App)
2020-11-04 10:30:14 +08:00
2021-01-19 23:49:07 +08:00
app.use(ElementPlus, {
i18n: i18n.global.t,
2020-11-04 10:30:14 +08:00
})
2021-01-19 23:49:07 +08:00
// Remember to use this plugin.
app.use(i18n)
2020-11-04 10:30:14 +08:00
```
2021-01-19 23:49:07 +08:00
### 他の i18n プラグインとの互換性
Element は vue-i18n 以外の i18n プラグインとは互換性がないかもしれませんが、Element がどのように i18n を処理するかをカスタマイズすることができます。
2020-11-04 10:30:14 +08:00
2021-01-19 23:49:07 +08:00
Need translation
2020-11-04 10:30:14 +08:00
2021-01-19 23:49:07 +08:00
:::tip
Once you set this method, the internal translation function will be invalid, only the customized translation method will be used, be sure that your custom translation method can translate format like `el.scope.subName` , other wise the internal translation string will be raw string.
:::
```typescript
2020-11-04 10:30:14 +08:00
import Vue from 'vue'
import ElementPlus from 'element-plus'
import enLocale from 'element-plus/lib/locale/lang/en'
2020-11-14 01:39:03 +08:00
import zhLocale from 'element-plus/lib/locale/lang/zh-cn'
2020-11-04 10:30:14 +08:00
2021-01-19 23:49:07 +08:00
// The is the signature of i18n method.
type i18n = (...args: any[]) => string
Vue.use(Element, {
i18n: function(path, options) {
// ...
2020-11-04 10:30:14 +08:00
},
2021-01-19 23:49:07 +08:00
// others.
2020-11-04 10:30:14 +08:00
})
```
2021-01-19 23:49:07 +08:00
### オンデマンドコンポーネントのカスタム i18n
2020-11-04 10:30:14 +08:00
2021-01-19 23:49:07 +08:00
If you need to know how to lazy loading translation strings, please check this out[Lazy loading](https://vue-i18n-next.intlify.dev/guide/advanced/lazy.html)
2020-11-04 10:30:14 +08:00
2021-01-19 23:49:07 +08:00
```typescript
import { createApp } from 'vue'
import { createI18n } from 'vue-i18n'
import ElementPlus from 'element-plus'
2020-11-04 10:30:14 +08:00
import enLocale from 'element-plus/lib/locale/lang/en'
2020-11-14 01:39:03 +08:00
import zhLocale from 'element-plus/lib/locale/lang/zh-cn'
2020-11-04 10:30:14 +08:00
import ElementLocale from 'element-plus/lib/locale'
2021-01-19 23:49:07 +08:00
import App from './App.vue'
2020-11-04 10:30:14 +08:00
const messages = {
2021-01-19 23:49:07 +08:00
[enLocale.name]: {
// el property is critical, set this in order for ElementPlus translate strings correctly.
el: enLocale.el,
// Define your own dictionary with your own namespace, but DO NOT use the namespace `el` ,
// Because that makes the ElementPlus internal translation invalid.
message: {
hello: 'hello world',
},
},
[zhLocale.name]: {
el: zhLocale.el,
// Define your own dictionary with your own namespace, but DO NOT use the namespace `el` ,
// Because that makes the ElementPlus internal translation invalid.
message: {
hello: '你好,世界',
},
},
testLocale: {
el: {},
// No message translations, it will fallback to en lang, the definition of fallbackLocale is below 👇
2020-11-04 10:30:14 +08:00
},
}
2021-01-19 23:49:07 +08:00
const i18n = createI18n({
locale: zhLocale.name,
fallbackLocale: enLocale.name,
messages,
2020-11-04 10:30:14 +08:00
})
2021-01-19 23:49:07 +08:00
ElementLocale.i18n(i18n.global.t)
const app = createApp(App)
app.use(i18n)
2020-11-04 10:30:14 +08:00
```
2021-01-19 23:49:07 +08:00
### CDN を経由してインポート
2020-11-04 10:30:14 +08:00
```html
2021-01-25 16:23:15 +08:00
< script src = "//unpkg.com/vue@next" > < / script >
< script src = "//unpkg.com/element-plus/lib/index.full.js" > < / script >
< script src = "//unpkg.com/element-plus/lib/umd/locale/jp.js" > < / script >
2020-11-04 10:30:14 +08:00
< script >
2021-01-25 16:23:15 +08:00
ElementPlus.locale(ElementPlus.lang.jp)
2020-11-04 10:30:14 +08:00
< / script >
```
`vue-i18n` との互換性
```html
2021-01-25 16:23:15 +08:00
< script src = "//unpkg.com/vue@next" > < / script >
2020-11-04 10:30:14 +08:00
< script src = "//unpkg.com/vue-i18n/dist/vue-i18n.js" > < / script >
2021-01-25 16:23:15 +08:00
< script src = "//unpkg.com/element-plus/lib/index.full.js" > < / script >
2020-11-14 01:39:03 +08:00
< script src = "//unpkg.com/element-plus/lib/umd/locale/zh-cn.js" > < / script >
2020-11-04 10:30:14 +08:00
< script src = "//unpkg.com/element-plus/lib/umd/locale/en.js" > < / script >
< script >
2021-01-19 23:49:07 +08:00
// need translation
const i18n = Vue18n.createI18n({
2021-01-25 16:23:15 +08:00
locale: ElementPlus.lang.zhCN.name,
fallbackLocale: ElementPlus.lang.en.name,
2021-01-19 23:49:07 +08:00
messages: {
2021-01-25 16:23:15 +08:00
[ElementPlus.lang.en.name]: {
2021-01-19 23:49:07 +08:00
// el property is critical, set this in order for ElementPlus translate strings correctly.
2021-01-25 16:23:15 +08:00
el: ElementPlus.lang.en.el,
2021-01-19 23:49:07 +08:00
// Define your own dictionary with your own namespace, but DO NOT use the namespace `el` ,
// Because that makes the ElementPlus internal translation invalid.
message: {
hello: 'hello world',
},
},
2021-01-25 16:23:15 +08:00
[ElementPlus.lang.zhCN.name]: {
el: ElementPlus.lang.zhCN.el,
2021-01-19 23:49:07 +08:00
// Define your own dictionary with your own namespace, but DO NOT use the namespace `el` ,
// Because that makes the ElementPlus internal translation invalid.
message: {
hello: '你好,世界',
},
},
testLocale: {
el: {},
// No message translations, it will fallback to en lang.
},
},
})
const app = Vue.createApp()
app.use(i18n)
2020-11-04 10:30:14 +08:00
< / script >
```
2021-01-19 23:49:07 +08:00
現在、Element Plus は以下の言語をフォローしています。:
2020-11-04 10:30:14 +08:00
< ul class = "language-list" >
2020-11-09 14:53:43 +08:00
< li > Simplified Chinese (zh-cn)< / li >
2020-11-04 10:30:14 +08:00
< li > English (en)< / li >
< li > German (de)< / li >
< li > Portuguese (pt)< / li >
< li > Spanish (es)< / li >
< li > Danish (da)< / li >
< li > French (fr)< / li >
2020-11-19 15:58:25 +08:00
< li > Norwegian (nb-NO)< / li >
2020-11-09 14:53:43 +08:00
< li > Traditional Chinese (zh-tw)< / li >
2020-11-04 10:30:14 +08:00
< li > Italian (it)< / li >
< li > Korean (ko)< / li >
< li > Japanese (ja)< / li >
< li > Dutch (nl)< / li >
< li > Vietnamese (vi)< / li >
2020-11-19 15:58:25 +08:00
< li > Russian (ru)< / li >
< li > Turkish (tr)< / li >
2020-11-04 10:30:14 +08:00
< li > Brazilian Portuguese (pt-br)< / li >
< li > Farsi (fa)< / li >
< li > Thai (th)< / li >
< li > Indonesian (id)< / li >
< li > Bulgarian (bg)< / li >
< li > Polish (pl)< / li >
< li > Finnish (fi)< / li >
2020-11-19 15:58:25 +08:00
< li > Swedish (sv)< / li >
2020-11-04 10:30:14 +08:00
< li > Greek (el)< / li >
< li > Slovak (sk)< / li >
< li > Catalunya (ca)< / li >
2020-11-19 15:58:25 +08:00
< li > Czech (cs)< / li >
< li > Ukrainian (uk)< / li >
2020-11-04 10:30:14 +08:00
< li > Turkmen (tk)< / li >
< li > Tamil (ta)< / li >
< li > Latvian (lv)< / li >
2020-11-19 15:58:25 +08:00
< li > Afrikaans (af)< / li >
< li > Estonian (et)< / li >
2020-11-04 10:30:14 +08:00
< li > Slovenian (sl)< / li >
< li > Arabic (ar)< / li >
< li > Hebrew (he)< / li >
< li > Lithuanian (lt)< / li >
< li > Mongolian (mn)< / li >
2020-11-19 15:58:25 +08:00
< li > Kazakh (kk)< / li >
2020-11-04 10:30:14 +08:00
< li > Hungarian (hu)< / li >
< li > Romanian (ro)< / li >
< li > Kurdish (ku)< / li >
2020-11-09 14:53:43 +08:00
< li > Uighur (ug-cn)< / li >
2020-11-04 10:30:14 +08:00
< li > Khmer (km)< / li >
< li > Serbian (sr)< / li >
< li > Basque (eu)< / li >
2020-11-19 15:58:25 +08:00
< li > Kyrgyz (ky)< / li >
< li > Armenian (hy-am)< / li >
2020-11-04 10:30:14 +08:00
< li > Croatian (hr)< / li >
< li > Esperanto (eo)< / li >
< / ul >
2020-11-09 14:53:43 +08:00
もしあなたのターゲット言語が含まれていない場合、貢献することは大歓迎です: 別の言語の設定を追加して [ここで ](https://github.com/element-plus/element-plus/tree/dev/packages/locale/lang )、プルリクエストを作成してください。