From 6f3c31ad3bae07996c7df60afd79cab482e463ff Mon Sep 17 00:00:00 2001 From: Souheil CHELFOUH Date: Thu, 18 Oct 2018 11:56:43 +0200 Subject: Corrected interpolation marker comments and added tests for plurals using floats. --- src/zope/i18n/gettextmessagecatalog.py | 4 ++-- src/zope/i18n/tests/en-default.mo | Bin 478 -> 734 bytes src/zope/i18n/tests/en-default.po | 10 ++++++++++ src/zope/i18n/tests/test_plurals.py | 34 +++++++++++++++++++++++++++++++++ 4 files changed, 46 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/zope/i18n/gettextmessagecatalog.py b/src/zope/i18n/gettextmessagecatalog.py index ff9ef71..675c7f2 100644 --- a/src/zope/i18n/gettextmessagecatalog.py +++ b/src/zope/i18n/gettextmessagecatalog.py @@ -32,9 +32,9 @@ class _KeyErrorRaisingFallback(object): def plural_formatting(func): - """This decorator interpolates the `%d` possibly present in the string. + """This decorator interpolates the possible formatting marker. This interpolation marker is usally present for plurals. - Example: `There are %d apples`. + Example: `There are %d apples` or `They have %s pies.' Please note that the interpolation can be done, alternatively, using the mapping. This is only present as a conveniance. diff --git a/src/zope/i18n/tests/en-default.mo b/src/zope/i18n/tests/en-default.mo index 3e9dc9e..1ee7c89 100644 Binary files a/src/zope/i18n/tests/en-default.mo and b/src/zope/i18n/tests/en-default.mo differ diff --git a/src/zope/i18n/tests/en-default.po b/src/zope/i18n/tests/en-default.po index fc45a11..2c75c64 100644 --- a/src/zope/i18n/tests/en-default.po +++ b/src/zope/i18n/tests/en-default.po @@ -18,3 +18,13 @@ msgid "There is one file." msgid_plural "There are %d files." msgstr[0] "There is one file." msgstr[1] "There are %d files." + +msgid "The item is rated 1/5 star." +msgid_plural "The item is rated %s/5 stars." +msgstr[0] "The item is rated 1/5 star." +msgstr[1] "The item is rated %s/5 stars." + +msgid "There is %d chance." +msgid_plural "There are %f chances." +msgstr[0] "There is %d chance." +msgstr[1] "There are %f chances." diff --git a/src/zope/i18n/tests/test_plurals.py b/src/zope/i18n/tests/test_plurals.py index 2020f85..50e192d 100644 --- a/src/zope/i18n/tests/test_plurals.py +++ b/src/zope/i18n/tests/test_plurals.py @@ -98,3 +98,37 @@ class TestPlurals(unittest.TestCase): self.assertEqual(catalog.getPluralMessage( 'There is one file.', 'There are %d files.', 28), "Istnieją 28 plików.") + + def test_floater(self): + """Test with the number being a float. + We can use %f or %s to make sure it works. + """ + catalog = self._getMessageCatalog('en') + self.assertEqual(catalog.language, 'en') + + # It's cast to integer because of the %d in the translation string. + self.assertEqual(catalog.getPluralMessage( + 'There is one file.', 'There are %d files.', 1.0), + 'There is one file.') + + self.assertEqual(catalog.getPluralMessage( + 'There is one file.', 'There are %d files.', 3.5), + 'There are 3 files.') + + # It's cast to a string because of the %s in the translation string. + self.assertEqual(catalog.getPluralMessage( + 'The item is rated 1/5 star.', + 'The item is rated %s/5 stars.', 3.5), + 'The item is rated 3.5/5 stars.') + + # It's cast either to an int or a float because of the %s in + # the translation string. + self.assertEqual(catalog.getPluralMessage( + 'There is %d chance.', + 'There are %f chances.', 1.5), + 'There are 1.500000 chances.') + + self.assertEqual(catalog.getPluralMessage( + 'There is %d chance.', + 'There are %f chances.', 3.5), + 'There are 3.500000 chances.') -- cgit v1.2.1