Skip to content

Commit

Permalink
fix(projects): update tab label when locale change
Browse files Browse the repository at this point in the history
  • Loading branch information
honghuangdc committed Nov 6, 2023
1 parent ecd0df2 commit 50b722c
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 3 deletions.
38 changes: 36 additions & 2 deletions src/store/modules/tab/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { computed, ref } from 'vue';
import { computed, ref, watch, effectScope, onScopeDispose } from 'vue';
import type { Router } from 'vue-router';
import { defineStore } from 'pinia';
import { SetupStoreId } from '@/enum';
Expand All @@ -9,11 +9,16 @@ import {
isTabInTabs,
filterTabsById,
getFixedTabIds,
filterTabsByIds
filterTabsByIds,
updateTabsByI18nKey,
updateTabByI18nKey
} from './shared';
import { useRouterPush } from '@/hooks/common/router';
import { useAppStore } from '../app';

export const useTabStore = defineStore(SetupStoreId.Tab, () => {
const app = useAppStore();
const scope = effectScope();
const { routerPush } = useRouterPush(false);

/**
Expand Down Expand Up @@ -216,6 +221,35 @@ export const useTabStore = defineStore(SetupStoreId.Tab, () => {
return fixedTabIds.includes(tabId);
}

/**
* update all tabs by i18n key
*/
function updateAllTabsByI18nKey() {
tabs.value = updateTabsByI18nKey(tabs.value);

if (homeTab.value) {
homeTab.value = updateTabByI18nKey(homeTab.value);
}
}

// watch store
scope.run(() => {
// update menus when locale changed
watch(
() => app.locale,
() => {
updateAllTabsByI18nKey();
}
);
});

/**
* on scope dispose
*/
onScopeDispose(() => {
scope.stop();
});

return {
/**
* all tabs
Expand Down
24 changes: 23 additions & 1 deletion src/store/modules/tab/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ export function getTabByRoute(route: App.Global.TabRoute) {
fullPath,
fixedIndex: fixedIndexInTab,
icon,
localIcon
localIcon,
i18nKey
};

return tab;
Expand Down Expand Up @@ -146,3 +147,24 @@ function updateTabsLabel(tabs: App.Global.Tab[]) {
label: tab.newLabel || tab.label
}));
}

/**
* update tab by i18n key
* @param tab
*/
export function updateTabByI18nKey(tab: App.Global.Tab) {
const { i18nKey, label } = tab;

return {
...tab,
label: i18nKey ? $t(i18nKey) : label
};
}

/**
* update tabs by i18n key
* @param tabs
*/
export function updateTabsByI18nKey(tabs: App.Global.Tab[]) {
return tabs.map(tab => updateTabByI18nKey(tab));
}
4 changes: 4 additions & 0 deletions src/typings/app.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ declare namespace App {
* @description local icon
*/
localIcon?: string;
/**
* i18n key
*/
i18nKey?: I18n.I18nKey;
};
}

Expand Down

0 comments on commit 50b722c

Please sign in to comment.