diff --git a/src/renderer/lib/formatters.ts b/src/renderer/lib/formatters.ts index 8a3523b3..b480fb85 100644 --- a/src/renderer/lib/formatters.ts +++ b/src/renderer/lib/formatters.ts @@ -1,6 +1,15 @@ +import { useAppStore } from "../state/store"; + +function getActiveLocale(): string { + return useAppStore.getState().locale === "zh" ? "zh-CN" : "en-US"; +} + export function formatMoney(value: number | null | undefined) { - if (typeof value !== "number" || Number.isNaN(value)) return "$0.00"; - return new Intl.NumberFormat("en-US", { + const locale = getActiveLocale(); + if (typeof value !== "number" || Number.isNaN(value)) { + return new Intl.NumberFormat(locale, { style: "currency", currency: "USD", minimumFractionDigits: 2 }).format(0); + } + return new Intl.NumberFormat(locale, { style: "currency", currency: "USD", minimumFractionDigits: 2, @@ -8,23 +17,24 @@ export function formatMoney(value: number | null | undefined) { } export function formatTime(value: string | null | undefined) { - if (!value) return "Not yet"; - return new Date(value).toLocaleString(); + if (!value) return useAppStore.getState().locale === "zh" ? "未开始" : "Not yet"; + return new Date(value).toLocaleString(getActiveLocale()); } export function timeAgo(dateStr: string | null): string { - if (!dateStr) return "never"; + if (!dateStr) return useAppStore.getState().locale === "zh" ? "从未" : "never"; const now = Date.now(); const then = new Date(dateStr).getTime(); const diff = Math.max(0, now - then); const seconds = Math.floor(diff / 1000); - if (seconds < 60) return "just now"; + const isZh = useAppStore.getState().locale === "zh"; + if (seconds < 60) return isZh ? "刚刚" : "just now"; const minutes = Math.floor(seconds / 60); - if (minutes < 60) return `${minutes}m ago`; + if (minutes < 60) return isZh ? `${minutes}分钟前` : `${minutes}m ago`; const hours = Math.floor(minutes / 60); - if (hours < 24) return `${hours}h ago`; + if (hours < 24) return isZh ? `${hours}小时前` : `${hours}h ago`; const days = Math.floor(hours / 24); - return `${days}d ago`; + return isZh ? `${days}天前` : `${days}d ago`; } export function getAgentInitials(name: string): string { @@ -46,15 +56,15 @@ export function formatDuration(seconds: number): string { } export function formatMessageTime(dateStr: string): string { + const locale = getActiveLocale(); const d = new Date(dateStr); const now = new Date(); + const time = d.toLocaleTimeString(locale, { hour: "2-digit", minute: "2-digit" }); const isToday = d.getDate() === now.getDate() && d.getMonth() === now.getMonth() && d.getFullYear() === now.getFullYear(); - if (isToday) { - return d.toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" }); - } + if (isToday) return time; const yesterday = new Date(now); yesterday.setDate(yesterday.getDate() - 1); const isYesterday = @@ -62,9 +72,8 @@ export function formatMessageTime(dateStr: string): string { d.getMonth() === yesterday.getMonth() && d.getFullYear() === yesterday.getFullYear(); if (isYesterday) { - return `Yesterday ${d.toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" })}`; + const rtf = new Intl.RelativeTimeFormat(locale, { numeric: "auto" }); + return `${rtf.format(-1, "day")} ${time}`; } - return d.toLocaleDateString([], { month: "short", day: "numeric" }) + - " " + - d.toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" }); + return `${d.toLocaleDateString(locale, { month: "short", day: "numeric" })} ${time}`; }