From bbd6adf9048fc7231eff076228595d4bdea554c6 Mon Sep 17 00:00:00 2001 From: Sylvain Viollon Date: Wed, 24 Oct 2018 08:41:32 +0200 Subject: [PATCH 1/2] Fix defaults again. --- src/zope/i18n/tests/test_plurals.py | 130 ++++++++++++++++++---------- src/zope/i18n/translationdomain.py | 4 +- 2 files changed, 86 insertions(+), 48 deletions(-) diff --git a/src/zope/i18n/tests/test_plurals.py b/src/zope/i18n/tests/test_plurals.py index 28d6b3a..d4f57c7 100644 --- a/src/zope/i18n/tests/test_plurals.py +++ b/src/zope/i18n/tests/test_plurals.py @@ -41,41 +41,6 @@ def _getTranslationDomain(self, locale, variant="default"): domain.addCatalog(catalog) return domain - def test_translate_without_defaults(self): - domain = self._getTranslationDomain('en') - zope.component.provideUtility(domain, ITranslationDomain, 'default') - self.assertEqual( - translate('One apple', domain='default', - msgid_plural='%d apples', number=0), - '0 apples') - self.assertEqual( - translate('One apple', domain='default', - msgid_plural='%d apples', number=1), - 'One apple') - self.assertEqual( - translate('One apple', domain='default', - msgid_plural='%d apples', number=2), - '2 apples') - - def test_translate_with_defaults(self): - domain = self._getTranslationDomain('en') - zope.component.provideUtility(domain, ITranslationDomain, 'default') - self.assertEqual( - translate('One apple', domain='default', - msgid_plural='%d apples', number=0, - default='One fruit', default_plural='%d fruits'), - '0 fruits') - self.assertEqual( - translate('One apple', domain='default', - msgid_plural='%d apples', number=1, - default='One fruit', default_plural='%d fruits'), - 'One fruit') - self.assertEqual( - translate('One apple', domain='default', - msgid_plural='%d apples', number=2, - default='One fruit', default_plural='%d fruits'), - '2 fruits') - def test_missing_queryPluralMessage(self): catalog = self._getMessageCatalog('en') self.assertEqual(catalog.language, 'en') @@ -208,10 +173,81 @@ def test_floater(self): 'There are %f chances.', 3.5), 'There are 3.500000 chances.') - def test_recursive_translation(self): + def test_translate_without_defaults(self): + domain = self._getTranslationDomain('en') + zope.component.provideUtility(domain, ITranslationDomain, 'default') + self.assertEqual( + translate('One apple', domain='default', + msgid_plural='%d apples', number=0), + '0 apples') + self.assertEqual( + translate('One apple', domain='default', + msgid_plural='%d apples', number=1), + 'One apple') + self.assertEqual( + translate('One apple', domain='default', + msgid_plural='%d apples', number=2), + '2 apples') + + def test_translate_with_defaults(self): + domain = self._getTranslationDomain('en') + zope.component.provideUtility(domain, ITranslationDomain, 'default') + self.assertEqual( + translate('One apple', domain='default', + msgid_plural='%d apples', number=0, + default='One fruit', default_plural='%d fruits'), + '0 fruits') + self.assertEqual( + translate('One apple', domain='default', + msgid_plural='%d apples', number=1, + default='One fruit', default_plural='%d fruits'), + 'One fruit') + self.assertEqual( + translate('One apple', domain='default', + msgid_plural='%d apples', number=2, + default='One fruit', default_plural='%d fruits'), + '2 fruits') + + def test_translate_message_without_defaults(self): + domain = self._getTranslationDomain('en') + factory = MessageFactory('default') + zope.component.provideUtility(domain, ITranslationDomain, 'default') + self.assertEqual( + translate(factory('One apple', msgid_plural='%d apples', + number=0)), + '0 apples') + self.assertEqual( + translate(factory('One apple', msgid_plural='%d apples', + number=1)), + 'One apple') + self.assertEqual( + translate(factory('One apple', msgid_plural='%d apples', + number=2)), + '2 apples') + + def test_translate_message_with_defaults(self): + domain = self._getTranslationDomain('en') + factory = MessageFactory('default') + zope.component.provideUtility(domain, ITranslationDomain, 'default') + self.assertEqual( + translate(factory('One apple', msgid_plural='%d apples', number=0, + default='One fruit', + default_plural='%d fruits')), + '0 fruits') + self.assertEqual( + translate(factory('One apple', msgid_plural='%d apples', number=1, + default='One fruit', + default_plural='%d fruits')), + 'One fruit') + self.assertEqual( + translate(factory('One apple', msgid_plural='%d apples', number=2, + default='One fruit', + default_plural='%d fruits')), + '2 fruits') + + def test_translate_recursive(self): domain = self._getTranslationDomain('en') factory = MessageFactory('default') - translate = domain.translate # Singular banana = factory('banana', msgid_plural='bananas', number=1) @@ -219,7 +255,7 @@ def test_recursive_translation(self): msgid_plural='There are %d ${type}.', number=1, mapping={'type': banana}) self.assertEqual( - translate(phrase, target_language="en"), + domain.translate(phrase, target_language="en"), 'There is 1 banana.') # Plural @@ -228,16 +264,16 @@ def test_recursive_translation(self): msgid_plural='There are %d ${type}.', number=10, mapping={'type': apple}) self.assertEqual( - translate(phrase, target_language="en"), + domain.translate(phrase, target_language="en"), 'There are 10 apples.') # Straight translation with translatable mapping apple = factory('apple', msgid_plural='apples', number=75) self.assertEqual( - translate(msgid='There is %d ${type}.', - msgid_plural='There are %d ${type}.', - mapping={'type': apple}, - target_language="en", number=75), + domain.translate(msgid='There is %d ${type}.', + msgid_plural='There are %d ${type}.', + mapping={'type': apple}, + target_language="en", number=75), 'There are 75 apples.') # Add another catalog, to test the domain's catalogs iteration @@ -249,8 +285,8 @@ def test_recursive_translation(self): apple = factory('apple', msgid_plural='apples', number=42) self.assertEqual( - translate(msgid='There is %d ${type}.', - msgid_plural='There are %d ${type}.', - mapping={'type': apple}, - target_language="de", number=42), + domain.translate(msgid='There is %d ${type}.', + msgid_plural='There are %d ${type}.', + mapping={'type': apple}, + target_language="de", number=42), 'There are 42 oranges.') diff --git a/src/zope/i18n/translationdomain.py b/src/zope/i18n/translationdomain.py index c7f064e..b9287f0 100644 --- a/src/zope/i18n/translationdomain.py +++ b/src/zope/i18n/translationdomain.py @@ -102,7 +102,7 @@ def _recursive_translate(self, msgid, mapping, target_language, default, # Recursively translate mappings, if they are translatable if (mapping is not None - and Message in (type(m) for m in mapping.values())): + and Message in (type(m) for m in mapping.values())): if seen is None: seen = set() seen.add((msgid, msgid_plural)) @@ -121,6 +121,8 @@ def _recursive_translate(self, msgid, mapping, target_language, default, if default is None: default = text_type(msgid) + if msgid_plural is not None and default_plural is None: + default_plural = text_type(msgid_plural) # Get the translation. Use the specified fallbacks if this fails catalog_names = self._catalogs.get(target_language) From ee3fc563f07af5aa1b96bd449afe859280ad0e6a Mon Sep 17 00:00:00 2001 From: Sylvain Viollon Date: Wed, 24 Oct 2018 08:42:59 +0200 Subject: [PATCH 2/2] Update CHANGES.txt. --- CHANGES.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGES.rst b/CHANGES.rst index 4e0a8d0..2475534 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -5,7 +5,8 @@ 4.6.1 (unreleased) ================== -- Nothing changed yet. +- Fix ``default_plural`` again if a ``zope.i18n.messageid.Message`` is + used with ``translate()``. 4.6.0 (2018-10-22)