← Back to Docs
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

  1. Detect user locale from Accept-Language header
  2. Map locale to currency code (en-US → USD, de-DE → EUR)
  3. Fetch exchange rates from a cached KV store
  4. Format with Intl.NumberFormat
  5. 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