Closed Don-Yin closed 2 years ago
The problem is that google translate mobile requires the target language to be either "zh-CN" or "zh-TW" (case sensitive). "zh" alone sometimes works (haven't figured out just when or why), but google translate changes "zh" to "zh-CN" internally.
Just to track down the problem and allow Google Chinese translation, I added the _zh_map code to google_trans.py (around line 92)
if self.payload_key:
self._url_params[self.payload_key] = text
# ---- fix ------
zh_map = {'zh': 'zh-CN', 'zh-cn': 'zh-CN', 'zh-tw': 'zh-TW'}
if self._url_params['tl'] in _zh_map.keys():
self._url_params['tl'] = _zh_map[self._url_params['tl']]
# ---------
response = requests.get(self.__base_url,
params=self._url_params,
proxies=self.proxies)
I'm not sure where the language codes are getting lower cased, so I kept this proof-of-concept hack localized to the HTTP request.
The problem is that google translate mobile requires the target language to be either "zh-CN" or "zh-TW" (case sensitive). "zh" alone sometimes works (haven't figured out just when or why), but google translate changes "zh" to "zh-CN" internally.
Just to track down the problem and allow Google Chinese translation, I added the _zh_map code to google_trans.py (around line 92)
if self.payload_key: self._url_params[self.payload_key] = text # ---- fix ------ zh_map = {'zh': 'zh-CN', 'zh-cn': 'zh-CN', 'zh-tw': 'zh-TW'} if self._url_params['tl'] in _zh_map.keys(): self._url_params['tl'] = _zh_map[self._url_params['tl']] # --------- response = requests.get(self.__base_url, params=self._url_params, proxies=self.proxies)
I'm not sure where the language codes are getting lower cased, so I kept this proof-of-concept hack localized to the HTTP request.
Thanks for your reply! It explains a lot!
I've tried
translated = GoogleTranslator('en', language).translate(sub_list[i])
where language has been
and it still wont translate to chinese.
My code works for english, spanish, french, german, and japanese. Does this code https://github.com/nidhaloff/deep-translator/issues/105#issuecomment-926983184 work for the current version of deep-translator? @davidsands
Still not working for me neither. It return English (identical to what was sent).
@morpheus65535 Can you show me your example? or open a new issue using your example that it's not working?
This is an example that it is working on my simple test
trans = GoogleTranslator(source='auto', target='zh-CN')
res = trans.translate("good")
print("translation: ", res)
@nidhaloff that seems to be working now. You can forget my November comment. Thanks for the followup. :-)
Description
What happened:
GoogleTranslator not working when target is set to chinese.
I am trying to translate english paragraphs to Chinese, here is what I did:
where SentenceSplitter returns a list of strings each is a sentence from the original paragraph, and:
where to_translate is piece of random text. It returns the original text:
when it is expected to turn its chinese translation.
This method functions perfectly well when
GoogleTranslator
is replaced withMyMemoryTranslator
,MyMemoryTranslator
however raisesTooManyRequests
:it looks like its time.sleep is not functioning properly.
To summarise:
GoogleTranslator
doesn't work at all when target is chinese.MyMemoryTranslator
needs a propertime.sleep
mechanism.