fix: Update the value of locale (#97)

This commit is contained in:
sven 2024-05-11 16:40:03 +08:00 committed by GitHub
parent 17097a44d7
commit b144aec08c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 20 additions and 5 deletions

View File

@ -9,11 +9,8 @@ import zhCN from 'vant/es/locale/lang/zh-CN'
import messages from '@intlify/unplugin-vue-i18n/messages'
import { Locale, type PickerColumn } from 'vant'
export const i18n = createI18n({
locale: localStorage.getItem('language') || navigator.language,
fallbackLocale: 'zhCN',
messages,
})
/** 默认语言包名称 */
const FALLBACK_LOCALE = 'zh-CN'
/** 多语言 picker columns */
export const languageColumns: PickerColumn = [
@ -21,6 +18,24 @@ export const languageColumns: PickerColumn = [
{ text: 'English', value: 'en-US' },
]
/** 获取当前语言对应的语言包名称 */
function getI18nLocale() {
const locale = localStorage.getItem('language') || navigator.language
for (const l of languageColumns) {
const value = l.value as string
if (value === locale)
return locale // 存在当前语言的语言包
else if (value.indexOf(locale) === 0)
return value // 存在当前语言的任意地区的语言包
}
return FALLBACK_LOCALE // 使用默认语言包
}
export const i18n = createI18n({
locale: getI18nLocale(),
messages,
})
/** 当前语言 */
export const locale = computed({
get() {