Ticket #3594: 3594-r12362.diff

File 3594-r12362.diff, 5.6 KB (added by Ramiro Morales, 15 years ago)

Patch by arkx unified and updated to r12362, with fixes to tests and an optimization to the jsi18n view code

  • django/views/i18n.py

    diff --git a/django/views/i18n.py b/django/views/i18n.py
    a b  
    177177    locale = to_locale(get_language())
    178178    t = {}
    179179    paths = []
     180    en_catalog_missing = False
    180181    # first load all english languages files for defaults
    181182    for package in packages:
    182183        p = importlib.import_module(package)
     
    186187            catalog = gettext_module.translation(domain, path, ['en'])
    187188            t.update(catalog._catalog)
    188189        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.
    190196            pass
    191197    # next load the settings.LANGUAGE_CODE translations if it isn't english
    192198    if default_locale != 'en':
     
    199205                t.update(catalog._catalog)
    200206    # last load the currently selected language, if it isn't identical to the default.
    201207    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)
    209223    src = [LibHead]
    210224    plural = None
    211225    if '' in t:
  • deleted file tests/regressiontests/views/locale/en/LC_MESSAGES/djangojs.po

    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
    Binary file tests/regressiontests/views/locale/en/LC_MESSAGES/djangojs.mo has changed
    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
    + -  
    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"
    21  No newline at end of file
  • tests/regressiontests/views/tests/i18n.py

    diff --git a/tests/regressiontests/views/tests/i18n.py b/tests/regressiontests/views/tests/i18n.py
    a b  
    2121
    2222    def test_jsi18n(self):
    2323        """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']:
    2525            activate(lang_code)
    2626            catalog = gettext.translation('djangojs', locale_dir, [lang_code])
    2727            trans_txt = catalog.ugettext('this is to be translated')
     
    3030            # catalog['this is to be translated'] = 'same_that_trans_txt'
    3131            # javascript_quote is used to be able to check unicode strings
    3232            self.assertContains(response, javascript_quote(trans_txt), 1)
     33
     34class 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')
Back to Top