Skip to content

Commit

Permalink
Merge pull request #36 from zopefoundation/fix-default-plural
Browse files Browse the repository at this point in the history
Fix default plural (again)
  • Loading branch information
thefunny42 committed Oct 24, 2018
2 parents 1ac9750 + ee3fc56 commit bb0642b
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 49 deletions.
3 changes: 2 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
130 changes: 83 additions & 47 deletions src/zope/i18n/tests/test_plurals.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -208,18 +173,89 @@ 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)
phrase = factory('There is %d ${type}.',
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
Expand All @@ -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
Expand All @@ -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.')
4 changes: 3 additions & 1 deletion src/zope/i18n/translationdomain.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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)
Expand Down

0 comments on commit bb0642b

Please sign in to comment.