Skip to content

Commit 9a0062c

Browse files
committed
feat: Lazy loading the localization files
1 parent e0ac6be commit 9a0062c

File tree

1 file changed

+39
-27
lines changed

1 file changed

+39
-27
lines changed

src/utils/i18n.ts

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
import { createI18n } from 'vue-i18n'
22
import enUS from 'vant/es/locale/lang/en-US'
33
import zhCN from 'vant/es/locale/lang/zh-CN'
4-
5-
/**
6-
* All i18n resources specified in the plugin `include` option can be loaded
7-
* at once using the import syntax
8-
*/
9-
import messages from '@intlify/unplugin-vue-i18n/messages'
104
import { Locale, type PickerColumn } from 'vant'
115

126
/** 默认语言包名称 */
@@ -18,34 +12,16 @@ export const languageColumns: PickerColumn = [
1812
{ text: 'English', value: 'en-US' },
1913
]
2014

21-
/** 获取当前语言对应的语言包名称 */
22-
function getI18nLocale() {
23-
const storedLocale = localStorage.getItem('language') || navigator.language
24-
25-
const langs = languageColumns.map(v => v.value as string)
26-
const foundLocale = langs.find(v => v === storedLocale || v.indexOf(storedLocale) === 0) // 存在当前语言的语言包 或 存在当前语言的任意地区的语言包
27-
const locale = foundLocale || FALLBACK_LOCALE // 若未找到,则使用 默认语言包
28-
29-
document.querySelector('html').setAttribute('lang', locale)
30-
return locale
31-
}
32-
33-
export const i18n = createI18n({
34-
locale: getI18nLocale(),
35-
legacy: false,
36-
messages,
37-
})
15+
export const i18n = setupI18n()
16+
type I18n = typeof i18n
3817

3918
/** 当前语言 */
4019
export const locale = computed({
4120
get() {
4221
return i18n.global.locale.value
4322
},
4423
set(language: string) {
45-
document.querySelector('html').setAttribute('lang', language)
46-
localStorage.setItem('language', language)
47-
i18n.global.locale.value = language
48-
Locale.use(language)
24+
setLang(language, i18n)
4925
},
5026
})
5127

@@ -55,3 +31,39 @@ Locale.use('en-US', enUS)
5531

5632
// 根据当前语言切换 vant 语言包
5733
Locale.use(locale.value)
34+
35+
/** 初始化 i18n */
36+
function setupI18n() {
37+
const locale = getI18nLocale()
38+
const i18n = createI18n({
39+
locale,
40+
legacy: false,
41+
})
42+
setLang(locale, i18n)
43+
return i18n
44+
}
45+
46+
async function setLang(lang: string, i18n: I18n) {
47+
await loadLocaleMsg(lang, i18n)
48+
document.querySelector('html').setAttribute('lang', lang)
49+
localStorage.setItem('language', lang)
50+
i18n.global.locale.value = lang
51+
Locale.use(lang)
52+
}
53+
54+
/** 加载语言包 */
55+
async function loadLocaleMsg(locale: string, i18n: I18n) {
56+
const messages = await import(`../locales/${locale}.json`)
57+
i18n.global.setLocaleMessage(locale, messages.default)
58+
}
59+
60+
/** 获取当前语言对应的语言包名称 */
61+
function getI18nLocale() {
62+
const storedLocale = localStorage.getItem('language') || navigator.language
63+
64+
const langs = languageColumns.map(v => v.value as string)
65+
const foundLocale = langs.find(v => v === storedLocale || v.indexOf(storedLocale) === 0) // 存在当前语言的语言包 或 存在当前语言的任意地区的语言包
66+
const locale = foundLocale || FALLBACK_LOCALE // 若未找到,则使用 默认语言包
67+
68+
return locale
69+
}

0 commit comments

Comments
 (0)