Localization
localization
Drop translations into JSON files and reach for them anywhere in my app with a tiny helper. The locale lives in context, so each request transparently picks up its own language without extra plumbing.
Prepare translations
- Create a
lang/directory (the default lookup path) next to my entry point or point the helper elsewhere withset_locale_path()orLOCALE_PATH. - Add
<locale>.jsonfiles such aslang/en.json. Dot keys map to nested dictionaries:
json
{
"messages": {
"welcome": "Welcome back, {name}!"
},
"cart_count": "You have {count} item",
"cart_count_plural": "You have {count} items"
}
Pick the locale
LOCALE_DEFAULTdecides the starting locale (defaults toen). Override it in.envor at runtime withset_locale("cs"). The value is stored in a context variable, so each async task or request keeps its own setting.LOCALE_FALLBACK(defaults toen) provides a second chance when the active locale lacks a key.- If the folder contents were changed on the fly, use
clear_cache()to wipe cache.
Translate in code
from fast_app.core.localization import __, set_locale
set_locale("en")
message = __("messages.welcome", {"name": "Alice"}, default="Hello {name}")
__()looks up the key using dot notation, applies the parameters withstr.format, and falls back to the key (ordefault=) when nothing is found.- Passing
locale="es"lets me force a one-off translation without touching the global context.
Handle plurals
trans_choice("cart_count", count, {"count": count})automatically checkscart_count_pluralwhencount != 1and otherwise reusescart_count.
Environment cheat sheet
LOCALE_PATH: absolute or relative path to my JSON files (defaults to<cwd>/lang).LOCALE_DEFAULT: initial locale for new contexts.LOCALE_FALLBACK: locale used when the active one misses a key.
TLDR:
- Drop JSON files in app/lang (e.g. en.json),
- Set the locale per request when necessary with set_locale("en") or env LOCALE_DEFAULT
- Call __() or trans_choice() wherever you need translated strings.