コード例 #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
ファイル: translator.py プロジェクト: fmieres/mutatix
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')