diff --git a/samples/snippets/snippets.py b/samples/snippets/snippets.py index 139b717a..91aef63d 100644 --- a/samples/snippets/snippets.py +++ b/samples/snippets/snippets.py @@ -23,8 +23,8 @@ import argparse - from google.cloud import translate +import six def detect_language(text): @@ -74,12 +74,13 @@ def translate_text_with_model(target, text, model=translate.NMT): """ translate_client = translate.Client() + if isinstance(text, six.binary_type): + text = text.decode('utf-8') + # Text can also be a sequence of strings, in which case this method # will return a sequence of results for each text. result = translate_client.translate( - text.decode('utf-8'), - target_language=target, - model=model) + text, target_language=target, model=model) print(u'Text: {}'.format(result['input'])) print(u'Translation: {}'.format(result['translatedText'])) @@ -95,11 +96,13 @@ def translate_text(target, text): """ translate_client = translate.Client() + if isinstance(text, six.binary_type): + text = text.decode('utf-8') + # Text can also be a sequence of strings, in which case this method # will return a sequence of results for each text. result = translate_client.translate( - text.decode('utf-8'), - target_language=target) + text, target_language=target) print(u'Text: {}'.format(result['input'])) print(u'Translation: {}'.format(result['translatedText'])) diff --git a/samples/snippets/snippets_test.py b/samples/snippets/snippets_test.py index fd53bbc5..153ee104 100644 --- a/samples/snippets/snippets_test.py +++ b/samples/snippets/snippets_test.py @@ -43,7 +43,7 @@ def test_translate_text(capsys): def test_translate_utf8(capsys): - text = '나는 파인애플을 좋아한다.' + text = u'나는 파인애플을 좋아한다.' snippets.translate_text('en', text) out, _ = capsys.readouterr() - assert 'I like pineapple.' in out + assert u'I like pineapple.' in out