diff options
Diffstat (limited to 'src/zope/i18n/tests')
-rw-r--r-- | src/zope/i18n/tests/de-default.mo | bin | 347 -> 483 bytes | |||
-rw-r--r-- | src/zope/i18n/tests/de-default.po | 6 | ||||
-rw-r--r-- | src/zope/i18n/tests/en-alt.mo | bin | 318 -> 361 bytes | |||
-rw-r--r-- | src/zope/i18n/tests/en-alt.po | 5 | ||||
-rw-r--r-- | src/zope/i18n/tests/en-default.mo | bin | 343 -> 932 bytes | |||
-rw-r--r-- | src/zope/i18n/tests/en-default.po | 31 | ||||
-rw-r--r-- | src/zope/i18n/tests/pl-default.mo | bin | 0 -> 570 bytes | |||
-rw-r--r-- | src/zope/i18n/tests/pl-default.po | 22 | ||||
-rw-r--r-- | src/zope/i18n/tests/test_imessagecatalog.py | 4 | ||||
-rw-r--r-- | src/zope/i18n/tests/test_plurals.py | 183 |
10 files changed, 247 insertions, 4 deletions
diff --git a/src/zope/i18n/tests/de-default.mo b/src/zope/i18n/tests/de-default.mo Binary files differindex 392a0ff..f49b8da 100644 --- a/src/zope/i18n/tests/de-default.mo +++ b/src/zope/i18n/tests/de-default.mo diff --git a/src/zope/i18n/tests/de-default.po b/src/zope/i18n/tests/de-default.po index 99a5f8d..7d3b2f6 100644 --- a/src/zope/i18n/tests/de-default.po +++ b/src/zope/i18n/tests/de-default.po @@ -6,9 +6,15 @@ msgstr "" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=ISO-8859-1\n" "Content-Transfer-Encoding: 8bit\n" +"Plural-Forms: nplurals=2; plural=n != 1;\n" msgid "short_greeting" msgstr "Hallo!" msgid "greeting" msgstr "Hallo $name, wie geht es Dir?" + +msgid "There is one file." +msgid_plural "There are %d files." +msgstr[0] "Es gibt eine Datei." +msgstr[1] "Es gibt %d Dateien." diff --git a/src/zope/i18n/tests/en-alt.mo b/src/zope/i18n/tests/en-alt.mo Binary files differindex e08bb59..2e7a16a 100644 --- a/src/zope/i18n/tests/en-alt.mo +++ b/src/zope/i18n/tests/en-alt.mo diff --git a/src/zope/i18n/tests/en-alt.po b/src/zope/i18n/tests/en-alt.po index de55bbc..eb439fb 100644 --- a/src/zope/i18n/tests/en-alt.po +++ b/src/zope/i18n/tests/en-alt.po @@ -12,3 +12,8 @@ msgstr "Hey!" msgid "special" msgstr "Wow" + +msgid "apple" +msgid_plural "apple" +msgstr[0] "orange" +msgstr[1] "oranges" diff --git a/src/zope/i18n/tests/en-default.mo b/src/zope/i18n/tests/en-default.mo Binary files differindex 4164029..0dc0588 100644 --- a/src/zope/i18n/tests/en-default.mo +++ b/src/zope/i18n/tests/en-default.mo diff --git a/src/zope/i18n/tests/en-default.po b/src/zope/i18n/tests/en-default.po index a3d843b..2432498 100644 --- a/src/zope/i18n/tests/en-default.po +++ b/src/zope/i18n/tests/en-default.po @@ -6,9 +6,40 @@ msgstr "" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=ISO-8859-1\n" "Content-Transfer-Encoding: 8bit\n" +"Plural-Forms: nplurals=2; plural=n != 1;\n" msgid "short_greeting" msgstr "Hello!" msgid "greeting" msgstr "Hello $name, how are you?" + +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." + +msgid "There is %d ${type}." +msgid_plural "There are %d ${type}." +msgstr[0] "There is %d ${type}." +msgstr[1] "There are %d ${type}." + +msgid "apple" +msgid_plural "apples" +msgstr[0] "apple" +msgstr[1] "apples" + +msgid "banana" +msgid_plural "bananas" +msgstr[0] "banana" +msgstr[1] "bananas" diff --git a/src/zope/i18n/tests/pl-default.mo b/src/zope/i18n/tests/pl-default.mo Binary files differnew file mode 100644 index 0000000..37b73fe --- /dev/null +++ b/src/zope/i18n/tests/pl-default.mo diff --git a/src/zope/i18n/tests/pl-default.po b/src/zope/i18n/tests/pl-default.po new file mode 100644 index 0000000..3951a9c --- /dev/null +++ b/src/zope/i18n/tests/pl-default.po @@ -0,0 +1,22 @@ +msgid "" +msgstr "" +"Project-Id-Version: Zope 3\n" +"PO-Revision-Date: 2018-09-04 11:05+0100\n" +"Last-Translator: Zope 3 contributors\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=utf-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Plural-Forms: nplurals=3; plural=n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2;\n" + + +msgid "short_greeting" +msgstr "Cześć !" + +msgid "greeting" +msgstr "Cześć $name, jak się masz?" + +msgid "There is one file." +msgid_plural "There are %d files." +msgstr[0] "Istnieje %d plik." +msgstr[1] "Istnieją %d pliki." +msgstr[2] "Istnieją %d plików." diff --git a/src/zope/i18n/tests/test_imessagecatalog.py b/src/zope/i18n/tests/test_imessagecatalog.py index 2856c67..803d4f0 100644 --- a/src/zope/i18n/tests/test_imessagecatalog.py +++ b/src/zope/i18n/tests/test_imessagecatalog.py @@ -43,24 +43,20 @@ class TestIMessageCatalog(unittest.TestCase): self.assertEqual(catalog.getMessage('short_greeting'), 'Hello!') self.assertRaises(KeyError, catalog.getMessage, 'foo') - def testQueryMessage(self): catalog = self._catalog self.assertEqual(catalog.queryMessage('short_greeting'), 'Hello!') self.assertEqual(catalog.queryMessage('foo'), None) self.assertEqual(catalog.queryMessage('foo', 'bar'), 'bar') - def testGetLanguage(self): catalog = self._catalog self.assertEqual(catalog.language, 'en') - def testGetDomain(self): catalog = self._catalog self.assertEqual(catalog.domain, 'default') - def testGetIdentifier(self): catalog = self._catalog self.assertEqual(catalog.getIdentifier(), self._getUniqueIndentifier()) diff --git a/src/zope/i18n/tests/test_plurals.py b/src/zope/i18n/tests/test_plurals.py new file mode 100644 index 0000000..4f7813e --- /dev/null +++ b/src/zope/i18n/tests/test_plurals.py @@ -0,0 +1,183 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# Copyright (c) 2001, 2002 Zope Foundation and Contributors. +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS +# FOR A PARTICULAR PURPOSE. +# +############################################################################## +"""Test a gettext implementation of a Message Catalog. +""" +import os +import unittest +from zope.i18n import tests +from zope.i18n.translationdomain import TranslationDomain +from zope.i18n.gettextmessagecatalog import GettextMessageCatalog +from zope.i18nmessageid import MessageFactory + + +class TestPlurals(unittest.TestCase): + + def _getMessageCatalog(self, locale, variant="default"): + path = os.path.dirname(tests.__file__) + self._path = os.path.join(path, '%s-%s.mo' % (locale, variant)) + catalog = GettextMessageCatalog(locale, variant, self._path) + return catalog + + def _getTranslationDomain(self, locale, variant="default"): + path = os.path.dirname(tests.__file__) + self._path = os.path.join(path, '%s-%s.mo' % (locale, variant)) + catalog = GettextMessageCatalog(locale, variant, self._path) + domain = TranslationDomain('default') + domain.addCatalog(catalog) + return domain + + def test_GermanPlurals(self): + """Germanic languages such as english and german share the plural + rule. We test the german here. + """ + catalog = self._getMessageCatalog('de') + self.assertEqual(catalog.language, 'de') + + self.assertEqual(catalog.getPluralMessage( + 'There is one file.', 'There are %d files.', 1), + 'Es gibt eine Datei.') + self.assertEqual(catalog.getPluralMessage( + 'There is one file.', 'There are %d files.', 3), + 'Es gibt 3 Dateien.') + self.assertEqual(catalog.getPluralMessage( + 'There is one file.', 'There are %d files.', 0), + 'Es gibt 0 Dateien.') + + # Unknown id + self.assertRaises(KeyError, catalog.getPluralMessage, + 'There are %d files.', 'bar', 6) + + # Query without default values + self.assertEqual(catalog.queryPluralMessage( + 'There is one file.', 'There are %d files.', 1), + 'Es gibt eine Datei.') + self.assertEqual(catalog.queryPluralMessage( + 'There is one file.', 'There are %d files.', 3), + 'Es gibt 3 Dateien.') + + # Query with default values + self.assertEqual(catalog.queryPluralMessage( + 'There are %d files.', 'There is one file.', 1, + 'Es gibt 1 Datei.', 'Es gibt %d Dateien !', ), + 'Es gibt 1 Datei.') + self.assertEqual(catalog.queryPluralMessage( + 'There are %d files.', 'There is one file.', 3, + 'Es gibt 1 Datei.', 'Es gibt %d Dateien !', ), + 'Es gibt 3 Dateien !') + + def test_PolishPlurals(self): + """Polish has a complex rule for plurals. It makes for a good + test subject. + """ + catalog = self._getMessageCatalog('pl') + self.assertEqual(catalog.language, 'pl') + + self.assertEqual(catalog.getPluralMessage( + 'There is one file.', 'There are %d files.', 0), + u"Istnieją 0 plików.") + self.assertEqual(catalog.getPluralMessage( + 'There is one file.', 'There are %d files.', 1), + u"Istnieje 1 plik.") + self.assertEqual(catalog.getPluralMessage( + 'There is one file.', 'There are %d files.', 3), + u"Istnieją 3 pliki.") + self.assertEqual(catalog.getPluralMessage( + 'There is one file.', 'There are %d files.', 17), + u"Istnieją 17 plików.") + self.assertEqual(catalog.getPluralMessage( + 'There is one file.', 'There are %d files.', 23), + u"Istnieją 23 pliki.") + self.assertEqual(catalog.getPluralMessage( + 'There is one file.', 'There are %d files.', 28), + u"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.') + + def test_recursive_translation(self): + domain = self._getTranslationDomain('en') + factory = MessageFactory('default') + translate = domain.translate + + # Singular + banana = factory('banana', msgid_plural='bananas', number=1) + phrase = factory('There is %d ${type}.', + msgid_plural='There are %d ${type}.', + number=1, mapping={'type': banana}) + self.assertEqual( + translate(phrase, target_language="en"), + 'There is 1 banana.') + + # Plural + apple = factory('apple', msgid_plural='apples', number=10) + phrase = factory('There is %d ${type}.', + msgid_plural='There are %d ${type}.', + number=10, mapping={'type': apple}) + self.assertEqual( + 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), + 'There are 75 apples.') + + # Add another catalog, to test the domain's catalogs iteration + # We add this catalog in first position, to resolve the translations + # there first. + alt_en = self._getMessageCatalog('en', variant="alt") + domain._data[alt_en.getIdentifier()] = alt_en + domain._catalogs[alt_en.language].insert(0, alt_en.getIdentifier()) + + 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), + 'There are 42 oranges.') |