Nikhil Mathur
Nikhil Mathur

Reputation: 21

google translate library is printing <googletrans.models.Translated at 0x1eaf9bfd198> instead of text

I have used google translate library to convert languages and it is running properly but in output it is not showing the name of language and it showing some code .

from googletrans import Translator
translator = Translator()
translator.translate('안녕하세요.')

<googletrans.models.Translated at 0x1eaf9bfd198>

Upvotes: 1

Views: 4289

Answers (3)

Sudhanva Pathak
Sudhanva Pathak

Reputation: 1

I also had encountered this problem.

Just assign any variable to the function translator.translate, such as:

k = translator.translate('안녕하세요.')

and print k only. This worked for me. You can also give it a try.

Upvotes: 0

Paul Rooney
Paul Rooney

Reputation: 21609

Just use the text property to see the translated text.

>>> from googletrans import Translator
>>> translator = Translator()
>>> result = translator.translate('안녕하세요.')
>>> result.text
'Hi.'

To see destination and source languages use the dest and src properties.

>>> result.src
'ko'
>>> result.dest
'en'

Seems like googletrans.models.Translated does not override the __repr__ function and so shows the default (which displays its address in memory). The docs indicate it should show the fields, but for me it doesn't. So the docs are either too old or too new.

You can introspect the properties of the Translated object using dir, if you want to see whats available to you. Look at the properties that don't have the double underscores at each end.

>>> dir(result)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__unicode__', '__weakref__', 'dest', 'extra_data', 'origin', 'pronunciation', 'src', 'text']

Upvotes: 3

Vasu Deo.S
Vasu Deo.S

Reputation: 1850

Replace

translator.translate('안녕하세요.')

By

translator.detect('안녕하세요.').lang

Upvotes: 1

Related Questions