Ticket #21579: i18n_patterns_with_script_name_patch.diff

File i18n_patterns_with_script_name_patch.diff, 2.9 KB (added by buettgenbach@…, 11 years ago)
  • django/middleware/locale.py

    diff --git a/django/middleware/locale.py b/django/middleware/locale.py
    index 87f904d..413561c 100644
    a b from collections import OrderedDict  
    44
    55from django.conf import settings
    66from django.core.urlresolvers import (is_valid_path, get_resolver,
    7                                       LocaleRegexURLResolver)
     7                                      LocaleRegexURLResolver, get_script_prefix)
    88from django.http import HttpResponseRedirect
    99from django.utils.cache import patch_vary_headers
    1010from django.utils import translation
    class LocaleMiddleware(object):  
    5050                path_valid = is_valid_path("%s/" % language_path, urlconf)
    5151
    5252            if path_valid:
    53                 language_url = "%s://%s/%s%s" % (
    54                     request.scheme, request.get_host(), language,
    55                     request.get_full_path())
     53                script_prefix = get_script_prefix()
     54                language_url = "%s://%s%s" % (
     55                    request.scheme, request.get_host(),
     56                        request.get_full_path().replace(script_prefix,
     57                            '%s%s/' % (script_prefix, language), 1))
    5658                return self.response_redirect_class(language_url)
    5759
    5860        # Store language back into session if it is not present
  • tests/i18n/patterns/tests.py

    diff --git a/tests/i18n/patterns/tests.py b/tests/i18n/patterns/tests.py
    index 78f5a46..e1af856 100644
    a b from __future__ import unicode_literals  
    33import os
    44
    55from django.core.exceptions import ImproperlyConfigured
    6 from django.core.urlresolvers import reverse, clear_url_caches
     6from django.core.urlresolvers import (reverse, clear_url_caches,
     7                                      set_script_prefix)
    78from django.http import HttpResponsePermanentRedirect
    89from django.middleware.locale import LocaleMiddleware
    910from django.test import TestCase
    class URLResponseTests(URLTestCaseBase):  
    291292        self.assertEqual(response.context['LANGUAGE_CODE'], 'pt-br')
    292293
    293294
     295class URLRedirectWithScriptAliasTests(URLTestCaseBase):
     296    """
     297    Tests if the response has the right language-code.
     298    """
     299    def setUp(self):
     300        super(URLRedirectWithScriptAliasTests, self).setUp()
     301        self.script_prefix = '/script_prefix'
     302        set_script_prefix(self.script_prefix)
     303
     304    def tearDown(self):
     305        super(URLRedirectWithScriptAliasTests, self).tearDown()
     306        # reset script prefix
     307        set_script_prefix('')
     308       
     309    def test_language_prefix_with_script_prefix(self):
     310        response = self.client.get('/prefixed/', HTTP_ACCEPT_LANGUAGE='en',
     311            SCRIPT_NAME=self.script_prefix)
     312       
     313        self.assertRedirects(response, '%s/en/prefixed/' % self.script_prefix,
     314            target_status_code=404)
     315
     316
    294317class URLTagTests(URLTestCaseBase):
    295318    """
    296319    Test if the language tag works.
Back to Top