Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #3594 - Added ability to discard the language catalog in the Ja…

…vaScript i18n view in case the selected language is English but no English translation catalog actual exists, e.g. due to being the language translated from. Thanks to msaelices, aryx and Ramiro Morales.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12384 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 8600ad4c5087bbd86467c854c0c498802690b63c 1 parent cdeb90e
Jannis Leidel authored February 05, 2010
30  django/views/i18n.py
@@ -177,6 +177,7 @@ def javascript_catalog(request, domain='djangojs', packages=None):
177 177
     locale = to_locale(get_language())
178 178
     t = {}
179 179
     paths = []
  180
+    en_catalog_missing = False
180 181
     # first load all english languages files for defaults
181 182
     for package in packages:
182 183
         p = importlib.import_module(package)
@@ -186,7 +187,12 @@ def javascript_catalog(request, domain='djangojs', packages=None):
186 187
             catalog = gettext_module.translation(domain, path, ['en'])
187 188
             t.update(catalog._catalog)
188 189
         except IOError:
189  
-            # 'en' catalog was missing. This is harmless.
  190
+            # 'en' catalog was missing.
  191
+            if locale.startswith('en'):
  192
+                # If 'en' is the selected language this would cause issues
  193
+                # later on if default_locale is something other than 'en'.
  194
+                en_catalog_missing = True
  195
+            # Otherwise it is harmless.
190 196
             pass
191 197
     # next load the settings.LANGUAGE_CODE translations if it isn't english
192 198
     if default_locale != 'en':
@@ -199,13 +205,21 @@ def javascript_catalog(request, domain='djangojs', packages=None):
199 205
                 t.update(catalog._catalog)
200 206
     # last load the currently selected language, if it isn't identical to the default.
201 207
     if locale != default_locale:
202  
-        for path in paths:
203  
-            try:
204  
-                catalog = gettext_module.translation(domain, path, [locale])
205  
-            except IOError:
206  
-                catalog = None
207  
-            if catalog is not None:
208  
-                t.update(catalog._catalog)
  208
+        # If the flag en_catalog_missing has been set, the currently
  209
+        # selected language is English but it doesn't have a translation
  210
+        # catalog (presumably due to being the language translated from).
  211
+        # If that is the case, a wrong language catalog might have been
  212
+        # loaded in the previous step. It needs to be discarded.
  213
+        if en_catalog_missing:
  214
+            t = {}
  215
+        else:
  216
+            for path in paths:
  217
+                try:
  218
+                    catalog = gettext_module.translation(domain, path, [locale])
  219
+                except IOError:
  220
+                    catalog = None
  221
+                if catalog is not None:
  222
+                    t.update(catalog._catalog)
209 223
     src = [LibHead]
210 224
     plural = None
211 225
     if '' in t:
BIN  tests/regressiontests/views/locale/en/LC_MESSAGES/djangojs.mo
Binary file not shown
20  tests/regressiontests/views/locale/en/LC_MESSAGES/djangojs.po
... ...
@@ -1,20 +0,0 @@
1  
-# SOME DESCRIPTIVE TITLE.
2  
-# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
3  
-# This file is distributed under the same license as the PACKAGE package.
4  
-# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
5  
-#
6  
-#, fuzzy
7  
-msgid ""
8  
-msgstr ""
9  
-"Project-Id-Version: PACKAGE VERSION\n"
10  
-"Report-Msgid-Bugs-To: \n"
11  
-"POT-Creation-Date: 2007-09-15 16:45+0200\n"
12  
-"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
13  
-"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
14  
-"Language-Team: LANGUAGE <LL@li.org>\n"
15  
-"MIME-Version: 1.0\n"
16  
-"Content-Type: text/plain; charset=UTF-8\n"
17  
-"Content-Transfer-Encoding: 8bit\n"
18  
-
19  
-msgid "this is to be translated"
20  
-msgstr "this is to be translated in english"
39  tests/regressiontests/views/tests/i18n.py
@@ -21,7 +21,7 @@ def test_setlang(self):
21 21
 
22 22
     def test_jsi18n(self):
23 23
         """The javascript_catalog can be deployed with language settings"""
24  
-        for lang_code in ['es', 'fr', 'en', 'ru']:
  24
+        for lang_code in ['es', 'fr', 'ru']:
25 25
             activate(lang_code)
26 26
             catalog = gettext.translation('djangojs', locale_dir, [lang_code])
27 27
             trans_txt = catalog.ugettext('this is to be translated')
@@ -30,3 +30,40 @@ def test_jsi18n(self):
30 30
             # catalog['this is to be translated'] = 'same_that_trans_txt'
31 31
             # javascript_quote is used to be able to check unicode strings
32 32
             self.assertContains(response, javascript_quote(trans_txt), 1)
  33
+
  34
+class JsI18NTests(TestCase):
  35
+    """
  36
+    Tests django views in django/views/i18n.py that need to change
  37
+    settings.LANGUAGE_CODE.
  38
+    """
  39
+
  40
+    def setUp(self):
  41
+        self.old_language_code = settings.LANGUAGE_CODE
  42
+
  43
+    def tearDown(self):
  44
+        settings.LANGUAGE_CODE = self.old_language_code
  45
+
  46
+    def test_jsi18n_with_missing_en_files(self):
  47
+        """
  48
+        The javascript_catalog shouldn't load the fallback language in the
  49
+        case that the current selected language is actually the one translated
  50
+        from, and hence missing translation files completely.
  51
+
  52
+        This happens easily when you're translating from English to other
  53
+        languages and you've set settings.LANGUAGE_CODE to some other language
  54
+        than English.
  55
+        """
  56
+        settings.LANGUAGE_CODE = 'es'
  57
+        activate('en-us')
  58
+        response = self.client.get('/views/jsi18n/')
  59
+        self.assertNotContains(response, 'esto tiene que ser traducido')
  60
+
  61
+    def test_jsi18n_fallback_language(self):
  62
+        """
  63
+        Let's make sure that the fallback language is still working properly
  64
+        in cases where the selected language cannot be found.
  65
+        """
  66
+        settings.LANGUAGE_CODE = 'fr'
  67
+        activate('fi')
  68
+        response = self.client.get('/views/jsi18n/')
  69
+        self.assertContains(response, 'il faut le traduire')

0 notes on commit 8600ad4

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