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'))
示例#2
0
    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'))
示例#3
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}))
示例#4
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}))
示例#5
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'))
    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 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}))
    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'))
示例#9
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'))
示例#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}))
示例#11
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)
示例#12
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)
示例#13
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'))
示例#14
0
 def testSetFallbackInvalidLocales(self):
     for locale, in self.getInvalidLocalesTests():
         translator = Translator('fr')
         self.assertRaises(
             ValueError, lambda: translator.set_fallback_locales(['fr', locale]))
示例#15
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'))
示例#16
0
 def testSetFallbackValidLocales(self):
     for locale, in self.getValidLocalesTests():
         translator = Translator(locale)
         translator.set_fallback_locales(['fr', locale])
示例#17
0
 def testSetFallbackInvalidLocales(self):
     for locale, in self.getInvalidLocalesTests():
         translator = Translator('fr')
         self.assertRaises(
             ValueError,
             lambda: translator.set_fallback_locales(['fr', locale]))
示例#18
0
 def testSetFallbackValidLocales(self):
     for locale, in self.getValidLocalesTests():
         translator = Translator(locale)
         translator.set_fallback_locales(['fr', locale])