Esempio n. 1
0
    def testSetGetLocale(self):
        translator = Translator('en')

        self.assertEquals('en', translator.locale)

        translator.locale = 'fr'
        self.assertEquals('fr', translator.locale)
    def testSetGetLocale(self):
        translator = Translator('en')

        self.assertEquals('en', translator.locale)

        translator.locale = 'fr'
        self.assertEquals('fr', translator.locale)
 def testadd_resourceInvalidLocales(self):
     for locale, in self.getInvalidLocalesTests():
         translator = Translator('fr')
         self.assertRaises(
             ValueError, lambda: translator.add_resource(
                 'dict', {
                     'foo': 'foofoo'}, locale))
Esempio n. 4
0
    def testget_catalogue(self):
        translator = Translator('en')

        self.assertEquals(MessageCatalogue('en'), translator.get_catalogue())

        translator.locale = 'fr'
        self.assertEquals(MessageCatalogue('fr'),
                          translator.get_catalogue('fr'))
    def testTransWithFallbackLocale(self):
        translator = Translator('fr_FR')
        translator.add_loader('dict', loaders.DictLoader())
        translator.add_resource('dict', {'foo': 'foofoo'}, 'en_US')
        translator.add_resource('dict', {'bar': 'foobar'}, 'en')

        translator.set_fallback_locales(['en'])

        self.assertEquals('foobar', translator.trans('bar'))
    def testget_catalogue(self):
        translator = Translator('en')

        self.assertEquals(MessageCatalogue('en'), translator.get_catalogue())

        translator.locale = 'fr'
        self.assertEquals(
            MessageCatalogue('fr'),
            translator.get_catalogue('fr'))
    def testset_fallback_localesMultiple(self):
        translator = Translator('en')
        translator.add_loader('dict', loaders.DictLoader())
        translator.add_resource('dict', {'foo': 'foo (en)'}, 'en')
        translator.add_resource('dict', {'bar': 'bar (fr)'}, 'fr')

        # force catalogue loading
        translator.trans('bar')

        translator.set_fallback_locales(['fr_FR', 'fr'])
        self.assertEquals('bar (fr)', translator.trans('bar'))
    def testTransWithFallbackLocaleFile(self):
        for format, loader in self.getTransFileTests():
            loader_class = getattr(loaders, loader)
            translator = Translator('en_GB')
            translator.add_loader(format, loader_class())
            translator.add_resource(
                format,
                __DIR__ +
                '/fixtures/non-existing',
                'en_GB')
            translator.add_resource(
                format,
                __DIR__ +
                '/fixtures/resources.' +
                format,
                'en',
                'resources')

            self.assertRaises(
                loaders.NotFoundResourceException,
                self.assertEquals(
                    'bar',
                    translator.trans(
                        'foo',
                        {},
                        'resources')))
Esempio n. 9
0
    def testtranschoiceValidLocale(self):
        for locale, in self.getValidLocalesTests():
            translator = Translator('en')
            translator.add_loader('dict', loaders.DictLoader())
            translator.add_resource('dict', {'foo': 'foofoo'}, 'en')

            translator.transchoice('foo', 1, {}, '', locale)
Esempio n. 10
0
    def testtranschoiceFallbackBis(self):
        translator = Translator('ru')
        translator.set_fallback_locales(['en_US', 'en'])
        translator.add_loader('dict', loaders.DictLoader())
        translator.add_resource('dict',
                                {'some_message2': 'one thing|{count} things'},
                                'en_US')

        self.assertEquals(
            '10 things',
            translator.transchoice('some_message2', 10, {'count': 10}))
Esempio n. 11
0
    def testSetInvalidLocale(self):
        for locale, in self.getInvalidLocalesTests():
            translator = Translator('fr')

            def set():
                translator.locale = locale

            self.assertRaises(ValueError, set)
Esempio n. 12
0
    def testFlattenedTrans(self):
        for expected, messages, id in self.getFlattenedTransTests():
            translator = Translator('en')
            translator.add_loader('dict', loaders.DictLoader())
            translator.add_resource('dict', messages, 'fr', '')

            self.assertEquals(expected, translator.trans(id, {}, '', 'fr'))
