Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Fix unicode
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Wayne Parrott authored and danoscarmike committed Jul 31, 2020
1 parent 1e99c6e commit 5795640
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
15 changes: 9 additions & 6 deletions samples/snippets/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@

import argparse


from google.cloud import translate
import six


def detect_language(text):
Expand Down Expand Up @@ -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']))
Expand All @@ -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']))
Expand Down
4 changes: 2 additions & 2 deletions samples/snippets/snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 5795640

Please sign in to comment.