Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #1821 from Bouke/tickets/14170

#14170 -- Reset i18n cache when settings changed
  • Loading branch information...
commit cb2c3ce15443a0666646e8b60984830c38d3ecde 2 parents 37a2e70 + d066984
Aymeric Augustin authored November 04, 2013
1  django/test/utils.py
@@ -427,7 +427,6 @@ def flush_caches(self):
427 427
         trans_real._translations = {}
428 428
         trans_real._active = local()
429 429
         trans_real._default = None
430  
-        trans_real._accepted = {}
431 430
         trans_real._checked_languages = {}
432 431
 
433 432
     def tearDown(self):
13  django/utils/translation/trans_real.py
@@ -11,6 +11,8 @@
11 11
 from threading import local
12 12
 import warnings
13 13
 
  14
+from django.dispatch import receiver
  15
+from django.test.signals import setting_changed
14 16
 from django.utils.encoding import force_str, force_text
15 17
 from django.utils.functional import memoize
16 18
 from django.utils._os import upath
@@ -47,6 +49,17 @@
47 49
 language_code_prefix_re = re.compile(r'^/([\w-]+)(/|$)')
48 50
 
49 51
 
  52
+@receiver(setting_changed)
  53
+def reset_cache(**kwargs):
  54
+    """
  55
+    Reset global state when LANGUAGES setting has been changed, as some
  56
+    languages should no longer be accepted.
  57
+    """
  58
+    if kwargs['setting'] == 'LANGUAGES':
  59
+        global _accepted
  60
+        _accepted = {}
  61
+
  62
+
50 63
 def to_locale(language, to_lower=False):
51 64
     """
52 65
     Turns a language name (en-us) into a locale name (en_US). If 'to_lower' is
4  django/views/static.py
@@ -15,7 +15,7 @@
15 15
 from django.template import loader, Template, Context, TemplateDoesNotExist
16 16
 from django.utils.http import http_date, parse_http_date
17 17
 from django.utils.six.moves.urllib.parse import unquote
18  
-from django.utils.translation import ugettext as _, ugettext_noop
  18
+from django.utils.translation import ugettext as _, ugettext_lazy
19 19
 
20 20
 
21 21
 def serve(request, path, document_root=None, show_indexes=False):
@@ -94,7 +94,7 @@ def serve(request, path, document_root=None, show_indexes=False):
94 94
   </body>
95 95
 </html>
96 96
 """
97  
-template_translatable = ugettext_noop("Index of %(directory)s")
  97
+template_translatable = ugettext_lazy("Index of %(directory)s")
98 98
 
99 99
 
100 100
 def directory_index(path, fullpath):
13  tests/i18n/tests.py
@@ -940,6 +940,19 @@ def test_percent_formatting_in_blocktrans(self):
940 940
             self.assertEqual(t_plur.render(Context({'percent': 42, 'num': 1})), '%(percent)s% represents 1 object')
941 941
             self.assertEqual(t_plur.render(Context({'percent': 42, 'num': 4})), '%(percent)s% represents 4 objects')
942 942
 
  943
+    def test_cache_resetting(self):
  944
+        """
  945
+        #14170 after setting LANGUAGE, cache should be cleared and languages
  946
+        previously valid should not be used.
  947
+        """
  948
+        g = get_language_from_request
  949
+        r = self.rf.get('/')
  950
+        r.COOKIES = {}
  951
+        r.META = {'HTTP_ACCEPT_LANGUAGE': 'pt-br'}
  952
+        self.assertEqual('pt-br', g(r))
  953
+        with self.settings(LANGUAGES=(('en', 'English'),)):
  954
+            self.assertNotEqual('pt-br', g(r))
  955
+
943 956
 
944 957
 class ResolutionOrderI18NTests(TransRealMixin, TestCase):
945 958
 

0 notes on commit cb2c3ce

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