Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #14170 -- Reset i18n cache when settings change

  • Loading branch information...
commit d0669843d0310a5c633548686ad8ce19404fc594 1 parent b44d42b
Bouke Haarsma Bouke authored
1  django/test/utils.py
View
@@ -427,7 +427,6 @@ def flush_caches(self):
trans_real._translations = {}
trans_real._active = local()
trans_real._default = None
- trans_real._accepted = {}
trans_real._checked_languages = {}
def tearDown(self):
13 django/utils/translation/trans_real.py
View
@@ -11,6 +11,8 @@
from threading import local
import warnings
+from django.dispatch import receiver
+from django.test.signals import setting_changed
Claude Paroz Collaborator
claudep added a note

Note that this is adding a new dependency between django runtime and the test framework (see https://code.djangoproject.com/ticket/20349)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
from django.utils.encoding import force_str, force_text
from django.utils.functional import memoize
from django.utils._os import upath
@@ -47,6 +49,17 @@
language_code_prefix_re = re.compile(r'^/([\w-]+)(/|$)')
+@receiver(setting_changed)
+def reset_cache(**kwargs):
+ """
+ Reset global state when LANGUAGES setting has been changed, as some
+ languages should no longer be accepted.
+ """
+ if kwargs['setting'] == 'LANGUAGES':
+ global _accepted
+ _accepted = {}
+
+
def to_locale(language, to_lower=False):
"""
Turns a language name (en-us) into a locale name (en_US). If 'to_lower' is
4 django/views/static.py
View
@@ -15,7 +15,7 @@
from django.template import loader, Template, Context, TemplateDoesNotExist
from django.utils.http import http_date, parse_http_date
from django.utils.six.moves.urllib.parse import unquote
-from django.utils.translation import ugettext as _, ugettext_noop
+from django.utils.translation import ugettext as _, ugettext_lazy
def serve(request, path, document_root=None, show_indexes=False):
"""
@@ -93,7 +93,7 @@ def serve(request, path, document_root=None, show_indexes=False):
</body>
</html>
"""
-template_translatable = ugettext_noop("Index of %(directory)s")
+template_translatable = ugettext_lazy("Index of %(directory)s")
def directory_index(path, fullpath):
try:
13 tests/i18n/tests.py
View
@@ -940,6 +940,19 @@ def test_percent_formatting_in_blocktrans(self):
self.assertEqual(t_plur.render(Context({'percent': 42, 'num': 1})), '%(percent)s% represents 1 object')
self.assertEqual(t_plur.render(Context({'percent': 42, 'num': 4})), '%(percent)s% represents 4 objects')
+ def test_cache_resetting(self):
+ """
+ #14170 after setting LANGUAGE, cache should be cleared and languages
+ previously valid should not be used.
+ """
+ g = get_language_from_request
+ r = self.rf.get('/')
+ r.COOKIES = {}
+ r.META = {'HTTP_ACCEPT_LANGUAGE': 'pt-br'}
+ self.assertEqual('pt-br', g(r))
+ with self.settings(LANGUAGES=(('en', 'English'),)):
+ self.assertNotEqual('pt-br', g(r))
+
class ResolutionOrderI18NTests(TransRealMixin, TestCase):
Claude Paroz

Note that this is adding a new dependency between django runtime and the test framework (see https://code.djangoproject.com/ticket/20349)

Please sign in to comment.
Something went wrong with that request. Please try again.