简体   繁体   中英

googletrans error with dictionary translate

Trying to use googletrans in my project, using version 4.0.0-rc.1, Python 13.10 on Windows 10 local machine.

The script is simple, it must translate an array of the phrases into English from Russian. When trying to translate one phrase - it's ok, but when trying to translate a dictionary - the error occurs:

Traceback (most recent call last):
  File "c:\Users\fire\Dropbox\my_soft\python_code_lessons\py_version.py", line 107, in <module>
    translated = tr.translate(data, dest='en')
  File "C:\Users\fire\Dropbox\my_soft\python_code_lessons\venv\lib\site-packages\googletrans\client.py", line 219, in translate
    parsed = json.loads(data[0][2])
  File "C:\Users\fire\AppData\Local\Programs\Python\Python310\lib\json\__init__.py", line 339, in loads
    raise TypeError(f'the JSON object must be str, bytes or bytearray, '
TypeError: the JSON object must be str, bytes or bytearray, not NoneType

Part of the script where this error occurs:

import googletrans
from googletrans import Translator
print(googletrans.__version__)

tr = Translator()
data = ['привет', 'мой мир', 'лучший']

translated = tr.translate(data, dest='en')

for trans in translated:
    print(f'{trans.origin} -> {trans.text}')

Where should I dig? Or maybe someone had such a problem?

This seems to be the problem of this particular API not the google translation itself. I found that this issue has been logged to py-googletrans github and does not seem to be resolved yet.

I do not know this API at all, but when I looked into the code on githb the file mentioned in the error message googletrans\client.py seems to be changed as line 219 is completely different to the part in the error. Maybe some update will solve the case...

Anyway you should try Google APIs for Transactions. There are 3 of them available and all have Python API so it should match your needs.

You may find comparison of them here .

Basic and Advance Translation API has nice python quickstarts in the documentations .

AutoML Translation API is quite hard to find so please check here . It does not contain so nice Python quickstart however there is nice samles part where you can find nice python examples like this one .

As mentioned by @vitooh, there is a known issue on the googletrans library and is yet to be resolved. You may check the given references of @vitooh for more information.

As a work around on your case, you may use the code below:

import googletrans
from googletrans import Translator
print(googletrans.__version__)

tr = Translator()
data = ['привет', 'мой мир', 'лучший']
for lang in data:
    translated = tr.translate(lang, dest='en')
    print(f'{translated.origin} -> {translated.text}')

The code loops the given data so that it will be translated per String.

Please see below screenshot of my testing for your reference:

测试证明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM