Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 5062b2d

Browse filesBrowse files
author
Jon Wayne Parrott
committed
Fix unicode
1 parent 4a68883 commit 5062b2d
Copy full SHA for 5062b2d

File tree

Expand file treeCollapse file tree

2 files changed

+11
-8
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+11
-8
lines changed

‎translate/cloud-client/snippets.py

Copy file name to clipboardExpand all lines: translate/cloud-client/snippets.py
+9-6Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323

2424
import argparse
2525

26-
2726
from google.cloud import translate
27+
import six
2828

2929

3030
def detect_language(text):
@@ -74,12 +74,13 @@ def translate_text_with_model(target, text, model=translate.NMT):
7474
"""
7575
translate_client = translate.Client()
7676

77+
if isinstance(text, six.binary_type):
78+
text = text.decode('utf-8')
79+
7780
# Text can also be a sequence of strings, in which case this method
7881
# will return a sequence of results for each text.
7982
result = translate_client.translate(
80-
text.decode('utf-8'),
81-
target_language=target,
82-
model=model)
83+
text, target_language=target, model=model)
8384

8485
print(u'Text: {}'.format(result['input']))
8586
print(u'Translation: {}'.format(result['translatedText']))
@@ -95,11 +96,13 @@ def translate_text(target, text):
9596
"""
9697
translate_client = translate.Client()
9798

99+
if isinstance(text, six.binary_type):
100+
text = text.decode('utf-8')
101+
98102
# Text can also be a sequence of strings, in which case this method
99103
# will return a sequence of results for each text.
100104
result = translate_client.translate(
101-
text.decode('utf-8'),
102-
target_language=target)
105+
text, target_language=target)
103106

104107
print(u'Text: {}'.format(result['input']))
105108
print(u'Translation: {}'.format(result['translatedText']))

‎translate/cloud-client/snippets_test.py

Copy file name to clipboardExpand all lines: translate/cloud-client/snippets_test.py
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def test_translate_text(capsys):
4343

4444

4545
def test_translate_utf8(capsys):
46-
text = '나는 파인애플을 좋아한다.'
46+
text = u'나는 파인애플을 좋아한다.'
4747
snippets.translate_text('en', text)
4848
out, _ = capsys.readouterr()
49-
assert 'I like pineapple.' in out
49+
assert u'I like pineapple.' in out

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.