SPB Git forge

spb/countryatlas

Public
20commits 1branches 0releases
268.3 MBsize
maindefault branch
12 days agolast push
TypeScript 57% Python 38.6% JavaScript 3.6% CSS 0.6%
1003 B · 29 lines typescript
Raw Blame History
1import { en, type DictKey } from './en';23/** Fixed locale for every Intl call, server and client, so hydration never diverges. */4export const LOCALE = 'en-US' as const;56type Params = Record<string, string | number | null | undefined>;78/**9 * Translate a dictionary key, interpolating `{name}` placeholders.10 * Usage: `t('metric.rankWorld', { rank: '12th', n: 190 })`.11 * Missing keys return the key itself so a typo is visible rather than silent.12 */13export function t(key: DictKey, params?: Params): string {14  const raw: string = en[key] ?? key;15  if (!params) return raw;16  return raw.replace(/\{(\w+)\}/g, (_, k: string) => {17    const v = params[k];18    return v == null ? '' : String(v);19  });20}2122/** Optional-key variant for dynamic keys (e.g. `change.kind.${kind}`); returns `fallback` when the key is unknown. */23export function tOpt(key: string, fallback: string, params?: Params): string {24  if (key in en) return t(key as DictKey, params);25  return fallback;26}2728export type { DictKey };29