summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSylvain Viollon <thefunny@gmail.com>2018-10-24 12:11:46 +0200
committerGitHub <noreply@github.com>2018-10-24 12:11:46 +0200
commitbb0642b6401806af0c6dc3b225c09179e0a35903 (patch)
treefd8a54735c324b417f1ab8b131e44363d40af6c4
parent1ac9750446d179284a45d13c34996c9224a3782e (diff)
parentee3fc563f07af5aa1b96bd449afe859280ad0e6a (diff)
downloadzope-i18n-bb0642b6401806af0c6dc3b225c09179e0a35903.tar.gz
Merge pull request #36 from zopefoundation/fix-default-plural
Fix default plural (again)
-rw-r--r--CHANGES.rst3
-rw-r--r--src/zope/i18n/tests/test_plurals.py130
-rw-r--r--src/zope/i18n/translationdomain.py4
3 files changed, 88 insertions, 49 deletions
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)
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 @@ class TestPlurals(unittest.TestCase):
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 @@ class TestPlurals(unittest.TestCase):
'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 @@ class TestPlurals(unittest.TestCase):
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 @@ class TestPlurals(unittest.TestCase):
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 @@ class TestPlurals(unittest.TestCase):
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 @@ class TranslationDomain(object):
# 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 @@ class TranslationDomain(object):
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)