uiv
Version:
Bootstrap 3 components implemented by Vue 2.
56 lines (48 loc) • 1.27 kB
JavaScript
// https://github.com/ElemeFE/element/blob/dev/src/locale/index.js
import { isFunction, isExist } from '../utils/object.utils'
import defaultLang from './lang/en-US'
let lang = defaultLang
let i18nHandler = function () {
const vuei18n = Object.getPrototypeOf(this).$t
/* istanbul ignore else */
/* istanbul ignore next */
if (isFunction(vuei18n)) {
/* istanbul ignore next */
try {
return vuei18n.apply(this, arguments)
} catch (err) {
return this.$t.apply(this, arguments)
}
}
}
export const t = function (path, options) {
options = options || {}
let value
try {
value = i18nHandler.apply(this, arguments)
/* istanbul ignore next */
if (isExist(value) && !options.$$locale) {
return value
}
} catch (e) {
// ignore
}
const array = path.split('.')
let current = options.$$locale || lang
for (let i = 0, j = array.length; i < j; i++) {
const property = array[i]
value = current[property]
if (i === j - 1) return value
if (!value) return ''
current = value
}
/* istanbul ignore next */
return ''
}
export const use = function (l) {
lang = l || lang
}
export const i18n = function (fn) {
i18nHandler = fn || i18nHandler
}
export default { use, t, i18n }