at-ui
Version:
A UI Component Library with Vue.js
40 lines (31 loc) • 810 B
JavaScript
/**
* Simplified version
* https://github.com/ElemeFE/element/blob/dev/src/locale/index.js
*/
import Vue from 'vue'
import defaultLang from './lang/en-US'
const i18nHandler = function (...args) {
const vuei18n = Object.getPrototypeOf(this || Vue).$t
if (typeof vuei18n === 'function') {
return vuei18n.apply(this, args)
}
}
export function t (path, options) {
const array = path.split('.')
let value = i18nHandler.apply(this, arguments)
let current = defaultLang
if (value !== null && typeof value !== 'undefined') {
return value
}
for (let i = 0, len = array.length; i < len; i++) {
const property = array[i]
value = current[property]
if (i === len - 1) {
return value
} else if (!value) {
return ''
}
current = value
}
return ''
}