fixes #750 - languages for language-selection can now be restricted by setting LANGUAGES in the projects setting file to some subset of the global_settings provided list.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@1204 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
9da6eb5c26
commit
5f39a6a240
|
@ -256,7 +256,8 @@ ngettext_lazy = lazy(ngettext, str)
|
||||||
def check_for_language(lang_code):
|
def check_for_language(lang_code):
|
||||||
"""
|
"""
|
||||||
Checks whether there is a global language file for the given language code.
|
Checks whether there is a global language file for the given language code.
|
||||||
This is used to decide whether a user-provided language is available.
|
This is used to decide whether a user-provided language is available. this is
|
||||||
|
only used for language codes from either the cookies or session.
|
||||||
"""
|
"""
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
globalpath = os.path.join(os.path.dirname(settings.__file__), 'locale')
|
globalpath = os.path.join(os.path.dirname(settings.__file__), 'locale')
|
||||||
|
@ -268,18 +269,21 @@ def check_for_language(lang_code):
|
||||||
def get_language_from_request(request):
|
def get_language_from_request(request):
|
||||||
"""
|
"""
|
||||||
Analyzes the request to find what language the user wants the system to show.
|
Analyzes the request to find what language the user wants the system to show.
|
||||||
|
Only languages listed in settings.LANGUAGES are taken into account. If the user
|
||||||
|
requests a sublanguage where we have a main language, we send out the main language.
|
||||||
"""
|
"""
|
||||||
global _accepted
|
global _accepted
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
globalpath = os.path.join(os.path.dirname(settings.__file__), 'locale')
|
globalpath = os.path.join(os.path.dirname(settings.__file__), 'locale')
|
||||||
|
supported = dict(settings.LANGUAGES)
|
||||||
|
|
||||||
if hasattr(request, 'session'):
|
if hasattr(request, 'session'):
|
||||||
lang_code = request.session.get('django_language', None)
|
lang_code = request.session.get('django_language', None)
|
||||||
if lang_code is not None and check_for_language(lang_code):
|
if lang_code in supported and lang_code is not None and check_for_language(lang_code):
|
||||||
return lang_code
|
return lang_code
|
||||||
|
|
||||||
lang_code = request.COOKIES.get('django_language', None)
|
lang_code = request.COOKIES.get('django_language', None)
|
||||||
if lang_code is not None and check_for_language(lang_code):
|
if lang_code in supported and lang_code is not None and check_for_language(lang_code):
|
||||||
return lang_code
|
return lang_code
|
||||||
|
|
||||||
accept = request.META.get('HTTP_ACCEPT_LANGUAGE', None)
|
accept = request.META.get('HTTP_ACCEPT_LANGUAGE', None)
|
||||||
|
@ -297,24 +301,27 @@ def get_language_from_request(request):
|
||||||
else:
|
else:
|
||||||
lang = el
|
lang = el
|
||||||
order = 100
|
order = 100
|
||||||
if lang.find('-') >= 0:
|
p = lang.find('-')
|
||||||
(lang, sublang) = lang.split('-')
|
if p >= 0:
|
||||||
lang = lang.lower() + '_' + sublang.upper()
|
mainlang = lang[:p]
|
||||||
return (lang, order)
|
else:
|
||||||
|
mainlang = lang
|
||||||
|
return (lang, mainlang, order)
|
||||||
|
|
||||||
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[2], b[2]))
|
||||||
|
|
||||||
for lang, order in langs:
|
for lang, mainlang, order in langs:
|
||||||
langfile = gettext_module.find('django', globalpath, [to_locale(lang)])
|
if lang in supported or mainlang in supported:
|
||||||
if langfile:
|
langfile = gettext_module.find('django', globalpath, [to_locale(lang)])
|
||||||
# reconstruct the actual language from the language
|
if langfile:
|
||||||
# filename, because otherwise we might incorrectly
|
# reconstruct the actual language from the language
|
||||||
# report de_DE if we only have de available, but
|
# filename, because otherwise we might incorrectly
|
||||||
# did find de_DE because of language normalization
|
# report de_DE if we only have de available, but
|
||||||
lang = langfile[len(globalpath):].split(os.path.sep)[1]
|
# did find de_DE because of language normalization
|
||||||
_accepted[accept] = lang
|
lang = langfile[len(globalpath):].split(os.path.sep)[1]
|
||||||
return lang
|
_accepted[accept] = lang
|
||||||
|
return lang
|
||||||
|
|
||||||
return settings.LANGUAGE_CODE
|
return settings.LANGUAGE_CODE
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue