Internationalization and Localization

This chapter will cover the basics of how to enable support for multiple languages in a Flask application.

Web applications are usually not limited to one geographical region or serve people from one linguistic domain. For example, a web application intended for users in Europe will be expected to support other European languages, such as German, French, Italian, and Spanish, apart from English.

Adding support for a second language in any web application is a tricky affair. It increases a bit of overhead every time some change is made to the application, and this increases with the number of languages. There can be a number of things that need to be taken care of, apart from just changing the text as per the language. Some of the major ones are currency, number, time, and date formatting.

Flask-Babel , an extension that adds internationalization (i18n ) and localization (l10n) support to any Flask application, provides a number of tools and techniques to make this process simpler and easy to implement.

In this chapter, we will cover the following recipes:

  • Adding a new language
  • Implementing lazy evaluation and the gettext/ngettext functions
  • Implementing the global language switching action