/** Adding a locale: add the code here and add one catalog file. Nothing else. */ export const SUPPORTED_LOCALES = ['es', 'en'] as const; export type Locale = (typeof SUPPORTED_LOCALES)[number]; export const DEFAULT_LOCALE: Locale = 'es'; export function isLocale(value: unknown): value is Locale { return typeof value === 'string' && (SUPPORTED_LOCALES as readonly string[]).includes(value); } /** * Picks a locale from an `Accept-Language` header. Used by the API only as the * last fallback: a stored `profiles.locale` always wins when the user is known. */ export function localeFromAcceptLanguage(header: string | null | undefined): Locale { if (!header) return DEFAULT_LOCALE; const ranked = header .split(',') .map((part) => { const [tag = '', ...rest] = part.trim().split(';'); const q = rest.find((p) => p.trim().startsWith('q='))?.split('=')[1]; return { tag: tag.trim().toLowerCase(), q: q ? Number(q) : 1 }; }) .filter((entry) => entry.tag.length > 0 && Number.isFinite(entry.q)) .sort((a, b) => b.q - a.q); for (const { tag } of ranked) { const base = tag.split('-')[0]; if (isLocale(base)) return base; } return DEFAULT_LOCALE; }