|
| 1 | +/** |
| 2 | + * Locale-aware formatting utilities. |
| 3 | + * All functions accept an optional `locale` parameter (BCP 47 tag). |
| 4 | + * When omitted they fall back to the current i18n language or browser default. |
| 5 | + */ |
| 6 | + |
| 7 | +import i18n from './index'; |
| 8 | + |
| 9 | +function currentLocale() { |
| 10 | + return i18n.language || navigator.language || 'en'; |
| 11 | +} |
| 12 | + |
| 13 | +/** |
| 14 | + * Format a number for the current locale. |
| 15 | + * @param {number} value |
| 16 | + * @param {Intl.NumberFormatOptions} [options] |
| 17 | + * @param {string} [locale] |
| 18 | + */ |
| 19 | +export function formatNumber(value, options = {}, locale = currentLocale()) { |
| 20 | + return new Intl.NumberFormat(locale, options).format(value); |
| 21 | +} |
| 22 | + |
| 23 | +/** |
| 24 | + * Format a currency amount. |
| 25 | + * @param {number} amount |
| 26 | + * @param {string} currency ISO 4217 code, e.g. 'USD', 'EUR' |
| 27 | + * @param {string} [locale] |
| 28 | + */ |
| 29 | +export function formatCurrency(amount, currency, locale = currentLocale()) { |
| 30 | + return new Intl.NumberFormat(locale, { |
| 31 | + style: 'currency', |
| 32 | + currency, |
| 33 | + minimumFractionDigits: 2, |
| 34 | + maximumFractionDigits: 7, |
| 35 | + }).format(amount); |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * Format a date/time value. |
| 40 | + * @param {Date|number|string} value |
| 41 | + * @param {Intl.DateTimeFormatOptions} [options] |
| 42 | + * @param {string} [locale] |
| 43 | + */ |
| 44 | +export function formatDate(value, options = { dateStyle: 'medium' }, locale = currentLocale()) { |
| 45 | + return new Intl.DateTimeFormat(locale, options).format(new Date(value)); |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Format a date+time. |
| 50 | + * @param {Date|number|string} value |
| 51 | + * @param {string} [locale] |
| 52 | + */ |
| 53 | +export function formatDateTime(value, locale = currentLocale()) { |
| 54 | + return formatDate(value, { dateStyle: 'medium', timeStyle: 'short' }, locale); |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * Format a relative time (e.g. "3 minutes ago"). |
| 59 | + * @param {Date|number} value Past or future date |
| 60 | + * @param {string} [locale] |
| 61 | + */ |
| 62 | +export function formatRelativeTime(value, locale = currentLocale()) { |
| 63 | + const diffMs = new Date(value) - Date.now(); |
| 64 | + const diffSec = Math.round(diffMs / 1000); |
| 65 | + const rtf = new Intl.RelativeTimeFormat(locale, { numeric: 'auto' }); |
| 66 | + |
| 67 | + const thresholds = [ |
| 68 | + [60, 'second', 1], |
| 69 | + [3600, 'minute', 60], |
| 70 | + [86400, 'hour', 3600], |
| 71 | + [Infinity, 'day', 86400], |
| 72 | + ]; |
| 73 | + |
| 74 | + for (const [limit, unit, divisor] of thresholds) { |
| 75 | + if (Math.abs(diffSec) < limit) { |
| 76 | + return rtf.format(Math.round(diffSec / divisor), unit); |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * Map a locale code to its preferred currency code. |
| 83 | + * Falls back to USD for unknown locales. |
| 84 | + */ |
| 85 | +const LOCALE_CURRENCY_MAP = { |
| 86 | + en: 'USD', 'en-GB': 'GBP', 'en-AU': 'AUD', 'en-CA': 'CAD', |
| 87 | + ar: 'SAR', 'ar-AE': 'AED', 'ar-EG': 'EGP', |
| 88 | + he: 'ILS', |
| 89 | + fr: 'EUR', 'fr-CH': 'CHF', |
| 90 | + es: 'EUR', 'es-MX': 'MXN', 'es-AR': 'ARS', 'es-CO': 'COP', |
| 91 | + zh: 'CNY', 'zh-TW': 'TWD', 'zh-HK': 'HKD', |
| 92 | + pt: 'EUR', 'pt-BR': 'BRL', |
| 93 | +}; |
| 94 | + |
| 95 | +/** |
| 96 | + * Get the preferred currency code for a locale. |
| 97 | + * @param {string} [locale] |
| 98 | + * @returns {string} ISO 4217 currency code |
| 99 | + */ |
| 100 | +export function getLocaleCurrency(locale = currentLocale()) { |
| 101 | + return LOCALE_CURRENCY_MAP[locale] |
| 102 | + ?? LOCALE_CURRENCY_MAP[locale.split('-')[0]] |
| 103 | + ?? 'USD'; |
| 104 | +} |
0 commit comments