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:
Georg Bauer 2005-11-12 21:27:46 +00:00
parent 9da6eb5c26
commit 5f39a6a240
1 changed files with 25 additions and 18 deletions

View File

@ -256,7 +256,8 @@ ngettext_lazy = lazy(ngettext, str)
def check_for_language(lang_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
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):
"""
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
from django.conf import settings
globalpath = os.path.join(os.path.dirname(settings.__file__), 'locale')
supported = dict(settings.LANGUAGES)
if hasattr(request, 'session'):
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
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
accept = request.META.get('HTTP_ACCEPT_LANGUAGE', None)
@ -297,15 +301,18 @@ def get_language_from_request(request):
else:
lang = el
order = 100
if lang.find('-') >= 0:
(lang, sublang) = lang.split('-')
lang = lang.lower() + '_' + sublang.upper()
return (lang, order)
p = lang.find('-')
if p >= 0:
mainlang = lang[:p]
else:
mainlang = lang
return (lang, mainlang, order)
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:
if lang in supported or mainlang in supported:
langfile = gettext_module.find('django', globalpath, [to_locale(lang)])
if langfile:
# reconstruct the actual language from the language