Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(): dispose temp i18n bundles #1459

Merged
merged 1 commit into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 13 additions & 16 deletions bricks/nav/src/data-providers/get-menu-config-options.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { createProviderClass } from "@next-core/utils/general";
import { i18n } from "@next-core/i18n";
import type { MenuRawData } from "./get-menu-config-tree";
import { smartDisplayForMenuTitle } from "./shared/smartDisplayForMenuTitle";
import {
initializeMenuI18n,
smartDisplayForMenuTitle,
} from "./shared/smartDisplayForMenuTitle";

export interface MenuOption {
label: string;
Expand All @@ -11,29 +13,24 @@ export interface MenuOption {
/**
* 构造用于菜单自定义的下拉选项数据。
*
* 将对菜单标题进行表达式解析,支持 I8N 和 APP。
* 将对菜单标题进行表达式解析,支持 I18N 和 APP。
*/
export async function getMenuConfigOptions(
menuList: MenuRawData[]
): Promise<MenuOption[]> {
const options: MenuOption[] = [];

const { menuWithI18n, dispose } = initializeMenuI18n(menuList);

for (const menu of menuList) {
if (menu.type === "main") {
const menuI18nNamespace = `customize-menu/${menu.menuId}~${menu.app[0].appId}+${
menu.instanceId
}`;
// Support any language in `menu.i18n`.
Object.entries(menu.i18n ?? {}).forEach(([lang, resources]) => {
i18n.addResourceBundle(lang, menuI18nNamespace, resources);
});
options.push({
label: `${smartDisplayForMenuTitle(menu.title, menuI18nNamespace, menu.overrideApp) ?? ""} (${menu.menuId})`,
value: menu.menuId,
});
}
options.push({
label: `${smartDisplayForMenuTitle(menu.title, menuWithI18n.get(menu), menu.overrideApp) ?? ""} (${menu.menuId})`,
value: menu.menuId,
});
}

dispose();

return options;
}

Expand Down
23 changes: 8 additions & 15 deletions bricks/nav/src/data-providers/get-menu-config-tree.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import type { MetaI18n, MicroApp } from "@next-core/types";
import { i18n } from "@next-core/i18n";
import { createProviderClass } from "@next-core/utils/general";
import { sortBy } from "lodash";
import { smartDisplayForMenuTitle } from "./shared/smartDisplayForMenuTitle";
import {
initializeMenuI18n,
smartDisplayForMenuTitle,
} from "./shared/smartDisplayForMenuTitle";

const symbolMenuI18nNamespace = Symbol("menuI18nNamespace");
const symbolOverrideApp = Symbol("overrideApp");
Expand Down Expand Up @@ -73,7 +75,7 @@ const DEFAULT_ICON = {
/**
* 构造用于菜单自定义的树形结构数据。
*
* 将对菜单标题进行表达式解析,支持 I8N 和 APP。
* 将对菜单标题进行表达式解析,支持 I18N 和 APP。
*/
export async function getMenuConfigTree(
menuList: MenuRawData[]
Expand Down Expand Up @@ -111,18 +113,7 @@ export async function getMenuConfigTree(

const validMenuList: MenuRawData[] = [];
const injectWithMenus = new Map<string, MenuRawData[]>();
const menuWithI18n = new WeakMap<MenuRawData, string>();

for (const menu of menuList) {
const menuI18nNamespace = `customize-menu/${menu.menuId}~${menu.app[0].appId}+${
menu.instanceId
}`;
// Support any language in `menu.i18n`.
Object.entries(menu.i18n ?? {}).forEach(([lang, resources]) => {
i18n.addResourceBundle(lang, menuI18nNamespace, resources);
});
menuWithI18n.set(menu, menuI18nNamespace);
}
const { menuWithI18n, dispose } = initializeMenuI18n(menuList);

for (const menu of menuList) {
if (!(menu.dynamicItems && menu.itemsResolve) && menu.items?.length) {
Expand Down Expand Up @@ -165,6 +156,8 @@ export async function getMenuConfigTree(
},
];

dispose();

return tree;
}

Expand Down
28 changes: 27 additions & 1 deletion bricks/nav/src/data-providers/shared/smartDisplayForMenuTitle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
beforeVisitGlobalMember,
type MemberUsageInExpressions,
} from "@next-core/utils/storyboard";
import type { MenuRawData } from "../get-menu-config-tree";

const allowedAppProps = new Set(["name", "id", "homepage", "localeName"]);

/**
* 对菜单标题进行表达式解析,支持 I8N 和 APP。
* 对菜单标题进行表达式解析,支持 I18N 和 APP。
*/
export function smartDisplayForMenuTitle(
title: unknown,
Expand Down Expand Up @@ -106,3 +107,28 @@
}
return title;
}

export function initializeMenuI18n(menuList: MenuRawData[]) {
const menuWithI18n = new WeakMap<MenuRawData, string>();
const menuI18nBundles: [lang: string, ns: string][] = [];

for (const menu of menuList) {
const menuI18nNamespace = `customize-menu/${menu.menuId}~${menu.app[0].appId}+${
menu.instanceId
}`;
// Support any language in `menu.i18n`.
Object.entries(menu.i18n ?? {}).forEach(([lang, resources]) => {
i18n.addResourceBundle(lang, menuI18nNamespace, resources);
});
menuWithI18n.set(menu, menuI18nNamespace);
}

return {
menuWithI18n,
dispose: () => {
for (const [lang, ns] of menuI18nBundles) {
i18n.removeResourceBundle(lang, ns);

Check warning on line 130 in bricks/nav/src/data-providers/shared/smartDisplayForMenuTitle.ts

View check run for this annotation

Codecov / codecov/patch

bricks/nav/src/data-providers/shared/smartDisplayForMenuTitle.ts#L130

Added line #L130 was not covered by tests
}
},
};
}
Loading