Пример #1
0
 def test_memoization_with_file(self):
     '''This test creates a temporary file with the help of the
     tempfile library and writes a simple key: value dictionary in it.
     It will then use that file to load the translations and, after having
     enabled memoization, try to access it, causing the file to be (hopefully)
     memoized. It will then _remove_ the temporary file and try to access again,
     asserting that an error is not raised, thus making sure the data is
     actually loaded from memory and not from disk access.'''
     memoization_file_name = 'memoize.en.yml'
     # create the file and write the data in it
     try:
         d = tempfile.TemporaryDirectory()
         tmp_dir_name = d.name
     except AttributeError:
         # we are running python2, use mkdtemp
         tmp_dir_name = tempfile.mkdtemp()
     fd = open('{}/{}'.format(tmp_dir_name, memoization_file_name), 'w')
     fd.write('en:\n  key: value')
     fd.close()
     # create the loader and pass the file to it
     resource_loader.init_yaml_loader()
     resource_loader.load_translation_file(memoization_file_name,
                                           tmp_dir_name)
     # try loading the value to make sure it's working
     self.assertEqual(t('memoize.key'), 'value')
     # now delete the file and directory
     # we are running python2, delete manually
     import shutil
     shutil.rmtree(tmp_dir_name)
     # test the translation again to make sure it's loaded from memory
     self.assertEqual(t('memoize.key'), 'value')
Пример #2
0
 def test_skip_locale_root_data_nested_json_dict__other_locale(self):
     config.set("file_format", "json")
     config.set("load_path", [os.path.join(RESOURCE_FOLDER, "translations", "nested_dict_json")])
     config.set("filename_format", "{locale}.{format}")
     config.set('skip_locale_root_data', True)
     config.set("locale", "en")
     resource_loader.init_json_loader()
     self.assertEqual(t('COMMON.EXECUTE', locale="pl"), 'Wykonaj')
Пример #3
0
 def test_skip_locale_root_data(self):
     config.set('filename_format', '{locale}.{format}')
     config.set('file_format', 'json')
     config.set('locale', 'gb')
     config.set('skip_locale_root_data', True)
     resource_loader.init_loaders()
     self.assertEqual(t('foo'), 'Lorry')
     config.set('skip_locale_root_data', False)
Пример #4
0
 def test_skip_locale_root_data_nested_json_dict__default_locale(self):
     config.set("file_format", "json")
     config.set("load_path", [os.path.join(RESOURCE_FOLDER, "translations", "nested_dict_json")])
     config.set("filename_format", "{locale}.{format}")
     config.set('skip_locale_root_data', True)
     config.set("locale", "en")
     resource_loader.init_json_loader()
     self.assertEqual(t('COMMON.START'), 'Start')
Пример #5
0
def translate(string):
    resource_loader.init_loaders()
    config.set('load_path', ['/usr/src/locale'])
    locale = 'en'
    try:
        locale = os.environ['MUTATIX_LOCALE']
    except:
        pass
    config.set('locale', locale)
    return t(string)
Пример #6
0
 def test_bad_pluralization(self):
     self.assertEqual(t('foo.normal_key', count=5), 'normal_value')
     config.set('error_on_missing_plural', True)
     with self.assertRaises(KeyError):
         t('foo.bad_plural', count=0)
Пример #7
0
    def test_custom_formatter(self):
        class MyFormatter(Template):
            delimiter = '!'

        config.set('translation_formatter', MyFormatter)
        self.assertEqual(t('try_formatter', name="bar"), "foo bar")
Пример #8
0
 def test_missing_placehoder(self):
     self.assertEqual(t('foo.hi'), 'Hello %{name} !')
Пример #9
0
 def test_basic_pluralization(self):
     self.assertEqual(t('foo.basic_plural', count=0), '0 elems')
     self.assertEqual(t('foo.basic_plural', count=1), '1 elem')
     self.assertEqual(t('foo.basic_plural', count=2), '2 elems')
Пример #10
0
 def test_locale_change(self):
     config.set('locale', 'fr')
     self.assertEqual(t('foo.hello', name='Bob'), 'Salut Bob !')
Пример #11
0
 def test_fallback_from_resource(self):
     config.set('fallback', 'ja')
     self.assertEqual(t('foo.fallback_key'), 'フォールバック')
Пример #12
0
 def test_locale_change(self):
     config.set('locale', 'fr')
     self.assertEqual(t('foo.hello', name='Bob'), 'Salut Bob !')
