71535629827eb7f2749988728a363870ac330507
[prawokultury.git] / migdal / middleware.py
1 # -*- coding: utf-8 -*-
2 # This file is part of PrawoKultury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 #
5 from django.utils import translation
6 from django.conf import settings
7 from django.http import Http404
8
9
10 class URLLocaleMiddleware(object):
11     """Decides which translation to use, based on path only."""
12
13     def process_request(self, request):
14         language = translation.get_language_from_path(request.path_info)
15         if language == settings.LANGUAGE_CODE:
16             raise Http404
17         if language:
18             translation.activate(language)
19         request.LANGUAGE_CODE = translation.get_language()
20
21     def process_response(self, request, response):
22         language = translation.get_language()
23         translation.deactivate()
24         if 'Content-Language' not in response:
25             response['Content-Language'] = language
26         return response