feat(i18n): locale-aware formatters and useLanguage hook

- Create useLanguage() hook following useCurrency/useWeightUnit pattern
- Update formatPrice() to use Intl.NumberFormat for locale-aware currency display
- Update formatWeight() to use Intl.NumberFormat for locale-aware number formatting
- Update formatDualPrice() to pass locale through
- Update useFormatters() to pass locale to all formatters
- Add formatter tests for en/de locales (15 tests passing)

Phase 34, Plan 03
This commit is contained in:
2026-04-13 18:20:23 +02:00
parent 672b17fd13
commit f759dd0fde
4 changed files with 127 additions and 22 deletions

79
tests/formatters.test.ts Normal file
View File

@@ -0,0 +1,79 @@
import { describe, expect, test } from "bun:test";
import { formatPrice, formatWeight } from "../src/client/lib/formatters";
describe("formatPrice", () => {
test("returns -- for null", () => {
expect(formatPrice(null)).toBe("--");
});
test("returns -- for undefined", () => {
expect(formatPrice(undefined)).toBe("--");
});
test("formats USD with en locale", () => {
const result = formatPrice(12345, "USD", "en");
expect(result).toContain("123.45");
expect(result).toContain("$");
});
test("formats EUR with de locale", () => {
const result = formatPrice(12345, "EUR", "de");
// German uses comma for decimal
expect(result).toContain("123,45");
});
test("formats JPY with no decimals", () => {
const result = formatPrice(10000, "JPY", "en");
expect(result).toContain("100");
});
test("formats large amounts with en locale", () => {
const result = formatPrice(123456789, "USD", "en");
expect(result).toContain("$");
});
test("defaults to en locale when no locale provided", () => {
const result = formatPrice(12345, "USD");
expect(result).toContain("$");
});
});
describe("formatWeight", () => {
test("returns -- for null", () => {
expect(formatWeight(null)).toBe("--");
});
test("returns -- for undefined", () => {
expect(formatWeight(undefined)).toBe("--");
});
test("formats grams with en locale", () => {
const result = formatWeight(500, "g", "en");
expect(result).toBe("500g");
});
test("formats large grams with en locale thousands separator", () => {
const result = formatWeight(1234, "g", "en");
expect(result).toContain("g");
});
test("formats ounces", () => {
const result = formatWeight(100, "oz", "en");
expect(result).toContain("oz");
});
test("formats kilograms", () => {
const result = formatWeight(1500, "kg", "en");
expect(result).toContain("kg");
});
test("formats pounds", () => {
const result = formatWeight(1000, "lb", "en");
expect(result).toContain("lb");
});
test("defaults to en locale when no locale provided", () => {
const result = formatWeight(500, "g");
expect(result).toBe("500g");
});
});