From 40a525ecdc494b40713a7d7175bb453efd73bbf7 Mon Sep 17 00:00:00 2001 From: Estrella Pan Date: Fri, 9 Oct 2026 13:33:48 +0200 Subject: [PATCH] =?UTF-8?q?fix(webui):=20idLabel=20=E8=AF=86=E5=88=AB?= =?UTF-8?q?=E9=A2=84=E7=BC=96=E8=AF=91=E7=9A=84=20i18n=20=E6=B6=88?= =?UTF-8?q?=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 构建时 unplugin-vue-i18n 把 i18n/*.json 的文案编译成消息函数, idLabel 只认字符串,扩展点、权限与重命名方式在应用里一直显示原始 id。 --- webui/src/utils/__tests__/id-label.test.ts | 15 ++++++++++++++- webui/src/utils/id-label.ts | 3 ++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/webui/src/utils/__tests__/id-label.test.ts b/webui/src/utils/__tests__/id-label.test.ts index 60e976a0..d196f1bb 100644 --- a/webui/src/utils/__tests__/id-label.test.ts +++ b/webui/src/utils/__tests__/id-label.test.ts @@ -1,10 +1,23 @@ -import { describe, expect, it } from 'vitest'; +import { describe, expect, it, vi } from 'vitest'; import { idLabel } from '../id-label'; import en from '@/i18n/en.json'; import zhCN from '@/i18n/zh-CN.json'; const t = (key: string) => `t:${key}`; +// 应用里 @intlify/unplugin-vue-i18n 把 i18n/*.json 的文案预编译成消息函数, +// 这里同样把叶子换成函数,按生产环境的形态测试 +vi.mock('@/i18n/en.json', async (importOriginal) => { + const compile = (node: unknown): unknown => + typeof node === 'string' + ? () => node + : Object.fromEntries( + Object.entries(node as object).map(([k, v]) => [k, compile(v)]) + ); + const { default: source } = await importOriginal<{ default: object }>(); + return { default: compile(source) }; +}); + describe('idLabel', () => { it.each([ ['config.plugins_set.point_labels', 'downloader', 'downloader'], diff --git a/webui/src/utils/id-label.ts b/webui/src/utils/id-label.ts index 56d4f43b..07858a67 100644 --- a/webui/src/utils/id-label.ts +++ b/webui/src/utils/id-label.ts @@ -17,5 +17,6 @@ export function idLabel( (node, part) => (node as Record | undefined)?.[part], en ); - return typeof found === 'string' ? t(key) : id; + // 构建时 i18n 插件把文案预编译成消息函数,测试里则是原始字符串 + return typeof found === 'string' || typeof found === 'function' ? t(key) : id; }