In configuration file:
config.add_route('lang', '/lang-{code}')
In views:
@view_config(route_name='lang')
def lang(request):
code = request.matchdict['code']
response = Response()
response.set_cookie('lang', value=code, max_age=31536000) # max_age = year
return HTTPFound(location=request.environ['HTTP_REFERER'])
The mechanism is simple: there is a dropped down menu item with languages and clicking on anyone must refresh site with new locale.
Runs without errors, but no cookie set up... What I did wrong?
Thanks!
This answer is excellent. Another option is usage of the HTTPFound
instance as a Response
:
@view_config(route_name='lang')
def lang(request):
code = request.matchdict['code']
response = HTTPFound(location=request.environ['HTTP_REFERER'])
response.set_cookie('lang', value=code, max_age=31536000) # max_age = year
return response
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With