import Vue from 'vue'; import VueI18n from 'vue-i18n'; import store from '../store'; import { getToken } from '../utils/auth'; import { getStaticContent } from '@/api/ajax'; import ElementLocale from 'element-ui/lib/locale'; import zhLocal from 'element-ui/lib/locale/lang/zh-CN'; import arLocal from 'element-ui/lib/locale/lang/ar'; import enLocal from 'element-ui/lib/locale/lang/en'; import jaLocal from 'element-ui/lib/locale/lang/ja'; import deLocal from 'element-ui/lib/locale/lang/de'; import ruLocal from 'element-ui/lib/locale/lang/ru-RU'; Vue.use(VueI18n); let locale = 'ZH'; if (getToken()) { const user = JSON.parse(getToken()); let language_type = user.language_type; locale = language_type || localStorage.getItem('language_type') || "ZH"; } else { locale = localStorage.getItem('language_type') || "ZH"; } const i18n = new VueI18n({ locale: locale,//store.getters.language_type, messages: { ZH: { ...zhLocal }, AR: { ...arLocal }, EN: { ...enLocal }, JA: { ...jaLocal }, DE: { ...deLocal }, RU: { ...ruLocal } }, silentTranslationWarn: true }); ElementLocale.i18n((key, value) => i18n.t(key, value)); export async function setI18nLang(language_type) { await store.dispatch('lang/updateLanguageType', language_type); } /** * @description 更新语言列表 * @param {Object} Parameter word_key_list 需要读取的词汇 */ export function updateWordPack(Parameter) { return new Promise((resolve, reject) => { Parameter.language_type = store.getters.language_type; let MethodName = 'language_manager-GetWordPack'; getStaticContent(MethodName, Parameter).then(data => { let localWord = i18n.messages[data.language_type]; if (localWord === undefined) { localWord = {}; } let wordPack = Object.assign(localWord, data.word_pack); i18n.setLocaleMessage(data.language_type, wordPack); resolve() }).catch(err => { reject(err) }); }) } export default i18n;