Two middlewares to handle languages via HTTP GET
original code by stefan reinhard, check against django.conf.settings by me
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
from django.conf import settings from django.utils.cache import patch_vary_headers from django.utils import translation class SessionBasedLocaleMiddleware(object): """ This Middleware saves the desired content language in the user session. The SessionMiddleware has to be activated. """ def process_request(self, request): if request.method == 'GET' and 'lang' in request.GET: language = request.GET['lang'] request.session['language'] = language elif 'language' in request.session: language = request.session['language'] else: language = translation.get_language_from_request(request) for lang in settings.LANGUAGES: if lang == language: translation.activate(language) request.LANGUAGE_CODE = translation.get_language() def process_response(self, request, response): patch_vary_headers(response, ('Accept-Language',)) if 'Content-Language' not in response: response['Content-Language'] = translation.get_language() translation.deactivate() return response
More like this
- Stuff by NixonDash 1 month ago
- Add custom fields to the built-in Group model by jmoppel 3 months, 1 week ago
- Month / Year SelectDateWidget based on django SelectDateWidget by pierreben 6 months, 3 weeks ago
- Python Django CRUD Example Tutorial by tuts_station 7 months, 1 week ago
- Browser-native date input field by kytta 8 months, 3 weeks ago
I need to talk, this one is perfect. I sugest that you send it as one improvement to Django project, have a lot of people wanting for this :)
Thanks for this one!
Please login first before commenting.