简体   繁体   中英

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"?

It's just a dictionary exported by the module.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM