mirror of
https://github.com/django/django.git
synced 2025-07-04 09:49:12 +00:00
i18n: fixed language code reporting - now the actually used language code is reported, not the requested language code (as before). Additionally explicit language codes requested in GET, P OST or otherwise are checked against availability, too.
git-svn-id: http://code.djangoproject.com/svn/django/branches/i18n@851 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
18d0c31d51
commit
b34e844c2f
@ -1,5 +1,5 @@
|
|||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
||||||
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
|
<html xmlns="http://www.w3.org/1999/xhtml" lang="{{ LANGUAGE_CODE }}" xml:lang="{{ LANGUAGE_CODE }}">
|
||||||
<head>
|
<head>
|
||||||
<title>{% block title %}{% endblock %}</title>
|
<title>{% block title %}{% endblock %}</title>
|
||||||
<link rel="stylesheet" type="text/css" href="{% block stylesheet %}{% load adminmedia %}{% admin_media_prefix %}css/base.css{% endblock %}" />
|
<link rel="stylesheet" type="text/css" href="{% block stylesheet %}{% load adminmedia %}{% admin_media_prefix %}css/base.css{% endblock %}" />
|
||||||
|
@ -18,9 +18,6 @@ class LocaleMiddleware:
|
|||||||
def process_view(self, request, view_func, param_dict):
|
def process_view(self, request, view_func, param_dict):
|
||||||
global _module_to_app
|
global _module_to_app
|
||||||
|
|
||||||
lang = translation.get_language_from_request(request)
|
|
||||||
|
|
||||||
|
|
||||||
def findapp(module):
|
def findapp(module):
|
||||||
app = _module_to_app.get(view_func.__module__, None)
|
app = _module_to_app.get(view_func.__module__, None)
|
||||||
if app is not None:
|
if app is not None:
|
||||||
@ -35,10 +32,12 @@ class LocaleMiddleware:
|
|||||||
|
|
||||||
app = findapp(view_func.__module__)
|
app = findapp(view_func.__module__)
|
||||||
|
|
||||||
request.LANGUAGE_CODE = lang
|
lang = translation.get_language_from_request(request)
|
||||||
|
|
||||||
translation.activate(app, lang)
|
translation.activate(app, lang)
|
||||||
|
|
||||||
|
request.LANGUAGE_CODE = translation.get_language()
|
||||||
|
|
||||||
def process_response(self, request, response):
|
def process_response(self, request, response):
|
||||||
patch_vary_headers(response, ('Accept-Language',))
|
patch_vary_headers(response, ('Accept-Language',))
|
||||||
return response
|
return response
|
||||||
|
@ -180,10 +180,14 @@ def get_language():
|
|||||||
"""
|
"""
|
||||||
t = _active.get(currentThread(), None)
|
t = _active.get(currentThread(), None)
|
||||||
if t is not None:
|
if t is not None:
|
||||||
return t.language()
|
try:
|
||||||
else:
|
return t.language()
|
||||||
from django.conf.settings import LANGUAGE_CODE
|
except AttributeError:
|
||||||
return LANGUAGE_CODE
|
pass
|
||||||
|
# if we don't have a real translation object, we assume
|
||||||
|
# it's the default language.
|
||||||
|
from django.conf.settings import LANGUAGE_CODE
|
||||||
|
return LANGUAGE_CODE
|
||||||
|
|
||||||
def gettext(message):
|
def gettext(message):
|
||||||
"""
|
"""
|
||||||
@ -236,25 +240,32 @@ def get_language_from_request(request):
|
|||||||
"""
|
"""
|
||||||
global _accepted
|
global _accepted
|
||||||
|
|
||||||
|
from django.conf import settings
|
||||||
|
globalpath = os.path.join(os.path.dirname(settings.__file__), 'locale')
|
||||||
|
|
||||||
if request.GET or request.POST:
|
if request.GET or request.POST:
|
||||||
lang = request.GET.get('django_language', None) or request.POST.get('django_language', None)
|
lang_code = request.GET.get('django_language', None) or request.POST.get('django_language', None)
|
||||||
if lang is not None:
|
if lang_code is not None:
|
||||||
if hasattr(request, 'session'):
|
lang = gettext_module.find('django', globalpath, [lang_code])
|
||||||
request.session['django_language'] = lang
|
if lang is not None:
|
||||||
else:
|
if hasattr(request, 'session'):
|
||||||
request.set_cookie('django_language', lang)
|
request.session['django_language'] = lang_code
|
||||||
return lang
|
else:
|
||||||
|
request.set_cookie('django_language', lang_code)
|
||||||
|
return lang_code
|
||||||
|
|
||||||
if hasattr(request, 'session'):
|
if hasattr(request, 'session'):
|
||||||
lang = request.session.get('django_language', None)
|
lang_code = request.session.get('django_language', None)
|
||||||
|
if lang_code is not None:
|
||||||
|
lang = gettext_module.find('django', globalpath, [lang_code])
|
||||||
|
if lang is not None:
|
||||||
|
return lang_code
|
||||||
|
|
||||||
|
lang_code = request.COOKIES.get('django_language', None)
|
||||||
|
if lang_code is not None:
|
||||||
|
lang = gettext_module.find('django', globalpath, [lang_code])
|
||||||
if lang is not None:
|
if lang is not None:
|
||||||
return lang
|
return lang_code
|
||||||
|
|
||||||
lang = request.COOKIES.get('django_language', None)
|
|
||||||
if lang is not None:
|
|
||||||
return lang
|
|
||||||
|
|
||||||
from django.conf import settings
|
|
||||||
|
|
||||||
accept = request.META.get('HTTP_ACCEPT_LANGUAGE', None)
|
accept = request.META.get('HTTP_ACCEPT_LANGUAGE', None)
|
||||||
if accept is not None:
|
if accept is not None:
|
||||||
@ -279,12 +290,24 @@ def get_language_from_request(request):
|
|||||||
langs = [_parsed(el) for el in accept.split(',')]
|
langs = [_parsed(el) for el in accept.split(',')]
|
||||||
langs.sort(lambda a,b: -1*cmp(a[1], b[1]))
|
langs.sort(lambda a,b: -1*cmp(a[1], b[1]))
|
||||||
|
|
||||||
globalpath = os.path.join(os.path.dirname(settings.__file__), 'locale')
|
|
||||||
|
|
||||||
for lang, order in langs:
|
for lang, order in langs:
|
||||||
if lang == 'en' or lang.startswith('en_') or gettext_module.find('django', globalpath, [lang]):
|
if lang == 'en' or lang.startswith('en_'):
|
||||||
|
# special casing for language en and derivates, because we don't
|
||||||
|
# have an english language file available, but just fallback
|
||||||
|
# to NullTranslation on those languages (as the source itself
|
||||||
|
# is in english)
|
||||||
_accepted[accept] = lang
|
_accepted[accept] = lang
|
||||||
return lang
|
return lang
|
||||||
|
else:
|
||||||
|
langfile = gettext_module.find('django', globalpath, [lang])
|
||||||
|
if langfile:
|
||||||
|
# reconstruct the actual language from the language
|
||||||
|
# filename, because otherwise we might incorrectly
|
||||||
|
# report de_DE if we only have de available, but
|
||||||
|
# did find de_DE because of language normalization
|
||||||
|
lang = langfile[len(globalpath):].split('/')[1]
|
||||||
|
_accepted[accept] = lang
|
||||||
|
return lang
|
||||||
|
|
||||||
return settings.LANGUAGE_CODE
|
return settings.LANGUAGE_CODE
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user