fix(formatters): use active locale for money/time/relative formatting (P2-29, P2-30)

Pull locale from useAppStore.getState() so zh users get zh-CN number/date
formatting and Chinese relative-time strings; drop en-US lock.
This commit is contained in:
ZacharyZhang-NY committed 2026-04-16 22:59:41 -04:00
1 parent fcd2cf6e43
commit 17aa92d18d
1 file changed
+25 -16
+25 -16
View File
@@ -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) { export function formatMoney(value: number | null | undefined) {
if (typeof value !== "number" || Number.isNaN(value)) return "$0.00"; const locale = getActiveLocale();
return new Intl.NumberFormat("en-US", { 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", style: "currency",
currency: "USD", currency: "USD",
minimumFractionDigits: 2, minimumFractionDigits: 2,
@@ -8,23 +17,24 @@ export function formatMoney(value: number | null | undefined) {
} }
export function formatTime(value: string | null | undefined) { export function formatTime(value: string | null | undefined) {
if (!value) return "Not yet"; if (!value) return useAppStore.getState().locale === "zh" ? "未开始" : "Not yet";
return new Date(value).toLocaleString(); return new Date(value).toLocaleString(getActiveLocale());
} }
export function timeAgo(dateStr: string | null): string { export function timeAgo(dateStr: string | null): string {
if (!dateStr) return "never"; if (!dateStr) return useAppStore.getState().locale === "zh" ? "从未" : "never";
const now = Date.now(); const now = Date.now();
const then = new Date(dateStr).getTime(); const then = new Date(dateStr).getTime();
const diff = Math.max(0, now - then); const diff = Math.max(0, now - then);
const seconds = Math.floor(diff / 1000); 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); 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); 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); const days = Math.floor(hours / 24);
return `${days}d ago`; return isZh ? `${days}天前` : `${days}d ago`;
} }
export function getAgentInitials(name: string): string { export function getAgentInitials(name: string): string {
@@ -46,15 +56,15 @@ export function formatDuration(seconds: number): string {
} }
export function formatMessageTime(dateStr: string): string { export function formatMessageTime(dateStr: string): string {
const locale = getActiveLocale();
const d = new Date(dateStr); const d = new Date(dateStr);
const now = new Date(); const now = new Date();
const time = d.toLocaleTimeString(locale, { hour: "2-digit", minute: "2-digit" });
const isToday = const isToday =
d.getDate() === now.getDate() && d.getDate() === now.getDate() &&
d.getMonth() === now.getMonth() && d.getMonth() === now.getMonth() &&
d.getFullYear() === now.getFullYear(); d.getFullYear() === now.getFullYear();
if (isToday) { if (isToday) return time;
return d.toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" });
}
const yesterday = new Date(now); const yesterday = new Date(now);
yesterday.setDate(yesterday.getDate() - 1); yesterday.setDate(yesterday.getDate() - 1);
const isYesterday = const isYesterday =
@@ -62,9 +72,8 @@ export function formatMessageTime(dateStr: string): string {
d.getMonth() === yesterday.getMonth() && d.getMonth() === yesterday.getMonth() &&
d.getFullYear() === yesterday.getFullYear(); d.getFullYear() === yesterday.getFullYear();
if (isYesterday) { 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" }) + return `${d.toLocaleDateString(locale, { month: "short", day: "numeric" })} ${time}`;
" " +
d.toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" });
} }