Create a project to Translate Languages in Python

Published on:

To master a language, one should start creating mini projects and practically use the language. Here we are going to create yet another exciting project using Python. We Created a project to Translate Languages in Python, so let’s get started.

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

About Python Language Translation Project

Our objective is to create a Language Translator (Translate Languages in Python) that would help us translate a word, sentence, or paragraph into 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 the googletrans library to present us with several languages. 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 to Translate Languages in Python

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.

Steps to Proceed for the Project to Translate Languages in Python

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 an easy GUI in Python.
  • Messagebox – This is for displaying a message box.
  • TextBlob – This is for analyzing and processing textual data.
  • Googletrans – This is for importing several languages that we will be using during the project to translate from one to another.
Read also: Mastering HubSpot: A Comprehensive Overview and Step-by-Step Guide

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("Language Translation by GetProjects")
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 the reading state. We have created two drop-down lists using combo1 and combo2. The set() function will set a desired text to the drop-down list when 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 a textarea to them.
  • 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 that will perform translation of the text when triggered. Command= translate gives the command that the translate function will be executed when a button is clicked.
  • place() – the place function is for making the appropriate place on the window and placing it on it.

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 user’s chosen languages 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, the 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 with just a click.

Output of project to Translate Languages in Python

This is how our language translator will look after completing the project (project to Translate Languages in Python).

Summary

We have successfully created a Python Language Translation Project. We used Tkinter Library to create an easy and effective GUI. We also used the 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.

Some Recommended Projects
1. Create School Management System in Python, Download Source Code
2. Create School Management System with PHP & MySQL
3. Convert Text to Speech in Python, Download Source Code
4. How to make a simple audio player in HTML, JavaScript, CSS. Download Source Code

Keywords

  • Translate Languages in Python
  • Create a Project to Translate Languages in Python with Source code
Related Articles

Related

Leave a Reply

Please enter your comment!
Please enter your name here