From 9b41878ae71f3e639e0493bf8e9190d07cd0e756 Mon Sep 17 00:00:00 2001 From: YANG JIANKUAN Date: Fri, 3 Apr 2026 13:26:57 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96=20i18n=20?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E5=AE=89=E5=85=A8=E4=B8=8E=E6=B8=B2=E6=9F=93?= =?UTF-8?q?=E6=80=A7=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 导出 TranslationKey 类型,翻译 key 拼写错误编译期即报错 - zh.ts 使用 TranslationKey 约束,确保中英文 key 同步 - useMemo 包装 context value,避免不必要的全局重渲染 - ConfirmDialog confirmText 默认值改用 t() 而非硬编码英文 - SchemaProperties 递归组件改为 prop 传递 t,减少 useContext 调用 Co-Authored-By: Claude Opus 4.6 --- packages/web/src/components/ConfirmDialog.tsx | 4 ++-- packages/web/src/components/SchemaView.tsx | 17 +++++++------- packages/web/src/components/ThemeToggle.tsx | 4 ++-- packages/web/src/lib/i18n.tsx | 19 +++++++++++----- packages/web/src/lib/i18n/en.ts | 2 +- packages/web/src/lib/i18n/zh.ts | 4 +++- .../web/src/pages/landing/PricingSection.tsx | 22 +++++++++---------- 7 files changed, 40 insertions(+), 32 deletions(-) diff --git a/packages/web/src/components/ConfirmDialog.tsx b/packages/web/src/components/ConfirmDialog.tsx index 7bd807c..2eb72de 100644 --- a/packages/web/src/components/ConfirmDialog.tsx +++ b/packages/web/src/components/ConfirmDialog.tsx @@ -11,7 +11,7 @@ type ConfirmDialogProps = { variant?: 'danger' | 'warning'; }; -export default function ConfirmDialog({ open, onConfirm, onCancel, title, description, confirmText = 'Confirm', variant = 'danger' }: ConfirmDialogProps) { +export default function ConfirmDialog({ open, onConfirm, onCancel, title, description, confirmText, variant = 'danger' }: ConfirmDialogProps) { const { t } = useI18n(); const iconColor = variant === 'danger' ? 'text-danger bg-danger-muted' : 'text-warning bg-warning-muted'; @@ -35,7 +35,7 @@ export default function ConfirmDialog({ open, onConfirm, onCancel, title, descri onClick={onConfirm} className={variant === 'danger' ? 'btn-danger' : 'btn-primary'} > - {confirmText} + {confirmText ?? t('common.confirm')} diff --git a/packages/web/src/components/SchemaView.tsx b/packages/web/src/components/SchemaView.tsx index 8315237..320ff90 100644 --- a/packages/web/src/components/SchemaView.tsx +++ b/packages/web/src/components/SchemaView.tsx @@ -3,7 +3,7 @@ * Replaces raw JSON.stringify output with readable tables and schema trees. */ -import { useI18n } from '../lib/i18n'; +import { useI18n, type TFunction } from '../lib/i18n'; /* ===== Helpers ===== */ @@ -159,8 +159,7 @@ export function ParametersView({ parameters }: { parameters: unknown }) { /* ===== Schema Properties Tree ===== */ -function SchemaProperties({ schema, depth = 0 }: { schema: SchemaObj; depth?: number }) { - const { t } = useI18n(); +function SchemaProperties({ schema, depth = 0, t }: { schema: SchemaObj; depth?: number; t: TFunction }) { const properties = schema.properties; const requiredSet = new Set(schema.required || []); @@ -217,8 +216,8 @@ function SchemaProperties({ schema, depth = 0 }: { schema: SchemaObj; depth?: nu {t('dashboard.schema.default')} {JSON.stringify(prop.default)} )} - {hasChildren && } - {isArray && prop.items && } + {hasChildren && } + {isArray && prop.items && } ); })} @@ -247,7 +246,7 @@ export function RequestBodyView({ requestBody }: { requestBody: unknown }) { {body.required && {t('dashboard.schema.required')}}

- +
); @@ -274,7 +273,7 @@ export function RequestBodyView({ requestBody }: { requestBody: unknown }) {
{media.schema ? ( media.schema.properties ? ( - + ) : (
@@ -351,13 +350,13 @@ export function ResponsesView({ responses }: { responses: unknown }) { {schema && (schema.properties || schema.items?.properties || schema.type) && (
{schema.properties ? ( - + ) : schema.type === 'array' && schema.items?.properties ? (
{t('dashboard.schema.ofObjects')}
- +
) : (
diff --git a/packages/web/src/components/ThemeToggle.tsx b/packages/web/src/components/ThemeToggle.tsx index c1468cf..74ee49a 100644 --- a/packages/web/src/components/ThemeToggle.tsx +++ b/packages/web/src/components/ThemeToggle.tsx @@ -1,5 +1,5 @@ import { useTheme } from '../lib/theme'; -import { useI18n } from '../lib/i18n'; +import { useI18n, type TranslationKey } from '../lib/i18n'; const icons = { light: ( @@ -31,7 +31,7 @@ export default function ThemeToggle() { ) : ( - {t(`pricing.${key}.cta`)} + {t(tk(`pricing.${key}.cta`))} )}