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
| Name | Summary |
|---|---|
detectLanguage | Detects the user's preferred language from the browser or Node.js environment. |
formatDateI18n | Formats a date as a localized string. |
formatNumber | Formats a number as a localized string. |
t | Simple translation function using a dictionary object. |