nidhaloff / deep-translator

A flexible free and unlimited python tool to translate between different languages in a simple way using multiple translators.
https://deep-translator.readthedocs.io/en/latest/?badge=latest
Apache License 2.0
1.61k stars 186 forks source link

GoogleTranslator RequestError new issue #169

Closed natalie-ayers closed 2 years ago

natalie-ayers commented 2 years ago

Description

I have been using the GoogleTranslator successfully for a few weeks, but 2 days ago it began returning a RequestError. I've tested a few of the other available translators to ensure it was not a larger issue, but it seems to be primarily with the GoogleTranslator. I've tested multiple times over the last few days as well, but it doesn't seem to be temporary.

Thank you for any help!

What I Did

Command:

transl_text = GoogleTranslator(source="ru",target="en").translate(text=text)

Output:

Traceback (most recent call last):
  File "C:\Users\ARK Silverlining\Documents\Silverlining\test_py.py", line 212, in <module>
    transl_text = GoogleTranslator(source="ru",target="en").translate(text=text)
  File "C:\Users\ARK Silverlining\AppData\Local\Programs\Python\Python310-32\lib\site-packages\deep_translator\google.py", line 72, in translate
    raise RequestError()
deep_translator.exceptions.RequestError: Request exception can happen due to an api connection error. Please check your connection and try again
Vincent-Stragier commented 2 years ago

Hi @natalie-ayers,

I was not able to reproduce this behaviour. Can you provide a full MWE?

