-1

I am trying to run a very simple Python code that is supposed to translate a word from French to English. The code is as follows.

from googletrans import Translator
translate = Translator()
result = Translator.translate(text='Bonjour', src='fr', dest='en')
print(result.text)

When I run the code from within the Pycharm Editor (using Python 3), it produces an error:

TypeError: translate() missing 1 required positional argument: 'self'

What is wrong here?

niamulbengali
  • 292
  • 1
  • 3
  • 16
Malte
  • 21
  • 2
  • 2
    I think you need to replace `Translator.translate(...)` with `translate.translate(...)`. Currently you are calling `.translate` with the Class, instead you need to call from *object* of the class – Moinuddin Quadri Dec 31 '20 at 20:46
  • Thanks for your help. I tried your suggestion, and I get a new error instead: AttributeError: 'NoneType' object has no attribute 'group' – Malte Dec 31 '20 at 20:50
  • Does this answer your question? [TypeError: Missing 1 required positional argument: 'self'](https://stackoverflow.com/questions/17534345/typeerror-missing-1-required-positional-argument-self) – mkrieger1 Dec 31 '20 at 21:05
  • For your second error see [googletrans stopped working with error 'NoneType' object has no attribute 'group'](https://stackoverflow.com/a/52456197/8106583) – wuerfelfreak Dec 31 '20 at 21:10

2 Answers2

2

The problem is that in Translator.translate( the first t is capitalized an or instead of an e is appended.

As you can read in the docs translate = Translator() creates a Translator instance.

.translate() is then a method of your variable/instance translate and not of the Class Translator

Capitalization matters.

Complete code:

from googletrans import Translator
translate = Translator()
result = translate.translate(text='Bonjour', src='fr', dest='en')
print(result.text)
wuerfelfreak
  • 2,363
  • 1
  • 14
  • 29
  • 1
    The problem is also that in `Translator` the `e` is misspelled as `or`. – mkrieger1 Dec 31 '20 at 20:58
  • Yes indeed. I added your suggestion. Thankyou! – wuerfelfreak Dec 31 '20 at 21:01
  • Your code suggestion from above produces this new error: AttributeError: 'NoneType' object has no attribute 'group' – Malte Dec 31 '20 at 21:10
  • Can verify. This seems to be some kind of bug in the `googletrans`-package. See [googletrans stopped working with error 'NoneType' object has no attribute 'group'](https://stackoverflow.com/a/52456197/8106583) As of the documentation it should be working. Good luck. – wuerfelfreak Dec 31 '20 at 21:12
  • 2
    Installing this specific version worked for me `pip install googletrans==3.1.0a0` Try it – wuerfelfreak Dec 31 '20 at 21:13
1

Now it works -- with google_trans_new:

from google_trans_new import google_translator
translate = google_translator()
result = translate.translate(text='Bonjour', lang_tgt = 'en')
print(result)

Thank you very much for your advice.

Malte
  • 21
  • 2
  • Happy the problem is solved. But please don't reply to answers with another answer. Just [accept and upvote](https://stackoverflow.com/help/someone-answers) the best answer . – wuerfelfreak Dec 31 '20 at 22:35