summaryrefslogtreecommitdiff
path: root/src/zope/i18n/tests/test_plurals.py
blob: bc97bf3f88fd3c03598b869c0273d723438450d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# -*- 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

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):

    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_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.
        """
        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_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')

        # 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(
            domain.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(
            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(
            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
        # 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(
            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.')