2323
2424import argparse
2525
26-
2726from google .cloud import translate
27+ import six
2828
2929
3030def 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' ]))
0 commit comments