Skip to content

Commit 79772c5

Browse files
committed
fix(i18n): locale-aware number formatting
Read the active locale from the i18n store at format time instead of hardcoding 'en-US', so thousand separators, decimal markers and compact suffixes match the user's chosen language (e.g. '1.234,56' for German, '1\u00a0234,56' for French, '1,23 \u4e07' for Chinese).
1 parent d8f9814 commit 79772c5

1 file changed

Lines changed: 14 additions & 3 deletions

File tree

src/lib/format.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
11
// Compact, locale-aware formatters shared across the home dashboard.
22

3+
import { getLocale, type Locale } from './i18n'
4+
5+
/**
6+
* Map our Locale code to a BCP 47 tag for Intl.NumberFormat. The locale codes
7+
* happen to be valid BCP 47 already (en, es, fr, de, ru, zh, ja, ko), but route
8+
* through this helper so a future tag like `pt-BR` only changes here.
9+
*/
10+
function intlTag(locale: Locale): string {
11+
return locale
12+
}
13+
314
/** Abbreviate large numbers (e.g. 1.23M, 4.5K) with a sensible precision. */
415
export function formatCompactNumber(value: number, maximumFractionDigits = 2): string {
516
if (!Number.isFinite(value)) return '—'
6-
return new Intl.NumberFormat('en-US', {
17+
return new Intl.NumberFormat(intlTag(getLocale()), {
718
notation: 'compact',
819
maximumFractionDigits,
920
}).format(value)
@@ -12,7 +23,7 @@ export function formatCompactNumber(value: number, maximumFractionDigits = 2): s
1223
/** Full grouped integer/decimal formatting (e.g. 11,790). */
1324
export function formatNumber(value: number, maximumFractionDigits = 0): string {
1425
if (!Number.isFinite(value)) return '—'
15-
return new Intl.NumberFormat('en-US', { maximumFractionDigits }).format(value)
26+
return new Intl.NumberFormat(intlTag(getLocale()), { maximumFractionDigits }).format(value)
1627
}
1728

1829
/** Human-readable byte sizes (e.g. 462 B, 1.2 KB, 3.4 MB). */
@@ -34,7 +45,7 @@ export function shortHash(hash: string, lead = 8, tail = 6): string {
3445
export function formatUsd(value: number): string {
3546
if (!Number.isFinite(value)) return '—'
3647
const fractionDigits = value > 0 && value < 1 ? 4 : 2
37-
return new Intl.NumberFormat('en-US', {
48+
return new Intl.NumberFormat(intlTag(getLocale()), {
3849
style: 'currency',
3950
currency: 'USD',
4051
minimumFractionDigits: 2,

0 commit comments

Comments
 (0)