Translation (i18n)

How to translate Subscribie into different languages & update translations.

Prerequisite

You must run Subscribie, see running Subscribie locally.

After Subscribie is set-up locally, you can stop Subscribie (ctrl + c) then view the translation commands:

export FLASK_APP=subscribie
flask translate --help
Usage: flask translate [OPTIONS] COMMAND [ARGS]...

  Translation and localization commands.

Options:
  --help  Show this message and exit.

Commands:
  compile  Compile all languages.
  init     Initialize a new language.
  update   Update all languages.

Steps to update translations

When new text gets added to Subscribie, it needs to be translated. For example:

Before marked for translation:

@app.route("/new-route")
def my_new_route():
    return "This is my new route"

After marked for translation:


from flask_babel import _

@app.route("/new-route")
def my_new_route():
    return _("This is my new route")

warning Make sure you didn’t miss above, the _ is a function, from the flask_babel library. babel uses _ to help find all the translatable text.

For more complex translation markets in Jinja2 templates see examples:

The main steps to perform static translation in Subscribie are:

  1. Update the code replacing hard-coded strings with the _ function
  2. Running flask translate update
  3. Edit the updated .po file with translations (e.g. subscribie/subscribie/translations/de/LC_MESSAGES/messages.po)
  4. Running flask translate compile, which generates a speed optimised translation file (e.g. subscribie/subscribie/translations/de/LC_MESSAGES/messages.po)
  5. Test the site flask run, commit the changes and raise a pull request.

Credits

The Subscribie i18n translation process was expedited thanks to Miguel Grinberg who wrote an article the-flask-mega-tutorial-part-xiii-i18n-and-l10n, and is the author of Flask Web Development, 2nd Edition

Last modified November 16, 2022: document translation how to (9db0e8eb)