fix: update locale mixin

This commit is contained in:
zazzaz 2020-08-03 19:16:13 +08:00 committed by jeremywu
parent 160e92f69e
commit 6d06b83228
3 changed files with 35 additions and 78 deletions

View File

@ -0,0 +1,28 @@
import { mount } from '@vue/test-utils'
import { t, use } from '../index'
import localeMixin from '../mixin'
import zhCn from '../lang/zh-CN'
describe('Locale', () => {
test('t', () => {
expect(t('el.popconfirm.confirmButtonText')).toBe('Yes')
})
test('return key name if not defined', () => {
expect(t('el.popconfirm.someThing')).toBe('someThing')
})
test('use', () => {
use(zhCn)
expect(t('el.popconfirm.confirmButtonText')).toBe('确定')
})
test('mixin', () => {
const component = {
template: `<p>{{ t('el.popconfirm.cancelButtonText') }}</p>`,
mixins: [localeMixin],
}
const wrapper = mount(component)
expect(wrapper.text()).toContain('No')
})
})

View File

@ -1,46 +0,0 @@
import { hasOwn } from 'element-ui/src/utils/util'
const RE_NARGS = /(%|)\{([0-9a-zA-Z_]+)\}/g
/**
* String format template
* - Inspired:
* https://github.com/Matt-Esch/string-template/index.js
*/
export default function(Vue) {
/**
* template
*
* @param {String} string
* @param {Array} ...args
* @return {String}
*/
function template(string, ...args) {
if (args.length === 1 && typeof args[0] === 'object') {
args = args[0]
}
if (!args || !args.hasOwnProperty) {
args = {}
}
return string.replace(RE_NARGS, (match, prefix, i, index) => {
let result
if (string[index - 1] === '{' &&
string[index + match.length] === '}') {
return i
} else {
result = hasOwn(args, i) ? args[i] : null
if (result === null || result === undefined) {
return ''
}
return result
}
})
}
return template
}

View File

@ -1,48 +1,23 @@
import defaultLang from './lang/zh-CN'
import Vue from 'vue'
import deepmerge from 'deepmerge'
import Format from './format'
import defaultLang from './lang/en'
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
export const t = (path:string): string => {
let 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)
value = current[property] || property
if (i === j - 1) return value
if (!value) return ''
current = value
}
return ''
}
export const use = function(l) {
export const use = (l): void => {
lang = l || lang
}
export const i18n = function(fn) {
i18nHandler = fn || i18nHandler
}
export default { use, t, i18n }
export default { use, t }