From 4344765cf997524049b122a010a1687e7609bbe2 Mon Sep 17 00:00:00 2001 From: wizardchen Date: Mon, 18 May 2026 20:37:31 +0800 Subject: [PATCH] feat(i18n): enhance notification templates with raw message handling Updated the notification system to utilize the `tm` function for template resolution, ensuring that placeholders like `{name}` and `{role}` remain intact for proper rendering. This change improves the display of workspace and role information in login and tenant switch notifications, allowing for better localization and visual consistency across the application. Adjustments were made in `App.vue`, `Login.vue`, and related utility functions to accommodate the new template handling. --- frontend/src/App.vue | 12 +- frontend/src/utils/loginNotify.ts | 15 +- frontend/src/utils/workspaceNotifyContent.ts | 189 ++++++++----------- frontend/src/views/auth/Login.vue | 4 +- 4 files changed, 99 insertions(+), 121 deletions(-) diff --git a/frontend/src/App.vue b/frontend/src/App.vue index 17026875..72a2731e 100644 --- a/frontend/src/App.vue +++ b/frontend/src/App.vue @@ -18,7 +18,7 @@ import zhCNConfig from 'tdesign-vue-next/esm/locale/zh_CN' import koKRConfig from 'tdesign-vue-next/esm/locale/ko_KR' import ruRUConfig from 'tdesign-vue-next/esm/locale/ru_RU' -const { locale, t } = useI18n() +const { locale, t, tm } = useI18n() const { formatRole, roleIcon } = useRoleLabel() const router = useRouter() const authStore = useAuthStore() @@ -144,7 +144,7 @@ const handleGlobalOIDCCallback = async () => { if (response.success) { clearOIDCCallbackState('/') await persistOIDCLoginResponse(response) - notifyLoginSuccess(response, t, formatRole, roleIcon) + notifyLoginSuccess(response, t, tm, formatRole, roleIcon) return } @@ -207,10 +207,16 @@ const showPendingTenantSwitchToast = () => { const templateKey = pending.role ? 'tenant.switchSuccessContentWithRole' : 'tenant.switchSuccessContent' + // Use tm() not t() — vue-i18n v11's `t()` replaces unspecified named + // placeholders with empty strings, which would strip {name}/{role} + // before the chip renderer can split on them. tm() returns the raw + // message verbatim. + const rawTemplate = tm(templateKey) + const template = typeof rawTemplate === 'string' ? rawTemplate : '' NotifyPlugin.success({ title: t('tenant.switchSuccessTitle'), content: renderWorkspaceNotifyContent({ - template: t(templateKey), + template, name: pending.name, roleLabel: pending.role, roleEnum: pending.roleEnum, diff --git a/frontend/src/utils/loginNotify.ts b/frontend/src/utils/loginNotify.ts index 94cd2d5c..e37e9d8b 100644 --- a/frontend/src/utils/loginNotify.ts +++ b/frontend/src/utils/loginNotify.ts @@ -11,6 +11,12 @@ import { NotifyPlugin } from 'tdesign-vue-next' import { renderWorkspaceNotifyContent } from './workspaceNotifyContent' type Translator = (key: string, params?: Record) => string +// TemplateResolver returns the raw i18n message verbatim — placeholders +// like `{name}` are left untouched. Pass `tm` from useI18n (which does +// no interpolation), not `t` (which would replace unspecified named +// placeholders with empty strings and strand the renderer with nothing +// to split on). +type TemplateResolver = (key: string) => unknown type RoleFormatter = (role: string | null | undefined) => string type RoleIconResolver = (role: string | null | undefined) => string @@ -26,6 +32,7 @@ interface LoginResponseLike { export function notifyLoginSuccess( response: LoginResponseLike | null | undefined, t: Translator, + tm: TemplateResolver, formatRole: RoleFormatter, roleIcon: RoleIconResolver, ): void { @@ -41,18 +48,16 @@ export function notifyLoginSuccess( const roleLabel = roleEnum ? formatRole(roleEnum) : '' const roleIconName = roleEnum ? roleIcon(roleEnum) : '' - // Fetch the template without interpolation so the workspace / role - // placeholders survive and the renderer can swap them for styled chips. - // vue-i18n returns the message verbatim when the named keys aren't - // provided in the params bag. const templateKey = roleLabel ? 'auth.loginSuccessContentWithRole' : 'auth.loginSuccessContent' + const rawTemplate = tm(templateKey) + const template = typeof rawTemplate === 'string' ? rawTemplate : '' NotifyPlugin.success({ title: t('auth.loginSuccessTitle'), content: renderWorkspaceNotifyContent({ - template: t(templateKey), + template, name: tenantName, roleLabel: roleLabel || undefined, roleEnum: roleEnum || undefined, diff --git a/frontend/src/utils/workspaceNotifyContent.ts b/frontend/src/utils/workspaceNotifyContent.ts index f6b470d5..97bd6794 100644 --- a/frontend/src/utils/workspaceNotifyContent.ts +++ b/frontend/src/utils/workspaceNotifyContent.ts @@ -1,94 +1,44 @@ // Shared content renderer for the post-login and post-tenant-switch // NotifyPlugin cards. Both cards present the same shape ("you are in // as "), so we render them with a unified visual -// language — a neutral chip for the workspace name and a role-coloured -// chip for the role — instead of plain interpolated text. +// language to keep the two surfaces consistent. +// +// Design choices: +// +// * The workspace name is the primary anchor of the sentence, so it +// is rendered as plain bold text (no chip / background) — the +// surrounding sentence already frames it and another box around +// would be double-emphasis. +// * The role is a categorical attribute and benefits from a visible +// coloured tag, so it goes through TDesign's with the same +// role→theme mapping used by TenantMembers (settings). This keeps +// "what an owner / admin / contributor / viewer looks like" +// consistent across the product instead of inventing a parallel +// chip palette. +// * No forced wrapping — let the Notify width drive line breaks +// naturally so the sentence reads as one phrase whenever it fits. // // The renderer interpolates a template string carrying `{name}` and -// optionally `{role}` placeholders. Everything around those placeholders -// is rendered verbatim, which lets translators reorder the sentence -// however their language wants without further code changes. -// -// Why not just pass html to NotifyPlugin's `content` string? -// TDesign's plugin treats `content` as text. Passing a render function -// (TNode) is the supported way to inject styled VNodes; doing it -// through a shared helper keeps the chip styles in one place rather -// than scattered across two call sites. +// optionally `{role}` placeholders. Anything around them is rendered +// verbatim so translators can reorder the sentence per locale. import { h, type VNode } from 'vue' -import { Icon as TIcon } from 'tdesign-vue-next' +import { Tag as TTag, Icon as TIcon } from 'tdesign-vue-next' -// Inline styles (rather than a global stylesheet) because NotifyPlugin -// renders into a teleported overlay outside any scoped