Have you ever wondered why we rely on translators? Hiring a human translator who can be available 24/7 for all our translation needs seems like a distant dream. That’s where Google comes in. Google offers a reliable and always available translation service for free. But what you might not know is that Python provides a library called googletrans, which serves as an API, allowing us to translate languages using Python programming.
This library supports a total of 107 different languages, and I utilized it to create a Language Translator Application using Python. In this tutorial, I’ll demonstrate how to translate from one language to another using the googletrans library. Additionally, I’ll share the source code for the Language Translator Application. The application features a user-friendly graphical interface and supports translation for a wide range of languages.
To create a Google translator in Python first let us install the required libraries:
Learn More about Python libraries
Code:
pip install googletrans==4.0.0-rc1
above code will install google trans library version 4 which will help us to create our own Google translator
Whole Code:
import tkinter as tk
from tkinter import ttk
from googletrans import Translator
from PIL import Image, ImageTk
# Function to perform translation
def translate_text():
# Get the text to translate
text = source_text.get("1.0", tk.END).strip()
# Perform translation
translator = Translator()
translated_text_str = translator.translate(text, dest=target_lang.get()).text
# Update the translated text field
translated_text.delete("1.0", tk.END)
translated_text.insert(tk.END, translated_text_str)
# Create the main window
root = tk.Tk()
root.title("Google Translator")
# Create source text input
source_label = ttk.Label(root, text="Enter text to translate:")
source_label.pack()
source_text = tk.Text(root, height=5, width=50)
source_text.pack()
# Create target language selection
target_label = ttk.Label(root, text="Select target language:")
target_label.pack()
target_lang = ttk.Combobox(root, values=["english", "Spanish", "French", "German", "Italian", "Japanese", "Korean", "Chinese (Simplified)", "Russian", "Hindi"])
target_lang.pack()
# Create translation button
translate_button = ttk.Button(root, text="Translate", command=translate_text)
translate_button.pack()
# Create translated text output
translated_label = ttk.Label(root, text="Translated text:")
translated_label.pack()
translated_text = tk.Text(root, height=5, width=50)
translated_text.pack()
# Add Google logo
google_logo = Image.open("C:/Users/Swarna Khushbu/Desktop/Coding/google.png")
google_logo = google_logo.resize((100, 100), Image.LANCZOS)
google_logo = ImageTk.PhotoImage(google_logo)
logo_label = ttk.Label(root, image=google_logo)
logo_label.pack()
# Exit button
exit_button = ttk.Button(root, text="Exit", command=root.quit)
exit_button.pack()
# Run the application
root.mainloop()
The above code creates a simple GUI application using Tkinter that allows users to enter text, select a target language, and translate the text using the Google Translate API. Here’s a step-by-step breakdown:
- Import necessary libraries:
tkinterfor creating the GUIttkfor themed widgetsTranslatorfromgoogletransfor translationImageandImageTkfromPILfor handling images
- Define a function
translate_textto perform translation:- Get the text to translate from the source text input (
source_text) - Use
googletransto translate the text to the selected target language (target_lang) - Update the translated text output (
translated_text) with the translated text
- Get the text to translate from the source text input (
- Create the main window (
root) and set its title to “Google Translator”. - Create a label (
source_label) and a text input widget (source_text) for entering text to translate. - Create a label (
target_label) and a dropdown (target_lang) for selecting the target language. - Create a button (
translate_button) that, when clicked, calls thetranslate_textfunction to perform the translation. - Create a label (
translated_label) for displaying “Translated text:” and a text widget (translated_text) for displaying the translated text. - Load and resize the Google logo image (
google_logo), and display it using a label (logo_label). - Create an exit button (
exit_button) that closes the application when clicked. - Start the main event loop (
root.mainloop()) to run the application.
This code provides a basic example of how to create a translation application using Tkinter and Google Translate.
Output:
Did you like the output? Comment down or like to let me know!
I have used 4-7 languages but you can add more, below is the list of all the 107 language codes which you can use. You can either use the full language or just the language code inside the inverted commas
- Afrikaans –
af - Albanian –
sq - Amharic –
am - Arabic –
ar - Armenian –
hy - Azerbaijani –
az - Basque –
eu - Belarusian –
be - Bengali –
bn - Bosnian –
bs - Bulgarian –
bg - Catalan –
ca - Cebuano –
ceb - Chichewa –
ny - Chinese (Simplified) –
zh-CN - Chinese (Traditional) –
zh-TW - Corsican –
co - Croatian –
hr - Czech –
cs - Danish –
da - Dutch –
nl - English –
en - Esperanto –
eo - Estonian –
et - Filipino –
tl - Finnish –
fi - French –
fr - Frisian –
fy - Galician –
gl - Georgian –
ka - German –
de - Greek –
el - Gujarati –
gu - Haitian Creole –
ht - Hausa –
ha - Hawaiian –
haw - Hebrew –
iw - Hindi –
hi - Hmong –
hmn - Hungarian –
hu - Icelandic –
is - Igbo –
ig - Indonesian –
id - Irish –
ga - Italian –
it - Japanese –
ja - Javanese –
jw - Kannada –
kn - Kazakh –
kk - Khmer –
km - Kinyarwanda –
rw - Korean –
ko - Kurdish (Kurmanji) –
ku - Kyrgyz –
ky - Lao –
lo - Latin –
la - Latvian –
lv - Lithuanian –
lt - Luxembourgish –
lb - Macedonian –
mk - Malagasy –
mg - Malay –
ms - Malayalam –
ml - Maltese –
mt - Maori –
mi - Marathi –
mr - Mongolian –
mn - Myanmar (Burmese) –
my - Nepali –
ne - Norwegian –
no - Odia (Oriya) –
or - Pashto –
ps - Persian –
fa - Polish –
pl - Portuguese –
pt - Punjabi –
pa - Romanian –
ro - Russian –
ru - Samoan –
sm - Scots Gaelic –
gd - Serbian –
sr - Sesotho –
st - Shona –
sn - Sindhi –
sd - Sinhala –
si - Slovak –
sk - Slovenian –
sl - Somali –
so - Spanish –
es - Sundanese –
su - Swahili –
sw - Swedish –
sv - Tajik –
tg - Tamil –
ta - Tatar –
tt - Telugu –
te - Thai –
th - Turkish –
tr - Turkmen –
tk - Ukrainian –
uk - Urdu –
ur - Uyghur –
ug - Uzbek –
uz - Vietnamese –
vi - Welsh –
cy - Xhosa –
xh - Yiddish –
yi
Creating a custom Google Translator app using Python’s Tkinter and Googletrans library is a rewarding project that enhances your understanding of GUI programming and API integration. Throughout this tutorial, we learned how to design a user-friendly interface with Tkinter, integrate translation functionality using Googletrans, and add features like language selection and text translation.
By following this tutorial, you’ve gained valuable experience in building GUI applications, handling API requests, and managing user interactions. You can further enhance this project by adding more features, such as voice translation, text-to-speech conversion, or even integrating with other translation APIs for comparison. This project opens up a world of possibilities for creating custom translation tools tailored to specific needs or languages, making it a valuable learning experience for Python developers of all levels.





Leave a Reply