2024-06-30 15:42:30 +08:00
|
|
|
import type { SupportedLanguagesType } from '@vben/types';
|
|
|
|
import type { Locale } from 'ant-design-vue/es/locale';
|
|
|
|
|
|
|
|
import { ref } from 'vue';
|
|
|
|
|
2024-07-07 00:17:44 +08:00
|
|
|
import { $t, loadLocalesMap, setupI18n } from '@vben-core/locales';
|
|
|
|
|
2024-07-20 23:01:49 +08:00
|
|
|
import antdEnLocale from 'ant-design-vue/es/locale/en_US';
|
|
|
|
import antdDefaultLocale from 'ant-design-vue/es/locale/zh_CN';
|
2024-06-30 15:42:30 +08:00
|
|
|
import dayjs from 'dayjs';
|
|
|
|
|
2024-07-20 23:01:49 +08:00
|
|
|
const antdLocale = ref<Locale>(antdDefaultLocale);
|
2024-06-30 15:42:30 +08:00
|
|
|
|
2024-07-10 21:20:11 +08:00
|
|
|
const modules = import.meta.glob('./langs/*.json');
|
2024-07-07 00:17:44 +08:00
|
|
|
|
|
|
|
const localesMap = loadLocalesMap(modules);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 加载应用特有的语言包
|
2024-07-10 21:55:16 +08:00
|
|
|
* 这里也可以改造为从服务端获取翻译数据
|
2024-07-07 00:17:44 +08:00
|
|
|
* @param lang
|
|
|
|
*/
|
|
|
|
async function loadMessages(lang: SupportedLanguagesType) {
|
|
|
|
const [appLocaleMessages] = await Promise.all([
|
|
|
|
localesMap[lang](),
|
|
|
|
loadThirdPartyMessage(lang),
|
|
|
|
]);
|
|
|
|
return appLocaleMessages.default;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 加载第三方组件库的语言包
|
|
|
|
* @param lang
|
|
|
|
*/
|
|
|
|
async function loadThirdPartyMessage(lang: SupportedLanguagesType) {
|
|
|
|
await Promise.all([loadAntdLocale(lang), loadDayjsLocale(lang)]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 加载dayjs的语言包
|
|
|
|
* @param lang
|
|
|
|
*/
|
2024-06-30 15:42:30 +08:00
|
|
|
async function loadDayjsLocale(lang: SupportedLanguagesType) {
|
|
|
|
let locale;
|
|
|
|
switch (lang) {
|
|
|
|
case 'zh-CN': {
|
|
|
|
locale = await import('dayjs/locale/zh-cn');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'en-US': {
|
|
|
|
locale = await import('dayjs/locale/en');
|
|
|
|
break;
|
|
|
|
}
|
2024-07-07 00:17:44 +08:00
|
|
|
// 默认使用英语
|
2024-06-30 15:42:30 +08:00
|
|
|
default: {
|
|
|
|
locale = await import('dayjs/locale/en');
|
2024-07-07 00:17:44 +08:00
|
|
|
}
|
2024-06-30 15:42:30 +08:00
|
|
|
}
|
|
|
|
dayjs.locale(locale);
|
|
|
|
}
|
|
|
|
|
2024-07-07 00:17:44 +08:00
|
|
|
/**
|
|
|
|
* 加载antd的语言包
|
|
|
|
* @param lang
|
|
|
|
*/
|
2024-06-30 15:42:30 +08:00
|
|
|
async function loadAntdLocale(lang: SupportedLanguagesType) {
|
|
|
|
switch (lang) {
|
|
|
|
case 'zh-CN': {
|
2024-07-20 23:01:49 +08:00
|
|
|
antdLocale.value = antdDefaultLocale;
|
2024-06-30 15:42:30 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'en-US': {
|
2024-07-20 23:01:49 +08:00
|
|
|
antdLocale.value = antdEnLocale;
|
2024-06-30 15:42:30 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-07 00:17:44 +08:00
|
|
|
export { $t, antdLocale, loadMessages, setupI18n };
|