Esempio n. 13
0
    def testtranschoiceValidLocale(self):
        for locale, in self.getValidLocalesTests():
            translator = Translator('en')
            translator.add_loader('dict', loaders.DictLoader())
            translator.add_resource('dict', {'foo': 'foofoo'}, 'en')

            translator.transchoice('foo', 1, {}, '', locale)
Esempio n. 14
0
    def testTransWithoutFallbackLocaleFile(self):
        for format, loader in self.getTransFileTests():
            loader_class = getattr(loaders, loader)
            translator = Translator('en')
            translator.add_loader(format, loader_class())
            translator.add_resource(format, __DIR__ + '/fixtures/non-existing',
                                    'en')
            translator.add_resource(format,
                                    __DIR__ + '/fixtures/resources.' + format,
                                    'en')

            # force catalogue loading
            self.assertRaises(loaders.NotFoundResourceException,
                              lambda: translator.trans('foo'))
Esempio n. 15
0
    def testTransWithFallbackLocaleFile(self):
        for format, loader in self.getTransFileTests():
            loader_class = getattr(loaders, loader)
            translator = Translator('en_GB')
            translator.add_loader(format, loader_class())
            translator.add_resource(format, __DIR__ + '/fixtures/non-existing',
                                    'en_GB')
            translator.add_resource(format,
                                    __DIR__ + '/fixtures/resources.' + format,
                                    'en', 'resources')

            self.assertRaises(
                loaders.NotFoundResourceException,
                self.assertEquals('bar',
                                  translator.trans('foo', {}, 'resources')))
Esempio n. 16
0
    def testTransInvalidLocale(self):
        for locale, in self.getInvalidLocalesTests():
            translator = Translator('en')
            translator.add_loader('dict', loaders.DictLoader())
            translator.add_resource('dict', {'foo': 'foofoo'}, 'en')

            self.assertRaises(ValueError,
                              lambda: translator.trans('foo', {}, '', locale))
Esempio n. 17
0
    def testtranschoiceFallbackBis(self):
        translator = Translator('ru')
        translator.set_fallback_locales(['en_US', 'en'])
        translator.add_loader('dict', loaders.DictLoader())
        translator.add_resource(
            'dict', {
                'some_message2': 'one thing|{count} things'}, 'en_US')

        self.assertEquals(
            '10 things', translator.transchoice(
                'some_message2', 10, {
                    'count': 10}))
Esempio n. 18
0
    def testGetMessages(self):
        for resources, locale, expected in self.dataProviderGetMessages():
            locales = list(resources.keys())
            _locale = locale if locale is not None else locales[0]
            locales = locales[0:locales.index(_locale, )]

            translator = Translator(_locale)
            translator.set_fallback_locales(locales[::-1])
            translator.add_loader('dict', loaders.DictLoader())
            for _locale, domainMessages in list(resources.items()):
                for domain, messages in list(domainMessages.items()):
                    translator.add_resource('dict', messages, _locale, domain)

            result = translator.get_messages(locale)

            self.assertEquals(expected, result)
Esempio n. 19
0
    def testtranschoiceFallbackWithNoTranslation(self):
        translator = Translator('ru')
        translator.set_fallback_locales(['en'])
        translator.add_loader('dict', loaders.DictLoader())

        # consistent behavior with Translator::trans(), which returns the string
        # unchanged if it can't be found
        self.assertEquals(
            'some_message2',
            translator.transchoice('some_message2', 10, {'count': 10}))
Esempio n. 20
0
    def testTrans(self):
        for expected, id, translation, parameters, locale, domain in self.getTransTests(
        ):
            translator = Translator('en')
            translator.add_loader('dict', loaders.DictLoader())
            translator.add_resource('dict', {str(id): translation}, locale,
                                    domain)

            self.assertEquals(expected,
                              translator.trans(id, parameters, domain, locale))
Esempio n. 21
0
    def testFlattenedTrans(self):
        for expected, messages, id in self. getFlattenedTransTests():
            translator = Translator('en')
            translator.add_loader('dict', loaders.DictLoader())
            translator.add_resource('dict', messages, 'fr', '')

            self.assertEquals(expected, translator.trans(id, {}, '', 'fr'))
