From 01d43168749abe027473b34002ca76b4ce3d1eaa Mon Sep 17 00:00:00 2001 From: Jannis Leidel Date: Fri, 5 Feb 2010 01:54:22 +0000 Subject: [PATCH] [1.1.X] 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. Backport of r12384. Conflicts: tests/regressiontests/views/tests/i18n.py git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.1.X@12385 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/views/i18n.py | 30 ++++++++++---- .../views/locale/en/LC_MESSAGES/djangojs.mo | Bin 452 -> 0 bytes .../views/locale/en/LC_MESSAGES/djangojs.po | 20 --------- tests/regressiontests/views/tests/i18n.py | 39 +++++++++++++++++- 4 files changed, 60 insertions(+), 29 deletions(-) delete mode 100644 tests/regressiontests/views/locale/en/LC_MESSAGES/djangojs.mo delete mode 100644 tests/regressiontests/views/locale/en/LC_MESSAGES/djangojs.po diff --git a/django/views/i18n.py b/django/views/i18n.py index 0280698aae..23d248a2bc 100644 --- a/django/views/i18n.py +++ b/django/views/i18n.py @@ -127,6 +127,7 @@ def javascript_catalog(request, domain='djangojs', packages=None): 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) @@ -136,7 +137,12 @@ def javascript_catalog(request, domain='djangojs', packages=None): 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': @@ -149,13 +155,21 @@ def javascript_catalog(request, domain='djangojs', packages=None): 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: diff --git a/tests/regressiontests/views/locale/en/LC_MESSAGES/djangojs.mo b/tests/regressiontests/views/locale/en/LC_MESSAGES/djangojs.mo deleted file mode 100644 index 7593e1eb4888dceb7e468736f9a91f52ac96df80..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 452 zcmZ{gPfx-y7{-Uu%Z{Ercz6?!SR6(aClV$rxVV3gL87<8>L{5l>59=0;@wZ?XYpMi z;pCJ4@;1%$zJ1#7!{c9Ls{<^j$T@O^^pILHa)LY?Mx%pug`DC32WtbVRrhYCm%IX8 zTEd)xR+*^EOtajGm0YofCSFcvOjTS88)9epLetHi0i;(WCvdJ zH43TeXO$-DE}K*~jHbR1p%YN((ZKQif#bRfolN>@6~!hi7)e?7ZiDZH<7pka=lg?_ zx1=iiX5a;M|EPA$GKW%VCh(50HHOvfQ(vshjEg&1EHhOxeLqb{q`UXkS>Bi;R4io9 WMPWmC&b4X$_np86Fj17eS{i>`0D3L} diff --git a/tests/regressiontests/views/locale/en/LC_MESSAGES/djangojs.po b/tests/regressiontests/views/locale/en/LC_MESSAGES/djangojs.po deleted file mode 100644 index 54ee2c4065..0000000000 --- a/tests/regressiontests/views/locale/en/LC_MESSAGES/djangojs.po +++ /dev/null @@ -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 , 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 \n" -"Language-Team: LANGUAGE \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 diff --git a/tests/regressiontests/views/tests/i18n.py b/tests/regressiontests/views/tests/i18n.py index ebe97ab2dc..d06773ce94 100644 --- a/tests/regressiontests/views/tests/i18n.py +++ b/tests/regressiontests/views/tests/i18n.py @@ -20,7 +20,7 @@ class I18NTests(TestCase): def test_jsi18n(self): """The javascript_catalog can be deployed with language settings""" - for lang_code in ['es', 'fr', 'en']: + for lang_code in ['es', 'fr']: activate(lang_code) catalog = gettext.translation('djangojs', locale_dir, [lang_code]) trans_txt = catalog.ugettext('this is to be translated') @@ -28,3 +28,40 @@ class I18NTests(TestCase): # in response content must to be a line like that: # catalog['this is to be translated'] = 'same_that_trans_txt' self.assertContains(response, 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')