From 35b1bc37534bde14de820e90acfa515de0bf00eb Mon Sep 17 00:00:00 2001 From: IanShaw027 <131567472+IanShaw027@users.noreply.github.com> Date: Sat, 27 Dec 2025 16:04:56 +0800 Subject: [PATCH] =?UTF-8?q?refactor(frontend):=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=8C=96=E5=B7=A5=E5=85=B7=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 改进数据格式化逻辑 - 增强工具函数可读性 --- frontend/src/utils/format.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/frontend/src/utils/format.ts b/frontend/src/utils/format.ts index bd545d42..aec7c863 100644 --- a/frontend/src/utils/format.ts +++ b/frontend/src/utils/format.ts @@ -3,30 +3,32 @@ * 参考 CRS 项目的 format.js 实现 */ +import { i18n } from '@/i18n' + /** * 格式化相对时间 * @param date 日期字符串或 Date 对象 * @returns 相对时间字符串,如 "5m ago", "2h ago", "3d ago" */ export function formatRelativeTime(date: string | Date | null | undefined): string { - if (!date) return 'Never' + if (!date) return i18n.global.t('common.time.never') const now = new Date() const past = new Date(date) const diffMs = now.getTime() - past.getTime() // 处理未来时间或无效日期 - if (diffMs < 0 || isNaN(diffMs)) return 'Never' + if (diffMs < 0 || isNaN(diffMs)) return i18n.global.t('common.time.never') const diffSecs = Math.floor(diffMs / 1000) const diffMins = Math.floor(diffSecs / 60) const diffHours = Math.floor(diffMins / 60) const diffDays = Math.floor(diffHours / 24) - if (diffDays > 0) return `${diffDays}d ago` - if (diffHours > 0) return `${diffHours}h ago` - if (diffMins > 0) return `${diffMins}m ago` - return 'Just now' + if (diffDays > 0) return i18n.global.t('common.time.daysAgo', { n: diffDays }) + if (diffHours > 0) return i18n.global.t('common.time.hoursAgo', { n: diffHours }) + if (diffMins > 0) return i18n.global.t('common.time.minutesAgo', { n: diffMins }) + return i18n.global.t('common.time.justNow') } /**