diff --git a/django/middleware/locale.py b/django/middleware/locale.py
index d42a615..3f562c5 100644
a
|
b
|
class LocaleMiddleware(object):
|
15 | 15 | """ |
16 | 16 | |
17 | 17 | 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) |
19 | 20 | translation.activate(language) |
20 | 21 | request.LANGUAGE_CODE = translation.get_language() |
21 | 22 | |
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):
|
144 | 144 | def to_locale(language): |
145 | 145 | return _trans.to_locale(language) |
146 | 146 | |
147 | | def get_language_from_request(request): |
148 | | return _trans.get_language_from_request(request) |
| 147 | def get_language_from_request(request, check_path=False): |
| 148 | return _trans.get_language_from_request(request, check_path) |
149 | 149 | |
150 | 150 | def get_language_from_path(path): |
151 | 151 | return _trans.get_language_from_path(path) |
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):
|
55 | 55 | else: |
56 | 56 | return language.lower() |
57 | 57 | |
58 | | def get_language_from_request(request): |
| 58 | def get_language_from_request(request, check_path): |
59 | 59 | return settings.LANGUAGE_CODE |
60 | 60 | |
61 | 61 | def get_language_from_path(request): |
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):
|
363 | 363 | if lang_code in supported and check_for_language(lang_code): |
364 | 364 | return lang_code |
365 | 365 | |
366 | | def get_language_from_request(request): |
| 366 | def get_language_from_request(request, check_path): |
367 | 367 | """ |
368 | 368 | Analyzes the request to find what language the user wants the system to |
369 | 369 | show. Only languages listed in settings.LANGUAGES are taken into account. |
370 | 370 | If the user requests a sublanguage where we have a main language, we send |
371 | 371 | 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. |
372 | 375 | """ |
373 | 376 | global _accepted |
374 | 377 | from django.conf import settings |
375 | 378 | supported = dict(settings.LANGUAGES) |
376 | 379 | |
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 |
380 | 384 | |
381 | 385 | if hasattr(request, 'session'): |
382 | 386 | lang_code = request.session.get('django_language', None) |
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
-
|
+
|
|
| 1 | LANGUAGE: {{ lang }} |
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):
|
292 | 292 | {% language 'pt-br' %}{% url 'no-prefix-translated-slug' slug='apo' %}{% endlanguage %}""") |
293 | 293 | self.assertEqual(tpl.render(Context({})).strip().split(), |
294 | 294 | [u'/vertaald/apo/', u'/traduzidos/apo/']) |
| 295 | |
| 296 | class 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') |
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
-
|
+
|
|
| 1 | from django.conf.urls import url |
| 2 | from django.conf.urls import patterns |
| 3 | from django.views.generic import TemplateView |
| 4 | from django.utils.translation import get_language |
| 5 | |
| 6 | class 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 | |
| 12 | view = LangTemplate.as_view(template_name='showlang.html') |
| 13 | |
| 14 | urlpatterns = patterns('', |
| 15 | url(r'^nl/notrans/', view, name='notrans'), |
| 16 | ) |
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
|
39 | 39 | from .patterns.tests import (URLRedirectWithoutTrailingSlashTests, |
40 | 40 | URLTranslationTests, URLDisabledTests, URLTagTests, URLTestCaseBase, |
41 | 41 | URLRedirectWithoutTrailingSlashSettingTests, URLNamespaceTests, |
42 | | URLPrefixTests, URLResponseTests, URLRedirectTests) |
| 42 | URLPrefixTests, URLResponseTests, URLRedirectTests, NoI18NPatternsTest) |
43 | 43 | from .test_warnings import DeprecationWarningTests |
44 | 44 | |
45 | 45 | |