diff options
Diffstat (limited to 'src/zope/i18n')
-rw-r--r-- | src/zope/i18n/__init__.py | 2 | ||||
-rw-r--r-- | src/zope/i18n/tests/test_plurals.py | 75 | ||||
-rw-r--r-- | src/zope/i18n/tests/test_translationdomain.py | 22 |
3 files changed, 88 insertions, 11 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..28d6b3a 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,76 @@ 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') + + 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..f8efbd9 100644 --- a/src/zope/i18n/tests/test_translationdomain.py +++ b/src/zope/i18n/tests/test_translationdomain.py @@ -113,21 +113,23 @@ class TestGlobalTranslationDomain(TestITranslationDomain, unittest.TestCase): msgid2.mapping['msg1'] = msgid1 self.assertRaises(ValueError, 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. + # Recursive 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 + # default translation. alt_factory = MessageFactory('alt') msgid_sub = alt_factory(u"special", default=u"oohhh") mapping = {'message': msgid_sub} msgid = factory(u"46-not-there", 'Message: ${message}', mapping=mapping) - # test we get a default with no domain registerd + # test we get a default with no domain registered self.assertEqual( translate(msgid, target_language='en', default="default"), "Message: oohhh") |