Ticket #17720: 17720.diff

File 17720.diff, 5.7 KB (added by Anssi Kääriäinen, 13 years ago)
  • django/middleware/locale.py

    diff --git a/django/middleware/locale.py b/django/middleware/locale.py
    index d42a615..3f562c5 100644
    a b class LocaleMiddleware(object):  
    1515    """
    1616
    1717    def process_request(self, request):
    18         language = translation.get_language_from_request(request)
     18        check_path = self.is_language_prefix_patterns_used()
     19        language = translation.get_language_from_request(request, check_path=check_path)
    1920        translation.activate(language)
    2021        request.LANGUAGE_CODE = translation.get_language()
    2122
  • django/utils/translation/__init__.py

    diff --git a/django/utils/translation/__init__.py b/django/utils/translation/__init__.py
    index 8ad9c5d..dbe4823 100644
    a b def check_for_language(lang_code):  
    144144def to_locale(language):
    145145    return _trans.to_locale(language)
    146146
    147 def get_language_from_request(request):
    148     return _trans.get_language_from_request(request)
     147def get_language_from_request(request, check_path=False):
     148    return _trans.get_language_from_request(request, check_path)
    149149
    150150def get_language_from_path(path):
    151151    return _trans.get_language_from_path(path)
  • django/utils/translation/trans_null.py

    diff --git a/django/utils/translation/trans_null.py b/django/utils/translation/trans_null.py
    index 5c9edb1..2884557 100644
    a b def to_locale(language):  
    5555    else:
    5656        return language.lower()
    5757
    58 def get_language_from_request(request):
     58def get_language_from_request(request, check_path):
    5959    return settings.LANGUAGE_CODE
    6060
    6161def get_language_from_path(request):
  • django/utils/translation/trans_real.py

    diff --git a/django/utils/translation/trans_real.py b/django/utils/translation/trans_real.py
    index 419b380..fb4bca4 100644
    a b def get_language_from_path(path, supported=None):  
    363363        if lang_code in supported and check_for_language(lang_code):
    364364            return lang_code
    365365
    366 def get_language_from_request(request):
     366def get_language_from_request(request, check_path):
    367367    """
    368368    Analyzes the request to find what language the user wants the system to
    369369    show. Only languages listed in settings.LANGUAGES are taken into account.
    370370    If the user requests a sublanguage where we have a main language, we send
    371371    out the main language.
     372
     373    If check_path is true, the URL path prefix will be checked for a language
     374    code, otherwise this is skipped for backwards compatibility.
    372375    """
    373376    global _accepted
    374377    from django.conf import settings
    375378    supported = dict(settings.LANGUAGES)
    376379
    377     lang_code = get_language_from_path(request.path_info, supported)
    378     if lang_code is not None:
    379         return lang_code
     380    if check_path:
     381        lang_code = get_language_from_path(request.path_info, supported)
     382        if lang_code is not None:
     383            return lang_code
    380384
    381385    if hasattr(request, 'session'):
    382386        lang_code = request.session.get('django_language', None)
  • new file tests/regressiontests/i18n/patterns/templates/showlang.html

    diff --git a/tests/regressiontests/i18n/patterns/templates/showlang.html b/tests/regressiontests/i18n/patterns/templates/showlang.html
    new file mode 100644
    index 0000000..f1e6c61
    - +  
     1LANGUAGE: {{ lang }}
  • tests/regressiontests/i18n/patterns/tests.py

    diff --git a/tests/regressiontests/i18n/patterns/tests.py b/tests/regressiontests/i18n/patterns/tests.py
    index 40a62d4..a9b4f22 100644
    a b class URLTagTests(URLTestCaseBase):  
    292292            {% language 'pt-br' %}{% url 'no-prefix-translated-slug' slug='apo' %}{% endlanguage %}""")
    293293        self.assertEqual(tpl.render(Context({})).strip().split(),
    294294                         [u'/vertaald/apo/', u'/traduzidos/apo/'])
     295
     296class NoI18NPatternsTest(URLTestCaseBase):
     297    urls = 'regressiontests.i18n.patterns.urls.no18n'
     298    def test_no_lang_activate(self):
     299        """
     300        Check that if no i18n_patterns is used in root urlconfs, then no
     301        language activation happens based on url prefix.
     302        """
     303        rev = reverse('notrans')
     304        response = self.client.get(rev)
     305        self.assertContains(response, 'LANGUAGE: en')
  • new file tests/regressiontests/i18n/patterns/urls/no18n.py

    diff --git a/tests/regressiontests/i18n/patterns/urls/no18n.py b/tests/regressiontests/i18n/patterns/urls/no18n.py
    new file mode 100644
    index 0000000..5d00fbe
    - +  
     1from django.conf.urls import url
     2from django.conf.urls import patterns
     3from django.views.generic import TemplateView
     4from django.utils.translation import get_language
     5
     6class LangTemplate(TemplateView):
     7    def get_context_data(self, **kwargs):
     8        context = super(LangTemplate, self).get_context_data(**kwargs)
     9        context['lang'] = get_language
     10        return context
     11
     12view = LangTemplate.as_view(template_name='showlang.html')
     13
     14urlpatterns = patterns('',
     15    url(r'^nl/notrans/', view, name='notrans'),
     16)
  • tests/regressiontests/i18n/tests.py

    diff --git a/tests/regressiontests/i18n/tests.py b/tests/regressiontests/i18n/tests.py
    index 4b543d2..c647f81 100644
    a b from .models import Company, TestModel  
    3939from .patterns.tests import (URLRedirectWithoutTrailingSlashTests,
    4040    URLTranslationTests, URLDisabledTests, URLTagTests, URLTestCaseBase,
    4141    URLRedirectWithoutTrailingSlashSettingTests, URLNamespaceTests,
    42     URLPrefixTests, URLResponseTests, URLRedirectTests)
     42    URLPrefixTests, URLResponseTests, URLRedirectTests, NoI18NPatternsTest)
    4343from .test_warnings import DeprecationWarningTests
    4444
    4545
Back to Top