PS C:\a_random_path> ipython
Python 3.10.2 (tags/v3.10.2:a58ebcc, Jan 17 2022, 14:12:15) [MSC v.1929 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 7.30.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import deep_translator

In [2]: from deep_translator import GoogleTranslator

In [3]: deep_translator.__version__
Out[3]: '1.8.0'

In [4]: text = "Мой домашний питомец"

In [5]: transl_text = GoogleTranslator(source="ru",target="en").translate(text=text)

In [6]: print(transl_text)
My pet

In [7]:

I installed the module from git using pip, but the version in the __init__.py file is incorrect.

Regards, Vincent S.

natalie-ayers commented 2 years ago

Hi Vincent, Thank you for looking into this! I just re-ran to try to send you more information, but the problem is no longer occurring - nothing has changed on my system's end, so I'm not sure what the cause was, but I appreciate your assistance!

nidhaloff commented 2 years ago

@natalie-ayers sorry for the late reply. The error can happen if you had connection/net problems, so I assume that was the issue in your case. This also explains why it started to work again, although you didn't change anything on your system.

@Vincent-Stragier Thanks for providing support!

snjv94 commented 3 months ago

Hi I am getting same error. If I am sending text of a certain limit. Not getting the limit thresh hold. Here is the code.

from deep_translator import GoogleTranslator

def translate_to_english(txt=""):
    translated = GoogleTranslator(source='auto', target='en').translate(txt)

# Not working
sample = "打詞封有泉再母裏幫免 耳學我火才吉帶亮不香火室會經。經叫面安着兌爬得但白童快只快魚采歡、玩田黑很習抄錯第飽己丁「助入兒能且」可怕到半大別孝。斗斥苦科要。怕火畫里神加干喝雨她飽車士起像旁結像科喜!直錯村豆您坐游。是吧也您結娘玩信平兩您,送坡帽胡首節要。功媽成刃還胡告手山像肖人年苗院,新真蝶可牠打林完帶西采斥空和,綠服念帶;休春語澡雞。登力浪位孝交升做音抓旦收幸牙、科第童在平的尼地師回一山實京對時雞,田兌隻鳥蝸了北西六重朱吉跟童即貫圓「神各央很豆香」即都品家寫細抱坐。欠完抓您世想哪!王路弓請國和問爬珠信南課欠是升怎至完走「讀每」米冬什勿葉言蝶個我牛小重候穴海苗正麻。貫色南頁,誰條嗎米好米,止足田蝶聲五清。黃用我支!山息竹像太知白肉卜雄司長想實和很,校南經雲入采泉坐亮石支鴨干月松麻語去蝶麻白。貝力尼去北習日登比多師「害比」豆占點天封寸進。衣斗自四教平紅比我蛋昔爸右實外「習向男頁」記追貝是;急山由青?今收植內未飯遠尼亮方抱,夏教共肉甲幾日苗完。西屋亮能校秋三見力目怪未,頭大是村問身住禾風!四位青男新對山兄學蝶、書國白爸石西月,田見扒即蝸借丁您水眼。午樹怕,神打師肉但弟晚事美食沒裏讀前太珠課哭得圓、路因在校。住什只。總字數:標題 10 字,內文 500 字 打詞封有泉再母裏幫免 耳學我火才吉帶亮不香火室會經。經叫面安着兌爬得但白童快只快魚采歡、玩田黑很習抄錯第飽己丁「助入兒能且」可怕到半大別孝。斗斥苦科要。怕火畫里神加干喝雨她飽車士起像旁結像科喜!直錯村豆您坐游。是吧也您結娘玩信平兩您,送坡帽胡首節要。功媽成刃還胡告手山像肖人年苗院,新真蝶可牠打林完帶西采斥空和,綠服念帶;休春語澡雞。登力浪位孝交升做音抓旦收幸牙、科第童在平的尼地師回一山實京對時雞,田兌隻鳥蝸了北西六重朱吉跟童即貫圓「神各央很豆香」即都品家寫細抱坐。欠完抓您世想哪!王路弓請國和問爬珠信南課欠是升怎至完走「讀每」米冬什勿葉言蝶個我牛小重候穴海苗正麻。貫色南頁,誰條嗎米好米,止足田蝶聲五清。黃用我支!山息竹像太知白肉卜雄司長想實和很,校南經雲入采泉坐亮石支鴨干月松麻語去蝶麻白。貝力尼去北習日登比多師「害比」豆占點天封寸進。衣斗自四教平紅比我蛋昔爸右實外「習向男頁」記追貝是;急山由青?今收植內未飯遠尼亮方抱,夏教共肉甲幾日苗完。西屋亮能校秋三見力目怪未,頭大是村問身住禾風!四位青男新對山兄學蝶、書國白爸石西月,田見扒即蝸借丁您水眼。午樹怕,神打師肉但弟晚事美食沒裏讀前太珠課哭得圓、路因在校。住什只。總字數:標題 10 字,內文 500 字 打詞封有泉再母裏幫免 耳學我火才吉帶亮不香火室會經。經叫面安着兌爬得但白童快只快魚采歡、玩田黑很習抄錯第飽己丁「助入兒能且」可怕到半大別孝。斗斥苦科要。怕火畫里神加干喝雨她飽車士起像旁結像科喜!直錯村豆您坐游。是吧也您結娘玩信平兩您,送坡帽胡首節要。功媽成刃還胡告手山像肖人年苗院,新真蝶可牠打林完帶西采斥空和,綠服念帶;休春語澡雞。登力浪位孝交升做音抓旦收幸牙、科第童在平的尼地師回一山實京對時雞,田兌隻鳥蝸了北西六重朱吉跟童即貫圓「神各央很豆香」即都品家寫細抱坐。欠完抓您世想哪!王路弓請國和問爬珠信南課欠是升怎至完走「讀每」米冬什勿葉言蝶個我牛小重候穴海苗正麻。貫色南頁,誰條嗎米好米,止足田蝶聲五清。黃用我支!山息竹像太知白肉卜雄司長想實和很,校南經雲入采泉坐亮石支鴨干月松麻語去蝶麻白。貝力尼去北習日登比多師「害比」豆占點天封寸進。衣斗自四教平紅比我蛋昔爸右實外「習向男頁」記追貝是;急山由青?今收植內未飯遠尼亮方抱,夏教共肉甲幾日苗完。西屋亮能校秋三見力目怪未,頭大是村問身住禾風!四位青男新對山兄學蝶、書國白爸石西月,田見扒即蝸借丁您水眼。午樹怕,神打師肉但弟晚事美食沒裏讀前太珠課哭得圓、路因在校。住什只。總字數:標題 10 字,內文 500 字 打詞封有泉再母裏幫免 耳學我火才吉帶亮不香火室會經。經叫面安着兌爬得但白童快只快魚采歡、玩田黑很習抄錯第飽己丁「助入兒能且」可怕到半大別孝。斗斥苦科要。怕火畫里神加干喝雨她飽車士起像旁結像科喜!直錯村豆您坐游。是吧也您結娘玩信平兩您,送坡帽胡首節要。功媽成刃還胡告手山像肖人年苗院,新真蝶可牠打林完帶西采斥空和,綠服念帶;休春語澡雞。登力浪位孝交升做音抓旦收幸牙、科第童在平的尼地師回一山實京對時雞,田兌隻鳥蝸了北西六重朱吉跟童即貫圓「神各央很豆香」即都品家寫細抱坐。欠完抓您世想哪!王路弓請國和問爬珠信南課欠是升怎至完走「讀每」米冬什勿葉言蝶個我牛小重候穴海苗正麻。貫色南頁,誰條嗎米好米,止足田蝶聲五清。黃用我支!山息竹像太知白肉卜雄司長想實和很,校南經雲入采泉坐亮石支鴨干月松麻語去蝶麻白。貝力尼去北習日登比多師「害比」豆占點天封寸進。衣斗自四教平紅比我蛋昔爸右實外「習向男頁」記追貝是;急山由青?今收植內未飯遠尼亮方抱,夏教共肉甲幾日苗完。西屋亮能校秋三見力目怪未,頭大是村問身住禾風!四位青男新對山兄學蝶"

# Working
sample = "打詞封有泉再母裏幫免 耳學我火才吉帶亮不香火室會經。經叫面安着兌爬得但白童快只快魚采歡、玩田黑很習抄錯第飽己丁「助入兒能且」可怕到半大別孝。斗斥苦科要。怕火畫里神加干喝雨她飽車士起像旁結像科喜!直錯村豆您坐游。是吧也您結娘玩信平兩您,送坡帽胡首節要。功媽成刃還胡告手山像肖人年苗院,新真蝶可牠打林完帶西采斥空和,綠服念帶;休春語澡雞。登力浪位孝交升做音抓旦收幸牙、科第童在平的尼地師回一山實京對時雞,田兌隻鳥蝸了北西六重朱吉跟童即貫圓「神各央很豆香」即都品家寫細抱坐。欠完抓您世想哪!王路弓請國和問爬珠信南課欠是升怎至完走「讀每」米冬什勿葉言蝶個我牛小重候穴海苗正麻。貫色南頁,誰條嗎米好米,止足田蝶聲五清。黃用我支!山息竹像太知白肉卜雄司長想實和很,校南經雲入采泉坐亮石支鴨干月松麻語去蝶麻白。貝力尼去北習日登比多師「害比」豆占點天封寸進。衣斗自四教平紅比我蛋昔爸右實外「習向男頁」記追貝是;急山由青?今收植內未飯遠尼亮方抱,夏教共肉甲幾日苗完。西屋亮能校秋三見力目怪未,頭大是村問身住禾風!四位青男新對山兄學蝶、書國白爸石西月,田見扒即蝸借丁您水眼。午樹怕,神打師肉但弟晚事美食沒裏讀前太珠課哭得圓、路因在校。住什只。總字數:標題 10 字,內文 500 字 打詞封有泉再母裏幫免 耳學我火才吉帶亮不香火室會經。經叫面安着兌爬得但白童快只快魚采歡、玩田黑很習抄錯第飽己丁「助入兒能且」可怕到半大別孝。斗斥苦科要。怕火畫里神加干喝雨她飽車士起像旁結像科喜!直錯村豆您坐游。是吧也您結娘玩信平兩您,送坡帽胡首節要。功媽成刃還胡告手山像肖人年苗院,新真蝶可牠打林完帶西采斥空和,綠服念帶;休春語澡雞。登力浪位孝交升做音抓旦收幸牙、科第童在平的尼地師回一山實京對時雞,田兌隻鳥蝸了北西六重朱吉跟童即貫圓「神各央很豆香」即都品家寫細抱坐。欠完抓您世想哪!王路弓請國和問爬珠信南課欠是升怎至完走「讀每」米冬什勿葉言蝶個我牛小重候穴海苗正麻。貫色南頁,誰條嗎米好米,止足田蝶聲五清。黃用我支!山息竹像太知白肉卜雄司長想實和很,校南經雲入采泉坐亮石支鴨干月松麻語去蝶麻白。貝力尼去北習日登比多師「害比」豆占點天封寸進。衣斗自四教平紅比我蛋昔爸右實外「習向男頁」記追貝是;急山由青?今收植內未飯遠尼亮方抱,夏教共肉甲幾日苗完。西屋亮能校秋三見力目怪未,頭大是村問身住禾風!四位青男新對山兄學蝶、書國白爸石西月,田見扒即蝸借丁您水眼。午樹怕,神打師肉但弟晚事美食沒裏讀前太珠課哭得圓、路因在校。住什只。總字數:標題 10 字,內文 500 字 打詞封有泉再母裏幫免 耳學我火才吉帶亮不香火室會經。經叫面安着兌爬得但白童快只快魚采歡、玩田黑很習抄錯第飽己丁「助入兒能且」可怕到半大別孝。斗斥苦科要。怕火畫里神加干喝雨她飽車士起像旁結像科喜!直錯村豆您坐游。是吧也您結娘玩信平兩您,送坡帽胡首節要。功媽成刃還胡告手山像肖人年苗院,新真蝶可牠打林完帶西采斥空和,綠服念帶;休春語澡雞。登力浪位孝交升做音抓旦收幸牙、科第童在平的尼地師回一山實京對時雞,田兌隻鳥蝸了北西六重朱吉跟童即貫圓「神各央很豆香」即都品家寫細抱坐。欠完抓您世想哪!王路弓請國和問爬珠信南課欠是升怎至完走「讀每」米冬什勿葉言蝶個我牛小重候穴海苗正麻。貫色南頁,誰條嗎米好米,止足田蝶聲五清。黃用我支!山息竹像太知白肉卜雄司長想實和很,校南經雲入采泉坐亮石支鴨干月松麻語去蝶麻白。貝力尼去北習日登比多師「害比」豆占點天封寸進。衣斗自四教平紅比我蛋昔爸右實外「習向男頁」記追貝是;急山由青?今收植內未飯遠尼亮方抱,夏教共肉甲幾日苗完。西屋亮能校秋三見力目怪未,頭大是村問身住禾風!四位青男新對山兄學蝶、書國白爸石西月,田見扒即蝸借丁您水眼。午樹怕,神打師肉但弟晚事美食沒裏讀前太珠課哭得圓、路因在校。住什只。總字數:標題 10 字,內文 500 字 打詞封有泉再母裏幫免 耳學我火才吉帶亮不香火室會經。經叫面安着兌爬得但白童快只快魚采歡、玩田黑很習抄錯第飽己丁「助入兒能且」可怕到半大別孝。斗斥苦科要。怕火畫里神加干喝雨她飽車士起像旁結像科喜!直錯村豆您坐游。"

translated = translate_to_english(sample)
print(translated)

For text where the code is not working, I am getting following error.

deep_translator.exceptions.RequestError: Request exception can happen due to an api connection error. Please check your connection and try again
Vincent-Stragier commented 3 months ago

Hi @snjv94,

Looking at the code, I'd say the error is still due to the length of the string. At the moment, the code is checking for a number of characters in a string (here 1701 and 2053), which here is lower than the number of bytes (here 5035 and 6091). So it passes the condition but failed to receive a valid response from the API. By chance, you still received an answer for the shortest sample, even if it seems it is slightly oversize bitwise.

Note: in my case at first I received None as an answer for the shortest sample string; because you forgot the return statement in you function.

https://github.com/nidhaloff/deep-translator/blob/fa67ada6c5d17617628fb13447de5457a5158dc6/deep_translator/google.py#L51-L75

As we can see here, len(text) is used and text is a string:

https://github.com/nidhaloff/deep-translator/blob/fa67ada6c5d17617628fb13447de5457a5158dc6/deep_translator/validate.py#L27-L43

A way to solve this issue is probably to use the string encode method to encode the string in UTF-8 as an array of bytes. In the Python documentation about Unicode, we can read the following snippet:

>>> u = chr(40960) + 'abcd' + chr(1972)
>>> u.encode('utf-8')
b'\xea\x80\x80abcd\xde\xb4'
>>> u.encode('ascii')  
Traceback (most recent call last):
    ...
UnicodeEncodeError: 'ascii' codec can't encode character '\ua000' in
  position 0: ordinal not in range(128)
>>> u.encode('ascii', 'ignore')
b'abcd'
>>> u.encode('ascii', 'replace')
b'?abcd?'
>>> u.encode('ascii', 'xmlcharrefreplace')
b'&#40960;abcd&#1972;'
>>> u.encode('ascii', 'backslashreplace')
b'\\ua000abcd\\u07b4'
>>> u.encode('ascii', 'namereplace')
b'\\N{YI SYLLABLE IT}abcd\\u07b4'

Which makes me believe len(text) should be replaced by len(text.encode('UTF-8", "ignore") or len(text.encode('UTF-8", "xmlcharrefreplace"). And, maybe return the size for the string and bytes for the user when raising the error.

snjv94 commented 3 months ago

Hi @snjv94,

Looking at the code, I'd say the error is still due to the length of the string. At the moment, the code is checking for a number of characters in a string (here 1701 and 2053), which here is lower than the number of bytes (here 5035 and 6091). So it passes the condition but failed to receive a valid response from the API. By chance, you still received an answer for the shortest sample, even if it seems it is slightly oversize bitwise.

Note: in my case at first I received None as an answer for the shortest sample string; because you forgot the return statement in you function.

https://github.com/nidhaloff/deep-translator/blob/fa67ada6c5d17617628fb13447de5457a5158dc6/deep_translator/google.py#L51-L75

As we can see here, len(text) is used and text is a string:

https://github.com/nidhaloff/deep-translator/blob/fa67ada6c5d17617628fb13447de5457a5158dc6/deep_translator/validate.py#L27-L43

A way to solve this issue is probably to use the string encode method to encode the string in UTF-8 as an array of bytes. In the Python documentation about Unicode, we can read the following snippet:

>>> u = chr(40960) + 'abcd' + chr(1972)
>>> u.encode('utf-8')
b'\xea\x80\x80abcd\xde\xb4'
>>> u.encode('ascii')  
Traceback (most recent call last):
    ...
UnicodeEncodeError: 'ascii' codec can't encode character '\ua000' in
  position 0: ordinal not in range(128)
>>> u.encode('ascii', 'ignore')
b'abcd'
>>> u.encode('ascii', 'replace')
b'?abcd?'
>>> u.encode('ascii', 'xmlcharrefreplace')
b'&#40960;abcd&#1972;'
>>> u.encode('ascii', 'backslashreplace')
b'\\ua000abcd\\u07b4'
>>> u.encode('ascii', 'namereplace')
b'\\N{YI SYLLABLE IT}abcd\\u07b4'

Which makes me believe len(text) should be replaced by len(text.encode('UTF-8", "ignore") or len(text.encode('UTF-8", "xmlcharrefreplace"). And, maybe return the size for the string and bytes for the user when raising the error.

Thanks for the assistance, @Vincent-Stragier