1
0
mirror of https://github.com/django/django.git synced 2025-07-04 01:39:20 +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:
Georg Bauer 2005-10-12 15:33:22 +00:00
parent 18d0c31d51
commit b34e844c2f
3 changed files with 49 additions and 27 deletions

View File

@ -1,5 +1,5 @@
<!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>
<title>{% block title %}{% endblock %}</title>
<link rel="stylesheet" type="text/css" href="{% block stylesheet %}{% load adminmedia %}{% admin_media_prefix %}css/base.css{% endblock %}" />

View File

@ -18,9 +18,6 @@ class LocaleMiddleware:
def process_view(self, request, view_func, param_dict):
global _module_to_app
lang = translation.get_language_from_request(request)
def findapp(module):
app = _module_to_app.get(view_func.__module__, None)
if app is not None:
@ -35,10 +32,12 @@ class LocaleMiddleware:
app = findapp(view_func.__module__)
request.LANGUAGE_CODE = lang
lang = translation.get_language_from_request(request)
translation.activate(app, lang)
request.LANGUAGE_CODE = translation.get_language()
def process_response(self, request, response):
patch_vary_headers(response, ('Accept-Language',))
return response

View File

@ -180,8 +180,12 @@ def get_language():
"""
t = _active.get(currentThread(), None)
if t is not None:
try:
return t.language()
else:
except AttributeError:
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
@ -236,25 +240,32 @@ def get_language_from_request(request):
"""
global _accepted
if request.GET or request.POST:
lang = request.GET.get('django_language', None) or request.POST.get('django_language', None)
if lang is not None:
if hasattr(request, 'session'):
request.session['django_language'] = lang
else:
request.set_cookie('django_language', lang)
return lang
if hasattr(request, 'session'):
lang = request.session.get('django_language', None)
if lang is not None:
return lang
lang = request.COOKIES.get('django_language', None)
if lang is not None:
return lang
from django.conf import settings
globalpath = os.path.join(os.path.dirname(settings.__file__), 'locale')
if request.GET or request.POST:
lang_code = request.GET.get('django_language', None) or request.POST.get('django_language', None)
if lang_code is not None:
lang = gettext_module.find('django', globalpath, [lang_code])
if lang is not None:
if hasattr(request, 'session'):
request.session['django_language'] = lang_code
else:
request.set_cookie('django_language', lang_code)
return lang_code
if hasattr(request, 'session'):
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:
return lang_code
accept = request.META.get('HTTP_ACCEPT_LANGUAGE', None)
if accept is not None:
@ -279,10 +290,22 @@ def get_language_from_request(request):
langs = [_parsed(el) for el in accept.split(',')]
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:
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
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