Translations

The Smarter web console is translated with gettext, the same way as Django itself. English is the source language of every message and the default. Each other language is a pair of .po message catalogs, which a translator fills in. The console is translated into Spanish, French and Brazilian Portuguese. The Spanish catalogs are written in Latin American Spanish, under the generic es code, so that a browser that asks for any variant of Spanish (es-MX, es-419, es-ES) gets them. Django’s LANG_INFO has no es-419, so the language menu would describe an es-419 language as es, and post es back. Brazilian Portuguese is Django’s regional pt-br code, whose catalogs are in the pt_BR directory, the locale name of the code. A browser that asks for pt, pt-BR or pt-PT gets it. The console’s Django templates, its Python views, its React apps and its plain JavaScript all share this one pipeline, so a translator works with one set of files, in one format. The documentation at docs.smarter.sh, and the Smarter api’s responses, are not translated.

How a Page Chooses Its Language

Django’s LocaleMiddleware activates a language for each request: the one in the language cookie, which the language menu sets, or else the best match for the browser’s Accept-Language header, or else settings.LANGUAGE_CODE, English. Only the languages in settings.LANGUAGES can be chosen. The page then renders in that language:

  • Django templates mark their text with {% translate %} and {% blocktranslate %}, after {% load i18n %}. base.html sets <html lang="..."> to the active language.

  • Python code marks messages that it shows in the console, such as sign-in errors, with gettext(), imported as _. Api responses are not marked: their consumers are programs.

  • The React apps call gettext() and friends from @smarter/common.

  • Plain JavaScript in smarter/static/js calls the global gettext().

The last two get their messages from Django’s JavaScriptCatalog view, at /jsi18n/, which base.html loads before any other script. It serves the active language’s djangojs messages, and defines window.django.gettext(), ngettext(), pgettext(), interpolate() and the global functions of the same names. In tests and in Storybook there is no catalog, and @smarter/common falls back to the English source text.

Smarter Chat, the LLMClient prompt engineering workbench, is its own npm package, which also runs on websites that Django didn’t render, so it has its own catalogs, in the same format and the same languages, in the smarter-chat repository. It follows the console’s language through the <html lang> attribute of base.html. See Developing Smarter Chat.

The language menu, in the user menu of the console’s navbar (the @smarter/dashboard-user-menu React app), lists each language in its own name, and posts the choice to Django’s set_language view at /i18n/setlang/. It is shown only when settings.LANGUAGES has more than one language.

Emails are often sent outside a request, for example from a management command, so they are written in the recipient’s preferred language instead, UserProfile.language. It is English by default. A console sign-up, with a password or through Google, GitHub or LinkedIn, stores the language of the sign-up request, which LocaleMiddleware chose as it does for any page, so a user whose browser asks for French is welcomed in French. UserProfile.save() adds a new user to the account’s contacts, and AccountContact.send_welcome_email(language=...) renders the welcome email (templates/account/email/welcome.html) and its subject inside translation.override(language). Without a language, it uses AccountContact.preferred_language(): the preference of the account’s user who has the contact’s email address, or English for a contact who isn’t a user. The create_user command’s --language option, e.g. --language=es, sets the new user’s preference, and its password email is written in it too. The add_account_contact and send_welcome_email commands take --language to override the preference. The Slack invitation email (templates/account/email/slack-invitation.html) works the same way, through AccountContact.send_slack_invitation_email(language=...) and the send_slack_invitation_email command. The local-only previews at /account/email/welcome/<first_name>/ and /account/email/slack-invitation/<first_name>/ render the emails in the request’s language, or in the language of their ?language= parameter.

The Message Catalogs

The catalogs are in smarter/smarter/locale/<language>/LC_MESSAGES/:

File

Messages

django.po

The Django templates, and the Python code.

djangojs.po

The web console’s messages, from the installed smarter-console release’s messages.js, and smarter/static/js.

Only the .po files are committed. The Docker image compiles them to the .mo files that Django reads, with manage.py compilemessages. The English catalogs have empty translations, because gettext shows the source text of an untranslated message.

