How to Create Language Translator in Python using Google APIs

Language Translator using Google Translate APIs in Python – Instantly Translate texts, words, phrases from one language to another.

Translation enables communication between people from different regions. It provides meaningful communication from one language to another language.

About Language Translator

A language translator or text translator is a tool to translate text, words, phrases from one language to any other language. It is like a dictionary where we can translate the text.

Language Translator Python Project

python language translator

The objective of this project is to translate text from one language to any other language in real-time with a button click. This project will be built using the Tkinter, googletrans libraries.

In this project, the user enters text in any language and get it translated in any other language by selecting the output language.

Project Prerequisites

To implement this project, we will use the basic concepts of Python, Tkinter, and googletrans libraries.

Tkinter is a standard GUI Python library. ttk module gives access to the Tk themed widget set.

googletrans is a module to translate text. We import the Translator from googletrans, which is used to do translations. We also import LANGUAGES from googletrans which lists all supported languages in a Python dictionary.

To install the library, use pip install to the command prompt:

pip install tkinter
pip install googletrans

Download Language Translator Python Project Code

Please download the source code of Language Translation Project: Python Language Translator

Steps to build the Text Translator Python Project:

  • Import required modules
  • Create a display window
  • Create input and output text widget
  • Define Combobox to select a language
  • Define function
  • Create a translate button

1. Import Modules

from tkinter import *
from tkinter import ttk
from googletrans import Translator, LANGUAGES

We import ttk modules from tkinter library and Translator, LANGUAGES modules from googletrans library.

2. Create a display window

root = Tk()
root.geometry('1080x400')

root.resizable(0,0)
root.config(bg = 'ghost white')

We use tkinter library to create a window where we’ll enter the text which we want to convert into voice.

  • Tk() initialized tkinter which means window created
  • geometry() set the width and height of the window
  • resizable(0,0) set the fixed size of the window
  • bg = ‘’ use to set the background color
root.title("Project Gurukul--Language Translator")

Label(root, text = "LANGUAGE TRANSLATOR", font = "arial 20 bold", bg='white smoke').pack()

Label(root,text ="Project Gurukul", font = 'arial 15 bold', bg ='white smoke' , width = '20').pack(side = 'bottom')
  • title() used to set the title of the window
  • Label() widget use to display one or more than one line of text that users aren’t able to modify.
    • root is the name which we refer to our window
    • text which we display on the label
    • font in which the text is written
    • pack organized widget in block

3. Create an Input-output text widget

Label(root,text ="Enter Text", font = 'arial 13 bold', bg ='white smoke').place(x=200,y=60)

Input_text = Text(root,font = 'arial 10', height = 11, wrap = WORD, padx=5, pady=5, width = 60)
Input_text.place(x=30,y = 100)

Label(root,text ="Output", font = 'arial 13 bold', bg ='white smoke').place(x=780,y=60)

Output_text = Text(root,font = 'arial 10', height = 11, wrap = WORD, padx=5, pady= 5, width =60)
Output_text.place(x = 600 , y = 100)

The above code creates two text widgets one for entering text and the other for displaying translated text.

  • Text() widget is used for multiple text lines.
  • wrap = WORD will stop the line after the last word that will fit.
  • padx puts an extra bit of space to the left and right of the widget.
  • pady adds an extra bit of space to the top and bottom.

4. Define Combobox to select the language

language = list(LANGUAGES.values())

src_lang = ttk.Combobox(root, values= language, width =22)
src_lang.place(x=20,y=60)
src_lang.set('choose input language')

dest_lang = ttk.Combobox(root, values= language, width =22)
dest_lang.place(x=890,y=60)
dest_lang.set('choose output language')

From the above code, users can pick a seperate language for both input data and to translate their data.

  • language gets all the values from the ‘LANGUAGES’ dictionary in the form of a list.
  • ttk.Combobox() widget is a class of ttk modules. It is a drop-down list, which can hold multi-value and show one item at a time. Combobox is useful to select one option from many option.

5. Define Function

def Translate():
    translator = Translator()
    translated=translator.translate(text= Input_text.get(1.0, END) , src = src_lang.get(), dest = dest_lang.get())

    Output_text.delete(1.0, END)
    Output_text.insert(END, translated.text)

The Translate function will translate the message and give the output.

  • src gets the language selected as input text language
  • dest gets the language select to translate
  • text gets the input text entered by the user.”1.0″ means that the input should be read from zero characters to line one
  • The END part means to read the text until the end is reached
  • translator = Translator() used to create a Translator class object
  • Output_text.delete(1.0, END) delete all the text from line one to end
  • Output_text.insert (END, translated.text) will insert the translated text in Output_text

6. Create a translate button

trans_btn = Button(root, text = 'Translate',font = 'arial 12 bold',pady = 5,command = Translate , bg = 'royal blue1', activebackground = 'sky blue')

trans_btn.place(x = 490, y= 180 )

root.mainloop()

When we click on the Translate button it will call the translate function

Button() widget used to display button on our window

  • command is called when we click the button
  • activebackground sets the background color to use when the button is active

root.mainloop() is a method that executes when we want to run our program.

Text Translation Project Output

text translation project output

Summary

We have successfully developed the Language Translator python project. We used the popular tkinter library for rendering graphics on a display window, googletrans library to translate text from one language to another.

We learned how to translate text, how to create Combobox, buttons widget, and pass the function to the button. In this way, we build a Language Translator. I hope you enjoyed building this python project.

2 Responses

  1. Raju kumar says:

    good morning sir ,
    sir,this code is not running on pycharm id

Leave a Reply

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