perf: languages settings

This commit is contained in:
Bai 2024-12-03 16:20:08 +08:00 committed by Bryan
parent 0342c73f07
commit b53b1d1e58
3 changed files with 18 additions and 34 deletions

View File

@ -1,12 +1,17 @@
import VueCookie from 'vue-cookie' import VueCookie from 'vue-cookie'
import store from '@/store'
export function getLangCode() { export function getLangCode(withInternalCode = false) {
const cookieLang = VueCookie.get('django_language') const cookieLang = VueCookie.get('django_language')
const browserLang = navigator.systemLanguage || navigator.language || navigator.userLanguage let lang = cookieLang || navigator.language.toLowerCase()
let lang = cookieLang || browserLang || 'en' if (withInternalCode) {
lang = lang.replace('-', '_') const languages = store.getters.publicSettings['LANGUAGES']
if (lang !== 'zh_hant') { for (const langObj of languages) {
lang = lang.slice(0, 2) if (langObj['other_codes'].indexOf(lang) > -1) {
lang = langObj['code']
break
}
}
} }
return lang return lang
} }

View File

@ -25,7 +25,6 @@ export default {
return { return {
langCookeName: 'django_language', // DjangoCOOKIE KEY langCookeName: 'django_language', // DjangoCOOKIE KEY
supportLanguages: [], supportLanguages: [],
currentLangCode: '',
defaultLang: { defaultLang: {
title: 'English', title: 'English',
code: 'en', code: 'en',
@ -41,45 +40,25 @@ export default {
}, {}) }, {})
}, },
currentLang() { currentLang() {
const lang = this.supportedLangMapper[this.currentLangCode] || this.defaultLang const lang = getLangCode(true)
return lang return this.supportedLangMapper[lang] || this.defaultLang
} }
}, },
mounted() { mounted() {
this.currentLangCode = getLangCode()
this.supportLanguages = store.getters.publicSettings['LANGUAGES'].map(item => { this.supportLanguages = store.getters.publicSettings['LANGUAGES'].map(item => {
let code = item.code.replace('-', '_')
if (code !== 'zh_hant') {
code = code.slice(0, 2)
}
return { return {
title: item.name, title: item.name,
code: code, code: item.code,
cookieCode: item.code cookieCode: item.code
} }
}) })
this.changeMomentLang() this.changeMomentLang()
}, },
methods: { methods: {
changeLang() {
if (this.currentLang.code !== this.$i18n.locale) {
this.changeLangTo(this.currentLang)
}
},
changeMomentLang() { changeMomentLang() {
if (this.currentLang.code.indexOf('en') > -1) { const lang = getLangCode()
this.$moment.locale('en') this.$moment.locale(lang)
document.documentElement.lang = 'en' document.documentElement.lang = lang
} 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'
}
}, },
changeLangTo(item) { changeLangTo(item) {
this.$axios.get(`/core/i18n/${item.cookieCode}/`).then(() => { this.$axios.get(`/core/i18n/${item.cookieCode}/`).then(() => {

View File

@ -9,7 +9,7 @@ function getTimeUnits(u) {
'm': '分', 'm': '分',
's': '秒' 's': '秒'
} }
if (getLangCode() === 'zh') { if (getLangCode(true) === 'zh-hans') {
return units[u] return units[u]
} else { } else {
return u return u