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 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
}

View File

@ -25,7 +25,6 @@ export default {
return {
langCookeName: 'django_language', // DjangoCOOKIE 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(() => {

View File

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