Author: jezdez
Date: 2010-02-04 19:43:13 -0600 (Thu, 04 Feb 2010)
New Revision: 12384

Removed:
   django/trunk/tests/regressiontests/views/locale/en/LC_MESSAGES/djangojs.mo
   django/trunk/tests/regressiontests/views/locale/en/LC_MESSAGES/djangojs.po
Modified:
   django/trunk/django/views/i18n.py
   django/trunk/tests/regressiontests/views/tests/i18n.py
Log:
Fixed #3594 - Added ability to discard the language catalog in the JavaScript 
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.

Modified: django/trunk/django/views/i18n.py
===================================================================
--- django/trunk/django/views/i18n.py   2010-02-05 00:44:56 UTC (rev 12383)
+++ django/trunk/django/views/i18n.py   2010-02-05 01:43:13 UTC (rev 12384)
@@ -177,6 +177,7 @@
     locale = to_locale(get_language())
     t = {}
     paths = []
+    en_catalog_missing = False
     # first load all english languages files for defaults
     for package in packages:
         p = importlib.import_module(package)
@@ -186,7 +187,12 @@
             catalog = gettext_module.translation(domain, path, ['en'])
             t.update(catalog._catalog)
         except IOError:
-            # 'en' catalog was missing. This is harmless.
+            # 'en' catalog was missing.
+            if locale.startswith('en'):
+                # If 'en' is the selected language this would cause issues
+                # later on if default_locale is something other than 'en'.
+                en_catalog_missing = True
+            # Otherwise it is harmless.
             pass
     # next load the settings.LANGUAGE_CODE translations if it isn't english
     if default_locale != 'en':
@@ -199,13 +205,21 @@
                 t.update(catalog._catalog)
     # last load the currently selected language, if it isn't identical to the 
default.
     if locale != default_locale:
-        for path in paths:
-            try:
-                catalog = gettext_module.translation(domain, path, [locale])
-            except IOError:
-                catalog = None
-            if catalog is not None:
-                t.update(catalog._catalog)
+        # If the flag en_catalog_missing has been set, the currently
+        # selected language is English but it doesn't have a translation
+        # catalog (presumably due to being the language translated from).
+        # If that is the case, a wrong language catalog might have been
+        # loaded in the previous step. It needs to be discarded.
+        if en_catalog_missing:
+            t = {}
+        else:
+            for path in paths:
+                try:
+                    catalog = gettext_module.translation(domain, path, 
[locale])
+                except IOError:
+                    catalog = None
+                if catalog is not None:
+                    t.update(catalog._catalog)
     src = [LibHead]
     plural = None
     if '' in t:

Deleted: 
django/trunk/tests/regressiontests/views/locale/en/LC_MESSAGES/djangojs.mo
===================================================================
(Binary files differ)

Deleted: 
django/trunk/tests/regressiontests/views/locale/en/LC_MESSAGES/djangojs.po
===================================================================
--- django/trunk/tests/regressiontests/views/locale/en/LC_MESSAGES/djangojs.po  
2010-02-05 00:44:56 UTC (rev 12383)
+++ django/trunk/tests/regressiontests/views/locale/en/LC_MESSAGES/djangojs.po  
2010-02-05 01:43:13 UTC (rev 12384)
@@ -1,20 +0,0 @@
-# SOME DESCRIPTIVE TITLE.
-# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
-# This file is distributed under the same license as the PACKAGE package.
-# FIRST AUTHOR <em...@address>, YEAR.
-#
-#, fuzzy
-msgid ""
-msgstr ""
-"Project-Id-Version: PACKAGE VERSION\n"
-"Report-Msgid-Bugs-To: \n"
-"POT-Creation-Date: 2007-09-15 16:45+0200\n"
-"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
-"Last-Translator: FULL NAME <em...@address>\n"
-"Language-Team: LANGUAGE <l...@li.org>\n"
-"MIME-Version: 1.0\n"
-"Content-Type: text/plain; charset=UTF-8\n"
-"Content-Transfer-Encoding: 8bit\n"
-
-msgid "this is to be translated"
-msgstr "this is to be translated in english"
\ No newline at end of file

Modified: django/trunk/tests/regressiontests/views/tests/i18n.py
===================================================================
--- django/trunk/tests/regressiontests/views/tests/i18n.py      2010-02-05 
00:44:56 UTC (rev 12383)
+++ django/trunk/tests/regressiontests/views/tests/i18n.py      2010-02-05 
01:43:13 UTC (rev 12384)
@@ -21,7 +21,7 @@
 
     def test_jsi18n(self):
         """The javascript_catalog can be deployed with language settings"""
-        for lang_code in ['es', 'fr', 'en', 'ru']:
+        for lang_code in ['es', 'fr', 'ru']:
             activate(lang_code)
             catalog = gettext.translation('djangojs', locale_dir, [lang_code])
             trans_txt = catalog.ugettext('this is to be translated')
@@ -30,3 +30,40 @@
             # catalog['this is to be translated'] = 'same_that_trans_txt'
             # javascript_quote is used to be able to check unicode strings
             self.assertContains(response, javascript_quote(trans_txt), 1)
+
+class JsI18NTests(TestCase):
+    """
+    Tests django views in django/views/i18n.py that need to change
+    settings.LANGUAGE_CODE.
+    """
+
+    def setUp(self):
+        self.old_language_code = settings.LANGUAGE_CODE
+
+    def tearDown(self):
+        settings.LANGUAGE_CODE = self.old_language_code
+
+    def test_jsi18n_with_missing_en_files(self):
+        """
+        The javascript_catalog shouldn't load the fallback language in the
+        case that the current selected language is actually the one translated
+        from, and hence missing translation files completely.
+
+        This happens easily when you're translating from English to other
+        languages and you've set settings.LANGUAGE_CODE to some other language
+        than English.
+        """
+        settings.LANGUAGE_CODE = 'es'
+        activate('en-us')
+        response = self.client.get('/views/jsi18n/')
+        self.assertNotContains(response, 'esto tiene que ser traducido')
+
+    def test_jsi18n_fallback_language(self):
+        """
+        Let's make sure that the fallback language is still working properly
+        in cases where the selected language cannot be found.
+        """
+        settings.LANGUAGE_CODE = 'fr'
+        activate('fi')
+        response = self.client.get('/views/jsi18n/')
+        self.assertContains(response, 'il faut le traduire')

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to django-upda...@googlegroups.com.
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to