Django

Code

Changeset 1204

Show
Ignore:
Timestamp:
11/12/05 15:27:46 (3 years ago)
Author:
hugo
Message:

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.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/utils/translation.py

    r1095 r1204  
    257257    """ 
    258258    Checks whether there is a global language file for the given language code. 
    259     This is used to decide whether a user-provided language is available. 
     259    This is used to decide whether a user-provided language is available. this is 
     260    only used for language codes from either the cookies or session. 
    260261    """ 
    261262    from django.conf import settings 
     
    269270    """ 
    270271    Analyzes the request to find what language the user wants the system to show. 
     272    Only languages listed in settings.LANGUAGES are taken into account. If the user 
     273    requests a sublanguage where we have a main language, we send out the main language. 
    271274    """ 
    272275    global _accepted 
    273276    from django.conf import settings 
    274277    globalpath = os.path.join(os.path.dirname(settings.__file__), 'locale') 
     278    supported = dict(settings.LANGUAGES) 
    275279 
    276280    if hasattr(request, 'session'): 
    277281        lang_code = request.session.get('django_language', None) 
    278         if lang_code is not None and check_for_language(lang_code): 
     282        if lang_code in supported and lang_code is not None and check_for_language(lang_code): 
    279283            return lang_code 
    280284 
    281285    lang_code = request.COOKIES.get('django_language', None) 
    282     if lang_code is not None and check_for_language(lang_code): 
     286    if lang_code in supported and lang_code is not None and check_for_language(lang_code): 
    283287        return lang_code 
    284288 
     
    298302                lang = el 
    299303                order = 100 
    300             if lang.find('-') >= 0: 
    301                 (lang, sublang) = lang.split('-') 
    302                 lang = lang.lower() + '_' + sublang.upper() 
    303             return (lang, order) 
     304            p = lang.find('-') 
     305            if p >= 0: 
     306                mainlang = lang[:p] 
     307            else: 
     308                mainlang = lang 
     309            return (lang, mainlang, order) 
    304310 
    305311        langs = [_parsed(el) for el in accept.split(',')] 
    306         langs.sort(lambda a,b: -1*cmp(a[1], b[1])) 
    307  
    308         for lang, order in langs: 
    309             langfile = gettext_module.find('django', globalpath, [to_locale(lang)]) 
    310             if langfile: 
    311                 # reconstruct the actual language from the language 
    312                 # filename, because otherwise we might incorrectly 
    313                 # report de_DE if we only have de available, but 
    314                 # did find de_DE because of language normalization 
    315                 lang = langfile[len(globalpath):].split(os.path.sep)[1] 
    316                 _accepted[accept] = lang 
    317                 return lang 
     312        langs.sort(lambda a,b: -1*cmp(a[2], b[2])) 
     313 
     314        for lang, mainlang, order in langs: 
     315            if lang in supported or mainlang in supported: 
     316                langfile = gettext_module.find('django', globalpath, [to_locale(lang)]) 
     317                if langfile: 
     318                    # reconstruct the actual language from the language 
     319                    # filename, because otherwise we might incorrectly 
     320                    # report de_DE if we only have de available, but 
     321                    # did find de_DE because of language normalization 
     322                    lang = langfile[len(globalpath):].split(os.path.sep)[1] 
     323                    _accepted[accept] = lang 
     324                    return lang 
    318325 
    319326    return settings.LANGUAGE_CODE