Error using googletrans api in python translate() missing 1 required positional argument: 'self'

Question:

I want to translate some text using googletrans.
This is my code:

inputtext = "Ich mag Schokolade"

srclang = "de"
dstlang = "en"

translation = Translator.translate(text=inputtext, src=srclang, dest=dstlang)

But when I run it, this Error comes up:

translate() missing 1 required positional argument: ‘self’

Asked By: Daniel Lehmann

||

Answers:

You need to initialize an instance of Translator:

translator = Translator()
inputtext = "Ich mag Schokolade"

srclang = "de"
dstlang = "en"

translation = translator.translate(text=inputtext, src=srclang, dest=dstlang)

translate(...) is not a static method of Translator so you need to call it on an instance rather than class.

Answered By: Yevhen Kuzmovych