Skip to content

Commit

Permalink
Handle source language inside TxNative
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantinos Bairaktaris committed Dec 26, 2020
1 parent f5d0af3 commit 5147823
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 30 deletions.
29 changes: 13 additions & 16 deletions tests/native/core/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,14 @@ class TestNative(object):

def _get_tx(self, **kwargs):
mytx = TxNative()
mytx.setup(languages=['en', 'el'], token='cds_token', **kwargs)
mytx.setup(source_language='en', languages=['en', 'el'],
token='cds_token', **kwargs)
return mytx

@patch('transifex.native.core.StringRenderer.render')
def test_translate_source_language_reaches_renderer(self, mock_render):
mytx = self._get_tx()
mytx.translate('My String', 'en', is_source=True)
mytx.translate('My String', 'en')
mock_render.assert_called_once_with(
source_string='My String',
string_to_render='My String',
Expand All @@ -74,13 +75,13 @@ 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)
mytx.translate('My String', 'foo')
mock_cache.assert_called_once_with(
generate_key(string='My String'), 'en')
generate_key(string='My String'), 'foo')
mock_render.assert_called_once_with(
source_string='My String',
string_to_render=None,
language_code='en',
language_code='foo',
escape=True,
missing_policy=mytx._missing_policy,
params={},
Expand All @@ -89,18 +90,18 @@ def test_translate_target_language_missing_reaches_renderer(self, mock_render,
def test_translate_target_language_missing_reaches_missing_policy(self):
missing_policy = MagicMock()
mytx = self._get_tx(missing_policy=missing_policy)
mytx.translate('My String', 'en', is_source=False)
mytx.translate('My String', 'foo')
missing_policy.get.assert_called_once_with('My String')

@patch('transifex.native.core.StringRenderer')
def test_translate_error_reaches_error_policy(self, mock_renderer):
error_policy = MagicMock()
mock_renderer.render.side_effect = Exception
mytx = self._get_tx(error_policy=error_policy)
mytx.translate('My String', 'en', is_source=False)
mytx.translate('My String', 'en')
error_policy.get.assert_called_once_with(
source_string='My String', translation=None, language_code='en',
escape=True, params={},
source_string='My String', translation="My String",
language_code='en', escape=True, params={},
)

def test_translate_error_reaches_source_string_error_policy(
Expand All @@ -111,7 +112,7 @@ def test_translate_error_reaches_source_string_error_policy(
mock_missing_policy = MagicMock()
mock_missing_policy.get.side_effect = Exception
mytx = self._get_tx(missing_policy=mock_missing_policy)
result = mytx.translate('My String', 'en', is_source=False)
result = mytx.translate('My String', 'en')
assert result == 'My String'

@patch('transifex.native.core.StringRenderer')
Expand All @@ -130,15 +131,14 @@ def test_source_string_policy_custom_text(
mytx = self._get_tx(
error_policy=error_policy
)
result = mytx.translate('My String', 'en', is_source=False)
result = mytx.translate('My String', 'en')
assert result == 'my-default-text'

def test_translate_source_language_renders_icu(self):
mytx = self._get_tx()
translation = mytx.translate(
u'{cnt, plural, one {{cnt} duck} other {{cnt} ducks}}',
'en',
is_source=True,
params={'cnt': 1}
)
assert translation == '1 duck'
Expand All @@ -149,8 +149,7 @@ def test_translate_target_language_renders_icu(self, mock_cache):
mytx = self._get_tx()
translation = mytx.translate(
u'{cnt, plural, one {{cnt} duck} other {{cnt} ducks}}',
'en',
is_source=False,
'el',
params={'cnt': 1}
)
assert translation == u'1 παπί'
Expand All @@ -160,7 +159,6 @@ def test_translate_source_language_escape_html_true(self):
translation = mytx.translate(
u'<script type="text/javascript">alert(1)</script>',
'en',
is_source=True,
escape=True,
params={'cnt': 1}
)
Expand All @@ -173,7 +171,6 @@ def test_translate_source_language_escape_html_false(self):
translation = mytx.translate(
u'<script type="text/javascript">alert(1)</script>',
'en',
is_source=True,
escape=False,
params={'cnt': 1}
)
Expand Down
2 changes: 2 additions & 0 deletions tests/native/django/test_templatetag.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from transifex.native import tx
from transifex.native.rendering import SourceStringPolicy

tx.setup(source_language="en_US")


def do_test(template_str, context_dict=None, autoescape=True,
lang_code="en-us"):
Expand Down
12 changes: 4 additions & 8 deletions transifex/native/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,14 @@ def fetch_translations(self, language_code=None, force=False):
self._cache.update(translations)

def translate(
self, source_string, language_code=None, is_source=False,
self, source_string, language_code=None,
_context=None, escape=True, params=None
):
"""Translate the given string to the provided language.
:param unicode source_string: the source string to get the translation
for e.g. 'Order: {num, plural, one {A table} other {{num} tables}}'
:param str language_code: the language to translate to
:param bool is_source: a boolean indicating whether `translate`
is being used for the source language
:param unicode _context: an optional context that accompanies
the string
:param bool escape: if True, the returned string will be HTML-escaped,
Expand All @@ -129,25 +127,23 @@ def translate(

translation_template = self.get_translation(source_string,
language_code,
_context,
is_source)
_context)

return self.render_translation(translation_template,
params,
source_string,
language_code,
escape)

def get_translation(self, source_string, language_code, _context,
is_source=False):
def get_translation(self, source_string, language_code, _context):
""" 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:
if language_code == self.source_language_code:
translation_template = source_string
else:
pluralized, plurals = parse_plurals(source_string)
Expand Down
3 changes: 0 additions & 3 deletions transifex/native/django/templatetags/transifex.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from copy import copy

from django.conf import settings
from django.template import Library, Node, TemplateSyntaxError
from django.template.base import (BLOCK_TAG_END, BLOCK_TAG_START,
COMMENT_TAG_END, COMMENT_TAG_START,
Expand Down Expand Up @@ -207,11 +206,9 @@ def render(self, context):
# ICU template. Then we perform ICU rendering against 'params'.
# Inbetween the two steps, if the tag used was 't' and not 'ut', we
# peform escaping on the ICU template.
is_source = get_language() == settings.LANGUAGE_CODE
locale = to_locale(get_language()) # e.g. from en-us to en_US
translation_icu_template = tx.get_translation(
source_icu_template, locale, params.get('_context', None),
is_source,
)
if self.tag_name == "t":
source_icu_template = escape_html(source_icu_template)
Expand Down
3 changes: 0 additions & 3 deletions transifex/native/django/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from django.conf import settings
from django.utils.translation import get_language, to_locale
from transifex.common.strings import LazyString
from transifex.native import tx
Expand All @@ -19,13 +18,11 @@ def translate(_string, _context=None, _escape=True, **params):
:return: the final translation in the current language
:rtype: unicode
"""
is_source = get_language() == settings.LANGUAGE_CODE
locale = to_locale(get_language()) # e.g. from en-us to en_US
return tx.translate(
_string,
locale,
_context=_context,
is_source=is_source,
escape=_escape,
params=params,
)
Expand Down

0 comments on commit 5147823

Please sign in to comment.