def test_escaping_is_done_on_translation(): hello_key = generate_key(string='hello', context=None) tx._cache.update( {'fr': (True, { hello_key: { 'string': "<xml>bonjour</xml>" } })}) assert (do_test('{% t "hello" %}', lang_code="fr") == '<xml>bonjour</xml>')
def test_source_filter_is_applied_on_translation(): # 'hello' => 'bonjour', 'HELLO' => 'I like pancakes' hello_key = generate_key(string='hello', context=None) HELLO_key = generate_key(string='HELLO', context=None) tx._cache.update({ 'fr': (True, { hello_key: { 'string': "bonjour" }, HELLO_key: { 'string': "I like pancakes" } }) }) assert do_test('{% t "hello"|upper %}', lang_code="fr") == "BONJOUR" # If the filter was applied on the source string, we would get # 'I like pancakes' translation.activate('en-us')
def add(self, source_string): """Add a source string to the collection. :param SourceString source_string: the object to add """ key = generate_key( string=source_string.string, context=source_string.context ) if key not in self.strings: self.strings[key] = source_string else: self.strings[key].occurrences = source_string.occurrences
def test_translation_missing(): old_missing_policy = tx._missing_policy tx._missing_policy = SourceStringPolicy() tx._cache._translations_by_lang = {} assert do_test('{% t "hello" %}', lang_code="fr") == "hello" hello_key = generate_key(string='hello', context=None) tx._cache.update({'fr': (True, {hello_key: {'string': None}})}) assert do_test('{% t "hello" %}', lang_code="fr") == "hello" tx._missing_policy = old_missing_policy
def test_translate_source_language_returns_overridden_translation(self): """If there is a newer translation in the cache for the source language use that instead of the original source string. """ # Populate the cache with two strings in the source language, # essentially providing custom translations for the source strings cache = MemoryCache() # Source based key source_string1 = u'{cnt, plural, one {{cnt} duck} other {{cnt} ducks}}' updated_string1 = u'{???, plural, one {# goose} other {# geese}}' key1 = generate_key(source_string1) # Hash based key source_string2 = u'Hello {username}' updated_string2 = u'Hello to you, {username}!' key2 = generate_hashed_key(source_string2) data = { 'en': ( True, { key1: {'string': updated_string1}, key2: {'string': updated_string2}, } ) } cache.update(data) # Make sure the updated translations are returned mytx = self._get_tx(cache=cache) translation = mytx.translate( source_string1, 'en', is_source=True, params={'cnt': 1} ) assert translation == u'1 goose' translation = mytx.translate( source_string1, 'en', is_source=True, params={'cnt': 10} ) assert translation == u'10 geese' translation = mytx.translate( source_string2, 'en', is_source=True, params={'username': '******'} ) assert translation == u'Hello to you, Jane!'
def __init__(self, string, _context=None, **meta): """Constructor. :param unicode string: the source string # check ./consts.py :param unicode _context: an optional context that accompanies the source string """ self.key = generate_key(string=string, context=_context) self.string = string self.context = ([x.strip() for x in _context.split(',')] if _context else None ) # type: list self.meta = self._transform_meta(meta)
def test_translate_target_language_missing_reaches_renderer( self, mock_render, mock_cache): mock_cache.return_value = None mytx = self._get_tx() mytx.translate('My String', 'en', is_source=False) mock_cache.assert_called_once_with(generate_key(string='My String'), 'en') mock_render.assert_called_once_with( source_string='My String', string_to_render=None, language_code='en', escape=True, missing_policy=mytx._missing_policy, params={}, )
def get_translation(self, source_string, language_code, _context, is_source=False, _key=None): """Return the proper translation for the given string and language code. A translation is a serialized source_string with ICU format support, e.g. '{num, plural, one {Ένα τραπέζι} other {{num} τραπέζια}}' Supports strings in the source language as well, which means that if there is a translation available in the cache for the source language, it will be used instead of the original source_string provided here. """ pluralized, plurals = parse_plurals(source_string) if _key is not None: # Custom key translation_template = self._cache.get(_key, language_code) else: # Source based key _key = generate_key(string=source_string, context=_context) translation_template = self._cache.get(_key, language_code) if not translation_template: # Fallback to hashed based key _key = generate_hashed_key(string=source_string, context=_context) translation_template = self._cache.get(_key, language_code) if (translation_template is not None and pluralized and translation_template.startswith('{???')): variable_name = source_string[1:source_string.index(',')].strip() translation_template = '{{{var}{content}'.format( var=variable_name, content=translation_template[4:], ) # If rendering the source language and there is no # (overridden) translation in the cache, use the original # source string if is_source and not translation_template: translation_template = source_string return translation_template
def get_translation(self, source_string, language_code, _context, is_source=False): """ Try to retrieve the translation. A translation is a serialized source_string with ICU format support, e.g. '{num, plural, one {Ένα τραπέζι} other {{num} τραπέζια}}' """ if is_source: translation_template = source_string else: pluralized, plurals = parse_plurals(source_string) key = generate_key(string=source_string, context=_context) translation_template = self._cache.get(key, language_code) if (translation_template is not None and pluralized and translation_template.startswith('{???')): variable_name = source_string[1:source_string.index(',')].\ strip() translation_template = ('{' + variable_name + translation_template[4:]) return translation_template
def test_translates(): hello_key = generate_key(string='hello', context=None) tx._cache.update({'fr': (True, {hello_key: {'string': "bonjour"}})}) assert do_test('{% t "hello" %}', lang_code="fr") == "bonjour"