Skip to content

Commit

Permalink
feat(projects): antd locale, dayjs locale
Browse files Browse the repository at this point in the history
  • Loading branch information
honghuangdc committed Nov 16, 2023
1 parent 388e504 commit 3b33ce9
Show file tree
Hide file tree
Showing 14 changed files with 78 additions and 8 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"@vueuse/core": "10.6.1",
"ant-design-vue": "4.0.7",
"clipboard": "2.0.11",
"dayjs": "^1.11.10",
"lodash-es": "4.17.21",
"nprogress": "0.2.0",
"pinia": "2.1.7",
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion src/App.vue
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
<script setup lang="ts">
import { computed } from 'vue';
import { ConfigProvider } from 'ant-design-vue';
import { useAppStore } from './store/modules/app';
import { useThemeStore } from './store/modules/theme';
import { antdLocales } from './locales/antd';
defineOptions({
name: 'App'
});
const appStore = useAppStore();
const themeStore = useThemeStore();
const antdLocale = computed(() => {
return antdLocales[appStore.locale];
});
</script>

<template>
<ConfigProvider :theme="themeStore.antdTheme">
<ConfigProvider :theme="themeStore.antdTheme" :locale="antdLocale">
<AppProvider>
<RouterView class="bg-layout" />
</AppProvider>
Expand Down
8 changes: 8 additions & 0 deletions src/locales/antd.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { Locale } from 'ant-design-vue/es/locale';
import zhCN from 'ant-design-vue/es/locale/zh_CN';
import enUS from 'ant-design-vue/es/locale/en_US';

export const antdLocales: Record<App.I18n.LangType, Locale> = {
'zh-CN': zhCN,
'en-US': enUS
};
19 changes: 19 additions & 0 deletions src/locales/dayjs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { locale } from 'dayjs';
import 'dayjs/locale/zh-cn';
import 'dayjs/locale/en';
import { localStg } from '@/utils/storage';

/**
* set dayjs locale
* @param lang
*/
export function setDayjsLocale(lang: App.I18n.LangType = 'zh-CN') {
const localMap = {
'zh-CN': 'zh-cn',
'en-US': 'en'
} satisfies Record<App.I18n.LangType, string>;

const l = lang || localStg.get('lang') || 'zh-CN';

locale(localMap[l]);
}
File renamed without changes.
4 changes: 2 additions & 2 deletions src/locales/locale.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import zhCN from './lang/zh-CN';
import en from './lang/en';
import enUS from './lang/en-us';

const locales: Record<App.I18n.LangType, App.I18n.Schema> = {
'zh-CN': zhCN,
en
'en-US': enUS
};

export default locales;
4 changes: 3 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createApp } from 'vue';
import './plugins/assets';
import { setupNProgress, setupIconifyOffline } from './plugins';
import { setupNProgress, setupIconifyOffline, setupDayjs } from './plugins';
import { setupStore } from './store';
import { setupRouter } from './router';
import { setupI18n } from './locales';
Expand All @@ -11,6 +11,8 @@ async function setupApp() {

setupIconifyOffline();

setupDayjs();

const app = createApp(App);

setupStore(app);
Expand Down
9 changes: 9 additions & 0 deletions src/plugins/dayjs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { extend } from 'dayjs';
import localeData from 'dayjs/plugin/localeData';
import { setDayjsLocale } from '../locales/dayjs';

export function setupDayjs() {
extend(localeData);

setDayjsLocale();
}
1 change: 1 addition & 0 deletions src/plugins/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './nprogress';
export * from './iconify';
export * from './dayjs';
18 changes: 17 additions & 1 deletion src/store/modules/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useBoolean } from '@sa/hooks';
import { SetupStoreId } from '@/enum';
import { router } from '@/router';
import { $t, setLocale } from '@/locales';
import { setDayjsLocale } from '@/locales/dayjs';
import { localStg } from '@/utils/storage';
import { useRouteStore } from '../route';
import { useTabStore } from '../tab';
Expand Down Expand Up @@ -53,7 +54,7 @@ export const useAppStore = defineStore(SetupStoreId.App, () => {
},
{
label: 'English',
key: 'en'
key: 'en-US'
}
];

Expand All @@ -74,6 +75,10 @@ export const useAppStore = defineStore(SetupStoreId.App, () => {
useTitle(documentTitle);
}

function init() {
setDayjsLocale(locale.value);
}

// watch store
scope.run(() => {
// watch isMobile, if is mobile, collapse sider
Expand All @@ -91,9 +96,17 @@ export const useAppStore = defineStore(SetupStoreId.App, () => {

// watch locale
watch(locale, () => {
// update document title by locale
updateDocumentTitleByLocale();

// update global menus by locale
routeStore.updateGlobalMenusByLocale();

// update tabs by locale
tabStore.updateTabsByLocale();

// sey dayjs locale
setDayjsLocale(locale.value);
});
});

Expand All @@ -104,6 +117,9 @@ export const useAppStore = defineStore(SetupStoreId.App, () => {
scope.stop();
});

// init
init();

return {
isMobile,
reloadFlag,
Expand Down
4 changes: 2 additions & 2 deletions src/typings/app.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,11 +336,11 @@ declare namespace App {
namespace I18n {
type RouteKey = import('@elegant-router/types').RouteKey;

type LangType = 'en' | 'zh-CN';
type LangType = 'en-US' | 'zh-CN';

type LangOption = {
label: string;
key: App.I18n.LangType;
key: LangType;
};

type I18nRouteKey = Exclude<RouteKey, 'root' | 'not-found'>;
Expand Down
1 change: 1 addition & 0 deletions src/typings/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ declare module 'vue' {
ABreadcrumb: typeof import('ant-design-vue/es')['Breadcrumb']
ABreadcrumbItem: typeof import('ant-design-vue/es')['BreadcrumbItem']
AButton: typeof import('ant-design-vue/es')['Button']
ACalendar: typeof import('ant-design-vue/es')['Calendar']
ACard: typeof import('ant-design-vue/es')['Card']
ACheckbox: typeof import('ant-design-vue/es')['Checkbox']
ADivider: typeof import('ant-design-vue/es')['Divider']
Expand Down
4 changes: 3 additions & 1 deletion src/views/home/index.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<script setup lang="ts"></script>

<template>
<LookForward />
<div>
<ACalendar :fullscreen="false" />
</div>
</template>

<style scoped></style>

0 comments on commit 3b33ce9

Please sign in to comment.