Skip to content

Commit 5147823

Browse files
author
Konstantinos Bairaktaris
committed
Handle source language inside TxNative
1 parent f5d0af3 commit 5147823

File tree

5 files changed

+19
-30
lines changed

5 files changed

+19
-30
lines changed

tests/native/core/test_core.py

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,14 @@ class TestNative(object):
5252

5353
def _get_tx(self, **kwargs):
5454
mytx = TxNative()
55-
mytx.setup(languages=['en', 'el'], token='cds_token', **kwargs)
55+
mytx.setup(source_language='en', languages=['en', 'el'],
56+
token='cds_token', **kwargs)
5657
return mytx
5758

5859
@patch('transifex.native.core.StringRenderer.render')
5960
def test_translate_source_language_reaches_renderer(self, mock_render):
6061
mytx = self._get_tx()
61-
mytx.translate('My String', 'en', is_source=True)
62+
mytx.translate('My String', 'en')
6263
mock_render.assert_called_once_with(
6364
source_string='My String',
6465
string_to_render='My String',
@@ -74,13 +75,13 @@ def test_translate_target_language_missing_reaches_renderer(self, mock_render,
7475
mock_cache):
7576
mock_cache.return_value = None
7677
mytx = self._get_tx()
77-
mytx.translate('My String', 'en', is_source=False)
78+
mytx.translate('My String', 'foo')
7879
mock_cache.assert_called_once_with(
79-
generate_key(string='My String'), 'en')
80+
generate_key(string='My String'), 'foo')
8081
mock_render.assert_called_once_with(
8182
source_string='My String',
8283
string_to_render=None,
83-
language_code='en',
84+
language_code='foo',
8485
escape=True,
8586
missing_policy=mytx._missing_policy,
8687
params={},
@@ -89,18 +90,18 @@ def test_translate_target_language_missing_reaches_renderer(self, mock_render,
8990
def test_translate_target_language_missing_reaches_missing_policy(self):
9091
missing_policy = MagicMock()
9192
mytx = self._get_tx(missing_policy=missing_policy)
92-
mytx.translate('My String', 'en', is_source=False)
93+
mytx.translate('My String', 'foo')
9394
missing_policy.get.assert_called_once_with('My String')
9495

9596
@patch('transifex.native.core.StringRenderer')
9697
def test_translate_error_reaches_error_policy(self, mock_renderer):
9798
error_policy = MagicMock()
9899
mock_renderer.render.side_effect = Exception
99100
mytx = self._get_tx(error_policy=error_policy)
100-
mytx.translate('My String', 'en', is_source=False)
101+
mytx.translate('My String', 'en')
101102
error_policy.get.assert_called_once_with(
102-
source_string='My String', translation=None, language_code='en',
103-
escape=True, params={},
103+
source_string='My String', translation="My String",
104+
language_code='en', escape=True, params={},
104105
)
105106

106107
def test_translate_error_reaches_source_string_error_policy(
@@ -111,7 +112,7 @@ def test_translate_error_reaches_source_string_error_policy(
111112
mock_missing_policy = MagicMock()
112113
mock_missing_policy.get.side_effect = Exception
113114
mytx = self._get_tx(missing_policy=mock_missing_policy)
114-
result = mytx.translate('My String', 'en', is_source=False)
115+
result = mytx.translate('My String', 'en')
115116
assert result == 'My String'
116117

117118
@patch('transifex.native.core.StringRenderer')
@@ -130,15 +131,14 @@ def test_source_string_policy_custom_text(
130131
mytx = self._get_tx(
131132
error_policy=error_policy
132133
)
133-
result = mytx.translate('My String', 'en', is_source=False)
134+
result = mytx.translate('My String', 'en')
134135
assert result == 'my-default-text'
135136

136137
def test_translate_source_language_renders_icu(self):
137138
mytx = self._get_tx()
138139
translation = mytx.translate(
139140
u'{cnt, plural, one {{cnt} duck} other {{cnt} ducks}}',
140141
'en',
141-
is_source=True,
142142
params={'cnt': 1}
143143
)
144144
assert translation == '1 duck'
@@ -149,8 +149,7 @@ def test_translate_target_language_renders_icu(self, mock_cache):
149149
mytx = self._get_tx()
150150
translation = mytx.translate(
151151
u'{cnt, plural, one {{cnt} duck} other {{cnt} ducks}}',
152-
'en',
153-
is_source=False,
152+
'el',
154153
params={'cnt': 1}
155154
)
156155
assert translation == u'1 παπί'
@@ -160,7 +159,6 @@ def test_translate_source_language_escape_html_true(self):
160159
translation = mytx.translate(
161160
u'<script type="text/javascript">alert(1)</script>',
162161
'en',
163-
is_source=True,
164162
escape=True,
165163
params={'cnt': 1}
166164
)
@@ -173,7 +171,6 @@ def test_translate_source_language_escape_html_false(self):
173171
translation = mytx.translate(
174172
u'<script type="text/javascript">alert(1)</script>',
175173
'en',
176-
is_source=True,
177174
escape=False,
178175
params={'cnt': 1}
179176
)

tests/native/django/test_templatetag.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
from transifex.native import tx
77
from transifex.native.rendering import SourceStringPolicy
88

9+
tx.setup(source_language="en_US")
10+
911

1012
def do_test(template_str, context_dict=None, autoescape=True,
1113
lang_code="en-us"):

transifex/native/core.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,14 @@ def fetch_translations(self, language_code=None, force=False):
101101
self._cache.update(translations)
102102

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

130128
translation_template = self.get_translation(source_string,
131129
language_code,
132-
_context,
133-
is_source)
130+
_context)
134131

135132
return self.render_translation(translation_template,
136133
params,
137134
source_string,
138135
language_code,
139136
escape)
140137

141-
def get_translation(self, source_string, language_code, _context,
142-
is_source=False):
138+
def get_translation(self, source_string, language_code, _context):
143139
""" Try to retrieve the translation.
144140
145141
A translation is a serialized source_string with ICU format
146142
support, e.g.
147143
'{num, plural, one {Ένα τραπέζι} other {{num} τραπέζια}}'
148144
"""
149145

150-
if is_source:
146+
if language_code == self.source_language_code:
151147
translation_template = source_string
152148
else:
153149
pluralized, plurals = parse_plurals(source_string)

transifex/native/django/templatetags/transifex.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
from copy import copy
44

5-
from django.conf import settings
65
from django.template import Library, Node, TemplateSyntaxError
76
from django.template.base import (BLOCK_TAG_END, BLOCK_TAG_START,
87
COMMENT_TAG_END, COMMENT_TAG_START,
@@ -207,11 +206,9 @@ def render(self, context):
207206
# ICU template. Then we perform ICU rendering against 'params'.
208207
# Inbetween the two steps, if the tag used was 't' and not 'ut', we
209208
# peform escaping on the ICU template.
210-
is_source = get_language() == settings.LANGUAGE_CODE
211209
locale = to_locale(get_language()) # e.g. from en-us to en_US
212210
translation_icu_template = tx.get_translation(
213211
source_icu_template, locale, params.get('_context', None),
214-
is_source,
215212
)
216213
if self.tag_name == "t":
217214
source_icu_template = escape_html(source_icu_template)

transifex/native/django/utils/__init__.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from django.conf import settings
21
from django.utils.translation import get_language, to_locale
32
from transifex.common.strings import LazyString
43
from transifex.native import tx
@@ -19,13 +18,11 @@ def translate(_string, _context=None, _escape=True, **params):
1918
:return: the final translation in the current language
2019
:rtype: unicode
2120
"""
22-
is_source = get_language() == settings.LANGUAGE_CODE
2321
locale = to_locale(get_language()) # e.g. from en-us to en_US
2422
return tx.translate(
2523
_string,
2624
locale,
2725
_context=_context,
28-
is_source=is_source,
2926
escape=_escape,
3027
params=params,
3128
)

0 commit comments

Comments
 (0)