element-plus/packages/locale/index.ts
2020-08-04 10:44:00 +08:00

49 lines
1.1 KiB
TypeScript

import defaultLang from './lang/zh-CN'
import Vue from 'vue'
import deepmerge from 'deepmerge'
import Format from './format'
const format = Format(Vue)
let lang = defaultLang
let merged = false
let i18nHandler = function(...args) {
const vuei18n = Object.getPrototypeOf(this || Vue).$t
if (typeof vuei18n === 'function' && !!Vue.locale) {
if (!merged) {
merged = true
Vue.locale(
Vue.config.lang,
deepmerge(lang, Vue.locale(Vue.config.lang) || {}, { clone: true }),
)
}
return vuei18n.apply(this, args)
}
}
export const t = function(path, options, ...args) {
let value = i18nHandler.apply(this, [path, options, args])
if (value !== null && value !== undefined) return value
const array = path.split('.')
let current = lang
for (let i = 0, j = array.length; i < j; i++) {
const property = array[i]
value = current[property]
if (i === j - 1) return format(value, options)
if (!value) return ''
current = value
}
return ''
}
export const use = function(l) {
lang = l || lang
}
export const i18n = function(fn) {
i18nHandler = fn || i18nHandler
}
export default { use, t, i18n }