Python-Text Translation
Python – Text Translation
With websites targeting international audiences, the need to translate text from one language to another is becoming increasingly common. The Python package that helps us do this is called “translate.”
This package can be installed as follows. It provides translations for major languages.
pip install translate
The following example translates a simple English sentence into German. The default source language is English.
from translate import Translator
translator = Translator(to_lang="German")
translation = translator.translate("Good morning!")
print translation
After executing the above program, we will get the following output:
Guten Morgen!
Between any two languages
If we need to specify the source and target languages, we can do so using the following procedure.
from translate import Translator
translator = Translator(from_lang="German", to_lang="Spanish")
translation = translator.translate("Guten Morgen")
print translation
After executing the above program, we will get the following output:
Buenos días