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

Global language-switching action


In the previous recipes, we saw that the languages change on the basis of the current language preferences in the browser. However, now, we want a mechanism where we can switch the language to be used irrespective of the language in the browser. For this, we need to handle the language at the application level.

Getting ready

We start by modifying the application from the last recipe, Lazy evaluation and the gettext/ngettext functions, to accommodate the changes to enable language switching. We will add an extra URL part to all our routes to add the current language. We can just change this language in the URL to switch between languages.

How to do it…

The first change that we need to do is modify all our URL rules to accommodate an extra URL part. So @app.route('/') will become @app.route('/<lang>/'), and @app.route('/home') will become @app.route('/<lang>/home'). Similarly, @app.route('/product-search/<int:page>') will become @app.route('/...