去掉vue-i18n包,换成更简单的实现。
parent
8eb136174d
commit
fac7e43faf
|
@ -16,7 +16,6 @@
|
|||
"element-ui": "^2.15.1",
|
||||
"file-saver": "^2.0.5",
|
||||
"vue": "^2.6.11",
|
||||
"vue-i18n": "^8.24.5",
|
||||
"vue2-editor": "^2.10.2",
|
||||
"vuedraggable": "^2.24.3"
|
||||
},
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import Vue from 'vue'
|
||||
import VueI18n from 'vue-i18n'
|
||||
import si18n from './smart-vue-i18n/index'
|
||||
|
||||
import enLocaleElement from "element-ui/lib/locale/lang/en";
|
||||
import zhLocaleElement from "element-ui/lib/locale/lang/zh-CN";
|
||||
|
@ -34,37 +34,32 @@ const langResources = {
|
|||
}
|
||||
}
|
||||
|
||||
Vue.use(VueI18n)
|
||||
|
||||
const i18n = new VueI18n({
|
||||
locale: localStorage.getItem('v_form_locale') || 'zh-CN',
|
||||
//locale: 'en-US',
|
||||
messages: langResources,
|
||||
Vue.use(si18n, {
|
||||
lang: localStorage.getItem('v_form_locale') || 'zh-CN',
|
||||
messages: langResources
|
||||
})
|
||||
|
||||
locale.i18n((key, value) => i18n.t(key, value))
|
||||
locale.i18n((key, value) => Vue.prototype.$st(key))
|
||||
|
||||
export const changeLocale = function(langName) {
|
||||
i18n.locale = langName
|
||||
Vue.prototype.$si18n.setLang(langName)
|
||||
localStorage.setItem('v_form_locale', langName)
|
||||
}
|
||||
|
||||
export const translate = function(key) {
|
||||
return i18n._t(key, i18n.locale, i18n._getMessages())
|
||||
return Vue.prototype.$st(key)
|
||||
}
|
||||
|
||||
export default {
|
||||
methods: {
|
||||
i18nt(key) {
|
||||
return i18n._t(key, i18n.locale, i18n._getMessages())
|
||||
return this.$st(key)
|
||||
},
|
||||
|
||||
/* 如果key1不存在,则查找key2 */
|
||||
i18n2t(key1, key2) {
|
||||
return i18n.te(key1) ? i18n._t(key1, i18n.locale, i18n._getMessages()) :
|
||||
i18n._t(key2, i18n.locale, i18n._getMessages())
|
||||
return this.$st2(key1, key2)
|
||||
},
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,56 @@
|
|||
import {deepAssign, get} from './utils'
|
||||
|
||||
const install = (Vue, options) => {
|
||||
const proto = Vue.prototype
|
||||
proto.$si18n = proto.$si18n || {}
|
||||
// 初始化多语言
|
||||
deepAssign(proto.$si18n, options)
|
||||
|
||||
const _vm = new Vue({
|
||||
data: options
|
||||
})
|
||||
Object.defineProperty(Vue.prototype.$si18n, 'lang', {
|
||||
get() {
|
||||
return _vm.lang
|
||||
}
|
||||
})
|
||||
|
||||
proto.$st = (path, ...args) => {
|
||||
let messages = _vm.messages[_vm.lang]
|
||||
if (!proto.$si18n.messages) {
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
console.error('[yxI18n] Locale not correctly registered')
|
||||
}
|
||||
return () => path
|
||||
}
|
||||
//const message = get(messages, path) || get(messages, path)
|
||||
const message = get(messages, path)
|
||||
return typeof message === 'function'
|
||||
? message(...args)
|
||||
: message || path
|
||||
}
|
||||
|
||||
proto.$st2 = (path, path2) => {
|
||||
let messages = _vm.messages[_vm.lang]
|
||||
const message = get(messages, path)
|
||||
return (message !== '') ? message : get(messages, path2)
|
||||
}
|
||||
|
||||
proto.$si18n.add = (messages = {}) => {
|
||||
deepAssign(proto.$si18n.messages, messages)
|
||||
}
|
||||
|
||||
proto.$si18n.setLang = lang => {
|
||||
_vm.lang = lang
|
||||
}
|
||||
|
||||
Vue.mixin({
|
||||
beforeCreate() {
|
||||
this.$options.i18n && this.$si18n.add(this.$options.i18n)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export default {
|
||||
install
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
// 值存在
|
||||
export function isDef(value) {
|
||||
return value !== undefined && value !== null
|
||||
}
|
||||
|
||||
// 对象映射 'a.b' {a: {b: 'val'}}
|
||||
export function get(object, path) {
|
||||
const keys = path.split('.')
|
||||
let result = object
|
||||
|
||||
keys.forEach(key => {
|
||||
result = isDef(result[key]) ? result[key] : ''
|
||||
})
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// 是否是对象
|
||||
export function isObj(x) {
|
||||
const type = typeof x
|
||||
return x !== null && (type === 'object' || type === 'function')
|
||||
}
|
||||
|
||||
// 深拷贝
|
||||
const { hasOwnProperty } = Object.prototype
|
||||
|
||||
function assignKey(to, from, key) {
|
||||
const val = from[key]
|
||||
|
||||
if (!isDef(val)) {
|
||||
return
|
||||
}
|
||||
|
||||
if (!hasOwnProperty.call(to, key) || !isObj(val)) {
|
||||
to[key] = val
|
||||
} else {
|
||||
to[key] = deepAssign(Object(to[key]), from[key])
|
||||
}
|
||||
}
|
||||
|
||||
export function deepAssign(to, from) {
|
||||
Object.keys(from).forEach(key => {
|
||||
assignKey(to, from, key)
|
||||
})
|
||||
|
||||
return to
|
||||
}
|
Loading…
Reference in New Issue