Files
blivechat/frontend/src/i18n.js
2023-10-08 22:55:12 +08:00

47 lines
1022 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import VueI18n from 'vue-i18n'
import zh from '@/lang/zh'
let lastSetLocale = 'zh'
let loadedLocales = ['zh']
export async function setLocale(locale) {
lastSetLocale = locale
if (loadedLocales.indexOf(locale) === -1) {
// eslint-disable-next-line prefer-template
let langModule = await import('@/lang/' + locale)
i18n.setLocaleMessage(locale, langModule.default)
loadedLocales.push(locale)
// 加载完成之前又调用了setLocale这次的不生效
if (locale !== lastSetLocale) {
return
}
}
window.localStorage.lang = i18n.locale = locale
}
export const i18n = new VueI18n({
locale: 'zh',
fallbackLocale: 'zh',
messages: {
zh
}
})
function getDefaultLocale() {
let locale = window.localStorage.lang
if (!locale) {
let lang = navigator.language
if (lang.startsWith('zh')) {
locale = 'zh'
} else if (lang.startsWith('ja')) {
locale = 'ja'
} else {
locale = 'en'
}
}
return locale
}
setLocale(getDefaultLocale())