useLocale.ts
1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* Multi-language related operations
*/
import type { LocaleType } from '/@/locales/types';
import type { Ref } from 'vue';
import { unref, ref } from 'vue';
import { useLocaleSetting } from '/@/hooks/setting/useLocaleSetting';
import { i18n } from './setupI18n';
import 'moment/dist/locale/zh-cn';
const antConfigLocaleRef = ref<any>(null);
export function useLocale() {
const { getLang, getLocale, setLocale: setLocalSetting } = useLocaleSetting();
// Switching the language will change the locale of useI18n
// And submit to configuration modification
function changeLocale(lang: LocaleType): void {
if (i18n.mode === 'legacy') {
i18n.global.locale = lang;
} else {
((i18n.global.locale as unknown) as Ref<string>).value = lang;
}
setLocalSetting({ lang });
// i18n.global.setLocaleMessage(locale, messages);
switch (lang) {
// Simplified Chinese
case 'zh_CN':
import('ant-design-vue/es/locale/zh_CN').then((locale) => {
antConfigLocaleRef.value = locale.default;
});
break;
// English
case 'en':
import('ant-design-vue/es/locale/en_US').then((locale) => {
antConfigLocaleRef.value = locale.default;
});
break;
// other
default:
break;
}
}
// initialization
function setLocale() {
const lang = unref(getLang);
lang && changeLocale(lang);
}
return {
setLocale,
getLocale,
getLang,
changeLocale,
antConfigLocale: antConfigLocaleRef,
};
}