From 073b5ea6169254e29059e26fcb053e13730db33c Mon Sep 17 00:00:00 2001 From: linus-liu-web <395141741@qq.com> Date: Sat, 17 Oct 2026 23:17:07 +0800 Subject: [PATCH] =?UTF-8?q?fix(datasource):=20=E6=95=B0=E6=8D=AE=E6=BA=90?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E5=9B=BE=E6=A0=87=E7=9A=84=20alt=20=E7=94=A8?= =?UTF-8?q?=E6=9C=AC=E5=9C=B0=E5=8C=96=E6=98=BE=E7=A4=BA=E5=90=8D=EF=BC=8C?= =?UTF-8?q?=E4=B8=8D=E5=86=8D=E7=94=A8=E8=A3=B8=20type=20(#4420)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/composables/useConnectorLabel.ts | 33 +++++++ .../knowledge/settings/DataSourceSettings.vue | 12 ++- .../settings/DataSourceTypeIcon.test.ts | 85 +++++++++++++++++++ .../knowledge/settings/DataSourceTypeIcon.vue | 8 +- 4 files changed, 133 insertions(+), 5 deletions(-) create mode 100644 frontend/src/composables/useConnectorLabel.ts create mode 100644 frontend/src/views/knowledge/settings/DataSourceTypeIcon.test.ts diff --git a/frontend/src/composables/useConnectorLabel.ts b/frontend/src/composables/useConnectorLabel.ts new file mode 100644 index 00000000..f2b53aa5 --- /dev/null +++ b/frontend/src/composables/useConnectorLabel.ts @@ -0,0 +1,33 @@ +import { useI18n } from 'vue-i18n' + +/** + * Format a data source connector type ('dingtalk', 'feishu', …) into the + * user-facing name declared under datasource.connector.* in the i18n bundle. + * + * The type icon's alt text, the card subtitle and the connector picker all render + * this name, so they resolve it through one function and cannot disagree about + * what a connector is called. They used to: the icon carried the raw type while + * the label right next to it showed the translation, which put the internal + * identifier "dingtalk" into the icon's alt text and into any text copied off the + * page. + * + * The lookup deliberately avoids `t(key) || type`: vue-i18n echoes the key back + * when the locale carries no message for it, so that expression never falls back + * and a connector without a translation would render as + * "datasource.connector.github" — worse than the raw type it was meant to rescue. + * The connector registry ships several types with no message (github, imap, + * google_drive, …). `te` tells us a real message exists; the `|| type` after it + * only covers a message that is present but empty. + * + * Same shape as useRoleLabel's formatRole, which solves the same echo problem for + * tenantMember.role.* by comparing the result with the key. + */ +export function useConnectorLabel() { + const { t, te } = useI18n() + const connectorLabel = (type: string | null | undefined): string => { + if (!type) return '' + const key = `datasource.connector.${type}` + return (te(key) ? t(key) : '') || type + } + return { connectorLabel } +} diff --git a/frontend/src/views/knowledge/settings/DataSourceSettings.vue b/frontend/src/views/knowledge/settings/DataSourceSettings.vue index 54a1feb8..1528dcec 100644 --- a/frontend/src/views/knowledge/settings/DataSourceSettings.vue +++ b/frontend/src/views/knowledge/settings/DataSourceSettings.vue @@ -2,6 +2,7 @@ import { ref, computed, onMounted, onBeforeUnmount } from 'vue' import { MessagePlugin } from 'tdesign-vue-next' import { useI18n } from 'vue-i18n' +import { useConnectorLabel } from '@/composables/useConnectorLabel' import { listDataSources, deleteDataSource, @@ -130,14 +131,17 @@ function statusLabel(status: string) { return t(`datasource.status.${status}`) } +// Shared with DataSourceTypeIcon: the icon's alt text and this subtitle have to +// name a connector the same way. The helper moved to a composable because the +// inline `t(key) || type` it replaced never fell back — vue-i18n echoes the key +// back for a missing message — so a connector with no translation (the registry +// ships several) rendered as "datasource.connector.github". +const { connectorLabel } = useConnectorLabel() + function syncModeLabel(mode: string) { return t(`datasource.syncMode.${mode}`) } -function connectorLabel(type: string) { - return t(`datasource.connector.${type}`) || type -} - function scheduleLabel(cron: string) { return humanizeCron(cron, t) } diff --git a/frontend/src/views/knowledge/settings/DataSourceTypeIcon.test.ts b/frontend/src/views/knowledge/settings/DataSourceTypeIcon.test.ts new file mode 100644 index 00000000..924b2017 --- /dev/null +++ b/frontend/src/views/knowledge/settings/DataSourceTypeIcon.test.ts @@ -0,0 +1,85 @@ +import assert from 'node:assert/strict' +import { after, before, test } from 'node:test' +import { fileURLToPath } from 'node:url' +import { createServer, type ViteDevServer } from 'vite' +import vue from '@vitejs/plugin-vue' +import { createSSRApp, h, type Component } from 'vue' +import { renderToString } from 'vue/server-renderer' +import { createI18n } from 'vue-i18n' +import en from '../../../i18n/locales/en-US' +import zh from '../../../i18n/locales/zh-CN' + +let server: ViteDevServer +let Icon: Component + +before(async () => { + server = await createServer({ + configFile: false, optimizeDeps: { noDiscovery: true, entries: [] }, plugins: [vue()], + resolve: { alias: { '@': fileURLToPath(new URL('../../../', import.meta.url)) } }, + server: { middlewareMode: true }, appType: 'custom', + }) + Icon = (await server.ssrLoadModule('/src/views/knowledge/settings/DataSourceTypeIcon.vue')).default +}) +after(async () => { await server?.close() }) + +async function render(type: string, locale: 'zh-CN' | 'en-US') { + const app = createSSRApp({ render: () => h(Icon, { type }) }) + app.use(createI18n({ + legacy: false, + locale, + fallbackLocale: locale, + messages: locale === 'zh-CN' ? { 'zh-CN': zh } : { 'en-US': en }, + })) + return renderToString(app) +} + +// The icon carries the same localized name the card subtitle and the type picker show, +// so screen readers and copied page text never see the internal connector identifier. +test('a connected source names its icon with the localized display name, not the raw type', async () => { + const html = await render('dingtalk', 'zh-CN') + assert.ok(html.includes('ds-type-icon__img'), 'the icon image must render, otherwise this asserts nothing') + assert.ok(html.includes('alt="钉钉文档"'), `expected the zh-CN display name, got: ${html}`) + assert.ok(!html.includes('alt="dingtalk"'), `the raw type leaked into alt: ${html}`) +}) + +test('the icon name follows the active locale', async () => { + const html = await render('dingtalk', 'en-US') + assert.ok(html.includes('ds-type-icon__img'), 'the icon image must render, otherwise this asserts nothing') + assert.ok(html.includes('alt="DingTalk Docs"'), `expected the en-US display name, got: ${html}`) + assert.ok(!html.includes('alt="dingtalk"'), `the raw type leaked into alt: ${html}`) +}) + +test('every icon-backed connector uses its localized name', async () => { + const icons = await server.ssrLoadModule('/src/views/knowledge/settings/datasourceIcons.ts') + const types = Object.keys(icons.datasourceIconMap) + assert.ok(types.length > 0, 'the icon map must not be empty') + for (const locale of ['zh-CN', 'en-US'] as const) { + const names: Record = (locale === 'zh-CN' ? zh : en).datasource.connector + for (const type of types) { + const html = await render(type, locale) + assert.ok( + html.includes(`alt="${names[type]}"`), + `${locale} ${type}: expected alt="${names[type]}", got: ${html}`, + ) + } + } +}) + +// A type can gain an icon before its `datasource.connector.*` message lands, and a data +// source created through the API can carry a type the UI never listed. vue-i18n echoes +// the key back for a missing message, so a plain `t(key) || type` would put the +// untranslated "datasource.connector.github" in the alt text. +test('an icon-backed type without a translation falls back to the bare type', async () => { + const icons = await server.ssrLoadModule('/src/views/knowledge/settings/datasourceIcons.ts') + const zhNames: Record = zh.datasource.connector + assert.ok(!zhNames.github, 'github must stay without a translation for this test to mean anything') + icons.datasourceIconMap.github = 'data:image/svg+xml,' + try { + const html = await render('github', 'zh-CN') + assert.ok(html.includes('ds-type-icon__img'), 'the icon image must render, otherwise this asserts nothing') + assert.ok(html.includes('alt="github"'), `expected a fallback to the raw type, got: ${html}`) + assert.ok(!html.includes('datasource.connector.github'), `an untranslated key leaked into alt: ${html}`) + } finally { + delete icons.datasourceIconMap.github + } +}) diff --git a/frontend/src/views/knowledge/settings/DataSourceTypeIcon.vue b/frontend/src/views/knowledge/settings/DataSourceTypeIcon.vue index 2de050e8..e3fb8651 100644 --- a/frontend/src/views/knowledge/settings/DataSourceTypeIcon.vue +++ b/frontend/src/views/knowledge/settings/DataSourceTypeIcon.vue @@ -1,5 +1,6 @@