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:
@@ -1,14 +1,17 @@
|
||||
import { formatPrice, formatWeight } from "../lib/formatters";
|
||||
import { useCurrency } from "./useCurrency";
|
||||
import { useLanguage } from "./useLanguage";
|
||||
import { useWeightUnit } from "./useWeightUnit";
|
||||
|
||||
export function useFormatters() {
|
||||
const unit = useWeightUnit();
|
||||
const { currency } = useCurrency();
|
||||
const locale = useLanguage();
|
||||
return {
|
||||
weight: (grams: number | null) => formatWeight(grams, unit),
|
||||
price: (cents: number | null) => formatPrice(cents, currency),
|
||||
weight: (grams: number | null) => formatWeight(grams, unit, locale),
|
||||
price: (cents: number | null) => formatPrice(cents, currency, locale),
|
||||
unit,
|
||||
currency,
|
||||
locale,
|
||||
};
|
||||
}
|
||||
|
||||
12
src/client/hooks/useLanguage.ts
Normal file
12
src/client/hooks/useLanguage.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { useSetting } from "./useSettings";
|
||||
|
||||
export const VALID_LANGUAGES = ["en", "de"] as const;
|
||||
export type Language = (typeof VALID_LANGUAGES)[number];
|
||||
|
||||
export function useLanguage(): Language {
|
||||
const { data } = useSetting("language");
|
||||
if (data && VALID_LANGUAGES.includes(data as Language)) {
|
||||
return data as Language;
|
||||
}
|
||||
return "en";
|
||||
}
|
||||
Reference in New Issue
Block a user