Пример #13
0
 def test_missing_translation(self):
     self.assertEqual(t('foo.inexistent'), 'foo.inexistent')
Пример #14
0
 def test_missing_translation(self):
     self.assertEqual(t('foo.inexistent'), 'foo.inexistent')
Пример #15
0
 def test_missing_translation_error(self):
     config.set('error_on_missing_translation', True)
     with self.assertRaises(KeyError):
         t('foo.inexistent')
Пример #16
0
 def test_basic_translation(self):
     self.assertEqual(t('foo.normal_key'), 'normal_value')
Пример #17
0
 def test_formatted_list(self):
     self.assertEqual(t('foo.welcome', user="******")[0], "Hi someone")
     self.assertEqual(
         t('foo.welcome2', count=2)[1], "We may need more pylons soon")
Пример #18
0
 def test_basic_list(self):
     self.assertEqual(len(t('foo.months')), 12)
Пример #19
0
    def test_invalid_formatter(self):
        class MyInvalidFormatter(Template):
            pass

        config.set('translation_formatter', MyInvalidFormatter)
        self.assertEqual(t('try_formatter', name="bar"), "foo !name")
Пример #20
0
 def test_fallback(self):
     config.set('fallback', 'fr')
     self.assertEqual(t('foo.hello', name='Bob'), 'Salut Bob !')
Пример #21
0
 def test_basic_translation(self):
     self.assertEqual(t('foo.normal_key'), 'normal_value')
Пример #22
0
 def test_fallback_from_resource(self):
     config.set('fallback', 'ja')
     self.assertEqual(t('foo.fallback_key'), 'フォールバック')
Пример #23
0
 def test_missing_translation_error(self):
     config.set('error_on_missing_translation', True)
     with self.assertRaises(KeyError):
         t('foo.inexistent')
Пример #24
0
 def test_basic_placeholder(self):
     self.assertEqual(t('foo.hi', name='Bob'), 'Hello Bob !')
Пример #25
0
 def test_fallback(self):
     config.set('fallback', 'fr')
     self.assertEqual(t('foo.hello', name='Bob'), 'Salut Bob !')
Пример #26
0
 def test_missing_placehoder(self):
     self.assertEqual(t('foo.hi'), 'Hello %{name} !')
Пример #27
0
 def test_basic_placeholder(self):
     self.assertEqual(t('foo.hi', name='Bob'), 'Hello Bob !')
Пример #28
0
 def test_missing_placeholder_error(self):
     config.set('error_on_missing_placeholder', True)
     with self.assertRaises(KeyError):
         t('foo.hi')
Пример #29
0
 def test_missing_placeholder_error(self):
     config.set('error_on_missing_placeholder', True)
     with self.assertRaises(KeyError):
         t('foo.hi')
Пример #30
0
 def test_custom_delimiter(self):
     config.set('translation_formatter', None)
     config.set('placeholder_delimiter', '^')
     self.assertEqual(t('try_delimiter', name="bar"), "foo bar")
Пример #31
0
 def test_full_pluralization(self):
     self.assertEqual(t('foo.plural', count=0), 'no mail')
     self.assertEqual(t('foo.plural', count=1), '1 mail')
     self.assertEqual(t('foo.plural', count=4), 'only 4 mails')
     self.assertEqual(t('foo.plural', count=12), '12 mails')
Пример #32
0
 def test_full_pluralization(self):
     self.assertEqual(t('foo.plural', count=0), 'no mail')
     self.assertEqual(t('foo.plural', count=1), '1 mail')
     self.assertEqual(t('foo.plural', count=4), 'only 4 mails')
     self.assertEqual(t('foo.plural', count=12), '12 mails')
Пример #33
0
 def test_default(self):
     self.assertEqual(t('inexistent_key', default='foo'), 'foo')
Пример #34
0
 def test_bad_pluralization(self):
     config.set('error_on_missing_plural', False)
     self.assertEqual(t('foo.normal_key', count=5), 'normal_value')
     config.set('error_on_missing_plural', True)
     with self.assertRaises(KeyError):
         t('foo.bad_plural', count=0)
Пример #35
0
 def test_basic_pluralization(self):
     self.assertEqual(t('foo.basic_plural', count=0), '0 elems')
     self.assertEqual(t('foo.basic_plural', count=1), '1 elem')
     self.assertEqual(t('foo.basic_plural', count=2), '2 elems')
Пример #36
0
 def test_default(self):
     self.assertEqual(t('inexistent_key', default='foo'), 'foo')