pythongoogle-translator-toolkit

translate() missing 1 required positional argument: 'text'


I want to translate a tweet from Bahasa Indonesia to English, so after I can get a tweet, I run this code:

from googletrans import Translator

tweet = tweet.text # This saves the twitter text
tw_inggris = Translator.translate(tweet, src='id', dest='en')

But I got this specific error:

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

Does everyone know what's the error, and how I can fix it?


Solution

  • This is because you're using the Translator class directly instead of creating an instance first. Calling the .translate() function on the class directly would consider the first parameter as self and the second one as text (hence the error you got). So you'll need to do something like this:

    from googletrans import Translator
    
    tweet = tweet.text
    translator = Translator()
    tw_inggris = translator.translate(tweet, src='id', dest='en')
    

    or for single use, you can simply do this:

    Translator().translate(tweet.text, src='ar', dest='en')