Load i18n from element-ui

This commit is contained in:
Pig Fang 2019-03-30 15:48:34 +08:00
parent 005f801c0d
commit fd01a352a4
2 changed files with 32 additions and 16 deletions

View File

@ -1,4 +1,5 @@
import Vue from 'vue'
import loadI18n from './scripts/i18n-text'
import './scripts'
import * as emitter from './scripts/event'
import routes from './scripts/route'
@ -7,22 +8,6 @@ Vue.config.productionTip = false
loadI18n().then(loadModules)
async function loadI18n() {
const langs = [
{ lang: 'en', load: () => import('../../lang/en/front-end.yml') },
{ lang: 'zh_CN', load: () => import('../../lang/zh_CN/front-end.yml') },
]
const texts = await langs.find(({ lang }) => lang === blessing.locale)
if (texts) {
blessing.i18n = Object.assign(blessing.i18n || Object.create(null), await texts.load())
} else {
blessing.i18n = Object.assign(
blessing.i18n || Object.create(null),
(await langs.find(({ lang }) => lang === blessing.fallback_locale)).load()
)
}
}
function loadModules() {
const route = routes.find(
// eslint-disable-next-line no-shadow

View File

@ -0,0 +1,31 @@
import elementLocale from 'element-ui/lib/locale'
const langs = {
en: {
bs: () => import('../../../lang/en/front-end.yml'),
element: () => import('element-ui/lib/locale/lang/en'),
},
zh_CN: {
bs: () => import('../../../lang/zh_CN/front-end.yml'),
element: () => import('element-ui/lib/locale/lang/zh-CN'),
},
}
async function load(language) {
const { bs: loadBS, element: loadElement } = langs[language]
await Promise.all([
(async () => {
const text = await loadBS()
blessing.i18n = Object.assign(blessing.i18n || Object.create(null), text)
})(),
(async () => {
elementLocale.use((await loadElement()).default)
})(),
])
}
export default function () {
return blessing.locale in langs
? load(blessing.locale)
: load(blessing.fallback_locale)
}