Python Text Translation

Python Text Translation

Translating text from one language to another is increasingly common for websites catering to international audiences. The Python package that helps us achieve this is called translate.

This package can be installed as follows. It provides translation capabilities for several major languages.

pip install translate

Below is an example that translates a simple sentence from English to German. The default source language is 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 need to specify the source and target languages, we can do so in the following 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

Leave a Reply

Your email address will not be published. Required fields are marked *