Localization (i18n)#
Localization helpers. Widget text is translated automatically when a translation
is registered for the active locale; wrap a string with L to mark it
translatable with interpolation, or a value with LV to format it for the
locale. Register your own translations with the catalog functions below.
For a task-oriented introduction — automatic translation, registering strings, locale-aware dates and numbers, switching locale at runtime — see the Localization (i18n) guide.
Translatable text and values#
- bootstack.i18n.L(key, *fmtargs, **fmtkwargs)#
Mark text as translatable, with optional
{}-style interpolation.The key is looked up in the translation catalog for the active locale, then formatted with Python
str.format— so placeholders use{0}/{name}style. The spec re-resolves whenever the locale changes.- Parameters:
- Returns:
A
LocalizedTextSpecyou can pass anywhere a widget takes text.- Return type:
LocalizedTextSpec
Examples
>>> L("Hello, {0}", name) # positional >>> L("Hello, {name}", name=user) # named
- bootstack.i18n.LV(value, format_spec)#
Create a LocalizedValueSpec for locale-aware value formatting.
Shorthand constructor that creates a value formatting spec for numbers, dates, times, and currency values.
- Parameters:
- Returns:
A LocalizedValueSpec instance.
- Return type:
LocalizedValueSpec
Examples
>>> spec = LV(1234.56, "currency") >>> # Will format as currency in the current locale
Extending the translation catalog#
- bootstack.i18n.add_translation(locale, source, translated)#
Register a single translation for a locale.
- bootstack.i18n.add_translations(locale, translations)#
Register multiple translations for a locale.
- bootstack.i18n.load_po(path, locale=None)#
Load translations from a gettext
.posource file into the catalog.The
.pois read directly with Babel — nomsgfmtcompilation and no.mofiles. The path is resolved in both a development run and a PyInstaller build (bundle the.powith your app, e.g. underassets/, which the default build config already includes).
- bootstack.i18n.load_translations(directory)#
Load translation catalogs from a directory of
.msgfiles.Each file is named for its locale (e.g.
es.msg) and holds the locale’s translations. This is the bulk alternative toadd_translations()for projects that keep translations in files.