提交 | 用户 | age
|
737b1b
|
1 |
/** |
V |
2 |
* Multi-language related operations |
|
3 |
*/ |
4d2fb0
|
4 |
import type { LocaleType } from '#/config'; |
737b1b
|
5 |
|
99ac30
|
6 |
import { i18n } from './setupI18n'; |
4d2fb0
|
7 |
import { useLocaleStoreWithOut } from '@/store/modules/locale'; |
9a1ba7
|
8 |
import { unref, computed } from 'vue'; |
d67772
|
9 |
import { loadLocalePool, setHtmlPageLang } from './helper'; |
6b919c
|
10 |
import { Locale } from 'ant-design-vue/es/locale'; |
6392b7
|
11 |
|
f6cef1
|
12 |
interface LangModule { |
V |
13 |
message: Recordable; |
3fcfac
|
14 |
dateLocale: Recordable; |
V |
15 |
dateLocaleName: string; |
ae3f83
|
16 |
} |
江 |
17 |
|
f6cef1
|
18 |
function setI18nLanguage(locale: LocaleType) { |
215d8b
|
19 |
const localeStore = useLocaleStoreWithOut(); |
V |
20 |
|
f6cef1
|
21 |
if (i18n.mode === 'legacy') { |
V |
22 |
i18n.global.locale = locale; |
|
23 |
} else { |
|
24 |
(i18n.global.locale as any).value = locale; |
|
25 |
} |
|
26 |
localeStore.setLocaleInfo({ locale }); |
d67772
|
27 |
setHtmlPageLang(locale); |
f6cef1
|
28 |
} |
8882d4
|
29 |
|
V |
30 |
export function useLocale() { |
215d8b
|
31 |
const localeStore = useLocaleStoreWithOut(); |
f6cef1
|
32 |
const getLocale = computed(() => localeStore.getLocale); |
V |
33 |
const getShowLocalePicker = computed(() => localeStore.getShowPicker); |
|
34 |
|
3c441a
|
35 |
const getAntdLocale = computed((): any => { |
6b919c
|
36 |
const localeMessage = i18n.global.getLocaleMessage<{ antdLocale: Locale }>(unref(getLocale)); |
IW |
37 |
return localeMessage?.antdLocale ?? {}; |
f6cef1
|
38 |
}); |
737b1b
|
39 |
|
V |
40 |
// Switching the language will change the locale of useI18n |
|
41 |
// And submit to configuration modification |
f6cef1
|
42 |
async function changeLocale(locale: LocaleType) { |
V |
43 |
const globalI18n = i18n.global; |
|
44 |
const currentLocale = unref(globalI18n.locale); |
215d8b
|
45 |
if (currentLocale === locale) { |
V |
46 |
return locale; |
|
47 |
} |
f6cef1
|
48 |
|
V |
49 |
if (loadLocalePool.includes(locale)) { |
|
50 |
setI18nLanguage(locale); |
|
51 |
return locale; |
99ac30
|
52 |
} |
f6cef1
|
53 |
const langModule = ((await import(`./lang/${locale}.ts`)) as any).default as LangModule; |
V |
54 |
if (!langModule) return; |
737b1b
|
55 |
|
3fcfac
|
56 |
const { message } = langModule; |
737b1b
|
57 |
|
f6cef1
|
58 |
globalI18n.setLocaleMessage(locale, message); |
V |
59 |
loadLocalePool.push(locale); |
737b1b
|
60 |
|
f6cef1
|
61 |
setI18nLanguage(locale); |
V |
62 |
return locale; |
8882d4
|
63 |
} |
V |
64 |
|
737b1b
|
65 |
return { |
V |
66 |
getLocale, |
f6cef1
|
67 |
getShowLocalePicker, |
737b1b
|
68 |
changeLocale, |
f6cef1
|
69 |
getAntdLocale, |
737b1b
|
70 |
}; |
V |
71 |
} |