Ushka

Internationalisation

app.use(I18n(locales=["en", "pt-BR"], default="en"))

Using it turns translations on. Catalogs are plain gettext files (a .mo from Django, msgfmt or Poedit works as it is), read with the standard library: nothing to install.

I18n(locales=, default=, **options): default is default_locale, the language of the texts in your code; locales are the only languages a client may pick; options are any other key of [i18n]. The file and the environment win over code, and the file alone works too:

[i18n]
enable = true
default_locale = "en"
locales = ["en", "pt-BR"]

Catalogs live in the usual gettext layout, next to ushka.toml:

locale/
  pt_BR/LC_MESSAGES/messages.po    # you write this
  pt_BR/LC_MESSAGES/messages.mo    # `ushka i18n compile` makes this

pt-BR falls back to the pt catalog, and a message with no translation (or a language with no catalog) is shown as written: a missing translation is never an error. Catalogs are read once and kept in memory; with debug = true an edited .mo is picked up without a restart.

In templates

<html lang="{{ lang }}">
  <h1>{{ _("Welcome") }}</h1>
  <p>{{ ngettext("%(num)d item", "%(num)d items", count) }}</p>
  <p>{{ pgettext("menu", "File") }}</p>
  <p>{% trans name=user.name %}Hello {{ name }}{% endtrans %}</p>
  <p>{% trans n=count %}{{ n }} comment{% pluralize %}{{ n }} comments{% endtrans %}</p>

Every template also gets lang (pt-BR, for <html lang>) and locale (pt_BR). Variables inside {% trans %} and %(name)s placeholders are escaped, so a user name cannot inject markup through a translated sentence. Messages are format strings: write a literal percent sign as %%.

In Python

from ushka import gettext as _, ngettext, lazy_gettext, translate

async def save(request):
    return _("Hello %(name)s", name="Ann")           # in the language of this request

TITLE = lazy_gettext("Dashboard")                    # translated when it is used

message = translate(request, "Saved")                # explicit, e.g. in a worker thread

gettext, ngettext, pgettext and translate follow the request being served (kept per request, so concurrent requests never mix languages). Outside a request, or with i18n off, they return the original text.

How the language is chosen

[i18n] order decides which sources are asked, first match wins (default ["query", "cookie", "header"]):

Source Read from
query ?lang=pt-BR (name set by query_param)
cookie the lang cookie (name set by cookie)
header Accept-Language, honouring q values: pt-PT;q=0.8, en;q=0.5 picks the best language you support

A language chosen with ?lang= is stored in the cookie (remember = false turns that off), so a link such as <a href="?lang=pt-BR">Português</a> is a complete language switcher. With nothing matching, default_locale is used.

All of this is client data. It is only compared with locales; the language and catalog path used afterwards are the configured names, never the client's text, so ?lang=../../etc/passwd or an invented code simply does not match. locales entries and domain must be plain names, or the app refuses to start.

Writing the catalogs

ushka i18n extract       # scan .py and templates/, write locale/messages.pot
mkdir -p locale/pt_BR/LC_MESSAGES
cp locale/messages.pot locale/pt_BR/LC_MESSAGES/messages.po   # translate it
ushka i18n compile       # every .po under locale/ becomes a .mo

extract finds literal strings in _(), gettext, ngettext, pgettext, translate, lazy_gettext and {% trans %}. The .po reader supports the usual file (context, plurals, multi-line strings, fuzzy entries skipped) and expects UTF-8; use msgmerge or Poedit to update an existing .po from a new .pot.

SEO

With i18n on, og:locale follows the language of the request (pt-BR becomes pt_BR; a bare en keeps [seo] locale when that is en_US). Add alternates={"pt-BR": "/pt/"} to seo() for hreflang links; see SEO.

Settings

See the [i18n] table in All settings.

Edit this page