Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

smart_translate_item_data: translate strings automatically #3453

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions kalite/topic_tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,15 +517,18 @@ def smart_translate_item_data(item_data):
item_data and translates only the content field.

"""
if isinstance(item_data, dict):
if 'content' in item_data:
item_data['content'] = _(item_data['content']) if item_data['content'] else ""

for field, field_data in item_data.iteritems():
if isinstance(field_data, dict):
item_data[field] = smart_translate_item_data(field_data)
elif isinstance(field_data, list):
item_data[field] = map(smart_translate_item_data, field_data)
# just translate strings immediately
if isinstance(item_data, basestring):
return _(item_data)

if 'content' in item_data:
item_data['content'] = _(item_data['content']) if item_data['content'] else ""

for field, field_data in item_data.iteritems():
if isinstance(field_data, dict):
item_data[field] = smart_translate_item_data(field_data)
elif isinstance(field_data, list):
item_data[field] = map(smart_translate_item_data, field_data)


return item_data
Expand Down
10 changes: 9 additions & 1 deletion kalite/topic_tools/tests/utils_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,19 @@ def test_content_in_widgets_field(self):
self.assertEqual(result, expected_data)

def test_content_inside_radio_field(self):
pass
test_data = {'radio 1': {'widgets': {'content': TRANS_STRING}}}
expected_data = {'radio 1': {'widgets': {'content': DUMMY_STRING}}}

result = mod.smart_translate_item_data(test_data)
ugettext_dummy.assert_called_once_with(TRANS_STRING)

self.assertEqual(result, expected_data)

def test_simple_string(self):
test_data = TRANS_STRING
expected_data = DUMMY_STRING

result = mod.smart_translate_item_data(test_data)
ugettext_dummy.assert_called_once_with(TRANS_STRING)

self.assertEqual(result, expected_data)