Files
DeepTutor/web/components/space/learning/format.ts
Botong dd29ff5a0a fix(i18n): sync frontend output language and remove zh-only UI fallbacks
Align frontend model-output language propagation and eliminate Simplified-Chinese-only UI shortcuts across chat, settings, books, reading, memory, and workspace surfaces:

- Preserve Traditional Chinese (zh-tw/zh-hk/zh-mo/zh-hant) and native aliases in resolveResponseLanguage(), and introduce shared effectiveResponseLanguage() and subscribeResponseLanguage() helpers.
- Sync response_language with language in SettingsStore when both were aligned, while preserving intentional divergence and clearing auto-synced pending response_language if an external desktop menu overrides language.
- Preserve per-conversation replyLanguageOverride when regenerating without a replay snapshot and subscribe ChatWorkspace's default reply-language label to live response_language changes.
- Route Book Creator, Book Chat fallback, Memory evolution/update, and Reading extension actions through effectiveResponseLanguage() instead of the UI language, and preserve zh-Hant in translationTarget().
- Route remaining zh ? ... : t(...) branches, hardcoded Memory labels, and binary zh/en date formatting through i18next and normalizeLanguage() so Traditional Chinese and all non-Simplified locales render through catalogs without modifying locale JSON files.

Related to #2933
Related to #684
2026-11-29 02:13:55 -06:00

85 lines
2.5 KiB
TypeScript

/** Shared presentation helpers for the Mastery Path dashboard. */
import i18n from "i18next";
import { normalizeLanguage } from "@/i18n/languages";
import { formatDate, getLocale } from "@/lib/datetime";
/** The `t` from `useTranslation()`, narrowed to what these helpers need. */
export type Translate = (key: string) => string;
const KNOWLEDGE_TYPE_LABELS: Record<string, string> = {
concept: "Concept",
memory: "Memory",
procedure: "Procedure",
design: "Design",
};
export function knowledgeTypeLabel(type: string, t: Translate): string {
const label = KNOWLEDGE_TYPE_LABELS[type];
return label ? t(label) : type;
}
/**
* Older paths did not require a human-readable title and sometimes stored the
* generated path id as the name. Keep a short trace suffix without exposing a
* database-shaped identifier as the primary label.
*/
export function topicDisplayName(
topic: { name: string; path_id: string },
t: Translate,
): string {
const name = topic.name.trim();
if (
name &&
name !== topic.path_id &&
!/^unified_\d+_[a-z0-9]+$/i.test(name)
) {
return name;
}
const suffix = topic.path_id.split("_").at(-1)?.slice(-4) || "map";
return `${t("Exploration trail")} · ${suffix}`;
}
const MINUTE = 60;
const HOUR = 60 * MINUTE;
const DAY = 24 * HOUR;
/**
* "3 分钟前" / "in 2 days" — engine timestamps are epoch seconds and can point
* either way (an attempt happened, a review is due).
*/
export function formatRelative(epochSeconds: number, zh: boolean): string {
const deltaSeconds = epochSeconds - Date.now() / 1000;
const past = deltaSeconds < 0;
const abs = Math.abs(deltaSeconds);
const [value, unit]: [number, Intl.RelativeTimeFormatUnit] =
abs < MINUTE
? [Math.round(abs), "second"]
: abs < HOUR
? [Math.round(abs / MINUTE), "minute"]
: abs < DAY
? [Math.round(abs / HOUR), "hour"]
: [Math.round(abs / DAY), "day"];
return new Intl.RelativeTimeFormat(getLocale(dateLanguage(zh)), {
numeric: "auto",
}).format(past ? -value : value, unit);
}
// Follow the active interface language (preserving zh-tw and every other locale).
function dateLanguage(_zh: boolean) {
return normalizeLanguage(i18n.language);
}
/** Calendar form, for when "in 3 days" is not precise enough. */
export function formatAbsolute(epochSeconds: number, zh: boolean): string {
return formatDate(new Date(epochSeconds * 1000), dateLanguage(zh), {
month: "short",
day: "numeric",
hour: "2-digit",
minute: "2-digit",
});
}