Recipe
Locale-aware pricing display
Display prices in the user's local currency with automatic formatting, symbol placement, and fallback chains.
Problem
Users see prices in unfamiliar currencies. A flat $9.99 means nothing to someone in Tokyo or Berlin. You need locale-aware formatting that respects symbol position, decimal separators, and grouping conventions.
Solution
Use Intl.NumberFormat with the user's detected locale and currency code. Store prices in a single base currency (USD) and convert at display time via cached exchange rates. Fall back gracefully when rates are stale.
Key steps
- Detect user locale from Accept-Language header
- Map locale to currency code (en-US → USD, de-DE → EUR)
- Fetch exchange rates from a cached KV store
- Format with
Intl.NumberFormat - Cache formatted strings per locale+price pair
Edge cases
- Zero-decimal currencies (JPY, KRW) — strip fractional digits
- Right-to-left locales — ensure symbol alignment
- Stale exchange rates — serve stale, refresh async
- Unknown locale — fall back to USD with a flag
Related: Geo-based redirects · A/B test routing