diff --git a/src/i18n/utils.js b/src/i18n/utils.js index cd5e4c245..443b0765f 100644 --- a/src/i18n/utils.js +++ b/src/i18n/utils.js @@ -1,12 +1,17 @@ import VueCookie from 'vue-cookie' +import store from '@/store' -export function getLangCode() { +export function getLangCode(withInternalCode = false) { const cookieLang = VueCookie.get('django_language') - const browserLang = navigator.systemLanguage || navigator.language || navigator.userLanguage - let lang = cookieLang || browserLang || 'en' - lang = lang.replace('-', '_') - if (lang !== 'zh_hant') { - lang = lang.slice(0, 2) + let lang = cookieLang || navigator.language.toLowerCase() + if (withInternalCode) { + const languages = store.getters.publicSettings['LANGUAGES'] + for (const langObj of languages) { + if (langObj['other_codes'].indexOf(lang) > -1) { + lang = langObj['code'] + break + } + } } return lang } diff --git a/src/layout/components/NavHeader/Language.vue b/src/layout/components/NavHeader/Language.vue index 72f12f223..88a7463b6 100644 --- a/src/layout/components/NavHeader/Language.vue +++ b/src/layout/components/NavHeader/Language.vue @@ -25,7 +25,6 @@ export default { return { langCookeName: 'django_language', // 后端Django需要的COOKIE KEY supportLanguages: [], - currentLangCode: '', defaultLang: { title: 'English', code: 'en', @@ -41,45 +40,25 @@ export default { }, {}) }, currentLang() { - const lang = this.supportedLangMapper[this.currentLangCode] || this.defaultLang - return lang + const lang = getLangCode(true) + return this.supportedLangMapper[lang] || this.defaultLang } }, mounted() { - this.currentLangCode = getLangCode() this.supportLanguages = store.getters.publicSettings['LANGUAGES'].map(item => { - let code = item.code.replace('-', '_') - if (code !== 'zh_hant') { - code = code.slice(0, 2) - } return { title: item.name, - code: code, + code: item.code, cookieCode: item.code } }) this.changeMomentLang() }, methods: { - changeLang() { - if (this.currentLang.code !== this.$i18n.locale) { - this.changeLangTo(this.currentLang) - } - }, changeMomentLang() { - if (this.currentLang.code.indexOf('en') > -1) { - this.$moment.locale('en') - document.documentElement.lang = 'en' - } else if (this.currentLang.code.indexOf('ja') > -1) { - this.$moment.locale('ja') - document.documentElement.lang = 'ja' - } else if (this.currentLang.code.indexOf('zh_hant') > -1) { - this.$moment.locale('zh-tw') - document.documentElement.lang = 'zh-tw' - } else { - this.$moment.locale('zh-cn') - document.documentElement.lang = 'zh-cn' - } + const lang = getLangCode() + this.$moment.locale(lang) + document.documentElement.lang = lang }, changeLangTo(item) { this.$axios.get(`/core/i18n/${item.cookieCode}/`).then(() => { diff --git a/src/utils/time.js b/src/utils/time.js index 21f851ffb..efe21d90e 100644 --- a/src/utils/time.js +++ b/src/utils/time.js @@ -9,7 +9,7 @@ function getTimeUnits(u) { 'm': '分', 's': '秒' } - if (getLangCode() === 'zh') { + if (getLangCode(true) === 'zh-hans') { return units[u] } else { return u