Esempio n. 22
0
    def testGetMessages(self):
        for resources, locale, expected in self.dataProviderGetMessages():
            locales = list(resources.keys())
            _locale = locale if locale is not None else locales[0]
            locales = locales[0:locales.index(_locale,)]

            translator = Translator(_locale)
            translator.set_fallback_locales(locales[::-1])
            translator.add_loader('dict', loaders.DictLoader())
            for _locale, domainMessages in list(resources.items()):
                for domain, messages in list(domainMessages.items()):
                    translator.add_resource('dict', messages, _locale, domain)

            result = translator.get_messages(locale)

            self.assertEquals(expected, result)
Esempio n. 23
0
    def testtranschoiceFallbackWithNoTranslation(self):
        translator = Translator('ru')
        translator.set_fallback_locales(['en'])
        translator.add_loader('dict', loaders.DictLoader())

        # consistent behavior with Translator::trans(), which returns the string
        # unchanged if it can't be found
        self.assertEquals(
            'some_message2', translator.transchoice(
                'some_message2', 10, {
                    'count': 10}))
Esempio n. 24
0
    def testTransInvalidLocale(self):
        for locale, in self.getInvalidLocalesTests():
            translator = Translator('en')
            translator.add_loader('dict', loaders.DictLoader())
            translator.add_resource('dict', {'foo': 'foofoo'}, 'en')

            self.assertRaises(
                ValueError,
                lambda: translator.trans(
                    'foo',
                    {},
                    '',
                    locale))
Esempio n. 25
0
    def testTransWithoutFallbackLocaleFile(self):
        for format, loader in self.getTransFileTests():
            loader_class = getattr(loaders, loader)
            translator = Translator('en')
            translator.add_loader(format, loader_class())
            translator.add_resource(
                format,
                __DIR__ +
                '/fixtures/non-existing',
                'en')
            translator.add_resource(
                format,
                __DIR__ +
                '/fixtures/resources.' +
                format,
                'en')

            # force catalogue loading
            self.assertRaises(
                loaders.NotFoundResourceException,
                lambda: translator.trans('foo'))
Esempio n. 26
0
    def testTrans(self):
        for expected, id, translation, parameters, locale, domain in self.getTransTests(
        ):
            translator = Translator('en')
            translator.add_loader('dict', loaders.DictLoader())
            translator.add_resource(
                'dict', {
                    str(id): translation}, locale, domain)

            self.assertEquals(
                expected,
                translator.trans(
                    id,
                    parameters,
                    domain,
                    locale))
Esempio n. 27
0
 def testConstructorInvalidLocale(self):
     for locale, in self.getInvalidLocalesTests():
         self.assertRaises(ValueError, lambda: Translator(locale))
Esempio n. 28
0
    def testWhenAResourceHasNoRegisteredLoader(self):
        translator = Translator('en')
        translator.add_resource('dict', {'foo': 'foofoo'}, 'en')

        self.assertRaises(RuntimeError, lambda: translator.trans('foo'))
Esempio n. 29
0
    def testSetValidLocale(self):
        for locale, in self.getValidLocalesTests():
            translator = Translator(locale)
            translator.locale = locale

            self.assertEquals(locale, translator.locale)
Esempio n. 30
0
 def testSetFallbackInvalidLocales(self):
     for locale, in self.getInvalidLocalesTests():
         translator = Translator('fr')
         self.assertRaises(
             ValueError, lambda: translator.set_fallback_locales(['fr', locale]))
Esempio n. 31
0
    def testset_fallback_localesMultiple(self):
        translator = Translator('en')
        translator.add_loader('dict', loaders.DictLoader())
        translator.add_resource('dict', {'foo': 'foo (en)'}, 'en')
        translator.add_resource('dict', {'bar': 'bar (fr)'}, 'fr')

        # force catalogue loading
        translator.trans('bar')

        translator.set_fallback_locales(['fr_FR', 'fr'])
        self.assertEquals('bar (fr)', translator.trans('bar'))
Esempio n. 32
0
    def testadd_resourceAfterTrans(self):
        translator = Translator('fr')
        translator.add_loader('dict', loaders.DictLoader())

        translator.set_fallback_locales(['en'])

        translator.add_resource('dict', {'foo': 'foofoo'}, 'en')
        self.assertEquals('foofoo', translator.trans('foo'))

        translator.add_resource('dict', {'bar': 'foobar'}, 'en')
        self.assertEquals('foobar', translator.trans('bar'))
