Ticket #750: translation.2.py.diff

File translation.2.py.diff, 3.3 KB (added by Esaj <jason at jasondavies.com>, 19 years ago)

Handle sublanguages correctly

  • django/utils/translation.py

     
    253253gettext_lazy = lazy(gettext, str)
    254254ngettext_lazy = lazy(ngettext, str)
    255255
    256 def check_for_language(lang_code):
     256def check_for_language(lang, supported_langs, globalpath):
    257257    """
    258258    Checks whether there is a global language file for the given language code.
    259259    This is used to decide whether a user-provided language is available.
    260260    """
    261     from django.conf import settings
    262     globalpath = os.path.join(os.path.dirname(settings.__file__), 'locale')
    263     if gettext_module.find('django', globalpath, [to_locale(lang_code)]) is not None:
    264         return True
    265     else:
    266         return False
     261    return ((lang in supported_langs or (lang.find('_')>=0 and lang.split('_')[0] in supported_langs))
     262            and gettext_module.find('django', globalpath, [to_locale(lang)]) is not None)
    267263
    268264def get_language_from_request(request):
    269265    """
     
    273269    from django.conf import settings
    274270    globalpath = os.path.join(os.path.dirname(settings.__file__), 'locale')
    275271
     272    supported_langs = [a for (a,b) in settings.LANGUAGES]
     273    # take into account language normalization
     274    sublangs = filter(lambda a:a.find('_')>=0, supported_langs)
     275    supported_langs += [a.split('_')[0] for a in sublangs]
     276
    276277    if hasattr(request, 'session'):
    277278        lang_code = request.session.get('django_language', None)
    278         if lang_code is not None and check_for_language(lang_code):
     279        if lang_code is not None and check_for_language(lang_code, supported_langs, globalpath):
    279280            return lang_code
    280281
    281282    lang_code = request.COOKIES.get('django_language', None)
    282     if lang_code is not None and check_for_language(lang_code):
     283    if lang_code is not None and check_for_language(lang_code, supported_langs, globalpath):
    283284        return lang_code
    284285
    285286    accept = request.META.get('HTTP_ACCEPT_LANGUAGE', None)
     
    306307        langs.sort(lambda a,b: -1*cmp(a[1], b[1]))
    307308
    308309        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
     310            if lang in supported_langs or (lang.find('_')>=0 and lang.split('_')[0] in supported_langs):
     311                langfile = gettext_module.find('django', globalpath, [to_locale(lang)])
     312                if langfile:
     313                    # reconstruct the actual language from the language
     314                    # filename, because otherwise we might incorrectly
     315                    # report de_DE if we only have de available, but
     316                    # did find de_DE because of language normalization
     317                    lang = langfile[len(globalpath):].split(os.path.sep)[1]
     318                    _accepted[accept] = lang
     319                    return lang
    318320
    319321    return settings.LANGUAGE_CODE
    320322
Back to Top