A minimal installation of Flask and Flask-Babel, so as to propose multiple languages depending on the request URL (e.g. /en/index
vs /fr/index
).
Install dependencies through pipenv
:
pipenv install
Everytime you want your Shell session to use the project's dependencies, run:
pipenv shell
Before running any flask
command, do setup the required environment variables, e.g. :
export FLASK_APP=flaskr # name of the python module containing our codebase
export FLASK_ENV=development
Flask-Babel takes care of business---go read their docs for info.
Only note that, instead of calling pybabel
as written in Flask-Babel's documentation, a CLI module is installed on the site to ease things (courtesy of @hazzillrodriguez's flask-multi-language
):
-
To generate the translation
.po
files files for a language, run:flask translate init <language-code>
-
Putting
fr
as the<language-code>
, the above would generate the fileflaskr/translations/fr/LC_MESSAGES/messages.po
. It will look like:#: flaskr/templates/base.html:10 msgid "Multilingual flask site" msgstr "" #: flaskr/templates/base.html:22 msgid "Home" msgstr "" #: flaskr/templates/base.html:81 msgid "No rights reserved" msgstr "" #: flaskr/templates/base.html:87 msgid "Source code" msgstr ""
Now fill-in the French translations!
-
Once you're done translating, you can generate Babel's binary output file (
.mo
):flask translate compile
-
Next time you edit your site, only
update
translations (your existing translations will be kept):flask translate update # Then recompile flask translate compile
Simply run:
# If not already done:
pipenv shell
export FLASK_ENV=development
export FLASK_APP=flaskr
# The righteous command:
flask run
Then go check it out on http://127.0.0.1:5000 !
Thanks, I know, I know... But really, credits go to the Foundation HTML framework.
No thanks! I already had too much.
Paying your taxes is a healthier way to contribute to my salary.
Public domain, aka "do whatever you please with this code, it ain't mine".