Language Translation using Python with Google APIs

Python course with 57 real-time projects - Learn Python

To master a language one should start creating mini projects on it and practically use the language. Here we are going to create yet another interesting project using Python. We are creating Python Language Translation project so let’s get started.

A language translator is a very handy application that helps us to communicate in different languages by translating our language to the desired language. In earlier times, when there were no Language Translation Applications, it was very difficult for people to communicate with people coming from different parts of the world. Today we can create our own language translation project using python. Let’s see how.

About Python Language Translation Project

Our objective is to create a Language Translator which would help us translate a word, sentence or even a paragraph to another language. We will try to incorporate as many languages as possible. We will be using Tkinter Module to build our GUI for the project and googletrans library to present us with a number of languages that are a part of it. Also, we will use the TextBlob library for processing textual data.

Project Prerequisites

This project requires a basic understanding of python and the Tkinter module.

To proceed with the project one needs to install the Tkinter Module, Textblob, and Googletrans library using the following commands.

  • pip install tk
  • pip install -U textblob
  • pip install googletrans

Project Structure

Let us discuss the steps we will be taking to proceed with the Python Language Translation project.

  1. Importing the required modules and libraries.
  2. Adding and importing the languages to the project.
  3. Creating the window.
  4. Adding frame, button, text area, and language drop-down to the window.
  5. Creating a Translate() function.
  6. Call the main command to run the window.

Download Python Language Translation Project

Click here to download the full source code of the python language translation project from the following link: Python Language Translation Project

Steps to Proceed with the Python Language Translation Project

Let’s discuss the steps to create the Language Translation Project-

1. Importing Required Modules and Libraries:

from tkinter import *
from tkinter import ttk,messagebox
import googletrans
import textblob
  • Tkinter Module – This is the module to create easy GUI in Python.
  • Messagebox – This is for displaying a message box.
  • TextBlob – This is for analysing and processing textual data.
  • Googletrans – This is for importing a number of languages that we will be using during the project to translate from one to another.

2. Adding Languages to the Project:

language=googletrans.LANGUAGES
lang_value=list(language.values())
lang1=language.keys()
  • This adds different languages from the googletrans library to the language variable.
  • Creating a list of values and adding it to the variable lang_value.

3. Creating a Window

window = Tk()
window.title("DataFlair Language Translation")
window.minsize(600,500)
window.maxsize(600,500)
  • Creating an empty window using Tk().
  • title() – is to give a desired title to the window.
  • minsize(),maxsize() – this is for giving the size to the window.

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("choose a language")

f1=Frame(window,bg='black',bd=4)
f1.place(x=100,y=100,width=150,height=150)

text1=Text(f1,font="Roboto 14",bg='white',relief=GROOVE,wrap=WORD)
text1.place(x=0,y=0,width=140,height=140)

combo2=ttk.Combobox(window,values=lang_value,state='r')
combo2.place(x=300,y=20)
combo2.set("choose a language")

f2=Frame(window,bg='black',bd=4)
f2.place(x=300,y=100,width=150,height=150)

text2=Text(f2,font="Roboto 14",bg='white',relief=GROOVE,wrap=WORD)
text2.place(x=0,y=0,width=140,height=140)

button = Button(window,text='Translate',font=('normal',15), bg='yellow', command=translate)
button.place(x=230,y=300)# button which when triggered performs translation
  • Combobox() – This is for creating a drop down list of languages in read state. We have created two drop down lists using combo1 and combo2. Set() function will set a desired text to the drop down list when it will be displayed on the window.
  • Frame() – This is for creating frames. We have created two black frames f1 and f2. After creating the frames we have added textarea to it.
  • Text() – This is for creating a text area over the frame. We can write over this text area.
  • Button() – This is for creating a button which when triggered will perform translation of the text. Command= translate gives the command that when a button is clicked then the translate function will be performed.
  • place() – the place function is for making the appropriate place on the window and placing it on the window.

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 i,j in language.items():
        if(j==c2):
         lan_=i
      words=words.translate(from_lang=lan,to=str(lan_))
      text2.delete(1.0,END)
      text2.insert(END,words)
except Exception as e:
   messagebox.showerror("try again")

We create a Translate() function to perform the main translation from one language to another language.

  • Create a global variable named language.
  • get() – this function is to get the value. We get the value of the languages the user has chosen and the text user has entered.
  • We use textblob to process the text entered and change the entered text one by one to another language. If a word is not present or some error occurs, messagebox is used to display an error saying ”try again”.

6. Main Command:

window.configure
window.mainloop()

mainloop() – this function initiates the program to display the window and output of what we have created.

Yay! We have successfully created a Python Language Translation Project. Now you can easily communicate in any language just by a click.

Python Language Translation Output

This is how our language translator will look after the completion of the project.

python language translation output

Summary

We have successfully created a Python Language Translation Project. We used Tkinter Library for creating an easy and effective GUI. We also used Googletrans library to import a set of languages that we will be using. Also, we learned how to use these and effectively create a Language Translator. Now we can easily communicate with anybody without even knowing their native language.

Did we exceed your expectations?
If Yes, share your valuable feedback on Google

follow dataflair on YouTube

11 Responses

  1. bunny says:

    how to solve “unexpected unindent” error in line except exception as e

    • DataFlair Team says:

      Mainly when the spaces are misplaced in the code the indentation error occurs .We can easily resolved the indentation error by having proper spaces in the code.

  2. belle says:

    The link to “download the full source code of the python language translation project” does not open, how to open?

    • DataFlair Team says:

      Below are the steps to open the folder
      Refresh the page and then download the folder again from the link.
      After downloading the folder extract it
      Within the extracted folder you will found the source code of the project.

  3. Kousuke says:

    I tried to execute this program. but, at first time it didn’t work properly.
    Then I revised some point of this code, which is the line “lan=words.detect_language() ” deleted.

    I would like to hear your opinion about affection for above change.

  4. Junaid says:

    It’s not working, just the window has open and I select the languages and after that I pressed translate button it does not translating just showing the message try again

  5. Rohini says:

    Error: ‘translate’ is not define
    How can I solve this

    • sai says:

      same problem

    • joost says:

      You first need to write the def translate() function after which you can write the code block that uses the ‘translate’ function. You first need to define translate before you can use it in your code. Currently, the code uses the translate function after which they define it, which won’t work

  6. devyanshi says:

    it doesnt give output

Leave a Reply

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