Skip to main content

I18n

A minimal localisation layer — locale detection, Intl-backed number and date formatting, and a dictionary-lookup t. Formatting delegates to Intl, so the locale data is the platform's rather than the bundle's, while translation is deliberately naive: a flat dictionary lookup. For ICU messages, per-language plural categories or namespaced catalogs, use i18next or FormatJS. English-only pluralisation lives in strings.

Example

import { formatNumber, t } from '@rtorcato/js-common/i18n'

const dict = {
en: { greeting: 'Hello' },
fr: { greeting: 'Bonjour' },
}

t('greeting', dict, 'fr') // 'Bonjour'
t('greeting', dict, 'de') // 'Hello' — falls back to English
t('missing', dict, 'fr') // 'missing' — then to the key itself

formatNumber(1234.5, 'de-DE') // '1.234,5'

Import

import { detectLanguage, formatDateI18n, formatNumber } from '@rtorcato/js-common/i18n'

Exports

NameSummary
detectLanguageDetects the user's preferred language from the browser or Node.js environment.
formatDateI18nFormats a date as a localized string.
formatNumberFormats a number as a localized string.
tSimple translation function using a dictionary object.

See also

  • numbers — sum, average, clamp, roundTo
  • currency — price formatting, parsing and currency codes
  • strings — slugify, truncate, casing, emoji stripping
  • date — calendar-day helpers — add, diff, compare, format