ECOSYSTEM / I18N

Catalogs

One JSON file per language, with placeholders and plurals, loaded once before the application starts.

Ecosystem package janelkit-i18n Translation catalogs, plurals, locale-aware number, currency and date formats, and language switching at run time. pip install janelkit-i18n
I18N · PAGE 2 / 4~1 min read
Alpha / 0.2 — These pages document JanelKit 0.2 and its packages. Each series (0.1, 0.2…) may change the public API, always with migration notes: see Changes and migration. The website uses Svelte; the desktop examples use Python and JanelKit, not JavaScript.

Write one catalog per language

A catalog is a JSON file named after its language, such as en.json or es.json, inside a folder of your project. Values may contain {placeholders}, filled with the keyword arguments of t. A plural entry is a dictionary with one and other, plus an optional zero; it is chosen with the n parameter.

json
{
  "hello": "Hello, {name}",
  "files": {"zero": "No files", "one": "{n} file", "other": "{n} files"}
}
Copy

Load catalogs once, use them anywhere

Load the catalogs before the application starts, then use t in any component. The fallback language for missing keys is Spanish by default; set i18n.default for an English application.

python
# main.py
from pathlib import Path
from janelkit import run
from janelkit_i18n import i18n

if __name__ == "__main__":
    root = Path(__file__).parent
    i18n.load(root / "locales")   # locales/en.json, es.json, ar.json…
    i18n.default = "en"           # fallback for missing keys
    i18n.set("en")
    raise SystemExit(run(root / "src" / "App.janel"))
Copy

How it works

  1. load reads every catalog in the folder. add(language, dict) does the same from Python data.
  2. default is the language used when a key is missing in the active one. If the key is missing there too, the key itself is shown and a warning is logged to the janelkit logger.
  3. set changes the active language. It raises I18nError when there is no catalog for that language.

Common mistakes

  • Do not change janelkit.locale directly to switch translations; call i18n.set so the catalog is checked first.

English documentation based on the JanelKit source. For additional details, browse the source docs ↗.