Loading django/utils/cache.py +1 −1 Original line number Diff line number Diff line Loading @@ -158,7 +158,7 @@ def has_vary_header(response, header_query): def _i18n_cache_key_suffix(request, cache_key): """If enabled, returns the cache key ending with a locale.""" if settings.USE_I18N: if settings.USE_I18N or settings.USE_L10N: # first check if LocaleMiddleware or another middleware added # LANGUAGE_CODE to request, then fall back to the active language # which in turn can also fall back to settings.LANGUAGE_CODE Loading docs/topics/cache.txt +4 −0 Original line number Diff line number Diff line Loading @@ -498,6 +498,10 @@ include the name of the active :term:`language<language code>` -- see also :ref:`how-django-discovers-language-preference`). This allows you to easily cache multilingual sites without having to create the cache key yourself. .. versionchanged:: 1.4 This also happens when :setting:`USE_L10N` is set to ``True``. __ `Controlling cache: Using other headers`_ The per-view cache Loading tests/regressiontests/cache/tests.py +15 −5 Original line number Diff line number Diff line Loading @@ -1154,23 +1154,33 @@ class CacheI18nTest(TestCase): request.session = {} return request @override_settings(USE_I18N=True) def test_cache_key_i18n(self): @override_settings(USE_I18N=True, USE_L10N=False) def test_cache_key_i18n_translation(self): request = self._get_request() lang = translation.get_language() response = HttpResponse() key = learn_cache_key(request, response) self.assertTrue(key.endswith(lang), "Cache keys should include the language name when i18n is active") self.assertIn(lang, key, "Cache keys should include the language name when translation is active") key2 = get_cache_key(request) self.assertEqual(key, key2) @override_settings(USE_I18N=False) @override_settings(USE_I18N=False, USE_L10N=True) def test_cache_key_i18n_formatting(self): request = self._get_request() lang = translation.get_language() response = HttpResponse() key = learn_cache_key(request, response) self.assertIn(lang, key, "Cache keys should include the language name when formatting is active") key2 = get_cache_key(request) self.assertEqual(key, key2) @override_settings(USE_I18N=False, USE_L10N=False) def test_cache_key_no_i18n (self): request = self._get_request() lang = translation.get_language() response = HttpResponse() key = learn_cache_key(request, response) self.assertFalse(key.endswith(lang), "Cache keys shouldn't include the language name when i18n is inactive") self.assertNotIn(lang, key, "Cache keys shouldn't include the language name when i18n isn't active") @override_settings( CACHE_MIDDLEWARE_KEY_PREFIX="test", Loading Loading
django/utils/cache.py +1 −1 Original line number Diff line number Diff line Loading @@ -158,7 +158,7 @@ def has_vary_header(response, header_query): def _i18n_cache_key_suffix(request, cache_key): """If enabled, returns the cache key ending with a locale.""" if settings.USE_I18N: if settings.USE_I18N or settings.USE_L10N: # first check if LocaleMiddleware or another middleware added # LANGUAGE_CODE to request, then fall back to the active language # which in turn can also fall back to settings.LANGUAGE_CODE Loading
docs/topics/cache.txt +4 −0 Original line number Diff line number Diff line Loading @@ -498,6 +498,10 @@ include the name of the active :term:`language<language code>` -- see also :ref:`how-django-discovers-language-preference`). This allows you to easily cache multilingual sites without having to create the cache key yourself. .. versionchanged:: 1.4 This also happens when :setting:`USE_L10N` is set to ``True``. __ `Controlling cache: Using other headers`_ The per-view cache Loading
tests/regressiontests/cache/tests.py +15 −5 Original line number Diff line number Diff line Loading @@ -1154,23 +1154,33 @@ class CacheI18nTest(TestCase): request.session = {} return request @override_settings(USE_I18N=True) def test_cache_key_i18n(self): @override_settings(USE_I18N=True, USE_L10N=False) def test_cache_key_i18n_translation(self): request = self._get_request() lang = translation.get_language() response = HttpResponse() key = learn_cache_key(request, response) self.assertTrue(key.endswith(lang), "Cache keys should include the language name when i18n is active") self.assertIn(lang, key, "Cache keys should include the language name when translation is active") key2 = get_cache_key(request) self.assertEqual(key, key2) @override_settings(USE_I18N=False) @override_settings(USE_I18N=False, USE_L10N=True) def test_cache_key_i18n_formatting(self): request = self._get_request() lang = translation.get_language() response = HttpResponse() key = learn_cache_key(request, response) self.assertIn(lang, key, "Cache keys should include the language name when formatting is active") key2 = get_cache_key(request) self.assertEqual(key, key2) @override_settings(USE_I18N=False, USE_L10N=False) def test_cache_key_no_i18n (self): request = self._get_request() lang = translation.get_language() response = HttpResponse() key = learn_cache_key(request, response) self.assertFalse(key.endswith(lang), "Cache keys shouldn't include the language name when i18n is inactive") self.assertNotIn(lang, key, "Cache keys shouldn't include the language name when i18n isn't active") @override_settings( CACHE_MIDDLEWARE_KEY_PREFIX="test", Loading