Skip to Content
Flask Framework Cookbook - Second Edition
book

Flask Framework Cookbook - Second Edition

by Shalabh Aggarwal
July 2019
Beginner to intermediate
302 pages
9h 38m
English
Packt Publishing
Content preview from Flask Framework Cookbook - Second Edition

How to do it...

Observe the following steps to understand how to implement language switching globally:

  1. First, modify all the URL rules to accommodate an extra URL part. @catalog.route('/') will become @catalog.route('/<lang>/'), and @catalog.route('/home') will become @catalog.route('/<lang>/home'). Similarly, @catalog.route('/product-search/<int:page>') will become @catalog.route('/<lang>/product-search/<int:page>'). The same needs to be done for all the URL rules.
  2. Now, add a function that will add the language passed in the URL to the global proxy object, g:
@app.before_request 
def before(): 
    if request.view_args and 'lang' in request.view_args: 
        g.current_lang = request.view_args['lang'] 
        request.view_args.pop('lang') 

This method will ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Writing a Web Application with Flask

Writing a Web Application with Flask

Doug Farrell
Mastering Flask

Mastering Flask

Jack Stouffer

Publisher Resources

ISBN: 9781789951295Supplemental Content