How to use Google Translate API with python

Ohidur Rahman
1 min readMar 30, 2020

We all are very familiar with Google Translate. This tutorial can be really helpful saving a lot of time when we need to do translation task in bulk or probably doing some creative work with Google Translate.

First thing first, we need to install the Unofficial Google Translate Library for python. We can do so with this little pip command.

pip install googletrans

Okay Now import the library and initialize it

from googletrans import Translatortranslator = Translator()

We can also initialize with local Translate Service urls of Google Translate also. In that case all we have to do is utitlize the constructor method as below:

translator = Translator(service_urls=['translate.google.com','translate.google.co.kr',])

Now use the following command simply to translate a line:

translated=translator.translate(“I am a programmer”,dest=’bn’)
print(translated.origin)
print(translated.text)

Where dest is the destination language code .

Instead, we can pass a list of string and iterate over the result with loop. Below is a code listing that takes an input file and translate them line by line and saves the output as a text file.

You can find all of the code used in this tutorial in this github repo: https://github.com/ohidurbappy/python

Thanks for reading.

--

--