2020-11-23 00:35:15 +08:00
|
|
|
/**
|
|
|
|
|
* Multi-language related operations
|
|
|
|
|
*/
|
2020-11-19 23:01:27 +08:00
|
|
|
import type { LocaleType } from '/@/locales/types';
|
2020-11-23 00:35:15 +08:00
|
|
|
|
|
|
|
|
import { unref, ref } from 'vue';
|
|
|
|
|
|
|
|
|
|
import { getI18n } from '/@/setup/i18n';
|
|
|
|
|
|
2020-11-23 23:24:13 +08:00
|
|
|
import { useLocaleSetting } from '/@/hooks/setting/useLocaleSetting';
|
2020-11-23 00:35:15 +08:00
|
|
|
|
|
|
|
|
import moment from 'moment';
|
|
|
|
|
|
|
|
|
|
import 'moment/dist/locale/zh-cn';
|
|
|
|
|
|
|
|
|
|
moment.locale('zh-cn');
|
|
|
|
|
|
|
|
|
|
const antConfigLocaleRef = ref<any>(null);
|
2020-11-19 23:01:27 +08:00
|
|
|
|
|
|
|
|
export function useLocale() {
|
2020-11-23 00:35:15 +08:00
|
|
|
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 {
|
|
|
|
|
(getI18n().global.locale as any).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;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
moment.locale('cn');
|
|
|
|
|
break;
|
|
|
|
|
// English
|
|
|
|
|
case 'en':
|
|
|
|
|
import('ant-design-vue/es/locale/en_US').then((locale) => {
|
|
|
|
|
antConfigLocaleRef.value = locale.default;
|
|
|
|
|
});
|
|
|
|
|
moment.locale('en-us');
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
// other
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
2020-11-19 23:01:27 +08:00
|
|
|
}
|
|
|
|
|
|
2020-11-23 00:35:15 +08:00
|
|
|
// initialization
|
|
|
|
|
function setupLocale() {
|
|
|
|
|
const lang = unref(getLang);
|
|
|
|
|
lang && changeLocale(lang);
|
2020-11-19 23:01:27 +08:00
|
|
|
}
|
|
|
|
|
|
2020-11-23 00:35:15 +08:00
|
|
|
return {
|
|
|
|
|
setupLocale,
|
|
|
|
|
getLocale,
|
|
|
|
|
getLang,
|
|
|
|
|
changeLocale,
|
|
|
|
|
antConfigLocale: antConfigLocaleRef,
|
|
|
|
|
};
|
|
|
|
|
}
|