Photo by Desola Lanre-Ologun on Unsplash
How to Build a Simple Language Translator With Tkinter, Google Trans, and gTTS Libraries in Python
In our increasingly interconnected world, the ability to cross linguistic barriers is more important than ever. As a Social Media Marketer, I had the opportunity to work for an e-commerce firm in the Philippines and was expected to always translate my posts from English to Tagalog. As such, picture being able to interact freely with persons from various language origins.
There are numerous approaches to language translation, and deep learning engineers have done exceptionally well in this subject. However, in this article, I will teach you how to create a basic yet powerful language translator. I will use three generally accessible tools such as Tkinter, Google Trans, and gTTS (Google Text-to-Speech). Let us get started without any further hesitation. Also, in this article, we will be using user-friendly IDEs like PyCharm, Jupyter Notebook, Sublime Text, Thonny, or VSCode. These integrated development environments offer intuitive interfaces, helpful debugging tools, and built-in support for Python libraries.
Please Note: This article is for intermediate and expert Python developers conversant with functions, conditional flow, and terminologies. However, I am open to questions and guidance for all newbie who may have the context and is finding the process difficult.
Installation of Libraries
Tkinter is a strong and widely used Python framework designed to create beautiful graphical user interfaces (GUIs). It's extremely easy to use and helps Python developers to create interactive and eye-catching applications. Tkinter makes it easier to create windows, buttons, menus, and other GUI elements in Python. To install Tkinter, you must first have Python installed on your machine, as Tkinter comes integrated with Python versions 3.1 and above. If not, use Python's package manager, pip, to install it in your IDE terminal with the command:
However, in a situation where you are using Python 3.0 above, instead of installing, all you have to do is import tkinter
"googletrans" is the next library to be installed for this project. This is a Python programming framework for Google Translate that allows developers to smoothly add language translation features to their applications. It's a wonderful tool for projects that require multilingual assistance or global communication. You can install googletrans in your terminal using the "pip" command just as demonstrated below:
The last and final package I'll install for this project is gTTS, which stands for Google Text-to-Speech. The gTTS is a Python library and command-line interface for interacting with Google Translate's text-to-speech API. This library converts text into natural-sounding speech, allowing for cross-language applications such as language learning tools and accessibility features. To install this library, run the line of code below:
Hurray!! You have successfully installed all the packages needed for this tutorial. In case you encounter any error using the pip command, you can use the python -m pip install command instead. If you are using Jupyternote book IDE, don’t panic; the process is the same.
What next? Import these libraries and get started with the main process of building your chat translator. I would have loved a step-by-step tutorial where I am able to guide you through all the stages. However, you will enjoy the article and my code methodology. All you have to do is read the comments!
Congratulations! You just imported your libraries, defined your function, and stated the conditions of the translation. When you execute this code, you will see an orange and squared tkinter window ready for a more colorful impact. You can go ahead to add as many languages as you want but make sure it is included in the “googletrans” documentation. You may check out the documentation here to learn more.
What next? If I may ask, what are some features of a language translator app if you’ve used one? Maybe a text frame where you can enter the text you would love to translate, a button that will execute the “translate” command, and a place where the translated text will be displayed. That is beautiful. In this language translator, we already included options for five (5) languages. So what changes? Absolutely nothing in the process but we will create a dropdown frame where users can select the language they want their text translated to and then colorful formatting using tkinter functionalities.
You'll notice the gorgeous tkinter magic pop up on your screen as soon as you execute this code which has been wonderfully designed with Tkinter. You will further see a frame where you can enter text. After you've entered your text, click the translate button and you'll be glad you did. Now, hit the translate button!! What did you see?
You can also format your code as much as you want according to your preferences.
Have fun translating!!!