Esempio n. 33
0
    def testConstructorWithoutLocale(self):
        translator = Translator(None)

        self.assertIsNone(translator.locale)
Esempio n. 34
0
 def testTransNonExistentWithFallback(self):
     translator = Translator('fr')
     translator.set_fallback_locales(['en'])
     translator.add_loader('dict', loaders.DictLoader())
     self.assertEquals('non-existent', translator.trans('non-existent'))
Esempio n. 35
0
    def testadd_resourceAfterTrans(self):
        translator = Translator('fr')
        translator.add_loader('dict', loaders.DictLoader())

        translator.set_fallback_locales(['en'])

        translator.add_resource('dict', {'foo': 'foofoo'}, 'en')
        self.assertEquals('foofoo', translator.trans('foo'))

        translator.add_resource('dict', {'bar': 'foobar'}, 'en')
        self.assertEquals('foobar', translator.trans('bar'))
Esempio n. 36
0
 def testConstructorValidLocale(self):
     for locale, in self.getValidLocalesTests():
         translator = Translator(locale)
         self.assertEquals(locale, translator.locale)
Esempio n. 37
0
 def testTransWithFallbackLocaleBis(self):
     translator = Translator('en_US')
     translator.add_loader('dict', loaders.DictLoader())
     translator.add_resource('dict', {'foo': 'foofoo'}, 'en_US')
     translator.add_resource('dict', {'bar': 'foobar'}, 'en')
     self.assertEquals('foobar', translator.trans('bar'))
Esempio n. 38
0
    def testTransWithFallbackLocaleTer(self):
        translator = Translator('fr_FR')
        translator.add_loader('dict', loaders.DictLoader())
        translator.add_resource('dict', {'foo': 'foo (en_US)'}, 'en_US')
        translator.add_resource('dict', {'bar': 'bar (en)'}, 'en')

        translator.set_fallback_locales(['en_US', 'en'])

        self.assertEquals('foo (en_US)', translator.trans('foo'))
        self.assertEquals('bar (en)', translator.trans('bar'))
Esempio n. 39
0
 def testTransNonExistentWithFallback(self):
     translator = Translator('fr')
     translator.set_fallback_locales(['en'])
     translator.add_loader('dict', loaders.DictLoader())
     self.assertEquals('non-existent', translator.trans('non-existent'))
Esempio n. 40
0
 def testadd_resourceValidLocales(self):
     for locale, in self.getValidLocalesTests():
         translator = Translator('fr')
         translator.add_resource('dict', {'foo': 'foofoo'}, locale)
Esempio n. 41
0
    def testWhenAResourceHasNoRegisteredLoader(self):
        translator = Translator('en')
        translator.add_resource('dict', {'foo': 'foofoo'}, 'en')

        self.assertRaises(RuntimeError, lambda: translator.trans('foo'))
Esempio n. 42
0
 def testSetFallbackValidLocales(self):
     for locale, in self.getValidLocalesTests():
         translator = Translator(locale)
         translator.set_fallback_locales(['fr', locale])
Esempio n. 43
0
    def testSetValidLocale(self):
        for locale, in self.getValidLocalesTests():
            translator = Translator(locale)
            translator.locale = locale

            self.assertEquals(locale, translator.locale)
Esempio n. 44
0
 def testadd_resourceInvalidLocales(self):
     for locale, in self.getInvalidLocalesTests():
         translator = Translator('fr')
         self.assertRaises(
             ValueError, lambda: translator.add_resource(
                 'dict', {'foo': 'foofoo'}, locale))
Esempio n. 45
0
 def testadd_resourceValidLocales(self):
     for locale, in self.getValidLocalesTests():
         translator = Translator('fr')
         translator.add_resource('dict', {'foo': 'foofoo'}, locale)
Esempio n. 46
0
 def testSetFallbackInvalidLocales(self):
     for locale, in self.getInvalidLocalesTests():
         translator = Translator('fr')
         self.assertRaises(
             ValueError,
             lambda: translator.set_fallback_locales(['fr', locale]))
Esempio n. 47
0
 def testSetFallbackValidLocales(self):
     for locale, in self.getValidLocalesTests():
         translator = Translator(locale)
         translator.set_fallback_locales(['fr', locale])