i18n

Message catalogues for Ecko: JSON files, {name} interpolation, and CLDR plural rules for the languages that need more than two forms.

ecko get github.com/ecko-lang/i18n
import i18n

Declares fs:read. A capability is only advisory in the manifest: what the package actually gets is the grant you give it when you import it.

Version 0.30.0 - source - MIT.


load(path, lang = null)

load(path, lang?) -> a catalogue read from a JSON file.

lang is the language tag used to pick plural forms. When it is not given it is taken from the file's name, so locales/fr.json is French and locales/pt-BR.json is Brazilian Portuguese.

Needs fs:read.

load("locales/fr.json")

catalog(messages, lang)

catalog(messages, lang) -> a catalogue from a map you already have.

The form to use when the messages are embedded, fetched, or assembled rather than read from disk. Needs no capabilities.

catalog({ greeting: "Hello, {name}" }, "en")

has(cat, key)

has(cat, key) -> whether the catalogue carries key.

For deciding whether to fall back to another language, rather than catching an error to find out.

tr(cat, key, vars = empty_map())

tr(cat, key, vars?) -> the message for key, with {name} placeholders filled.

A missing key raises rather than echoing the key back. A key echoed into a page looks deliberate and survives review; an error does not.

tr(t, "greeting", { name: "Ada" })

plural(cat, key, n, vars = empty_map())

plural(cat, key, n, vars?) -> the message form that n selects.

The entry is a map of CLDR categories to messages. {n} is filled with the count unless vars gives it another value, because every plural message wants the number in it.

plural(t, "items", 3)   # entry: { one: "{n} item", other: "{n} items" }