Python – 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.

pip install translate

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

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

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

>Guten Morgen!

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.

from translate import Translator
translator= Translator(from_lang="german",to_lang="spanish")
translation = translator.translate("Guten Morgen")
print translation

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

>Buenos días