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