Django

Code

Changeset 851

Show
Ignore:
Timestamp:
10/12/05 10:33:22 (3 years ago)
Author:
hugo
Message:

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.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/i18n/django/conf/admin_templates/base.html

    r763 r851  
    11<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
    2 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> 
     2<html xmlns="http://www.w3.org/1999/xhtml" lang="{{ LANGUAGE_CODE }}" xml:lang="{{ LANGUAGE_CODE }}"> 
    33<head> 
    44<title>{% block title %}{% endblock %}</title> 
  • django/branches/i18n/django/middleware/locale.py

    r816 r851  
    1919        global _module_to_app 
    2020 
    21         lang = translation.get_language_from_request(request) 
    22  
    23  
    2421        def findapp(module): 
    2522            app = _module_to_app.get(view_func.__module__, None) 
     
    3633        app = findapp(view_func.__module__) 
    3734 
    38         request.LANGUAGE_CODE = lang 
     35        lang = translation.get_language_from_request(request) 
    3936 
    4037        translation.activate(app, lang) 
     38 
     39        request.LANGUAGE_CODE = translation.get_language() 
    4140 
    4241    def process_response(self, request, response): 
  • django/branches/i18n/django/utils/translation.py

    r848 r851  
    181181    t = _active.get(currentThread(), None) 
    182182    if t is not None: 
    183         return t.language() 
    184     else: 
    185         from django.conf.settings import LANGUAGE_CODE 
    186         return LANGUAGE_CODE 
     183        try: 
     184            return t.language() 
     185        except AttributeError: 
     186            pass 
     187    # if we don't have a real translation object, we assume 
     188    # it's the default language. 
     189    from django.conf.settings import LANGUAGE_CODE 
     190    return LANGUAGE_CODE 
    187191 
    188192def gettext(message): 
     
    237241    global _accepted 
    238242 
     243    from django.conf import settings 
     244    globalpath = os.path.join(os.path.dirname(settings.__file__), 'locale') 
     245 
    239246    if request.GET or request.POST: 
    240         lang = request.GET.get('django_language', None) or request.POST.get('django_language', None) 
     247        lang_code = request.GET.get('django_language', None) or request.POST.get('django_language', None) 
     248        if lang_code is not None: 
     249            lang = gettext_module.find('django', globalpath, [lang_code]) 
     250            if lang is not None: 
     251                if hasattr(request, 'session'): 
     252                    request.session['django_language'] = lang_code 
     253                else: 
     254                    request.set_cookie('django_language', lang_code) 
     255                return lang_code 
     256 
     257    if hasattr(request, 'session'): 
     258        lang_code = request.session.get('django_language', None) 
     259        if lang_code is not None: 
     260            lang = gettext_module.find('django', globalpath, [lang_code]) 
     261            if lang is not None: 
     262                return lang_code 
     263     
     264    lang_code = request.COOKIES.get('django_language', None) 
     265    if lang_code is not None: 
     266        lang = gettext_module.find('django', globalpath, [lang_code]) 
    241267        if lang is not None: 
    242             if hasattr(request, 'session'): 
    243                 request.session['django_language'] = lang 
    244             else: 
    245                 request.set_cookie('django_language', lang) 
    246             return lang 
    247  
    248     if hasattr(request, 'session'): 
    249         lang = request.session.get('django_language', None) 
    250         if lang is not None: 
    251             return lang 
    252      
    253     lang = request.COOKIES.get('django_language', None) 
    254     if lang is not None: 
    255         return lang 
    256      
    257     from django.conf import settings 
    258  
     268            return lang_code 
     269     
    259270    accept = request.META.get('HTTP_ACCEPT_LANGUAGE', None) 
    260271    if accept is not None: 
     
    280291        langs.sort(lambda a,b: -1*cmp(a[1], b[1])) 
    281292 
    282         globalpath = os.path.join(os.path.dirname(settings.__file__), 'locale') 
    283  
    284293        for lang, order in langs: 
    285             if lang == 'en' or lang.startswith('en_') or gettext_module.find('django', globalpath, [lang]): 
     294            if lang == 'en' or lang.startswith('en_'): 
     295                # special casing for language en and derivates, because we don't 
     296                # have an english language file available, but just fallback 
     297                # to NullTranslation on those languages (as the source itself 
     298                # is in english) 
    286299                _accepted[accept] = lang 
    287300                return lang 
     301            else: 
     302                langfile = gettext_module.find('django', globalpath, [lang]) 
     303                if langfile: 
     304                    # reconstruct the actual language from the language 
     305                    # filename, because otherwise we might incorrectly 
     306                    # report de_DE if we only have de available, but 
     307                    # did find de_DE because of language normalization 
     308                    lang = langfile[len(globalpath):].split('/')[1] 
     309                    _accepted[accept] = lang 
     310                    return lang 
    288311     
    289312    return settings.LANGUAGE_CODE