Adding a new language
By default, English is the language for applications built in Flask (and almost all web frameworks). In this recipe, we will add a second language to our application and add some translations for the display strings used in the application. The language displayed to the user will vary depending on the language that is currently set in the browser.
Getting ready
We will start with the installation of the Flask-Babel
extension:
$ pip install Flask-Babel
This extension uses Babel and pytz to add i18n and l10n support to the application.
We will use our catalog application from Chapter 5, Web Forms with WTForms.
How to do it...
We will use French as the second language. Follow these steps to achieve this:
- Start with the configuration part by creating an instance of the
Babel
class, using theapp
object inmy_app/__init__.py
. We will also specify all the languages that will be available here:from flask import request
from flask_babel import...