Site icon DataFlair

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.

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

2. Adding Languages to the Project:

language=googletrans.LANGUAGES
lang_value=list(language.values())
lang1=language.keys()

3. Creating a Window

window = Tk()
window.title("DataFlair Language Translation")
window.minsize(600,500)
window.maxsize(600,500)

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

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.

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.

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.

Exit mobile version