How to Create Language Translator in Python using Google APIs

FREE Online Courses: Elevate Your Skills, Zero Cost Attached - Enroll Now!

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.

33 Responses

  1. Dhyanam Shah says:

    It is not showing any error but It is not showing the output as well

  2. malinga says:

    Exception in Tkinter callback
    Traceback (most recent call last):
    File “C:\Users\malin\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py”, line 1892, in __call__
    return self.func(*args)
    File “C:\Users\malin\Downloads\python-language-translator\python-language-translator.py”, line 43, in Translate
    translated=translator.translate(text= Input_text.get(1.0, END) , src = src_lang.get(), dest = dest_lang.get())
    File “C:\Users\malin\AppData\Local\Programs\Python\Python39\lib\site-packages\googletrans\client.py”, line 182, in translate
    data = self._translate(text, dest, src, kwargs)
    File “C:\Users\malin\AppData\Local\Programs\Python\Python39\lib\site-packages\googletrans\client.py”, line 78, in _translate
    token = self.token_acquirer.do(text)
    File “C:\Users\malin\AppData\Local\Programs\Python\Python39\lib\site-packages\googletrans\gtoken.py”, line 194, in do
    self._update()
    File “C:\Users\malin\AppData\Local\Programs\Python\Python39\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’

    sir i’m getting this error can uh reslove this

  3. Almog Beni says:

    For those of you who having trouble with this ERROR: ‘AttributeError: ‘NoneType’ object has no attribute ‘group’
    *This is how you fix it* : pip install googletrans == 3.1.0a0

    And more over, this is the correct code for 1080×400 to preview all the elements:
    # Improting the good stuff.
    from tkinter import ttk
    from tkinter import *
    from googletrans import Translator, LANGUAGES

    # * Creating the display window and environment.
    root = Tk()
    root.geometry(“1080×400”)
    root.resizable(0,0)
    root.config(bg = “ghost white”)
    root.title(“Language Translator”)
    Label(root, text = “Language Translator”, font = “Georgia 24 bold”, bg = “white smoke”).pack()
    Label(root,text =”Made By – (Enter your name”), font = ‘Georgia 15 bold’, bg =’white smoke’ , width = ’20’).pack(side = ‘bottom’)

    # – Creating the input/output text.
    Label(root, text = “Input”, font = ‘Georgia 15 bold’, bg = ‘white smoke’).place(x = 20, y = 60)
    input = Text(root, font = ‘Georgia 12’, height = 11, wrap = WORD, padx = 5, pady = 5, width = 60)
    input.place(x = 30, y = 100)
    Label(root, text = “Output”, font = ‘Georgia 15 bold’, bg = ‘white smoke’).place(x = 795, y = 60)
    output = Text(root, font = ‘Georgia 12’, height = 11, wrap = WORD, padx = 5, pady = 5, width = 60)
    output.place(x = 540, y = 100)

    # ! Defining the language selection.
    language = list(LANGUAGES.values())
    src_lang = ttk.Combobox(root, values = language, width = 22)
    src_lang.place(x = 100, y = 65)
    src_lang.set(‘Choose input language’)
    dest_lang = ttk.Combobox(root, values = language, width = 22)
    dest_lang.place(x = 890, y = 65)
    dest_lang.set(‘Choose output language’)

    # / Define function.
    def Translate():
    translator = Translator()
    translated=translator.translate(text= input.get(1.0, END) , src = src_lang.get(), dest = dest_lang.get())
    output.delete(1.0, END)
    output.insert(END, translated.text)

    # . Create the translate button.
    translate_btn = Button(root, text = ‘Translate’, font = ‘Georgia 12 bold’, pady = 5, command = Translate, bg = ‘white smoke’, activebackground = ‘white smoke’)
    translate_btn.place(x = 490, y = 300)

    # : Run main loop.
    root.mainloop()

    • subhrasmita das says:

      hey
      I am also getting the same issue but not in my system but while running it in any other system i am getting this none type error ,though updated it with this version but still the same but the problem is it is not showing any error to my code while running it in my system .How to resolve this ?? can you suggest

  4. Sejal says:

    sir,this code is not running on pycharm id

  5. vidhyalakshmi says:

    sir , when i run the pgm, getting the following error , may you correct this??????

    AttributeError: ‘NoneType’ object has no attribute ‘group’

  6. vidhyalakshmi says:

    sir , when i run the pgm, getting the following error , may you correct this??????
    C:\Users\UGVL\PycharmProjects\untitled\venv\Scripts\python.exe C:/Users/UGVL/PycharmProjects/vidhya/venv/Scripts/transtkin.py
    Exception in Tkinter callback
    Traceback (most recent call last):
    File “C:\Users\UGVL\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py”, line 1892, in __call__
    return self.func(*args)
    File “C:/Users/UGVL/PycharmProjects/vidhya/venv/Scripts/transtkin.py”, line 46, in Translate
    translated=translator.translate(text= Input_text.get(1.0, END) , src = src_lang.get(), dest = dest_lang.get())
    File “C:\Users\UGVL\PycharmProjects\untitled\venv\lib\site-packages\googletrans\client.py”, line 182, in translate
    data = self._translate(text, dest, src, kwargs)
    File “C:\Users\UGVL\PycharmProjects\untitled\venv\lib\site-packages\googletrans\client.py”, line 78, in _translate
    token = self.token_acquirer.do(text)
    File “C:\Users\UGVL\PycharmProjects\untitled\venv\lib\site-packages\googletrans\gtoken.py”, line 194, in do
    self._update()
    File “C:\Users\UGVL\PycharmProjects\untitled\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’

  7. C. Ushitha says:

    Sir, What are advantages,existing system and proposed system for this project

  8. safi says:

    I have used the command :
    pip install googletrans==3.1.0a0

    I still keep getting these:

    Exception in Tkinter callback
    Traceback (most recent call last):
    File “C:\Users\Safi\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py”, line 1892, in __call__
    return self.func(*args)
    File “C:\Users\Safi\PycharmProjects\pythonProject3\main.py”, line 40, in Translate
    translated = translator.translate(text=Input_text.get(1.0, END), src=src_lang.get(), dest=dest_lang.get())
    File “C:\Users\Safi\PycharmProjects\pythonProject3\venv\lib\site-packages\googletrans\client.py”, line 182, in translate
    data = self._translate(text, dest, src, kwargs)
    File “C:\Users\Safi\PycharmProjects\pythonProject3\venv\lib\site-packages\googletrans\client.py”, line 78, in _translate
    token = self.token_acquirer.do(text)
    File “C:\Users\Safi\PycharmProjects\pythonProject3\venv\lib\site-packages\googletrans\gtoken.py”, line 194, in do
    self._update()
    File “C:\Users\Safi\PycharmProjects\pythonProject3\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’

  9. Suhasini says:

    Need report of this project.

  10. chintu says:

    This error ocuring
    Exception in Tkinter callback
    Traceback (most recent call last):
    File “C:\ProgramData\Anaconda3\lib\tkinter\__init__.py”, line 1705, in __call__
    return self.func(*args)
    File “”, line 31, in Translate
    translated=translator.translate(text= Input_text.get(1.0, END) , src = src_lang.get(), dest = dest_lang.get())
    File “C:\ProgramData\Anaconda3\lib\site-packages\googletrans\client.py”, line 182, in translate
    “””
    File “C:\ProgramData\Anaconda3\lib\site-packages\googletrans\client.py”, line 78, in _translate
    File “C:\ProgramData\Anaconda3\lib\site-packages\googletrans\gtoken.py”, line 194, in do
    a %= 1000000 # int(1E6)
    File “C:\ProgramData\Anaconda3\lib\site-packages\googletrans\gtoken.py”, line 62, in _update
    # this will be the same as python code after stripping out a reserved word ‘var’
    AttributeError: ‘NoneType’ object has no attribute ‘group’

  11. Oiver North says:

    Trans_btn.place(x=490, y=180) not defined please help

  12. m.asadi says:

    C:\WINDOWS\system32>pip install tkinter
    ERROR: Could not find a version that satisfies the requirement tkinter
    ERROR: No matching distribution found for tkinter

  13. UD says:

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

  14. Ashwani Kumar Meena says:

    sir, it is not showing the translate button.

    code: –

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

    root = Tk()
    root.geometry(‘1080×400’)
    root.resizable(0,0)
    root.config(bg = ‘ghost white’)

    root.title(“PROJECT DOODLE—LANGUAGE TRANSLATOR”)
    Label(root, text = “DOODLE THE TRANSLATOR”, font = “Courier 20 bold”, bg=’white smoke’).pack()
    Label(root,text =”TopTextTranslations”, font = ‘Courier 15 bold’, bg =’white smoke’ , width = ’20’).pack(side = ‘bottom’)

    Label(root,text =”Enter Text”, font = ‘courier 13 bold’, bg =’white smoke’).place(x=200,y=60)
    Input_text = Text(root,font = ‘courier 10’, height = 11, wrap = WORD, padx=5, pady=5, width = 60)
    Input_text.place(x=30,y = 100)
    Label(root,text =”Output”, font = ‘courier 13 bold’, bg =’white smoke’).place(x=780,y=60)
    Output_text = Text(root,font = ‘courier 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’)

    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)

    trans_btn = Button(root, text = ‘Translate’,font = ‘forte 12 bold’,pady = 5,command = Translate , bg = ‘royal blue1’, activebackground = ‘sky blue’)
    trans_btn.place(x = 490, y= 180 )
    root.mainloop()

  15. 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’

  16. 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

  17. 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

      • chandrakant says:

        it works… thank you

      • safi says:

        I have used the command :
        pip install googletrans==3.1.0a0

        I still keep getting these:

        Exception in Tkinter callback
        Traceback (most recent call last):
        File “C:\Users\Safi\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py”, line 1892, in __call__
        return self.func(*args)
        File “C:\Users\Safi\PycharmProjects\pythonProject3\main.py”, line 40, in Translate
        translated = translator.translate(text=Input_text.get(1.0, END), src=src_lang.get(), dest=dest_lang.get())
        File “C:\Users\Safi\PycharmProjects\pythonProject3\venv\lib\site-packages\googletrans\client.py”, line 182, in translate
        data = self._translate(text, dest, src, kwargs)
        File “C:\Users\Safi\PycharmProjects\pythonProject3\venv\lib\site-packages\googletrans\client.py”, line 78, in _translate
        token = self.token_acquirer.do(text)
        File “C:\Users\Safi\PycharmProjects\pythonProject3\venv\lib\site-packages\googletrans\gtoken.py”, line 194, in do
        self._update()
        File “C:\Users\Safi\PycharmProjects\pythonProject3\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’

  18. 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

  19. Raju kumar says:

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

    • Gurukul Team says:

      Please post the issue you are facing, we will resolve the same

      • Vicky says:

        Sir, its showing an syntax error by mentioning the line…..

        dest_lang. set(‘choose output language’)

        • Gurukul Team says:

          It seems you have added space before calling the set function.
          Correct line: dest_lang.set(‘choose output language’)

          • Rahul Kumar Tiwari says:

            Traceback (most recent call last):
            File “C:/Users/admin/Desktop/many things/translator.py”, line 40, in
            trans_btn.place(x = 200, y= 100 )
            NameError: name ‘trans_btn’ is not defined

Leave a Reply

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