Skip to content

Messages and parameters

Let TypeScript check the keys and the values each message needs.

Interpolation

Write parameters in the form {name}. Each required argument accepts a string, number or boolean; a required value cannot be null or undefined.

JSON catalog json
{
  "greeting": "Hola {name}",
  "ready": "Listo para traducir",
  "cart": {
    "items": {
      "plural": "total",
      "forms": {
        "=0": "Tu cesta está vacía",
        "one": "{count} elemento en tu cesta",
        "other": "{count} elementos en tu cesta"
      }
    }
  }
}
Valid calls and expected errors ts
const { t } = useI18n()
t('greeting', { name: 'Meeko' })
t('ready')
t('cart.items', { total: 3 })

// Expected TypeScript diagnostics:
t('greeting')                        // missing name
t('cart.items', { total: 'three' })  // requires a number
t('unknown')                         // nonexistent key

The same contract between languages

Every language needs the same namespaces, keys and parameters. If one translation uses name and another uses user for the same message, preparing the module fails. Plural categories can vary by language.

Public types

Types exported from @meekojs/i18n
Name Type / default Description
I18n interface Contract of the translator and its formatters.
I18nValue string | number | boolean Values for interpolation and selectors.
I18nMessageValues<K> generic type Required arguments for a key of the generated registry.

Dynamic keys

A union of keys needs valid arguments for every alternative. Narrow the key with an if when each case has different parameters. If any alternative requires values, the argument cannot be omitted.

Old registries declared as true keep their open contract. The module generates new registries with explicit parameters.

Was this page helpful?

Add a comment

It is sent when you choose “Yes” or “No”. Only the page, your answer and the comment are stored: no cookies and no personal data.

0 of 2000 characters