Fixed #28221 -- Fixed plural fallback translations in JavaScriptCatalog view

Thanks Waldemar Kornewald for the report and initial patch.
This commit is contained in:
Claude Paroz 2017-05-20 13:28:20 +02:00
parent ed9bc4e576
commit 2cbb095bec
2 changed files with 19 additions and 3 deletions

View File

@ -273,8 +273,9 @@ class JavaScriptCatalog(View):
catalog = {} catalog = {}
trans_cat = self.translation._catalog trans_cat = self.translation._catalog
trans_fallback_cat = self.translation._fallback._catalog if self.translation._fallback else {} 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())): 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 continue
if isinstance(key, str): if isinstance(key, str):
catalog[key] = value catalog[key] = value
@ -283,6 +284,7 @@ class JavaScriptCatalog(View):
pdict.setdefault(msgid, {})[cnt] = value pdict.setdefault(msgid, {})[cnt] = value
else: else:
raise TypeError(key) raise TypeError(key)
seen_keys.add(key)
for k, v in pdict.items(): for k, v in pdict.items():
catalog[k] = [v.get(i, '') for i in range(num_plurals)] catalog[k] = [v.get(i, '') for i in range(num_plurals)]
return catalog return catalog

View File

@ -272,13 +272,27 @@ class I18NViewTests(SimpleTestCase):
def test_i18n_fallback_language_plural(self): def test_i18n_fallback_language_plural(self):
""" """
The fallback to a language with less plural forms maintains the real 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'): with self.settings(LANGUAGE_CODE='pt'), override('ru'):
response = self.client.get('/jsi18n/') response = self.client.get('/jsi18n/')
self.assertEqual( self.assertEqual(
response.context['catalog']['{count} plural3'], 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): def test_i18n_english_variant(self):