Search code examples
pythongoogle-translategoogle-translation-api

Get the name of language instead of language code in Python Googletrans


I'm using the googletrans package in python to make a translator. The documentation

At the bottom of the documentation page, there seems to be a large dictionary thing of all the languages and their corresponding codes.

Here's my code to make a basic translator to translate to English:

totranslate = translator.translate(totranslate)
sourcelang = totranslate.src
totranslate = totranslate.text
print('Translated to English from ' + sourcelang + ': ' + totranslate)

This is what is outputted when I input something like "hola":

Translated to English from es: hello

es is the language code in the documentation for Spanish. In the documentation, the list is called googletrans.LANGUAGES.

How can I make it so that instead of saying "Translated to English from es", I make it say "Translated to English from Spanish"?


Solution

  • It's just a dictionary exported by the module.

    import googletrans
    ...
    sourcelang = googletrans.LANGUAGES[totranslate.src]