Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/constants/i18n.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,28 @@ describe("i18n utility functions", () => {
expect(formatCurrency(1200, "USD", "en-US")).toContain("$");
});

it("uses the requested currency even when the locale has a different default", () => {
const usdInGerman = formatCurrency(100, "USD", "de-DE");
expect(usdInGerman).toContain("$");
expect(usdInGerman).not.toContain("€");
expect(formatCurrency(100, "EUR", "en-US")).toContain("€");
expect(formatCurrency(100, "GBP", "ja-JP")).toBe(
new Intl.NumberFormat("ja-JP", { style: "currency", currency: "GBP" }).format(100),
);
});

it("falls back to the locale default currency when none is requested", () => {
expect(formatCurrency(100, undefined, "de-DE")).toContain("€");
expect(formatCurrency(100, undefined, "ja-JP")).toBe(
new Intl.NumberFormat("ja-JP", { style: "currency", currency: "JPY" }).format(100),
);
});

it("only drops decimals when the requested currency is JPY", () => {
expect(formatCurrency(1234.5, "JPY", "en-US")).not.toMatch(/\.\d/);
expect(formatCurrency(1234.5, "USD", "ja-JP")).toContain(".50");
});

it("formats dates using locale settings", () => {
expect(formatDate("2026-05-10", "en-US")).toContain("2026");
expect(formatDate("2026-05-10", "de-DE")).toContain("2026");
Expand Down
20 changes: 10 additions & 10 deletions src/constants/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const LOCALE_FORMAT_SETTINGS: Record<string, LocaleMeta> = {
defaultCurrency: "USD",
date: { year: "numeric", month: "short", day: "numeric" },
number: { maximumFractionDigits: 2 },
currency: { style: "currency", currency: "USD", currencyDisplay: "symbol", maximumFractionDigits: 2 },
currency: { style: "currency", currencyDisplay: "symbol" },
},
"de-DE": {
code: "de-DE",
Expand All @@ -71,7 +71,7 @@ export const LOCALE_FORMAT_SETTINGS: Record<string, LocaleMeta> = {
defaultCurrency: "EUR",
date: { year: "numeric", month: "numeric", day: "numeric" },
number: { maximumFractionDigits: 2 },
currency: { style: "currency", currency: "EUR", currencyDisplay: "symbol", maximumFractionDigits: 2 },
currency: { style: "currency", currencyDisplay: "symbol" },
},
"ja-JP": {
code: "ja-JP",
Expand All @@ -80,7 +80,7 @@ export const LOCALE_FORMAT_SETTINGS: Record<string, LocaleMeta> = {
defaultCurrency: "JPY",
date: { year: "numeric", month: "numeric", day: "numeric" },
number: { maximumFractionDigits: 0, useGrouping: true },
currency: { style: "currency", currency: "JPY", currencyDisplay: "symbol", maximumFractionDigits: 0 },
currency: { style: "currency", currencyDisplay: "symbol" },
},
"ar-SA": {
code: "ar-SA",
Expand All @@ -89,7 +89,7 @@ export const LOCALE_FORMAT_SETTINGS: Record<string, LocaleMeta> = {
defaultCurrency: "SAR",
date: { year: "numeric", month: "numeric", day: "numeric" },
number: { maximumFractionDigits: 2, useGrouping: true },
currency: { style: "currency", currency: "SAR", currencyDisplay: "symbol", maximumFractionDigits: 2 },
currency: { style: "currency", currencyDisplay: "symbol" },
},
"fr-FR": {
code: "fr-FR",
Expand All @@ -98,7 +98,7 @@ export const LOCALE_FORMAT_SETTINGS: Record<string, LocaleMeta> = {
defaultCurrency: "EUR",
date: { year: "numeric", month: "numeric", day: "numeric" },
number: { maximumFractionDigits: 2 },
currency: { style: "currency", currency: "EUR", currencyDisplay: "symbol", maximumFractionDigits: 2 },
currency: { style: "currency", currencyDisplay: "symbol" },
},
"es-ES": {
code: "es-ES",
Expand All @@ -107,7 +107,7 @@ export const LOCALE_FORMAT_SETTINGS: Record<string, LocaleMeta> = {
defaultCurrency: "EUR",
date: { year: "numeric", month: "numeric", day: "numeric" },
number: { maximumFractionDigits: 2 },
currency: { style: "currency", currency: "EUR", currencyDisplay: "symbol", maximumFractionDigits: 2 },
currency: { style: "currency", currencyDisplay: "symbol" },
},
"zh-CN": {
code: "zh-CN",
Expand All @@ -116,7 +116,7 @@ export const LOCALE_FORMAT_SETTINGS: Record<string, LocaleMeta> = {
defaultCurrency: "CNY",
date: { year: "numeric", month: "numeric", day: "numeric" },
number: { maximumFractionDigits: 2, useGrouping: true },
currency: { style: "currency", currency: "CNY", currencyDisplay: "symbol", maximumFractionDigits: 2 },
currency: { style: "currency", currencyDisplay: "symbol" },
},
"en-GB": {
code: "en-GB",
Expand All @@ -125,7 +125,7 @@ export const LOCALE_FORMAT_SETTINGS: Record<string, LocaleMeta> = {
defaultCurrency: "GBP",
date: { year: "numeric", month: "short", day: "numeric" },
number: { maximumFractionDigits: 2 },
currency: { style: "currency", currency: "GBP", currencyDisplay: "symbol", maximumFractionDigits: 2 },
currency: { style: "currency", currencyDisplay: "symbol" },
},
};

Expand Down Expand Up @@ -165,13 +165,13 @@ export function formatCurrency(
locale: string = "en-US",
options: Intl.NumberFormatOptions = {},
): string {
const defaultCurr = currency ?? LOCALE_FORMAT_SETTINGS[locale]?.defaultCurrency ?? "USD";
const resolvedCurrency = currency ?? LOCALE_FORMAT_SETTINGS[locale]?.defaultCurrency ?? "USD";
const setting = LOCALE_FORMAT_SETTINGS[locale]?.currency ?? {};
return new Intl.NumberFormat(locale, {
style: "currency",
currency: defaultCurr,
currencyDisplay: "symbol",
...setting,
currency: resolvedCurrency,
...options,
}).format(value);
}
Expand Down