@@ -9,18 +9,33 @@ import zhCN from 'vant/es/locale/lang/zh-CN'
9
9
import messages from '@intlify/unplugin-vue-i18n/messages'
10
10
import { Locale , type PickerColumn } from 'vant'
11
11
12
- export const i18n = createI18n ( {
13
- locale : localStorage . getItem ( 'language' ) || navigator . language ,
14
- fallbackLocale : 'zhCN' ,
15
- messages,
16
- } )
12
+ /** 默认语言包名称 */
13
+ const FALLBACK_LOCALE = 'zh-CN'
17
14
18
15
/** 多语言 picker columns */
19
16
export const languageColumns : PickerColumn = [
20
17
{ text : '简体中文' , value : 'zh-CN' } ,
21
18
{ text : 'English' , value : 'en-US' } ,
22
19
]
23
20
21
+ /** 获取当前语言对应的语言包名称 */
22
+ function getI18nLocale ( ) {
23
+ const locale = localStorage . getItem ( 'language' ) || navigator . language
24
+ for ( const l of languageColumns ) {
25
+ const value = l . value as string
26
+ if ( value === locale )
27
+ return locale // 存在当前语言的语言包
28
+ else if ( value . indexOf ( locale ) === 0 )
29
+ return value // 存在当前语言的任意地区的语言包
30
+ }
31
+ return FALLBACK_LOCALE // 使用默认语言包
32
+ }
33
+
34
+ export const i18n = createI18n ( {
35
+ locale : getI18nLocale ( ) ,
36
+ messages,
37
+ } )
38
+
24
39
/** 当前语言 */
25
40
export const locale = computed ( {
26
41
get ( ) {
0 commit comments