How to build multilingual Flask application
Creating a suitable Flask sample to try out Gitloc
If you want to create your project from scratch, then you can use the following step-by-step guide, where we will create a multilingual Flask sample project using i18n and Gitloc
Create a Flask Application (more at flask.palletsprojects.com)
Prerequisites
Familiarity with the command line
Install Python version 3.8 or higher
First we need to install Flask (more in the Flask installation guide):
A minimal Flask application looks something like this:
Save it as app.py
.
To run the application, use the flask
command or python -m flask
. You need to tell the Flask where your application is with the --app
option.
Now let's use some templates in our app. Create new template hello.html
in templates
folder.
Add this template to our app:
Setup i18n (more at pypi.org)
In our example, we will create a project with 2 languages and separate .json
files for translations.
Install i18n:
Add i18n
to the app.py
and set i18n
translations config:
Add translations usage (more at pypi.org)
Place translations into dedicated .json
files in the public
folder:
Now let's define a language switcher and some variable content in our template:
We will need title translations for different application locales. Let's add it to welcome.json
.
And next we need to process our new routes /en
and /de
. Let's define a renderHello
function to render localized templates and use it in the router.
For more information on using translation files (as placeholders, plurals, etc.), see the python-i18n docs.
Finally, сonnect your local project folder to your new repository on GitHub.
Done! Now you can move to the next step - connect remote repository to Gitloc
Last updated