修复i18n跟element-ui内置多语言功能冲突的问题。
parent
aff51d7ebf
commit
a0593dfb5b
|
@ -34,12 +34,44 @@ const langResources = {
|
|||
}
|
||||
}
|
||||
|
||||
// ********************* 下述代码参考element-ui/lib/locale/format.js begin *****************//
|
||||
|
||||
const RE_NARGS = /(%|)\{([0-9a-zA-Z_]+)\}/g;
|
||||
function hasOwn(obj, key) {
|
||||
return Object.prototype.hasOwnProperty.call(obj, key);
|
||||
}
|
||||
|
||||
const elLocalFormatter = function template(string, 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;
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// ********************* 下述代码参考element-ui/lib/locale/format.js end ******************//
|
||||
|
||||
Vue.use(si18n, {
|
||||
lang: localStorage.getItem('v_form_locale') || 'zh-CN',
|
||||
messages: langResources
|
||||
})
|
||||
|
||||
locale.i18n((key, value) => Vue.prototype.$st(key))
|
||||
locale.i18n((key, value) => {
|
||||
let result = Vue.prototype.$st(key)
|
||||
//console.log('test-key', key)
|
||||
//console.log('test-result', result)
|
||||
return elLocalFormatter(result, value)
|
||||
})
|
||||
|
||||
export const changeLocale = function(langName) {
|
||||
Vue.prototype.$si18n.setLang(langName)
|
||||
|
|
|
@ -27,13 +27,13 @@ const install = (Vue, options) => {
|
|||
const message = get(messages, path)
|
||||
return typeof message === 'function'
|
||||
? message(...args)
|
||||
: message || path
|
||||
: (message !== null ? message : path)
|
||||
}
|
||||
|
||||
proto.$st2 = (path, path2) => {
|
||||
let messages = _vm.messages[_vm.lang]
|
||||
const message = get(messages, path)
|
||||
return (message !== '') ? message : get(messages, path2)
|
||||
return (message !== null) ? message : get(messages, path2)
|
||||
}
|
||||
|
||||
proto.$si18n.add = (messages = {}) => {
|
||||
|
|
|
@ -9,7 +9,7 @@ export function get(object, path) {
|
|||
let result = object
|
||||
|
||||
keys.forEach(key => {
|
||||
result = isDef(result[key]) ? result[key] : ''
|
||||
result = isDef(result) && isDef(result[key]) ? result[key] : null
|
||||
})
|
||||
|
||||
return result
|
||||
|
@ -44,4 +44,4 @@ export function deepAssign(to, from) {
|
|||
})
|
||||
|
||||
return to
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue