Translate Text is Wrong

169 views
Skip to first unread message

Ty Chan

unread,
May 25, 2021, 4:10:42 AM5/25/21
to Google Cloud Translation API
Translate from Chinese Simplified to English below :

translate_text('打网页不开') 

I got :

Translation: Can't open the webpage

When using Google Translate website, I got :

Translation: Can't open the webpage

Below is my code:

==

import six
import os
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = r"/Users/chanty/Desktop/Google_Text_Translation/testing1-314006-feb3316110eb.json"

from google.cloud import translate_v2 as translate
translate_client = translate.Client()

def translate_text(text):
if isinstance(text, six.binary_type):
text = text.decode("utf-8")

if translate_client.detect_language(text)['language'] == 'zh-CN':
target = 'en'
elif translate_client.detect_language(text)['language'] == 'en':
target = 'zh-CN'
else:
target = 'en'
result = translate_client.translate(text, target_language=target,model='nmt')

print(u"Text: {}".format(result["input"]))
text = result['translateText'].decode("utf-8")
print(text)
print(u"Translation: {}".format(result["translatedText"]))
print(u"Detected source language: {}".format(result["detectedSourceLanguage"]))

return result["translatedText"]
==

Any idea to fix it ?

Yingjie He

unread,
May 25, 2021, 11:10:26 AM5/25/21
to Google Cloud Translation API
Hi,

Could you try to unescape "'" ?

Felix (Google Cloud Platform Support)

unread,
May 27, 2021, 12:06:40 PM5/27/21
to Google Cloud Translation API

Hello,

I tried to reproduce your case through the shell and I got the same result. To fix it, I specified an extra parameter [1] when calling `translate_client`. Specifically, "format" should be set to “text”. Otherwise it is defaulted to “html”. 

If you have the gcloud SDK installed, you can check the outputs setting the field as “html” or “text”, respectively, in [2] and [3].

Best regards,

Felix L. 

------- 

[1] https://googleapis.dev/python/translation/latest/client.html#google.cloud.translate_v2.client.Client.translate

[2] curl -H"Authorization: Bearer $(gcloud auth print-access-token)" 'https://translation.googleapis.com/language/translate/v2?target=en&source=zh-CN&model=nmt&q=打网页不开&format=html'

[3] curl -H"Authorization: Bearer $(gcloud auth print-access-token)" 'https://translation.googleapis.com/language/translate/v2?target=en&source=zh-CN&model=nmt&q=打网页不开&format=text'

Ty Chan

unread,
May 31, 2021, 4:19:12 AM5/31/21
to Google Cloud Translation API

it is fixed. thanks

Neculai I. Fantanaru

unread,
Jun 12, 2021, 9:16:44 AM6/12/21
to Google Cloud Translation API
hi, Chan.. if you fixed the code, can you please share the new code that really works?

Ty Chan

unread,
Jun 17, 2021, 10:24:33 AM6/17/21
to Google Cloud Translation API
Here is the code that fixed my problem.

translate_client.translate(text, target_language='en', format_='text')

Reply all
Reply to author
Forward
0 new messages