perf: lang setting from core

This commit is contained in:
ibuler
2024-06-28 16:38:13 +08:00
committed by 老广
parent 32585e4abc
commit ada6479177
6 changed files with 45 additions and 55 deletions

View File

@@ -13,7 +13,7 @@ export default {
hour: 'numeric', minute: 'numeric' hour: 'numeric', minute: 'numeric'
} }
}, },
'cn': { 'zh': {
short: { short: {
year: 'numeric', month: 'short', day: 'numeric' year: 'numeric', month: 'short', day: 'numeric'
}, },

View File

@@ -4,19 +4,13 @@ import locale from 'element-ui/lib/locale'
import VueI18n from 'vue-i18n' import VueI18n from 'vue-i18n'
import messages from './langs' import messages from './langs'
import date from './date' import date from './date'
import VueCookie from 'vue-cookie'
import axios from 'axios' import axios from 'axios'
import store from '@/store' import store from '@/store'
import { getLangCode } from './utils'
Vue.use(VueI18n) Vue.use(VueI18n)
const cookieLang = VueCookie.get('django_language') const lang = getLangCode()
const browserLang = navigator.systemLanguage || navigator.language || navigator.userLanguage
let lang = cookieLang || browserLang || 'en'
if (lang === 'zh-hant') {
lang = 'zh_hant'
} else {
lang = lang.slice(0, 2)
}
const i18n = new VueI18n({ const i18n = new VueI18n({
locale: lang, locale: lang,
fallbackLocale: 'en', fallbackLocale: 'en',

View File

@@ -3,7 +3,7 @@ import zhTWLocale from 'element-ui/lib/locale/lang/zh-TW'
import enLocale from 'element-ui/lib/locale/lang/en' import enLocale from 'element-ui/lib/locale/lang/en'
import jaLocale from 'element-ui/lib/locale/lang/ja' import jaLocale from 'element-ui/lib/locale/lang/ja'
import zh from './zh.json' import zh from './zh.json'
import zhHant from './zh_Hant.json' import zhHant from './zh_hant.json'
import en from './en.json' import en from './en.json'
import ja from './ja.json' import ja from './ja.json'

13
src/i18n/utils.js Normal file
View File

@@ -0,0 +1,13 @@
import VueCookie from 'vue-cookie'
export function getLangCode() {
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)
}
return lang
}

View File

@@ -16,33 +16,21 @@
</template> </template>
<script> <script>
import { getLangCode } from '@/i18n/utils'
import store from '@/store'
export default { export default {
name: 'Language', name: 'Language',
data() { data() {
return { return {
LANG_COOKIE_NAME: 'django_language', // 后端Django需要的COOKIE KEY langCookeName: 'django_language', // 后端Django需要的COOKIE KEY
supportLanguages: [ supportLanguages: [],
{ currentLangCode: '',
title: '中文(简体)', defaultLang: {
code: 'cn',
cookieCode: 'zh-hans' // cookie code是为了让后端知道当前语言
},
{
title: '中文(繁體)',
code: 'zh_hant',
cookieCode: 'zh-hant' // cookie code是为了让后端知道当前语言
},
{
title: 'English', title: 'English',
code: 'en', code: 'en',
cookieCode: 'en' cookieCode: 'en'
},
{
title: '日本語',
code: 'ja',
cookieCode: 'ja' // cookie code是为了让后端知道当前语言
} }
]
} }
}, },
computed: { computed: {
@@ -50,18 +38,26 @@ export default {
return this.supportLanguages.reduce((map, obj) => { return this.supportLanguages.reduce((map, obj) => {
map[obj.code] = obj map[obj.code] = obj
return map return map
}) }, {})
}, },
currentLang() { currentLang() {
const langCode = this.getLangCode() const lang = this.supportedLangMapper[this.currentLangCode] || this.defaultLang
let lang = this.supportedLangMapper[langCode]
if (!lang) {
lang = this.supportLanguages[0]
}
return lang return lang
} }
}, },
mounted() { 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,
cookieCode: item.code
}
})
this.changeMomentLang() this.changeMomentLang()
}, },
methods: { methods: {
@@ -83,21 +79,8 @@ export default {
}, },
changeLangTo(item) { changeLangTo(item) {
this.$i18n.locale = item.code this.$i18n.locale = item.code
this.$cookie.set(this.LANG_COOKIE_NAME, item.cookieCode, { expires: 365 }) this.$cookie.set(this.langCookeName, item.cookieCode, { expires: 365 })
window.location.reload() window.location.reload()
},
getLangCode() {
let langCode = this.$cookie.get(this.LANG_COOKIE_NAME)
if (!langCode) {
langCode = navigator.systemLanguage || navigator.language || navigator.userLanguage
}
if (langCode === 'zh-hant') {
langCode = 'zh_hant'
} else {
langCode = langCode.slice(0, 2)
langCode = langCode.replace('zh', 'cn')
}
return langCode
} }
} }
} }

View File

@@ -1,5 +1,5 @@
import store from '@/store'
import { constantRoutes } from '@/router' import { constantRoutes } from '@/router'
import store from '@/store'
import { openWindow } from './common' import { openWindow } from './common'
export function openTaskPage(taskId, taskType, taskUrl) { export function openTaskPage(taskId, taskType, taskUrl) {