From d8f1d97804a7e5a972207a4a0543ab063e9a1f2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BB=9D=E5=B0=98?= <237809796@qq.com> Date: Mon, 15 Jun 2026 13:48:38 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96:=20CR-08=20P1-10=20time.ts?= =?UTF-8?q?=E7=9B=B8=E5=AF=B9=E6=97=B6=E9=97=B4=E8=B5=B0i18n(=E5=8E=9F?= =?UTF-8?q?=E7=A1=AC=E7=BC=96=E7=A0=81=E4=B8=AD=E6=96=87=E8=87=B4en=20loca?= =?UTF-8?q?le=E6=97=B6=E9=97=B4=E6=81=92=E4=B8=AD=E6=96=87,=E5=A4=8D?= =?UTF-8?q?=E7=94=A8common.justNow/minutesAgo/hoursAgo/dayAgo=E5=B7=B2?= =?UTF-8?q?=E6=9C=89key)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/time.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/utils/time.ts b/src/utils/time.ts index 901b57b..6ff138e 100644 --- a/src/utils/time.ts +++ b/src/utils/time.ts @@ -11,6 +11,12 @@ * 正确做法:先转 number 再 new Date。本模块统一处理毫秒字符串 / 秒级 / ISO / number / 空。 */ +import i18n from '../i18n' + +// CR-260615-08(P1-10):相对时间走 i18n(原硬编码中文,en locale 时间恒中文)。 +// utils 纯函数无 setup,用 vue-i18n 全局实例(参考 useAiEvents.ts:16-25),any 中转规避 TS2589。 +const t = ((i18n as any).global.t as (k: string, named?: Record) => string).bind((i18n as any).global) + /** 解析任意时间值为毫秒时间戳;无效或空返回 null */ export function parseTs(v: string | number | null | undefined): number | null { if (v == null) return null @@ -43,10 +49,10 @@ export function formatRelativeZh(v: string | number | null | undefined): string const ms = parseTs(v) if (ms == null) return '—' const diffMin = Math.floor((Date.now() - ms) / 60000) - if (diffMin < 1) return '刚刚' - if (diffMin < 60) return `${diffMin} 分钟前` + if (diffMin < 1) return t('common.justNow') + if (diffMin < 60) return t('common.minutesAgo', { n: diffMin }) const diffH = Math.floor(diffMin / 60) - if (diffH < 24) return `${diffH} 小时前` + if (diffH < 24) return t('common.hoursAgo', { n: diffH }) const diffD = Math.floor(diffH / 24) - return `${diffD} 天前` + return t('common.dayAgo', { n: diffD }) }