import {getMessageInfo} from '@/i18n'
async created() { const lang = getLanguage() console.log('当前系统语言:', lang) const {defaultLangObj, msgObj} = await this.loadSettingByLang(lang) this.$i18n.setLocaleMessage(lang, { ...defaultLangObj, ...msgObj }) }, methods: { async loadSettingByLang(lang = 'zh') { const msgObjStr = localStorage.getItem('local-data-mag') let msgObj if (msgObjStr) { msgObj = JSON.parse(msgObjStr) } else { const msgObjRes = await getMessageInfo(lang) msgObj = this.generateMsgObj(msgObjRes) } const defaultLangObj = DEFAULT_LOCALE_MSG[lang] return {defaultLangObj, msgObj} }, generateMsgObj(messageArray = {}) { localStorage.setItem('local-data-mag', JSON.stringify(messageArray)) return allMsgInfo }, clearLanguageCache() { localStorage.removeItem('local-data-mag') } }
|