py-googletrans: googletrans 4.0.0-rc1 fails with TypeError: 'NoneType' object is not iterable on client.py:222

Googletrans version:

  • 4.0.0rc1

I’m submitting a …

  • bug report
  • feature request

Current behavior:

python3
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul  8 2019, 19:29:22) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from googletrans import Translator
>>> translator = Translator()
>>> print(translator.translate("Author", src="en", dest="de").text)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "c:\Users\Max\AppData\Local\Programs\Python\Python37-32\lib\site-packages\googletrans\client.py", line 222, in translate
    translated_parts = list(map(lambda part: TranslatedPart(part[0], part[1] if len(part) >= 2 else []), parsed[1][0][0][5]))
TypeError: 'NoneType' object is not iterable

Expected behavior:

I expect the “Author” word in deuch

Steps to reproduce:

  • Install googletrans 4.0.0-rc1
  • Call python and make steps from “Current behavior”

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 4
  • Comments: 30 (3 by maintainers)

Most upvoted comments

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

Thank me later

I think I have the solution. Replace this line 222 of googletrans/client.py with this

try:
    translated_parts = list(map(lambda part: TranslatedPart(part[0], part[1] if len(part) >= 2 else []), parsed[1][0][0][5]))
except TypeError: # because of the gender-specific translate results
    translated_parts = [ TranslatedPart(parsed[1][0][1][0], [parsed[1][0][0][0], parsed[1][0][1][0]]) ]

Gender-specific translations now work.

>>> translator.translate("Author", src="en", dest="de").text
Autor

(This is my first comment on github, I don’t know how to put this code snippet in your pull request)

No one ever updated this thread, so I discovered this from stackoverflow, but this issue has already been solved. If you do “pip install googletrans==3.1.0a0” it will give you the first result of a gender-specific translate, no longer giving you this error.

Source: https://stackoverflow.com/questions/52455774/googletrans-stopped-working-with-error-nonetype-object-has-no-attribute-group

I think I have the solution. Replace this line 222 of googletrans/client.py with this

try:
    translated_parts = list(map(lambda part: TranslatedPart(part[0], part[1] if len(part) >= 2 else []), parsed[1][0][0][5]))
except TypeError: # because of the gender-specific translate results
    translated_parts = [ TranslatedPart(parsed[1][0][1][0], [parsed[1][0][0][0], parsed[1][0][1][0]]) ]

Gender-specific translations now work.

>>> translator.translate("Author", src="en", dest="de").text
Autor

(This is my first comment on github, I don’t know how to put this code snippet in your pull request)

Thanks for your reply, but this doesn’t work for me

Screen Shot 2020-12-13 at 5 25 21 AM

This probably is because of the gender-specific translate results. I will investigate and fix it soon.

Still an issue. Any gendered word causes this. Another example:

translator.translate(“white”, src=“en”, dest=“es”)

@noaho Thanks for giving me the raw response. I’ll fix it within a few days.

Hello! I have the same problem too. @ssut thank you very much for continuing with the project 😄 Regards.

@ahmedbr I’m still working on it. Give me a little time please.

@mr2rm You are more than welcome to work on this 😃

Hi, Still any solution to this issue? Can I work on this?