How I Made a Language Translator using Python

Project Description

I recently made a friend online who lives in Paris. I was very excited to learn about her culture and tell her about mine, but the problem here was how would I communicate with her as she was French. That’s when I thought of creating a Python script that would translate a text from one language to any other language in real-time.

Thus, in this project, I will demonstrate how you can create a Python script that instantly translates the given text from one language to another. In the script, we will use several libraries such as β€œtkinter” and β€œtranslate”.

Let’s dive into the different steps that will help us to complete this project.

Video Walkthrough

Step 1: Import the Necessary Libraries

The most important library in this project is the translate library which is used to make all the translations simply without the need for any browser. Since it is not a built-in Python library, therefore you need to install it using PIP. Open your terminal and type the following command –

pip install translate

Once you have installed the translate library, you will also need the tkinter module to develop the GUI application. So let’s go ahead and import all these libraries into our script.

from tkinter import *
from translate import Translator

Step 2: Initializing the GUI Window and Setting the Languages

Screen = Tk()
Screen.title("Language Translator")
 
Input_lang = StringVar()
Output_lang = StringVar()

Languages = {'Hindi', 'German', 'English', 'French', 'Spanish'}
Input_lang.set('English')
Output_lang.set('French')

Explanation: A window is an instance of the module’s Tk class. With the help of this class, a root window gets created. The two variables Input_lang, and Output_lang, are used to store the language of the text that is being translated and the language in which the text is to be translated, respectively. A set stores the languages from which the input and translated languages get chosen. Initially, I set the input language as English and the translated language as French. However, it can be changed afterward from the given set of languages.

Step 3: Define a Function that Translates the Text

Next, we need to create a function that will translate the given text from one language to another.

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

The Translator() function helps to translate the text, and the get() function returns the value of the given item (language). The text that needs to get translated gets stored in the TextVar variable. After the text gets translated, it gets stored in the OutputVar variable.

Step 4: Setting the Choice Of Languages (Input Language and the Language in which the Text gets Translated)

Next, we have to create a label box where we can set the language choices. The InputLanguageChoiceMenu will give the option to choose the input language, and the NewLanguageChoiceMenu will provide a choice of languages into which the text gets translated. OptionMenu() is used to give the available language options.

InputLanguageChoiceMenu = OptionMenu(Screen,Input_lang,*Languages)
Label(Screen,text = "Choose a Language").grid(row = 0,column = 1)
InputLanguageChoiceMenu.grid(row = 1,column = 1)
 
NewLanguageChoiceMenu = OptionMenu(Screen,Output_lang,*Languages)
Label(Screen,text = "Translated Language").grid(row = 0,column = 2)
NewLanguageChoiceMenu.grid(row = 1,column = 2)

Step 5: Displaying the Input Text and the Translated Text 

In the last step, we have to create a button that will instantly translate a text from one language to any other language in real-time by just clicking on it.

Code:

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)
 
B = Button(Screen,text = "Translate",command = Translate, relief = GROOVE).grid(row = 3,column = 1,columnspan = 3)
mainloop()

Note: The relief() function is used to provide 3D effects outside the widget and the mainloop() function runs the event loop.

Putting it All Together

We are now ready to put all the bits together and create our final complete code. So, here’s how the final code looks:

Code:

from tkinter import *
from translate import Translator

Screen = Tk()
Screen.title("Language Translator")

Input_lang = StringVar()
Output_lang = StringVar()

Languages = {'Hindi', 'German', 'English', 'French', 'Spanish'}
Input_lang.set('English')
Output_lang.set('French')


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


InputLanguageChoiceMenu = OptionMenu(Screen, Input_lang, *Languages)
Label(Screen, text="Choose a Language").grid(row=0, column=1)
InputLanguageChoiceMenu.grid(row=1, column=1)

NewLanguageChoiceMenu = OptionMenu(Screen, Output_lang, *Languages)
Label(Screen, text="Translated Language").grid(row=0, column=2)
NewLanguageChoiceMenu.grid(row=1, column=2)

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)

B = Button(Screen, text="Translate", command=Translate, relief=GROOVE).grid(row=3, column=1, columnspan=3)
mainloop()

Output:

Summary

In conclusion, the script used in this project is a useful tool for translating languages in real-time instantly. It creates a graphical user interface (GUI) using the Tkinter library. The GUI includes two drop-down menus for selecting an input and output language, a text entry field for inputting text, and a text entry field for displaying the translated text.

The code also imports the Translator module from the translate library. When the “Translate” button is clicked, the code creates an instance of the Translator class using the selected input and output languages, and uses the translate method from the Translator class to translate the input text and set the output text field with the translation. The GUI is displayed using the mainloop() function.

I hope this mini project adds some value to your coding journey. Please subscribe and stay tuned for more interesting solutions and discussions in the future. Happy coding!


πŸ‘‰ Recommended: How I Created a Code Translator Using GPT-3