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.

12 Responses

  1. Raju kumar says:

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

  2. Manasa says:

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

    • Gurukul Team says:

      Make sure you have installed all the prerequisites. If not here are the commands:
      pip install tkinter
      pip install googletrans

  3. BlueBerry says:

    Sir I am getting attribute error : ‘None type’ object has no attribute group

    • Gurukul Team says:

      This is because of changes in google translation API.Please run following command in cmd prompt / terminal:
      pip install googletrans==3.1.0a0

  4. Nicky says:

    Facing this issue–
    Traceback (most recent call last):
    File “C:\Users\Lenovo\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py”, line 1883, in __call__
    return self.func(*args)
    File “C:/pythonprojects/translatorr.py”, line 43, in getvalues
    text = t.run(txt=msg, src=s, dest=d)
    File “C:/pythonprojects/translatorr.py”, line 28, in run
    self.translated = self.trans.translate(self.txt)
    File “C:\pythonprojects\venv\lib\site-packages\googletrans\client.py”, line 182, in translate
    data = self._translate(text, dest, src, kwargs)
    File “C:\pythonprojects\venv\lib\site-packages\googletrans\client.py”, line 78, in _translate
    token = self.token_acquirer.do(text)
    File “C:\pythonprojects\venv\lib\site-packages\googletrans\gtoken.py”, line 194, in do
    self._update()
    File “C:\pythonprojects\venv\lib\site-packages\googletrans\gtoken.py”, line 62, in _update
    code = self.RE_TKK.search(r.text).group(1).replace(‘var ‘, ”)
    AttributeError: ‘NoneType’ object has no attribute ‘group’

    • Gurukul Team says:

      Please install this alpha version of googletrans to fix the issue, command:
      pip install googletrans==3.1.0a0

  5. R K says:

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

    root = Tk()
    root.geometry(‘1080×400′)
    root.resizable(0, 0)
    root.title(“Python Project | Language Translator”)
    root.config(bg=’ghost white’)

    # heading
    Label(root, text=”Where Communication Is The Key”, font=”arial 20 bold”, bg=’white smoke’).pack()
    Label(root, text=”Language Translator”, font=’arial 20 bold ‘, bg=’white smoke’, width=’20’).pack(side=’bottom’)

    # INPUT AND 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)

    ##################
    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’)

    ######## 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)

    ########## 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()

    below is the error: Please solve this

    Traceback (most recent call last):
    File “C:\Users\tahir\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py”, line 1883, in __call__
    return self.func(*args)
    File “C:/Users/tahir/PycharmProjects/firstProject/main.py”, line 43, in Translate
    Output_text.insert(END, translated.text)
    AttributeError: ‘NoneType’ object has no attribute ‘text’

  6. UD says:

    The code was executed successfully.
    Thank you for mentioning about the google trans update !

Leave a Reply

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