def render_translation(self, translation_template, params, source_string, language_code, escape=False): """Replace the variables in the ICU translation and return the final string in the given language. If any error occurs during rendering, the error policy is invoked. """ try: return StringRenderer.render( source_string=source_string, string_to_render=translation_template, language_code=language_code, escape=escape, missing_policy=self._missing_policy, params=params, ) except Exception: return self._error_policy.get( source_string=source_string, translation=translation_template, language_code=language_code, escape=escape, params=params, )
def test_simple_render_with_translation(self): translation = StringRenderer.render( u'{cnt, plural, one {{cnt} table} other {{cnt} tables}}', u'{cnt, plural, one {{cnt} τραπέζι} other {{cnt} τραπέζια}}', 'en', escape=True, missing_policy=SourceStringPolicy(), params={'cnt': 2}, ) assert translation == u'2 τραπέζια'
def test_simple_render_unescaped(self): translation = StringRenderer.render( JS_SCRIPT, JS_SCRIPT, 'en', escape=False, missing_policy=SourceStringPolicy(), params={'cnt': 2}, ) assert translation == JS_SCRIPT
def _complex(self, **params): params = dict(params) params.update({'host': "Jane", 'guest': "Joe"}) return StringRenderer.render( COMPLEX_STRINGS, None, 'en', escape=True, missing_policy=SourceStringPolicy(), params=params, )
def test_simple_render_escaped_with_missing_translation(self): translation = StringRenderer.render( JS_SCRIPT, None, 'en', escape=True, missing_policy=SourceStringPolicy(), params={'cnt': 2}, ) assert translation == ( u'<script type="text/javascript">alert(1)</script>' )
def test_no_missing_policy_and_error_raises_exception(self): with pytest.raises(Exception) as exc_info: translation = StringRenderer.render( 'source', '', 'en', escape=True, missing_policy=None, ) assert str(exc_info.value) == ( 'No string to render and no missing policy defined! ' '(Source String: `source`)')
def test_simple_render_with_missing_translation(self): translation = StringRenderer.render( u'{cnt, plural, one {{cnt} table} other {{cnt} tables}}', None, 'en', escape=True, missing_policy=SourceStringPolicy(), params={'cnt': 2}, ) # Should fall back to source assert translation == u'2 tables' translation = StringRenderer.render( u'{cnt, plural, one {{cnt} table} other {{cnt} tables}}', None, 'en', escape=True, missing_policy=PseudoTranslationPolicy(), params={'cnt': 2}, ) # Should use the proper missing policy assert translation == u'2 ťàƀĺêš'
def test_error_raises_exception(self, mock_logger, mock_escape): mock_escape.side_effect = Exception with pytest.raises(Exception): translation = StringRenderer.render( 'Source String', 'Translation', 'en', escape=True, missing_policy=SourceStringPolicy(), ) mock_logger.error.assert_called_with( 'RenderingError: Could not render string `%s` in language `%s` ' 'with parameters `%s` (Error: %s, Source String: %s)', 'Translation', 'en', '{}', '', 'Source String')
def render_translation(self, translation_template, params, source_string, language_code, escape=False): """ Replace the variables in the ICU translation """ try: return StringRenderer.render( source_string=source_string, string_to_render=translation_template, language_code=language_code, escape=escape, missing_policy=self._missing_policy, params=params, ) except Exception: return self._error_policy.get( source_string=source_string, translation=translation_template, language_code=language_code, escape=escape, params=params, )
def pluralized(one, other, cnt_value): """Render an ICU pluralized string with the given parameters. :param unicode one: the string for singular :param unicode other: the string for plural :param int cnt_value: the value of the plural counter variable :return: a rendered string that has taken into account the given :rtype: unicode """ icu_string = '{cnt, plural, one {[one]} other {[other]}}'\ .replace('[one]', one)\ .replace('[other]', other) return StringRenderer.render( icu_string, string_to_render=icu_string, language_code='en', escape=False, missing_policy=None, params={'cnt': cnt_value}, )