Django’s makemessages finds the messages in templates and Python code. The web console is its own repository, smarter-console, whose i18n/extract.ts finds its gettext() calls with the TypeScript compiler, which the xgettext that makemessages runs can’t parse, and writes each distinct message as one plain JavaScript call to i18n/messages.js. Each release of the console carries that file, and make console installs the pinned release’s in smarter/console/i18n/, where makemessages reads it for the djangojs domain. Each message is preceded by a Translators: comment that lists the console’s files that use it, which appears in djangojs.po. The console’s own test fails when its messages.js is out of date. A new console message shows in English until this repository’s catalogs have its translation.

Marking Text for Translation

Every message is a complete English sentence or label, written as a string literal, so that it can be extracted. Put values into it with named placeholders, after translating it, so that a translator can move them:

{% load i18n %}
<h1>{% translate "API Keys" %}</h1>
<p>{% blocktranslate with email=branding.support_email %}Email: {{ email }}{% endblocktranslate %}</p>
<input placeholder="{% translate "Email address" %}" />
from django.utils.translation import gettext as _

error_message = _("Invalid login attempt. Unknown user %(email)s") % {"email": email}
import { gettext, interpolate, interpolateNodes, ngettext } from "@smarter/common";

<button title={gettext("Rename: Rename this secret resource")} />
interpolate(gettext("Failed to load budgets (%(status)s)."), { status: response.status });
interpolate(ngettext("%(count)s path", "%(count)s paths", count), { count });
// a React element inside a sentence
interpolateNodes(gettext("Rename secret %(name)s."), { name: <strong>{secret.name}</strong> });
// smarter/static/js
Swal.fire({ text: gettext("Copied API key to the clipboard."), confirmButtonText: gettext("Ok, got it!") });

Some rules keep the catalogs translatable:

  • Don’t build sentences from pieces. "Successfully " + verb + "d" can’t be translated. Write one message for each case, as the list pages’ actionMessages() does for clone, rename and delete.

  • Use ngettext() for counts, never a conditional "s". Languages have different plural rules.

  • Use pgettext() when one English word has two meanings, e.g. a verb and a noun.

  • Explain anything ambiguous with a comment that begins with Translators:, on the line before the call, or before the {% translate %} tag. It is copied into the .po file.

  • Don’t translate resource kinds in code, manifest field names, commands, product names, log messages, or errors that only developers see, such as a missing attribute in main.tsx.

Updating the Catalogs

After changing marked text, regenerate the catalogs, with the containers running the current code:

make console                # the pinned console release, with its messages.js
make build && make run      # in another terminal, or in the background
make i18n-messages

make i18n-messages copies the console’s messages.js into the smarter-api container, then runs the update_console_messages management command in the smarter-api container, which runs makemessages for both domains and every language in settings.LANGUAGES, keeping the translations already in the .po files, then compilemessages. It copies the catalogs back to the host. Commit the changed .po files. make i18n-messages LANGUAGE=es updates one language, and so does LANGUAGE=pt-br, whose catalogs the command writes to locale/pt_BR.

Adding a Language

  1. Add the language to LANGUAGES in smarter/smarter/settings/base.py, e.g. ("es", "Spanish"). Use a code that Django’s django.conf.locale.LANG_INFO knows, such as es or pt-br, so that the language menu posts back the code that it lists.

  2. Add its date-fns locale to DATE_FNS_LOCALES in smarter-common/src/lib/formatDateTime.tsx, for relative times such as “3 minutes ago”. A regional code is its lowercase key, e.g. "pt-br": ptBR.

  3. Rebuild the containers, then run make i18n-messages. It creates smarter/smarter/locale/es/LC_MESSAGES/django.po and djangojs.po, or locale/pt_BR/LC_MESSAGES/ for pt-br.

  4. Translate every msgstr, with a .po editor such as Poedit, or by hand. Keep each %(name)s placeholder, and the HTML in blocktranslate messages.

  5. Rebuild the containers, which compile the catalogs. The language menu appears in the console’s navbar.

Technical Reference