Book Image

Flask Framework Cookbook

By : Shalabh Aggarwal
Book Image

Flask Framework Cookbook

By: Shalabh Aggarwal

Overview of this book

Table of Contents (19 chapters)
Flask Framework Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Adding a new language


By default, English is the language for applications built in Flask (and almost all web frameworks). 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 current language 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, pytz, and speaklater to add i18n and l10n support to the application.

We will use our catalog application from Chapter 5, Webforms with WTForms.

How to do it…

First, we will start with the configuration part by creating an instance of the Babel class using the app object. We will also specify what languages will be available here. French is added as the second language:

from flask.ext.babel import Babel

ALLOWED_LANGUAGES = {
    'en': 'English',
    'fr': 'French',
}
babel = Babel(app)

Tip

Here, we used en and fr as the language...