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; }