mirror of
https://github.com/AgentSeal/codeburn.git
synced 2026-11-29 02:39:13 +00:00
The desktop app and the macOS menu bar can now run in English, French, Japanese, Korean, Simplified Chinese and Traditional Chinese. A Language setting in the desktop Settings picks the language or follows the system, and the choice is shared so the menu bar switches with it, relaunching to pick up the language with a retry so a switch never leaves it down. Every user-facing string in the desktop renderer goes through a local lookup table, 1,249 keys per language, no framework and no network. The menu bar gains four .lproj to join English and Simplified Chinese, 626 keys each. A completeness gate fails the build if any language is missing a key, in both the desktop catalogs and the menu bar coverage suite. Fonts fall back per language so Japanese, Korean and both Chinese render their correct glyph forms, and dates and numbers format for the locale with values unchanged. English output is byte-identical. Strings produced by the CLI engine stay English until the CLI is localized, a separate effort. Validated live in all six languages: no missing text, no clipping, correct fonts, numbers unchanged, and the menu bar switching for every language.
102 lines
4.5 KiB
TypeScript
102 lines
4.5 KiB
TypeScript
import { localeTag, t } from '../i18n'
|
|
|
|
export type ActiveCurrency = { code: string; symbol: string; rate: number }
|
|
|
|
// Single source of truth for display currency. App.tsx sets it from the overview
|
|
// payload; every formatUsd/formatConverted call site then converts for free.
|
|
// Defaults to USD so the first render (before the payload arrives) is correct.
|
|
let activeCurrency: ActiveCurrency = { code: 'USD', symbol: '$', rate: 1 }
|
|
|
|
export function setActiveCurrency(currency: ActiveCurrency): void {
|
|
activeCurrency = currency
|
|
}
|
|
|
|
/** Raw-USD input: multiplies by the active FX rate, then prefixes the symbol. */
|
|
export function formatUsd(n: number): string {
|
|
return formatUsdWithCurrency(n, activeCurrency)
|
|
}
|
|
|
|
/** Raw-USD input formatted against an explicit payload currency. This keeps a
|
|
* persisted exact snapshot correct on its very first paint, before App's
|
|
* global active-currency effect has had a chance to run. */
|
|
export function formatUsdWithCurrency(n: number, currency: ActiveCurrency): string {
|
|
return `${currency.symbol}${(n * currency.rate).toLocaleString(localeTag(), { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`
|
|
}
|
|
|
|
/**
|
|
* Already-converted input (CLI-side convertCost values, e.g. plan budgets): only
|
|
* prefixes the active symbol and formats the magnitude — never re-applies the rate.
|
|
*/
|
|
export function formatConverted(n: number): string {
|
|
return `${activeCurrency.symbol}${n.toLocaleString(localeTag(), { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`
|
|
}
|
|
|
|
/** Shorten filesystem and CLI-mangled project paths to their useful trailing segments. */
|
|
export function shortenProjectPath(value: string, maxSegments = 3): string {
|
|
const trimmed = value.trim()
|
|
const pathDelimited = /[\\/]/.test(trimmed)
|
|
const parts = trimmed.split(pathDelimited ? /[\\/]+/ : /-+/).filter(Boolean)
|
|
let displayParts = parts.slice(-Math.max(1, maxSegments))
|
|
|
|
// A tail rooted directly under a home directory starts with the user name,
|
|
// which adds noise rather than useful project context.
|
|
const precedingPart = parts.at(-(displayParts.length + 1))
|
|
if (displayParts.length > 1 && /^(users?|home)$/i.test(precedingPart ?? '')) {
|
|
displayParts = displayParts.slice(1)
|
|
}
|
|
|
|
if (/^(projects|src|config)$/i.test(displayParts[0] ?? '')) {
|
|
displayParts[0] = displayParts[0].toLowerCase()
|
|
}
|
|
|
|
return displayParts.join('/') || trimmed
|
|
}
|
|
|
|
/** "1 session" / "2,048 sessions" — one count label for every count site, so the
|
|
* separator and the noun form never drift between screens. */
|
|
export function formatCount(n: number, singular: string, plural = `${singular}s`): string {
|
|
const count = n.toLocaleString(localeTag())
|
|
const stem = singular.replace(/\s+/g, '_')
|
|
const template = t(`common.count.${stem}.${n === 1 ? 'one' : 'other'}`)
|
|
// Fall back to English when a noun has no catalog entry (templates carry {count}).
|
|
return template.includes('{count}') ? template.replace('{count}', count) : `${count} ${n === 1 ? singular : plural}`
|
|
}
|
|
|
|
/** Compact token/count formatting: 1_842 → "1.8K", 184_000 → "184K", 1_200_000 → "1.2M". */
|
|
export function formatCompact(n: number): string {
|
|
if (!Number.isFinite(n)) return '—'
|
|
if (n === 0) return '0'
|
|
const abs = Math.abs(n)
|
|
if (abs < 1_000) return String(Math.round(n))
|
|
if (abs < 1_000_000) return `${trim(n / 1_000)}K`
|
|
if (abs < 1_000_000_000) return `${trim(n / 1_000_000)}M`
|
|
return `${trim(n / 1_000_000_000)}B`
|
|
}
|
|
|
|
// One decimal, but drop a trailing ".0" (184.0K → "184K", 1.2K stays "1.2K").
|
|
function trim(v: number): string {
|
|
const s = v.toFixed(1)
|
|
return s.endsWith('.0') ? s.slice(0, -2) : s
|
|
}
|
|
|
|
/** "Jul 10" — short month + day, no year. */
|
|
export function formatDayShort(iso: string): string {
|
|
const d = new Date(iso)
|
|
return Number.isNaN(d.getTime()) ? '—' : d.toLocaleDateString(localeTag(), { month: 'short', day: 'numeric' })
|
|
}
|
|
|
|
/** "Jul 10, 2026" — full date. */
|
|
export function formatDayLong(iso: string): string {
|
|
const d = new Date(iso)
|
|
return Number.isNaN(d.getTime()) ? '—' : d.toLocaleDateString(localeTag(), { month: 'short', day: 'numeric', year: 'numeric' })
|
|
}
|
|
|
|
/** "12 days" / "2h 14m" / "47m" / "38s" from a duration in ms. */
|
|
export function formatDuration(ms: number): string {
|
|
if (!Number.isFinite(ms) || ms <= 0) return '—'
|
|
const totalMin = Math.floor(ms / 60_000)
|
|
if (totalMin < 1) return `${Math.floor(ms / 1000)}s`
|
|
if (totalMin < 60) return `${totalMin}m`
|
|
if (totalMin >= 2_880) return t('common.duration.days', { count: Math.round(totalMin / 1_440) })
|
|
return `${Math.floor(totalMin / 60)}h ${totalMin % 60}m`
|
|
}
|