优化: CR-08 P1-10 time.ts相对时间走i18n(原硬编码中文致en locale时间恒中文,复用common.justNow/minutesAgo/hoursAgo/dayAgo已有key)
This commit is contained in:
@@ -11,6 +11,12 @@
|
|||||||
* 正确做法:先转 number 再 new Date。本模块统一处理毫秒字符串 / 秒级 / ISO / number / 空。
|
* 正确做法:先转 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, unknown>) => string).bind((i18n as any).global)
|
||||||
|
|
||||||
/** 解析任意时间值为毫秒时间戳;无效或空返回 null */
|
/** 解析任意时间值为毫秒时间戳;无效或空返回 null */
|
||||||
export function parseTs(v: string | number | null | undefined): number | null {
|
export function parseTs(v: string | number | null | undefined): number | null {
|
||||||
if (v == null) return null
|
if (v == null) return null
|
||||||
@@ -43,10 +49,10 @@ export function formatRelativeZh(v: string | number | null | undefined): string
|
|||||||
const ms = parseTs(v)
|
const ms = parseTs(v)
|
||||||
if (ms == null) return '—'
|
if (ms == null) return '—'
|
||||||
const diffMin = Math.floor((Date.now() - ms) / 60000)
|
const diffMin = Math.floor((Date.now() - ms) / 60000)
|
||||||
if (diffMin < 1) return '刚刚'
|
if (diffMin < 1) return t('common.justNow')
|
||||||
if (diffMin < 60) return `${diffMin} 分钟前`
|
if (diffMin < 60) return t('common.minutesAgo', { n: diffMin })
|
||||||
const diffH = Math.floor(diffMin / 60)
|
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)
|
const diffD = Math.floor(diffH / 24)
|
||||||
return `${diffD} 天前`
|
return t('common.dayAgo', { n: diffD })
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user