Observe the following steps to understand how to implement language switching globally:
- 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.
- 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 ...