How to Create a Language Translator in Python

For spreading new ideas, information and knowledge, the language translation is necessary. To effectively communicate between different cultures, language translation is important. This project helps in translating the text in other languages easily. Let’s start developing the project of Language Translator in Python.

Keeping you updated with latest technology trends, Join TechVidvan on Telegram

About Python Language Translator Project

In this project, a language will be chosen from a list of options in which the text is to be entered, and also the language in which the text is to be translated is also selected from the list of options. After selecting the languages, the translate button will be clicked to translate the text.

Python Language Translator with GUI project

The objective of this Python project is to translate a piece of text into another language. You need to install, translate and import two modules: tkinter, translate. Basic knowledge of tkinter is required along with the knowledge of functions in python.

Project Prerequisites

This project requires knowledge of the tkinter module. Basic knowledge of functions in python is also required.

Download Python Language Translator Code

Please download the source code of python language translator with GUI: Language Translator Python Code

Project File Structure:

Steps to develop Python Language Translator with GUI:

  1. Installing translate
  2. Importing translate and tkinter
  3. Initializing window
  4. Creating tuple for choosing languages
  5. Creating a function for translating the text
  6. Choice for input language and the language in which the text is to be translated
  7. Input and Output text

1. Installing translate:

Installation of this package is required before starting the project. Translation of major languages is provided by this package. To install this package run the following command on the command prompt or terminal window.

pip install translate

2. Importing translate and tkinter:

from tkinter import *
from translate import Translator

Code Explanation:

  • From tkinter import *: The easiest and most effective way to develop GUI applications is using tkinter. * means importing everything from tkinter.
  • From translate import Translator: This package helps to translate major languages.

3. Initializing window:

Screen = Tk()
Screen.title("Language Translator with GUI by- TechVidvan")
 
InputLanguageChoice = StringVar()
TranslateLanguageChoice = StringVar()

Code Explanation:

InputLanguageChoice is a variable that stores the language of the text that is being translated.

TranslateLanguageChoice is a variable that stores the language in which the text is to be translated.

  • Tk(): Root window is created with the help of Tk() class.
  • title(): It is used to display the title on the top of the root window.

4. Creating tuple for choosing languages :

LanguageChoices = {'Hindi','English','French','German','Spanish'}
InputLanguageChoice.set('English')
TranslateLanguageChoice.set('Hindi')

Code Explanation:
LanguageChoices is a tuple that stores five languages from which input language and translated language can be chosen.

  • set(): It helps to set the value of the variable. For instance: value of InputLanguageChoice is set as English initially which can be changed afterwards and selected from the tuple.

5. Creating a function for translating the text:

def Translate():
    translator = Translator(from_lang= InputLanguageChoice.get(),to_lang=TranslateLanguageChoice.get())
    Translation = translator.translate(TextVar.get())
    OutputVar.set(Translation)

Code Explanation:

This function is created to translate the text. OutputVar is a variable that stores the translated text. TextVar is a variable that contains the text that is to be translated.

  • Translator(): It helps to translate the text.
  • from_lang : It is the language of the text that is being translated.
  • to_lang: It is the language of the text in which the text is to be translated.
  • get(): Value of the item is returned with the help of this method.

6. Choice for input language and the language in which the text is to be translated :

#choice for input language
InputLanguageChoiceMenu = OptionMenu(Screen,InputLanguageChoice,*LanguageChoices)
Label(Screen,text="Choose a Language").grid(row=0,column=1)
InputLanguageChoiceMenu.grid(row=1,column=1)
 
#choice in which the language is to be translated
NewLanguageChoiceMenu = OptionMenu(Screen,TranslateLanguageChoice,*LanguageChoices)
Label(Screen,text="Translated Language").grid(row=0,column=2)
NewLanguageChoiceMenu.grid(row=1,column=2)

Code Explanation:

InputLanguageChoiceMenu provides a choice of input languages. NewLanguageChoiceMenu provides a choice of languages in which translation of text is possible.

  • OptionMenu(): It provides the options that are available to the user.
  • Label(): This widget helps to implement display boxes where the text can be placed.
  • grid(): Widgets are arranged on the screen with the help of grid.

7. Input and Output text :

Label(Screen,text="Enter Text").grid(row=2,column =0)
TextVar = StringVar()
TextBox = Entry(Screen,textvariable=TextVar).grid(row=2,column = 1)
 
Label(Screen,text="Output Text").grid(row=2,column =2)
OutputVar = StringVar()
TextBox = Entry(Screen,textvariable=OutputVar).grid(row=2,column = 3)
 
#Button for calling function
B = Button(Screen,text="Translate",command=Translate, relief = GROOVE).grid(row=3,column=1,columnspan = 3)
 
mainloop()

Code Explanation:

  • Entry(): This widget helps to enter or display a single line text on the screen.
  • Button(): This widget creates a button.
  • relief: It helps to provide 3-D effects around the outside of the widget.
  • mainloop(): It helps to run the tkinter event loop.

Python Language Translator Output

python language translator output

Summary

We have successfully developed python language translator project with translate and tkinter. This is an interesting python project for beginners, based on requirements you can add more functionalities.

Leave a Reply

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.