feat: Support dayjs and component library in multiple languages

This commit is contained in:
vben
2024-06-30 15:42:30 +08:00
parent ca1cad0cd3
commit 1d70d71537
15 changed files with 99 additions and 20 deletions

View File

@@ -0,0 +1,11 @@
# forward
用于扩展、转发大仓的包以及其他功能方便在app内自定义不同的逻辑
## request
用于扩展请求的功能例如添加header、错误响应等
## locale
用于扩展国际化的功能,例如扩展 dayjs、antd组件库的多语言切换

View File

@@ -1 +1,2 @@
export * from './locale';
export * from './request';

View File

@@ -0,0 +1,48 @@
import type { SupportedLanguagesType } from '@vben/types';
import type { Locale } from 'ant-design-vue/es/locale';
import { ref } from 'vue';
import defaultLocale from 'ant-design-vue/es/locale/zh_CN';
import dayjs from 'dayjs';
const antdLocale = ref<Locale>(defaultLocale);
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;
}
default: {
locale = await import('dayjs/locale/en');
} // 默认使用英语
}
dayjs.locale(locale);
}
async function loadAntdLocale(lang: SupportedLanguagesType) {
switch (lang) {
case 'zh-CN': {
antdLocale.value = defaultLocale;
break;
}
case 'en-US': {
antdLocale.value = (await import(
'ant-design-vue/es/locale/en_US'
)) as unknown as Locale;
break;
}
}
}
async function loadThirdPartyMessage(land: SupportedLanguagesType) {
await Promise.all([loadAntdLocale(land), loadDayjsLocale(land)]);
}
export { antdLocale, loadThirdPartyMessage };