2424import argparse
2525
2626from google .cloud import translate
27+ import six
2728
2829
2930def detect_language (text ):
@@ -73,12 +74,13 @@ def translate_text_with_model(target, text, model=translate.NMT):
7374 """
7475 translate_client = translate .Client ()
7576
77+ if isinstance (text , six .binary_type ):
78+ text = text .decode ('utf-8' )
79+
7680 # Text can also be a sequence of strings, in which case this method
7781 # will return a sequence of results for each text.
7882 result = translate_client .translate (
79- text ,
80- target_language = target ,
81- model = model )
83+ text , target_language = target , model = model )
8284
8385 print (u'Text: {}' .format (result ['input' ]))
8486 print (u'Translation: {}' .format (result ['translatedText' ]))
@@ -94,11 +96,13 @@ def translate_text(target, text):
9496 """
9597 translate_client = translate .Client ()
9698
99+ if isinstance (text , six .binary_type ):
100+ text = text .decode ('utf-8' )
101+
97102 # Text can also be a sequence of strings, in which case this method
98103 # will return a sequence of results for each text.
99104 result = translate_client .translate (
100- text ,
101- target_language = target )
105+ text , target_language = target )
102106
103107 print (u'Text: {}' .format (result ['input' ]))
104108 print (u'Translation: {}' .format (result ['translatedText' ]))
0 commit comments