|
0
|
1 |
"this is the locale selecting middleware that will look at accept headers" |
|
|
2 |
|
|
|
3 |
from django.utils.cache import patch_vary_headers |
|
|
4 |
from django.utils import translation |
|
|
5 |
|
|
|
6 |
class LocaleMiddleware(object): |
|
|
7 |
""" |
|
|
8 |
This is a very simple middleware that parses a request |
|
|
9 |
and decides what translation object to install in the current |
|
|
10 |
thread context. This allows pages to be dynamically |
|
|
11 |
translated to the language the user desires (if the language |
|
|
12 |
is available, of course). |
|
|
13 |
""" |
|
|
14 |
|
|
|
15 |
def process_request(self, request): |
|
|
16 |
language = translation.get_language_from_request(request) |
|
|
17 |
translation.activate(language) |
|
|
18 |
request.LANGUAGE_CODE = translation.get_language() |
|
|
19 |
|
|
|
20 |
def process_response(self, request, response): |
|
|
21 |
patch_vary_headers(response, ('Accept-Language',)) |
|
|
22 |
if 'Content-Language' not in response: |
|
|
23 |
response['Content-Language'] = translation.get_language() |
|
|
24 |
translation.deactivate() |
|
|
25 |
return response |