Python Language Translator Project

Himani Bansal
DataFlair
Published in
3 min readOct 21, 2023

About Python Language Translator Project:

In this project, we develop a Language Translator application in Python using the tkinter library for creating the graphical user interface. This application leverages the Google Translate API through the `googletrans` library and utilizes the text processing capabilities of the `textblob` library to provide users with a simple and intuitive way to translate text between different languages. Users can input text, select the source and target languages from dropdown menus, and click the “Translate” button to instantly view the translation. The application enhances accessibility by enabling easy communication across language barriers and can be customized further to suit specific needs.

Prerequisites For Python Language Translator Project :

  • First, we’ll initiate the project by installing the essential libraries and modules via the pip installer.
  • A strong grasp of Python, coupled with proficiency in the required libraries and modules, is crucial for the successful development of this project.
pip install tkinter
Language Translator

Importing the required libraries and modules:

from tkinter import *
from tkinter import ttk, messagebox
import googletrans
import textblob

tkinter-This library will help us in creating a GUI window for our app.

googletrans-The googletrans library is used for language translation.

textblob-The textblob library is used for text processing and translation.

Initializing GUI window-

We will initiate the GUI window for the application.


root=Tk()
root.geometry("700x400")
root.title("Language Translator")
root.config(bg='#D3D3D3')

root-It is the name of our GUI window.

Tk()-It initialises tkinter which means a GUI window is created.

geometry()-This method provides the length and breadth to the GUI window.

resizeable()-This method allows the window to change its size as per user need.

title()-This method gives title to the window

confg()-This method sets the configuration of the window.

Creating Labels and Frames-

We will create labels and frames.

Label(root,text="Language Translator-PythonFlood",font=('Arial,bold',20), bg='#8B8B7A',fg='White', width=50, pady=5).pack(pady=10)


f1 = Frame(root, width=320, height=200, bg='white', bd=5)
f1.place(x=15, y=70)
f2 = Frame(root, width=320, height=200, bg='white', bd=5)
f2.place(x=360, y=70)

Label()- It is used to display one line or more than one line of text.

text-It is used to display text on label.

font-It is a style in which font is written.

bg-It is the background Colour of the label.

pack()-This displays the widgets in the GUI window.

place()-It is used to set the position.

Frame()-It is like a container, which is used for positioning the widgets.

Creating Text widget, Combobox and Buttons-

Now we will create text widget, combobox and buttons widget for the application.

txt1=Text(f1,font=('Arial,bold'), width=28, height=8)
txt1.place(x=0,y=0)
txt2=Text(f2,font=('Arial,bold'), width=28, height=8)
txt2.place(x=0,y=0)


c1=ttk.Combobox(root,values=language_list,font=('Arial,bold',12),width=20)
c1.place(x=60,y=290)
c2=ttk.Combobox(root,values=language_list,font=('Arial,bold',12),width=20)
c2.place(x=420,y=290)


btn=Button(root,text="Translate",font=('Arial,bold',15),bd=5, bg='#8B8B7A',fg='White',command=trans_late)
btn.place(x=290,y=320)


root.mainloop()

Text-It allows you to enter multiline text.

ttk.Combobox-It is used to select one value from the set of values present in it.

values-It is the values present in the combobox.

Button()-It is a button used to display on our window.

bd-It is the border of the Button.

command-It is used as a function of a button when it is clicked.

place()-It is used to set the position.

root.mainloop()-It is simply a method in the main window that executes what we wish to execute in an application and ends the mainloop.

Code for language translation-

We will create main function which will translate the text.

 def trans_late():
txt2.delete(1.0, END)
try:
for key, value in languages.items():
if(value == c1.get()):
from_language_key = key
for key, value in languages.items():
if(value == c2.get()):
to_language_key = key


words = textblob.TextBlob(txt1.get(1.0, END))
words = words.translate(from_lang=from_language_key, to=to_language_key)
txt2.insert(1.0, words)



except Exception as e:
messagebox.showerror(("Translater", e))

trans_late()-This function is called when the “Translate” button is clicked. It attempts to translate the text entered in txt1 from the selected source language (c1) to the target language (c2) and displays the result in txt2.It handles exceptions and displays an error message using messagebox.showerror() if there’s an issue with translation.

Python Language Translator Output-

Language Translator Output Using Python

Conclusion:

Now you have acquired the knowledge of creating a Language Translator application with Python using googletrans library. This application was built with a graphical user interface (GUI) using tkinter. You have the flexibility to customize the application’s interface to meet your specific requirements and preferences.

--

--

Himani Bansal
DataFlair

Doing my Best to Explain Data Science (Data Scientist, technology freak & Blogger)