Bowe Strickland
2018-10-27 323fa95deea50f49c119728fc2eeacb9e0c51241
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
# -*- coding: utf-8 -*-
import os
import unittest
from pyramid import testing
 
here = os.path.dirname(__file__)
localedir = os.path.join(here, 'pkgs', 'localeapp', 'locale')
 
 
class TestTranslationString(unittest.TestCase):
    def _makeOne(self, *arg, **kw):
        from pyramid.i18n import TranslationString
 
        return TranslationString(*arg, **kw)
 
    def test_it(self):
        # this is part of the API, we don't actually need to test much more
        # than that it's importable
        ts = self._makeOne('a')
        self.assertEqual(ts, 'a')
 
 
class TestTranslationStringFactory(unittest.TestCase):
    def _makeOne(self, *arg, **kw):
        from pyramid.i18n import TranslationStringFactory
 
        return TranslationStringFactory(*arg, **kw)
 
    def test_it(self):
        # this is part of the API, we don't actually need to test much more
        # than that it's importable
        factory = self._makeOne('a')
        self.assertEqual(factory('').domain, 'a')
 
 
class TestLocalizer(unittest.TestCase):
    def _makeOne(self, *arg, **kw):
        from pyramid.i18n import Localizer
 
        return Localizer(*arg, **kw)
 
    def test_ctor(self):
        localizer = self._makeOne('en_US', None)
        self.assertEqual(localizer.locale_name, 'en_US')
        self.assertEqual(localizer.translations, None)
 
    def test_translate(self):
        translations = DummyTranslations()
        localizer = self._makeOne(None, translations)
        self.assertEqual(
            localizer.translate('123', domain='1', mapping={}), '123'
        )
        self.assertTrue(localizer.translator)
 
    def test_pluralize(self):
        translations = DummyTranslations()
        localizer = self._makeOne(None, translations)
        result = localizer.pluralize(
            'singular', 'plural', 1, domain='1', mapping={}
        )
        self.assertEqual(result, 'singular')
        self.assertTrue(localizer.pluralizer)
 
    def test_pluralize_pluralizer_already_added(self):
        translations = DummyTranslations()
        localizer = self._makeOne(None, translations)
 
        def pluralizer(*arg, **kw):
            return arg, kw
 
        localizer.pluralizer = pluralizer
        result = localizer.pluralize(
            'singular', 'plural', 1, domain='1', mapping={}
        )
        self.assertEqual(
            result, (('singular', 'plural', 1), {'domain': '1', 'mapping': {}})
        )
        self.assertTrue(localizer.pluralizer is pluralizer)
 
    def test_pluralize_default_translations(self):
        # test that even without message ids loaded that
        # "localizer.pluralize" "works" instead of raising an inscrutable
        # "translations object has no attr 'plural' error; see
        # see https://github.com/Pylons/pyramid/issues/235
        from pyramid.i18n import Translations
 
        translations = Translations()
        translations._catalog = {}
        localizer = self._makeOne(None, translations)
        result = localizer.pluralize(
            'singular', 'plural', 2, domain='1', mapping={}
        )
        self.assertEqual(result, 'plural')
 
 
class Test_negotiate_locale_name(unittest.TestCase):
    def setUp(self):
        testing.setUp()
 
    def tearDown(self):
        testing.tearDown()
 
    def _callFUT(self, request):
        from pyramid.i18n import negotiate_locale_name
 
        return negotiate_locale_name(request)
 
    def _registerImpl(self, impl):
        from pyramid.threadlocal import get_current_registry
 
        registry = get_current_registry()
        from pyramid.interfaces import ILocaleNegotiator
 
        registry.registerUtility(impl, ILocaleNegotiator)
 
    def test_no_registry_on_request(self):
        self._registerImpl(dummy_negotiator)
        request = DummyRequest()
        result = self._callFUT(request)
        self.assertEqual(result, 'bogus')
 
    def test_with_registry_on_request(self):
        from pyramid.threadlocal import get_current_registry
 
        registry = get_current_registry()
        self._registerImpl(dummy_negotiator)
        request = DummyRequest()
        request.registry = registry
        result = self._callFUT(request)
        self.assertEqual(result, 'bogus')
 
    def test_default_from_settings(self):
        from pyramid.threadlocal import get_current_registry
 
        registry = get_current_registry()
        settings = {'default_locale_name': 'settings'}
        registry.settings = settings
        request = DummyRequest()
        request.registry = registry
        result = self._callFUT(request)
        self.assertEqual(result, 'settings')
 
    def test_use_default_locale_negotiator(self):
        from pyramid.threadlocal import get_current_registry
 
        registry = get_current_registry()
        request = DummyRequest()
        request.registry = registry
        request._LOCALE_ = 'locale'
        result = self._callFUT(request)
        self.assertEqual(result, 'locale')
 
    def test_default_default(self):
        request = DummyRequest()
        result = self._callFUT(request)
        self.assertEqual(result, 'en')
 
 
