Language Translator Using Google API in Python

A language translator is a helpful tool that enables us to communicate in other languages by translating our native tongue into the target tongue. In the past, it took a lot of work to speak with people from other countries before the invention of language translation software. Today, Python allows us to build our own language translation projects. Let's see how it works.

Fundamental knowledge of Python and the Tkinter module is necessary for this.

The Tkinter Module, Textblob, and Googletrans libraries must be installed using the following commands to establish a language translator.

pip install tk

pip install -U textblob

pip install googletrans

Steps to Create the Python Language Translation

1. Importing Required Modules and Libraries:

from tkinter import *

from tkinter import ttk, messagebox

import googletrans

import textblob
  • Tkinter Module: This package allows effortless Python GUI creation.
  • Messagebox: A message box will be displayed using this.
  • TextBlob: Using this, textual data may be analyzed and processed.
  • Googletrans: To translate from one language to another, we will be importing a variety of languages.

2. Adding and Importing the Languages:

/prlanguage=googletrans.LANGUAGES

lang_value=list(language.values())

lang1=language.keys()
  • By doing this, the language variable is expanded to include more languages from the googletrans library.
  • Making a list of values and adding it to the lang_value variable.

3. Creating a Window

window = Tk()

window.title("Language Translation")

window.minsize(800,700)

window.maxsize(800,700)
  • Tk() is used to create a blank window.
  • Give the window the desired title using the title() function.
  • The window's size can be set using the functions minsize() and maxsize().

4. Adding frame, button, text area, and language drop-down to the window:

combo1=ttk.Combobox(window,values=lang_value,state='r')

combo1.place(x=100,y=20)

combo1.set("select a language")

f1=Frame(window,bg='blue',bd=4)

f1.place(x=100,y=100,width=150,height=150)

text1=Text(f1,font="Arial",bg='pink',relief=GROOVE,wrap=WORD)

text1.place(x=0,y=0,width=160,height=160)

combo2=ttk.Combobox(window,values=lang_value,state='r')

combo2.place(x=400,y=40)

combo2.set("select a language")

f2=Frame(window,bg='blue',bd=4)

f2.place(x=310,y=100,width=170,height=170)

text2=Text(f2,font="Arail",bg='pink',relief=GROOVE,wrap=WORD)

text2.place(x=0,y=0,width=160,height=160)

button = Button(window,text='Translate',font=('normal',15), bg='yellow', command=translate)

button.place(x=240,y=320)# button which when triggered, performs translation
  • Combobox() - This will create a drop-down menu of languages in the read state. Utilizing combos 1 and 2, we produced two drop-down lists. When a drop-down list is presented on a window, the Set() function will add the desired text to the list.
  • Frame() - To create frames, use this. Two black frames numbered f1 and f2, have been produced. The text area was added to it after the frames were made.
  • Text() – This creates a text area above the frame. We can write in this text area.
  • Button() - To translate the text, this is for making a button that, when pressed, will do it. When a button is clicked with the command "translate," the translate function is carried out.
  • Place () - The place function is used to create the proper location on the window and place it there.

5. Create Translate() function:

def translate():

 global language

 try:

    txt=text1.get(1.0,END)

    c1=combo1.get()

    c2=combo2.get()

    if(txt):

      words=textblob.TextBlob(txt)

      lan=words.detect_language()

      for x,y in language.items():

        if(y==c2):

         lan_=x

      words=words.translate(from_lang=lan,to=str(lan_))

      text2.delete(1.0,END)

      text2.insert(END,words)

except Exception as e:

   messagebox.showerror("Let's give another try")

To carry out the primary language translation from one to another, we develop the Translate() function.

  • Create a global variable named language.
  • get() - Use this function to obtain the value. In addition to the text the user typed, we also received the value of the selected languages.
  • To translate the entered text one at a time into another language, we utilize textblob to process the text entered. If a word is missing or some other mistake happens, a messagebox displays an error saying, "Let's give it another try".

6. Main Command:

window.configure

window.mainloop()
  • mainloop() - This function runs the program and causes it to display the output window that we generated.

These are the steps used to create a Python Language Translator using the Google API. After executing this code, one can easily communicate with anybody in any language in no time.

NO Output?