ushka.web.i18n
Internationalisation: translations from standard gettext catalogs, no extra packages.
Turn it on in ushka.toml:
[i18n]
enable = true
default_locale = "en"
locales = ["en", "pt-BR"]
Catalogs are the usual gettext layout, locale/<lang>/LC_MESSAGES/messages.mo,
so a .mo made by Django, msgfmt or ushka i18n compile works as it is.
In templates:
<html lang="{{ lang }}">
{{ _("Welcome") }} {{ ngettext("%(num)d item", "%(num)d items", n) }}
{% trans name=user.name %}Hello {{ name }}{% endtrans %}
In Python (the language of the request being served):
from ushka import gettext as _
message = _("Saved")
title = lazy_gettext("Dashboard") # translated when it is used, not when it is defined
The language of a request is picked from ?lang=, the lang cookie and the
Accept-Language header, in the order of [i18n] order. All of that is
client data: it is only ever matched against [i18n] locales, and the
catalog path is built from the configured name, never from what the client sent.
A missing catalog or message gives the original text back, never an error.
In this module
def parse_accept_language() | Language tags of an Accept-Language header, best first. |
class Catalogs | The .mo files of one app, read once and kept in memory. |
class Translator | The translation settings of one app ([i18n]) and the language choice (app.i18n). |
class I18n | app.use(I18n(locales=["en", "pt"], default="en")): translations on, in one line. |
def get_locale() | The language of the request being served ("pt-BR"); None outside one or with i18n off. |
def template_gettext() | |
def template_ngettext() | |
def template_pgettext() | |
def template_npgettext() | |
def template_variables() | lang (pt-BR, for <html lang>) and locale (pt_BR), added to every template. |
def gettext() | message in the language of the current request; %(name)s placeholders are filled from variables. |
def ngettext() | The plural form for n (%(num)d is filled with n). |
def pgettext() | message disambiguated by context (pgettext("menu", "File")). |
class LazyString | A message translated when it is turned into text, not when it is defined. |
def lazy_gettext() | Like gettext, but translated each time the result is used. |
def translate() | message in the language of request (no current-request context needed, e.g. in a thread). |
def ntranslate() | The plural form for n in the language of request. |
def parse_accept_language
def parse_accept_language(header: str) -> list[str]Language tags of an Accept-Language header, best first.
"pt-BR,pt;q=0.9,en;q=0.8,*;q=0.1" gives ["pt-br", "pt", "en"]. Tags
with q=0 (refused) or a malformed q are dropped; equal q keep
the order of the header. The header is capped, since anyone can send it.
class Catalogs
The .mo files of one app, read once and kept in memory.
With reload (debug), a file whose modification time changed is read
again, so editing a translation shows up without restarting.
get
def get(locale: str) -> AnyThe translations of locale (a configured one), falling back to its language.
class Translator
The translation settings of one app ([i18n]) and the language choice (app.i18n).
Methods
match | The configured locale for a client-supplied tag, or None. |
negotiate | (locale, source) for request; the source is query, cookie, header or default. |
translations | |
activate | Chooses the language of request and makes it the current one. |
deactivate |
match
def match(tag: Any) -> str | NoneThe configured locale for a client-supplied tag, or None.
pt-br finds pt-BR; pt-PT or pt find pt or, failing
that, the first configured pt-*. The result is always one of the
configured names, never the client's text.
negotiate
def negotiate(request: Request) -> tuple[str, str](locale, source) for request; the source is query, cookie, header or default.
translations
def translations(locale: str) -> Anyactivate
def activate(request: Request) -> Token | NoneChooses the language of request and makes it the current one.
Returns what deactivate needs. With i18n off the language is just the
default and messages come back untranslated.
deactivate
@staticmethod
def deactivate(token: Token | None) -> Noneclass I18n
app.use(I18n(locales=["en", "pt"], default="en")): translations on, in one line.
Using it turns i18n on (no enable = true); ushka.toml and the environment still
win. default is default_locale; **options are any other keys of [i18n].
install
def install(app: Any) -> Nonedef get_locale
def get_locale() -> str | NoneThe language of the request being served ("pt-BR"); None outside one or with i18n off.
def template_gettext
def template_gettext(message: str) -> strdef template_ngettext
def template_ngettext(singular: str, plural: str, n: int) -> strdef template_pgettext
def template_pgettext(context: str, message: str) -> strdef template_npgettext
def template_npgettext(
context: str,
singular: str,
plural: str,
n: int,
) -> strdef template_variables
def template_variables() -> dict[str, str]lang (pt-BR, for <html lang>) and locale (pt_BR), added to every template.
def gettext
def gettext(message: str, **variables: Any) -> strmessage in the language of the current request; %(name)s placeholders are filled from variables.
def ngettext
def ngettext(singular: str, plural: str, n: int, **variables: Any) -> strThe plural form for n (%(num)d is filled with n).
def pgettext
def pgettext(context: str, message: str, **variables: Any) -> strmessage disambiguated by context (pgettext("menu", "File")).
class LazyString
A message translated when it is turned into text, not when it is defined.
For module-level texts (form labels, constants) that are read before any
request exists: TITLE = lazy_gettext("Dashboard").
def lazy_gettext
def lazy_gettext(message: str, **variables: Any) -> LazyStringLike gettext, but translated each time the result is used.
def translate
def translate(request: Request, message: str, **variables: Any) -> strmessage in the language of request (no current-request context needed, e.g. in a thread).
def ntranslate
def ntranslate(
request: Request,
singular: str,
plural: str,
n: int,
**variables: Any,
) -> strThe plural form for n in the language of request.