class Test_get_locale_name(unittest.TestCase):
    def setUp(self):
        testing.setUp()
 
    def tearDown(self):
        testing.tearDown()
 
    def _callFUT(self, request):
        from pyramid.i18n import get_locale_name
 
        return get_locale_name(request)
 
    def test_name_on_request(self):
        request = DummyRequest()
        request.locale_name = 'ie'
        result = self._callFUT(request)
        self.assertEqual(result, 'ie')
 
 
class Test_make_localizer(unittest.TestCase):
    def setUp(self):
        testing.setUp()
 
    def tearDown(self):
        testing.tearDown()
 
    def _callFUT(self, locale, tdirs):
        from pyramid.i18n import make_localizer
 
        return make_localizer(locale, tdirs)
 
    def test_locale_from_mo(self):
        from pyramid.i18n import Localizer
 
        localedirs = [localedir]
        locale_name = 'de'
        result = self._callFUT(locale_name, localedirs)
        self.assertEqual(result.__class__, Localizer)
        self.assertEqual(
            result.translate('Approve', 'deformsite'), 'Genehmigen'
        )
        self.assertEqual(result.translate('Approve'), 'Approve')
        self.assertTrue(hasattr(result, 'pluralize'))
 
    def test_locale_from_mo_bad_mo(self):
        from pyramid.i18n import Localizer
 
        localedirs = [localedir]
        locale_name = 'be'
        result = self._callFUT(locale_name, localedirs)
        self.assertEqual(result.__class__, Localizer)
        self.assertEqual(result.translate('Approve', 'deformsite'), 'Approve')
 
    def test_locale_from_mo_mo_isdir(self):
        from pyramid.i18n import Localizer
 
        localedirs = [localedir]
        locale_name = 'gb'
        result = self._callFUT(locale_name, localedirs)
        self.assertEqual(result.__class__, Localizer)
        self.assertEqual(result.translate('Approve', 'deformsite'), 'Approve')
 
    def test_territory_fallback(self):
        from pyramid.i18n import Localizer
 
        localedirs = [localedir]
        locale_name = 'de_DE'
        result = self._callFUT(locale_name, localedirs)
        self.assertEqual(result.__class__, Localizer)
        self.assertEqual(
            result.translate('Submit', 'deformsite'), 'different'
        )  # prefer translations from de_DE locale
        self.assertEqual(
            result.translate('Approve', 'deformsite'), 'Genehmigen'
        )  # missing from de_DE locale, but in de
 
 
class Test_get_localizer(unittest.TestCase):
    def setUp(self):
        testing.setUp()
 
    def tearDown(self):
        testing.tearDown()
 
    def _callFUT(self, request):
        from pyramid.i18n import get_localizer
 
        return get_localizer(request)
 
    def test_it(self):
        request = DummyRequest()
        request.localizer = 'localizer'
        self.assertEqual(self._callFUT(request), 'localizer')
 
 
class Test_default_locale_negotiator(unittest.TestCase):
    def setUp(self):
        testing.setUp()
 
    def tearDown(self):
        testing.tearDown()
 
    def _callFUT(self, request):
        from pyramid.i18n import default_locale_negotiator
 
        return default_locale_negotiator(request)
 
    def test_from_none(self):
        request = DummyRequest()
        result = self._callFUT(request)
        self.assertEqual(result, None)
 
    def test_from_request_attr(self):
        request = DummyRequest()
        request._LOCALE_ = 'foo'
        result = self._callFUT(request)
        self.assertEqual(result, 'foo')
 
    def test_from_params(self):
        request = DummyRequest()
        request.params['_LOCALE_'] = 'foo'
        result = self._callFUT(request)
        self.assertEqual(result, 'foo')
 
    def test_from_cookies(self):
        request = DummyRequest()
        request.cookies['_LOCALE_'] = 'foo'
        result = self._callFUT(request)
        self.assertEqual(result, 'foo')
 
 
