diff options
Diffstat (limited to 'src/zope')
-rw-r--r-- | src/zope/i18n/__init__.py | 2 | ||||
-rw-r--r-- | src/zope/i18n/tests/test_plurals.py | 56 | ||||
-rw-r--r-- | src/zope/i18n/tests/test_translationdomain.py | 16 |
3 files changed, 66 insertions, 8 deletions
diff --git a/src/zope/i18n/__init__.py b/src/zope/i18n/__init__.py index 5f7d775..dd525ea 100644 --- a/src/zope/i18n/__init__.py +++ b/src/zope/i18n/__init__.py @@ -169,6 +169,8 @@ def translate(msgid, domain=None, mapping=None, context=None, 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) if domain: util = queryUtility(ITranslationDomain, domain) diff --git a/src/zope/i18n/tests/test_plurals.py b/src/zope/i18n/tests/test_plurals.py index 4f7813e..12b46cc 100644 --- a/src/zope/i18n/tests/test_plurals.py +++ b/src/zope/i18n/tests/test_plurals.py @@ -16,10 +16,13 @@ """ import os import unittest -from zope.i18n import tests + +import zope.component +from zope.i18n import tests, translate from zope.i18n.translationdomain import TranslationDomain from zope.i18n.gettextmessagecatalog import GettextMessageCatalog from zope.i18nmessageid import MessageFactory +from zope.i18n.interfaces import ITranslationDomain class TestPlurals(unittest.TestCase): @@ -38,6 +41,57 @@ class TestPlurals(unittest.TestCase): domain.addCatalog(catalog) return domain + def test_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_missing_queryPluralMessage(self): + catalog = self._getMessageCatalog('en') + self.assertEqual(catalog.language, 'en') + + self.assertEqual( + catalog.queryPluralMessage( + 'One apple', '%d apples', 0, + dft1='One fruit', dft2='%d fruits'), + '0 fruits') + + self.assertEqual( + catalog.queryPluralMessage( + 'One apple.', '%d apples.', 1, + dft1='One fruit', dft2='%d fruits'), + 'One fruit') + + self.assertEqual( + catalog.queryPluralMessage( + 'One apple.', '%d apples.', 2, + dft1='One fruit', dft2='%d fruits'), + '2 fruits') + + def test_missing_getPluralMessage(self): + catalog = self._getMessageCatalog('en') + self.assertEqual(catalog.language, 'en') + + with self.assertRaises(KeyError): + catalog.getPluralMessage('One apple', '%d fruits', 0) + + with self.assertRaises(KeyError): + catalog.getPluralMessage('One apple', '%d fruits', 1) + + with self.assertRaises(KeyError): + catalog.getPluralMessage('One apple', '%d fruits', 2) + def test_GermanPlurals(self): """Germanic languages such as english and german share the plural rule. We test the german here. diff --git a/src/zope/i18n/tests/test_translationdomain.py b/src/zope/i18n/tests/test_translationdomain.py index f206d5b..34ee21a 100644 --- a/src/zope/i18n/tests/test_translationdomain.py +++ b/src/zope/i18n/tests/test_translationdomain.py @@ -115,13 +115,15 @@ class TestGlobalTranslationDomain(TestITranslationDomain, unittest.TestCase): translate, msgid1, None, None, 'en', "default") # Recusrive translations also work if the original message id wasn't a # message id but a unicode with a directly passed mapping - self.assertEqual("Color: BLUE/YELLOW", - translate(u"Color: ${color1}/${color2}", mapping=mapping, - target_language='en')) - - # If we have mapping with a message id from a different domain, make sure - # we use that domain, not ours. If the message domain is not registered yet, - # we should return a defualt translation. + self.assertEqual( + "Color: BLUE/YELLOW", + translate(u"Color: ${color1}/${color2}", mapping=mapping, + target_language='en')) + + # If we have mapping with a message id from a different + # domain, make sure we use that domain, not ours. If the + # message domain is not registered yet, we should return a + # defualt translation. alt_factory = MessageFactory('alt') msgid_sub = alt_factory(u"special", default=u"oohhh") mapping = {'message': msgid_sub} |