From 2cbb095bec757b804e8b6d9d0930ef3c6446a591 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Sat, 20 May 2017 13:28:20 +0200 Subject: [PATCH] Fixed #28221 -- Fixed plural fallback translations in JavaScriptCatalog view Thanks Waldemar Kornewald for the report and initial patch. --- django/views/i18n.py | 4 +++- tests/view_tests/tests/test_i18n.py | 18 ++++++++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/django/views/i18n.py b/django/views/i18n.py index 0b510019d5b..71f8cd575ef 100644 --- a/django/views/i18n.py +++ b/django/views/i18n.py @@ -273,8 +273,9 @@ class JavaScriptCatalog(View): catalog = {} trans_cat = self.translation._catalog trans_fallback_cat = self.translation._fallback._catalog if self.translation._fallback else {} + seen_keys = set() for key, value in itertools.chain(iter(trans_cat.items()), iter(trans_fallback_cat.items())): - if key == '' or key in catalog: + if key == '' or key in seen_keys: continue if isinstance(key, str): catalog[key] = value @@ -283,6 +284,7 @@ class JavaScriptCatalog(View): pdict.setdefault(msgid, {})[cnt] = value else: raise TypeError(key) + seen_keys.add(key) for k, v in pdict.items(): catalog[k] = [v.get(i, '') for i in range(num_plurals)] return catalog diff --git a/tests/view_tests/tests/test_i18n.py b/tests/view_tests/tests/test_i18n.py index 2c12177a598..8cd63765077 100644 --- a/tests/view_tests/tests/test_i18n.py +++ b/tests/view_tests/tests/test_i18n.py @@ -272,13 +272,27 @@ class I18NViewTests(SimpleTestCase): def test_i18n_fallback_language_plural(self): """ The fallback to a language with less plural forms maintains the real - language's number of plural forms. + language's number of plural forms and correct translations. """ with self.settings(LANGUAGE_CODE='pt'), override('ru'): response = self.client.get('/jsi18n/') self.assertEqual( response.context['catalog']['{count} plural3'], - ['{count} plural3', '{count} plural3s', '{count} plural3 p3t'] + ['{count} plural3 p3', '{count} plural3 p3s', '{count} plural3 p3t'] + ) + self.assertEqual( + response.context['catalog']['{count} plural2'], + ['{count} plural2', '{count} plural2s', ''] + ) + with self.settings(LANGUAGE_CODE='ru'), override('pt'): + response = self.client.get('/jsi18n/') + self.assertEqual( + response.context['catalog']['{count} plural3'], + ['{count} plural3', '{count} plural3s'] + ) + self.assertEqual( + response.context['catalog']['{count} plural2'], + ['{count} plural2', '{count} plural2s'] ) def test_i18n_english_variant(self):