class TestTranslations(unittest.TestCase):
    def _getTargetClass(self):
        from pyramid.i18n import Translations
 
        return Translations
 
    def _makeOne(self):
        messages1 = [('foo', 'Voh'), (('foo1', 1), 'Voh1')]
        messages2 = [('foo', 'VohD'), (('foo1', 1), 'VohD1')]
 
        klass = self._getTargetClass()
 
        translations1 = klass(None, domain='messages')
        translations1._catalog = dict(messages1)
        translations1.plural = lambda *arg: 1
        translations2 = klass(None, domain='messages1')
        translations2._catalog = dict(messages2)
        translations2.plural = lambda *arg: 1
        translations = translations1.add(translations2, merge=False)
        return translations
 
    def test_load_locales_None(self):
        import gettext
 
        klass = self._getTargetClass()
        result = klass.load(localedir, None, domain=None)
        self.assertEqual(result.__class__, gettext.NullTranslations)
 
    def test_load_domain_None(self):
        import gettext
 
        locales = ['de', 'en']
        klass = self._getTargetClass()
        result = klass.load(localedir, locales, domain=None)
        self.assertEqual(result.__class__, gettext.NullTranslations)
 
    def test_load_found_locale_and_domain(self):
        locales = ['de', 'en']
        klass = self._getTargetClass()
        result = klass.load(localedir, locales, domain='deformsite')
        self.assertEqual(result.__class__, klass)
 
    def test_load_found_locale_and_domain_locale_is_string(self):
        locales = 'de'
        klass = self._getTargetClass()
        result = klass.load(localedir, locales, domain='deformsite')
        self.assertEqual(result.__class__, klass)
 
    def test___repr__(self):
        inst = self._makeOne()
        result = repr(inst)
        self.assertEqual(result, '<Translations: "None">')
 
    def test_merge_not_gnutranslations(self):
        inst = self._makeOne()
        self.assertEqual(inst.merge(None), inst)
 
    def test_merge_gnutranslations(self):
        inst = self._makeOne()
        inst2 = self._makeOne()
        inst2._catalog['a'] = 'b'
        inst.merge(inst2)
        self.assertEqual(inst._catalog['a'], 'b')
 
    def test_merge_gnutranslations_not_translations(self):
        import gettext
 
        t = gettext.GNUTranslations()
        t._catalog = {'a': 'b'}
        inst = self._makeOne()
        inst.merge(t)
        self.assertEqual(inst._catalog['a'], 'b')
 
    def test_add_different_domain_merge_true_notexisting(self):
        inst = self._makeOne()
        inst2 = self._makeOne()
        inst2.domain = 'domain2'
        inst.add(inst2)
        self.assertEqual(inst._domains['domain2'], inst2)
 
    def test_add_different_domain_merge_true_existing(self):
        inst = self._makeOne()
        inst2 = self._makeOne()
        inst3 = self._makeOne()
        inst2.domain = 'domain2'
        inst2._catalog['a'] = 'b'
        inst3.domain = 'domain2'
        inst._domains['domain2'] = inst3
        inst.add(inst2)
        self.assertEqual(inst._domains['domain2'], inst3)
        self.assertEqual(inst3._catalog['a'], 'b')
 
    def test_add_same_domain_merge_true(self):
        inst = self._makeOne()
        inst2 = self._makeOne()
        inst2._catalog['a'] = 'b'
        inst.add(inst2)
        self.assertEqual(inst._catalog['a'], 'b')
 
    def test_add_default_domain_replaces_plural_first_time(self):
        # Create three empty message catalogs in the default domain
        inst = self._getTargetClass()(None, domain='messages')
        inst2 = self._getTargetClass()(None, domain='messages')
        inst3 = self._getTargetClass()(None, domain='messages')
        inst._catalog = {}
        inst2._catalog = {}
        inst3._catalog = {}
 
        # The default plural scheme is the germanic one
        self.assertEqual(inst.plural(0), 1)
        self.assertEqual(inst.plural(1), 0)
        self.assertEqual(inst.plural(2), 1)
 
        # inst2 represents a message file that declares french plurals
        inst2.plural = lambda n: n > 1
        inst.add(inst2)
        # that plural rule should now apply to inst
        self.assertEqual(inst.plural(0), 0)
        self.assertEqual(inst.plural(1), 0)
        self.assertEqual(inst.plural(2), 1)
 
        # We load a second message file with different plural rules
        inst3.plural = lambda n: n > 0
        inst.add(inst3)
        # It doesn't override the previously loaded rule
        self.assertEqual(inst.plural(0), 0)
        self.assertEqual(inst.plural(1), 0)
        self.assertEqual(inst.plural(2), 1)
 
    def test_dgettext(self):
        t = self._makeOne()
        self.assertEqual(t.dgettext('messages', 'foo'), 'Voh')
        self.assertEqual(t.dgettext('messages1', 'foo'), 'VohD')
 
    def test_ldgettext(self):
        t = self._makeOne()
        self.assertEqual(t.ldgettext('messages', 'foo'), b'Voh')
        self.assertEqual(t.ldgettext('messages1', 'foo'), b'VohD')
 
    def test_dugettext(self):
        t = self._makeOne()
        self.assertEqual(t.dugettext('messages', 'foo'), 'Voh')
        self.assertEqual(t.dugettext('messages1', 'foo'), 'VohD')
 
    def test_dngettext(self):
        t = self._makeOne()
        self.assertEqual(t.dngettext('messages', 'foo1', 'foos1', 1), 'Voh1')
        self.assertEqual(t.dngettext('messages1', 'foo1', 'foos1', 1), 'VohD1')
 
    def test_ldngettext(self):
        t = self._makeOne()
        self.assertEqual(t.ldngettext('messages', 'foo1', 'foos1', 1), b'Voh1')
        self.assertEqual(
            t.ldngettext('messages1', 'foo1', 'foos1', 1), b'VohD1'
        )
 
    def test_dungettext(self):
        t = self._makeOne()
        self.assertEqual(t.dungettext('messages', 'foo1', 'foos1', 1), 'Voh1')
        self.assertEqual(
            t.dungettext('messages1', 'foo1', 'foos1', 1), 'VohD1'
        )
 
    def test_default_germanic_pluralization(self):
        t = self._getTargetClass()()
        t._catalog = {}
        result = t.dungettext('messages', 'foo1', 'foos1', 2)
        self.assertEqual(result, 'foos1')
 
 
