Skip to content
Related Articles

Related Articles

How to make a Google Translation API using Python?

View Discussion
Improve Article
Save Article
  • Last Updated : 07 Aug, 2020
View Discussion
Improve Article
Save Article

Google Translate is a free multilingual translation service, based on statistical and neural machine translation, developed by Google. It is widely used to translate complete websites or webpages from one languages to another. 

We will be creating a python terminal application which will take the source language, target language, a phrase to translate and return translated text. We will be implementing unit testing and web scraping techniques with selenium in python. Web scrapping is a concept of capturing the required data from a website. Selenium is an industry grade library used for web-scraping and unit testing of various software. As a prerequisite, we will be requiring the following tools to be installed in our system.

  • Python 3.x: A version of python 3.0 or above should be installed.
  • Selenium library: A python library required for scrapping the websites. Copy the following statement to install selenium on your system.
    Installation: python3 -m pip install selenium
  • Webdriver: An instance of a web browser required by selenium to open webpages. Download the latest version of Chrome Webdriver from the link below and save it in the same folder in which your main program is. 

We will divide the code section into three portions:

  • Setting up the selenium and chrome webdriver tool.
  • Taking input and testing for error in input.
  • Translating using Google Translate.

Part 1: Setting the selenium tool and webdriver settings.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import JavascriptException
# local variables
from import Options as ChromeOptions
chrome_op = ChromeOptions()
browser = webdriver.Chrome(executable_path ='chromedriver', options = chrome_op)

  • Importing webdriver object to connect to the chrome browser instance.
  • Importing keys library to connect the basic keyboard commands to the browser instance.
  • Importing exception handlers for browser instance.
  • Import browser options and set ‘–headless’ property to run the browser instance in background. Comment the ”chrome_op.add_argument(‘–headless’)” statement to bring the webdriver to foreground processes.

Part 2: Taking input and testing  for in input.

def takeInput():
    languages = {"English": 'en', "French": 'fr',
                 "Spanish": 'es', "German": 'de', "Italian": 'it'}
    print("Select a source and target language (enter codes)")
    print("Language", "   ", "Code")
    for x in languages:
        print(x, "   ", languages[x])
    print("\n\nSource: ", end ="")
    src = input()
    sflag = 0
    for x in languages:
        if(languages[x] == src and not sflag):
            sflag = 1
    if(not sflag):
        print("Source code not from the list, Exiting....")
    print("Target: ", end ="")
    trg = input()
    tflag = 0
    for x in languages:
        if(languages[x] == trg and not tflag):
            tflag = 1
    if(not tflag):
        print("Target code not from the list, Exiting....")
    if(src == trg):
        print("Source and Target cannot be same, Exiting...")
    print("Enter the phrase: ", end ="")
    phrase = input()
    return src, trg, phrase

This is a demo code so the languages code are kept limited to {English, Spanish, German, Italian, French}. You can add more languages and their codes later.

  • Taking input for source language and target language code.
  • Checking if the codes entered are supported or not.
  • Source language and target language code should not be same. 

Part 3: Translating using Google Translate:

def makeCall(url, script, default):
    response = default
        while(response == default):
            response = browser.execute_script(script)
    except JavascriptException:
    except NoSuchElementException:
    if(response != default):
        return response
        return 'Not Available'
def googleTranslate(src, trg, phrase):
    url = ' view = home&op = translate&sl =' + \
        src + '&tl =' + trg+'&text ='+phrase
    script = 'return document.getElementsByClassName("tlid-translation")[0].textContent'
    return makeCall(url, script, None)

  • googleTranslate() function receives the three parameters i.e. source code, target code and phrase. It generates the URL for the browser to request for. 
  • script contains a javascript statement which searches for an HTML element with class = “tlid-translation” and returns it’s text contents.
  • makeCall() function makes a request with the URL created, executes the script when the webpage is ready and returns the fetched text.

Combining the above three parts.

if __name__ == "__main__":
    src, trg, phrase = takeInput()
    print("\nResult: ", googleTranslate(src, trg, phrase))

Paste all the parts shown above in a single .py file and execute it using Python3.

Execution: python3 <>


Input section :

If you have commented the ‘–headless’ property statement, then a browser window like below, will appear:

The result will appear on the terminal window like below:

Note: This is demo project so language supported  are limited. You can increase the language support by adding more language codes in the declaration.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!