ssut / py-googletrans

(unofficial) Googletrans: Free and Unlimited Google translate API for Python. Translates totally free of charge.
http://py-googletrans.rtfd.io
MIT License
3.89k stars 717 forks source link

NoneType return on Advanced Bulk version 4.0.0-rc1 #264

Closed AkmalPratama closed 3 years ago

AkmalPratama commented 3 years ago

Googletrans version:

I'm submitting a ...

Current behavior:

I tried to use the bulk translate from sample code on documentation page. The translate method return error TypeError: the JSON object must be str, bytes or bytearray, not 'NoneType'

Expected behavior:

Should be same as written in documentation page. Bulk translate must return collection of translated object

Steps to reproduce:

Use list of string as an input parameter translate method

Related code:

translator = Translator(service_urls=[
      'translate.google.com',
      'translate.google.co.kr',
    ])

translations = translator.translate(['The quick brown fox', 'jumps over', 'the lazy dog'], dest='ko')
for translation in translations:
    print(translation.origin, ' -> ', translation.text)
TypeError                                 Traceback (most recent call last)
<ipython-input-21-14c1e5590012> in <module>()
      4     ])
      5 
----> 6 translations = translator.translate(['The quick brown fox', 'jumps over', 'the lazy dog'], dest='ko')
      7 for translation in translations:
      8     print(translation.origin, ' -> ', translation.text)

1 frames
/usr/lib/python3.6/json/__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    346         if not isinstance(s, (bytes, bytearray)):
    347             raise TypeError('the JSON object must be str, bytes or bytearray, '
--> 348                             'not {!r}'.format(s.__class__.__name__))
    349         s = s.decode(detect_encoding(s), 'surrogatepass')
    350 

TypeError: the JSON object must be str, bytes or bytearray, not 'NoneType'

Other information:

ShadowTeamCN commented 3 years ago

same with me

furkankadioglu commented 3 years ago

Same here.

zylkowski commented 3 years ago

Same here.

pingxg commented 3 years ago

Same here.

NikitaMishin commented 3 years ago

same

AkmalPratama commented 3 years ago

pip install googletrans==4.0.0rc1 this will fix the problem.

Not sure what you mean. The title of issue clearly stated that version

DanilKonon commented 3 years ago

I have the same error, but when the length of string is more than 5 000 characters.

Jaspernwd commented 3 years ago

Same problem

kclamap commented 3 years ago

Not a fix but I just had a glimpse at the source code and found that

class Translator:
  ...
  def translate(self, text, dest='en', src='auto', **kwargs):
    ...
    if isinstance(text, list):
      result = []
      for item in text:
          translated = self.translate(item, dest=dest, src=src, **kwargs)
          result.append(translated)
      return result

In other words, it just does a for loop to translate each string in the list if a list is passed in. Since I was looking for ways to speed up my application, I try bulk translation. (I was doing a for loop by myself originally) Now it turns out to me that whether it works or not has really no big deal...... I suggest you write the for loop in your code instead if you need it to work asap while waiting for a fix.

Saravananslb commented 3 years ago

Use this for google translation and it is working fine https://pypi.org/project/pygoogletranslation

Thank me later

AkmalPratama commented 3 years ago

Use this for google translation and it is working fine https://pypi.org/project/pygoogletranslation

Thank me later

yours doesn't working either, but that's another issue

AttributeError                            Traceback (most recent call last)
<ipython-input-69-9b258cdf2b47> in <module>()
      1 translator = Translator()
----> 2 translations = translator.translate(['this is google translation', 'Tamil language' ], dest='ta')
      3 for translation in translations:
      4   print(translation.origin, ' -> ', translation.text)

/usr/local/lib/python3.6/dist-packages/pygoogletranslation/translate.py in translate(self, text, src, dest)
     40 
     41     def translate(self, text, src='auto', dest='en'):
---> 42         text = text.replace('"', '')
     43         text = text.replace("'", "")
     44 

AttributeError: 'list' object has no attribute 'replace'
Saravananslb commented 3 years ago

Hi All,

We have updated our pygoogletranslation package to overcome the below issue,

1) bulk translation list error. 2) Character limit for old translation accept character upto 5000 but this package support unlimited translation 😛

For more information, visit our official pygoogletranslation page or GitHub page

Thanks, Saravananslb

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

ThePhilgrim commented 3 years ago

I had the same issue on 4.0.0-rc1.

To solve it, I reverted back to 3.1.0a0: python3 -m pip uninstall googletrans==4.0.0-rc1

python3 -m pip install googletrans==3.1.0a0

The following code works:

from googletrans import Translator

translate_list = ['This is a test', 'to see if this', 'works or if I', 'have to continue working']

translator = Translator()
translations = translator.translate(translate_list, dest='sv')

for translation in translations:
    print(translation.text)
stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Nirzak commented 3 years ago

Same issue here.. trying to bulk translations.

swaansverhuur commented 3 years ago

same

ghost commented 3 years ago

same

HRaf commented 2 years ago

same issue 😒