class TestLocalizerRequestMixin(unittest.TestCase):
    def setUp(self):
        self.config = testing.setUp()
 
    def tearDown(self):
        testing.tearDown()
 
    def _makeOne(self):
        from pyramid.i18n import LocalizerRequestMixin
 
        request = LocalizerRequestMixin()
        request.registry = self.config.registry
        request.cookies = {}
        request.params = {}
        return request
 
    def test_default_localizer(self):
        # `localizer` returns a default localizer for `en`
        from pyramid.i18n import Localizer
 
        request = self._makeOne()
        self.assertEqual(request.localizer.__class__, Localizer)
        self.assertEqual(request.locale_name, 'en')
 
    def test_custom_localizer_for_default_locale(self):
        from pyramid.interfaces import ILocalizer
 
        dummy = object()
        self.config.registry.registerUtility(dummy, ILocalizer, name='en')
        request = self._makeOne()
        self.assertEqual(request.localizer, dummy)
 
    def test_custom_localizer_for_custom_locale(self):
        from pyramid.interfaces import ILocalizer
 
        dummy = object()
        self.config.registry.registerUtility(dummy, ILocalizer, name='ie')
        request = self._makeOne()
        request._LOCALE_ = 'ie'
        self.assertEqual(request.localizer, dummy)
 
    def test_localizer_from_mo(self):
        from pyramid.interfaces import ITranslationDirectories
        from pyramid.i18n import Localizer
 
        localedirs = [localedir]
        self.config.registry.registerUtility(
            localedirs, ITranslationDirectories
        )
        request = self._makeOne()
        request._LOCALE_ = 'de'
        result = request.localizer
        self.assertEqual(result.__class__, Localizer)
        self.assertEqual(
            result.translate('Approve', 'deformsite'), 'Genehmigen'
        )
        self.assertEqual(result.translate('Approve'), 'Approve')
        self.assertTrue(hasattr(result, 'pluralize'))
 
    def test_localizer_from_mo_bad_mo(self):
        from pyramid.interfaces import ITranslationDirectories
        from pyramid.i18n import Localizer
 
        localedirs = [localedir]
        self.config.registry.registerUtility(
            localedirs, ITranslationDirectories
        )
        request = self._makeOne()
        request._LOCALE_ = 'be'
        result = request.localizer
        self.assertEqual(result.__class__, Localizer)
        self.assertEqual(result.translate('Approve', 'deformsite'), 'Approve')
 
 
class DummyRequest(object):
    def __init__(self):
        self.params = {}
        self.cookies = {}
 
 
def dummy_negotiator(request):
    return 'bogus'
 
 
class DummyTranslations(object):
    def ugettext(self, text):
        return text
 
    gettext = ugettext
 
    def ungettext(self, singular, plural, n):
        return singular
 
    ngettext = ungettext