Translation (i18n)
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 theflask_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:
- Update the code replacing hard-coded strings with the
_
function - Running
flask translate update
- Edit the updated
.po
file with translations (e.g.subscribie/subscribie/translations/de/LC_MESSAGES/messages.po
) - Running
flask translate compile
, which generates a speed optimised translation file (e.g.subscribie/subscribie/translations/de/LC_MESSAGES/messages.po
) - 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