Python Text Processing Useful Resources

Python Text Processing - Text Translation



Text translation from one language to another is increasingly becoming common for various websites as they cater to an international audience. The python package which helps us do this is called translate.

This package can be installed by the following way. It provides translation for major languages.

pip3 install translate

Example - Translating a Sentence

Below is an example of translating a simple sentence from English to Spanish. The default from language being English.

main.py

from translate import Translator
translator= Translator(to_lang="es")
translation = translator.translate("Good Morning!")
print translation

Output

When we run the above program, we get the following output −

¡Buenos días!

Example - Translation Between Any Two Languages

If we have the need specify the from-language and the to-language, then we can specify it as in the below program.

main.py

from translate import Translator
translator= Translator(from_lang="es",to_lang="en")
translation = translator.translate("¡Buenos días!")
print(translation)

Output

When we run the above program, we get the following output −

Good Morning!
Advertisements