Ticket #13726: 13726.diff

File 13726.diff, 6.8 KB (added by ramiro, 5 years ago)

Patch for this issue, includes tests. See notes in ticket comment below about .mo file generation

  • django/views/i18n.py

    diff -r 731b9d6d9c9a django/views/i18n.py
    a b  
    177177    locale = to_locale(get_language())
    178178    t = {}
    179179    paths = []
    180     en_catalog_missing = False
     180    en_selected = locale.startswith('en')
     181    en_catalog_missing = True
    181182    # first load all english languages files for defaults
    182183    for package in packages:
    183184        p = importlib.import_module(package)
     
    187188            catalog = gettext_module.translation(domain, path, ['en'])
    188189            t.update(catalog._catalog)
    189190        except IOError:
    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.
    196191            pass
     192        else:
     193            # 'en' is the selected language and at least one of the packages
     194            # listed in `packages` has an 'en' catalog
     195            if en_selected:
     196                en_catalog_missing = False
    197197    # next load the settings.LANGUAGE_CODE translations if it isn't english
    198198    if default_locale != 'en':
    199199        for path in paths:
     
    205205                t.update(catalog._catalog)
    206206    # last load the currently selected language, if it isn't identical to the default.
    207207    if locale != default_locale:
    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:
     208        # If the currently selected language is English but it doesn't have a
     209        # translation catalog (presumably due to being the language translated
     210        # from) then a wrong language catalog might have been loaded in the
     211        # previous step. It needs to be discarded.
     212        if en_selected and en_catalog_missing:
    214213            t = {}
    215214        else:
    216215            locale_t = {}
  • new file tests/regressiontests/views/app0/__init__.py

    diff -r 731b9d6d9c9a tests/regressiontests/views/app0/__init__.py
    - +  
     1#
  • new file tests/regressiontests/views/app0/locale/en/LC_MESSAGES/djangojs.po

    diff -r 731b9d6d9c9a tests/regressiontests/views/app0/locale/en/LC_MESSAGES/djangojs.mo
    Binary file tests/regressiontests/views/app0/locale/en/LC_MESSAGES/djangojs.mo has changed
    diff -r 731b9d6d9c9a tests/regressiontests/views/app0/locale/en/LC_MESSAGES/djangojs.po
    - +  
     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
     7msgid ""
     8msgstr ""
     9"Project-Id-Version: PACKAGE VERSION\n"
     10"Report-Msgid-Bugs-To: \n"
     11"POT-Creation-Date: 2007-09-15 19:15+0200\n"
     12"PO-Revision-Date: 2010-05-12 12:41-0300\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
     19msgid "il faut traduire cette chaîne de caractères de app0"
     20msgstr "this app0 string is to be translated"
  • tests/regressiontests/views/tests/i18n.py

    diff -r 731b9d6d9c9a tests/regressiontests/views/tests/i18n.py
    a b  
    4040
    4141    def setUp(self):
    4242        self.old_language_code = settings.LANGUAGE_CODE
     43        self.old_installed_apps = settings.INSTALLED_APPS
    4344
    4445    def tearDown(self):
     46        deactivate()
    4547        settings.LANGUAGE_CODE = self.old_language_code
     48        settings.INSTALLED_APPS = self.old_installed_apps
    4649
    4750    def test_jsi18n_with_missing_en_files(self):
    4851        """
     
    7275    def testI18NLanguageNonEnglishDefault(self):
    7376        """
    7477        Check if the Javascript i18n view returns an empty language catalog
    75         if the default language is non-English but the selected language
    76         is English. See #13388 and #3594 for more details.
     78        if the default language is non-English, the selected language
     79        is English and there is not 'en' translation available. See #13388,
     80        #3594 and #13726 for more details.
    7781        """
    7882        settings.LANGUAGE_CODE = 'fr'
    7983        activate('en-us')
    8084        response = self.client.get('/views/jsi18n/')
    8185        self.assertNotContains(response, 'Choisir une heure')
    82         deactivate()
     86
     87    def test_nonenglish_default_english_userpref(self):
     88        """
     89        Same as above with the difference that there IS an 'en' translation
     90        available. The Javascript i18n view must return a NON empty language catalog
     91        with the proper English translations. See #13726 for more details.
     92        """
     93        settings.LANGUAGE_CODE = 'fr'
     94        settings.INSTALLED_APPS = list(settings.INSTALLED_APPS) + ['regressiontests.views.app0']
     95        activate('en-us')
     96        response = self.client.get('/views/jsi18n_english_translation/')
     97        self.assertContains(response, javascript_quote('this app0 string is to be translated'))
    8398
    8499    def testI18NLanguageNonEnglishFallback(self):
    85100        """
     
    90105        activate('none')
    91106        response = self.client.get('/views/jsi18n/')
    92107        self.assertContains(response, 'Choisir une heure')
    93         deactivate()
    94108
    95109
    96110class JsI18NTestsMultiPackage(TestCase):
  • tests/regressiontests/views/urls.py

    diff -r 731b9d6d9c9a tests/regressiontests/views/urls.py
    a b  
    1616    'packages': ('regressiontests.views',),
    1717}
    1818
     19js_info_dict_english_translation = {
     20    'domain': 'djangojs',
     21    'packages': ('regressiontests.views.app0',),
     22}
     23
    1924js_info_dict_multi_packages1 = {
    2025    'domain': 'djangojs',
    2126    'packages': ('regressiontests.views.app1', 'regressiontests.views.app2'),
     
    4651    # i18n views
    4752    (r'^i18n/', include('django.conf.urls.i18n')),
    4853    (r'^jsi18n/$', 'django.views.i18n.javascript_catalog', js_info_dict),
     54    (r'^jsi18n_english_translation/$', 'django.views.i18n.javascript_catalog', js_info_dict_english_translation),
    4955    (r'^jsi18n_multi_packages1/$', 'django.views.i18n.javascript_catalog', js_info_dict_multi_packages1),
    5056    (r'^jsi18n_multi_packages2/$', 'django.views.i18n.javascript_catalog', js_info_dict_multi_packages2),
    5157
Back to Top