summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSylvain Viollon <sviollon@minddistrict.com>2018-10-24 08:41:32 +0200
committerSylvain Viollon <sviollon@minddistrict.com>2018-10-24 08:41:32 +0200
commitbbd6adf9048fc7231eff076228595d4bdea554c6 (patch)
treec5c205983061551439a2f7e2fa048c22bad6927c
parent555e4063e669e6b9ae26cb3621220618bd3debba (diff)
downloadzope-i18n-bbd6adf9048fc7231eff076228595d4bdea554c6.tar.gz
Fix defaults again.
-rw-r--r--src/zope/i18n/tests/test_plurals.py130
-rw-r--r--src/zope/i18n/translationdomain.py4
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 @@ 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)