fix(datasource): 数据源类型图标的 alt 用本地化显示名,不再用裸 type (#4420)
parent
d92728d88c
commit
073b5ea616
|
|
@ -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 }
|
||||
}
|
||||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<string, string> = (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<string, string> = 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,<svg xmlns="http://www.w3.org/2000/svg"/>'
|
||||
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
|
||||
}
|
||||
})
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
<script setup lang="ts">
|
||||
import { getDatasourceIconUrl, datasourceIconMap } from './datasourceIcons'
|
||||
import { useConnectorLabel } from '@/composables/useConnectorLabel'
|
||||
|
||||
const props = withDefaults(defineProps<{
|
||||
type: string
|
||||
|
|
@ -11,6 +12,11 @@ const props = withDefaults(defineProps<{
|
|||
variant: 'inline',
|
||||
})
|
||||
|
||||
// The card subtitle and the connector picker resolve the same name through this
|
||||
// composable, so the icon's alt text and the visible label next to it cannot
|
||||
// disagree. Screen readers and copied page text get "钉钉文档", not "dingtalk".
|
||||
const { connectorLabel } = useConnectorLabel()
|
||||
|
||||
const iconMap = datasourceIconMap
|
||||
|
||||
function fallbackText(type: string) {
|
||||
|
|
@ -40,7 +46,7 @@ function fallbackText(type: string) {
|
|||
<img
|
||||
v-if="iconMap[type]"
|
||||
:src="iconMap[type]"
|
||||
:alt="type"
|
||||
:alt="connectorLabel(type)"
|
||||
class="ds-type-icon__img"
|
||||
:style="variant === 'inline' ? { width: `${size}px`, height: `${size}px` } : undefined"
|
||||
>
|
||||
|
|
|
|||
Loading…
Reference in New Issue