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')
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')
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)
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')
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)
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)
def test_custom_formatter(self): class MyFormatter(Template): delimiter = '!' config.set('translation_formatter', MyFormatter) self.assertEqual(t('try_formatter', name="bar"), "foo bar")
def test_missing_placehoder(self): self.assertEqual(t('foo.hi'), 'Hello %{name} !')
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')
def test_locale_change(self): config.set('locale', 'fr') self.assertEqual(t('foo.hello', name='Bob'), 'Salut Bob !')
def test_fallback_from_resource(self): config.set('fallback', 'ja') self.assertEqual(t('foo.fallback_key'), 'フォールバック')
def test_missing_translation(self): self.assertEqual(t('foo.inexistent'), 'foo.inexistent')
def test_missing_translation_error(self): config.set('error_on_missing_translation', True) with self.assertRaises(KeyError): t('foo.inexistent')
def test_basic_translation(self): self.assertEqual(t('foo.normal_key'), 'normal_value')
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")
def test_basic_list(self): self.assertEqual(len(t('foo.months')), 12)
def test_invalid_formatter(self): class MyInvalidFormatter(Template): pass config.set('translation_formatter', MyInvalidFormatter) self.assertEqual(t('try_formatter', name="bar"), "foo !name")
def test_fallback(self): config.set('fallback', 'fr') self.assertEqual(t('foo.hello', name='Bob'), 'Salut Bob !')
def test_basic_placeholder(self): self.assertEqual(t('foo.hi', name='Bob'), 'Hello Bob !')
def test_missing_placeholder_error(self): config.set('error_on_missing_placeholder', True) with self.assertRaises(KeyError): t('foo.hi')
def test_custom_delimiter(self): config.set('translation_formatter', None) config.set('placeholder_delimiter', '^') self.assertEqual(t('try_delimiter', name="bar"), "foo bar")
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')
def test_default(self): self.assertEqual(t('inexistent_key', default='foo'), 'foo')
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)