i18n.js 1.64 KB
import Vue from 'vue'
import locale from 'element-ui/lib/locale'
import VueI18n from 'vue-i18n'
import axios from 'axios';
import $ from 'jquery';
/*******************************/
import cnLocale from 'element-ui/lib/locale/lang/zh-CN';
import usLocale from 'element-ui/lib/locale/lang/en';
import twLocale from 'element-ui/lib/locale/lang/zh-TW'
const uiLocale = {
    cn: cnLocale,
    us: usLocale,
    tw: twLocale
};
/*******************************/
Vue.use(VueI18n)

const i18n = new VueI18n({

})
var lang = ['zh_CN', 'en_US', 'zh_TW'];

function loadI18nMessages(lang) {
    let langUrl;
    let languageUrl = window._vionConfig.languageUrl;
    if (languageUrl && languageUrl == 'store') {
        langUrl = `https://store.keliuyun.com/report/i18n/get/${lang}.json`;
    } else if (languageUrl && languageUrl == 'mall') {
        langUrl = `https://mall.keliuyun.com/report/i18n/get/${lang}.json`;
    } else if (languageUrl && languageUrl == 'static') {
        langUrl = `static/langs/${lang}.json`;
    } else {
        langUrl = `${window._vionConfig.apiUrl}/i18n/get/${lang}.json`;
    }
    return new Promise((resolve, reject) => {
        $.getJSON(langUrl, function(msg) {
            let local = lang.split('_')[1].toLowerCase();
            let msgObj = Object.assign({}, msg, uiLocale[local]);
            i18n.setLocaleMessage(lang, msgObj)
            i18n.locale = localStorage.getItem('lang') || 'zh_CN'
            resolve()
        })
    })
}
lang.forEach((item, index) => {
    loadI18nMessages(item).then(() => {
        if (item == 'zh_CN') {
            locale.i18n((key, value) => i18n.t(key, value))
        }

    })
})
export default i18n