Google Translator in Python

Google Translator in Python: googletrans is a free and unlimited python library that implemented Google Translate API. This uses the Google Translate Ajax API to make calls to such methods as detect and translate.

Installation

pip install googletrans

Code

Example #1: Printing available languages

# print available languages with code (key)
import googletrans 
print(googletrans.LANGUAGES)

#Follow @code_snail

Output:

google translator in python

In the output, we can see the supported languages.

Example #2: Python googletrans simple translation

The translation is done with the Translator's translate() method.

# simple translate, by default english

from googletrans import Translator
translator=Translator()
translated=translator.translate('दैनिक कोडिंग सामग्री के लिए @code_snail का पालन करें')
print(translated.text)

#Follow @code_snail

If we do not specify the source and the destination languages, googletrans tries to detect the language and translates it into English.

translated=translator.translate('दैनिक कोडिंग सामग्री के लिए @code_snail का पालन करें')

We translate a Hindi text. We get a tranlated object.

print(translated.text)

To get the translation, we print the text field of the translated object.

Output:

google translate in python

Example #3: Translate between two language

In the following example, we specify the source and the destination languages.

# translate between two language

from googletrans import Translator
translator = Translator()

# greek to english
translated = translator.translate('ακολουθήστε τα όνειρά σας, αλλά πρώτα ακολουθήστε @code_snail', src='el', dest='en')
print(translated.text)

#Follow @code_snail

The example translates a Greek text into English.

translated = translator.translate('ακολουθήστε τα όνειρά σας, αλλά πρώτα ακολουθήστε @code_snail', src='el', dest='en')

The source language is specified with the src option and the destination with the dest option.

Output:

google translator in python

Example #4: Detect language and translate

# detect language and translate

from googletrans import Translator
text1 = "pentru codificare zilnică, sfaturi, trucuri, fapte, urmează @code_snail"

translator = Translator()

dt1 = translator.detect(text1)
print(dt1)

translated = translator.translate(text1, src=dt1.lang, dest='en')
print(translated.text)

#Follow @code_snail

In the examples, we have two different texts. We use the detect method to determine the lanugage of the text.

dt1 = translator.detect(text1)
print( dt1.lang)

We determine the language of the text with detect() and print the result to the console. The method prints the language and the confidence value, which is the probability of the correct guess of the language. After that we translate.

Output:

google translator in python

Read more about googletrans module: https://pypi.org/project/googletrans/

I hope you like this Google translator in python code. Must try and share it with your friends. For more codes click the bell icon on the left bottom corner.

Also see,