Language Translator App with Python Tkinter - Google Translate

It's a language translator project in python using google translate api and tkinter library

Introduction

Can you tell me why we need a Translator? You might be thinking what's so funny, is it a question for asking, or something else. Yes! everyone knows what a Translator is. But hiring a human translator who can translate our every needs 24/7 is like a dream.

That's why Google is here. Google offers a trustable and unstoppable Translator completely free 24/7. But you might not know that Python offers a library called googletrans which acts as an API to allow us to translate languages through Python Programming.

This library can work with a total of 107 different languages. I developed a Language Translator Project in Python exactly like Google Translate using Tkinter and this googletrans library which is very easy to use.

I will discuss this topic in this tutorial and at the end, I will share the Source Code of the beautiful Translate Application. Let's first see how you can translate one language to another through a python program.

👉Visit Also: Image to Pencil Sketch Converter in Python

Requirements

Before you start writing your code just take a break here. Install these necessary libraries if you didn't already.

☛Install Tkinter: pip install tk

☛Install googletrans: pip install googletrans

Find How many languages are there

Let's find out how many languages googletrans offers to us. This piece of code will help you.

Code


import googletrans

# Find all languages
languages = googletrans.LANGUAGES

print(f"Total no. of languages: {len(languages)}")
print("\nAll languages are here...\n")
print(languages)

Output

Total no. of languages: 107

All languages are here...

{'af': 'afrikaans', 'sq': 'albanian', 'am': 'amharic', 'ar': 'arabic', 'hy': 'armenian', 'az': 'azerbaijani', 'eu': 'basque', 'be': 'belarusian', 'bn': 'bengali', 'bs': 'bosnian', 'bg': 'bulgarian', 'ca': 'catalan', 'ceb': 'cebuano', 'ny': 'chichewa', 'zh-cn': 'chinese (simplified)', 'zh-tw': 'chinese (traditional)', 'co': 'corsican', 'hr': 'croatian', 'cs': 'czech', 'da': 'danish', 'nl': 'dutch', 'en': 'english', 'eo': 'esperanto', 'et': 'estonian', 'tl': 'filipino', 'fi': 'finnish', 'fr': 'french', 'fy': 'frisian', 'gl': 'galician', 'ka': 'georgian', 'de': 'german', 'el': 'greek', 'gu': 'gujarati', 'ht': 'haitian creole', 'ha': 'hausa', 'haw': 'hawaiian', 'iw': 'hebrew', 'he': 'hebrew', 'hi': 'hindi', 'hmn': 'hmong', 'hu': 'hungarian', 'is': 'icelandic', 'ig': 'igbo', 'id': 'indonesian', 'ga': 'irish', 'it': 'italian', 'ja': 'japanese', 'jw': 'javanese', 'kn': 'kannada', 'kk': 'kazakh', 'km': 'khmer', 'ko': 'korean', 'ku': 'kurdish (kurmanji)', 'ky': 'kyrgyz', 'lo': 'lao', 'la': 'latin', 'lv': 'latvian', 'lt': 'lithuanian', 'lb': 'luxembourgish', 'mk': 'macedonian', 'mg': 'malagasy', 'ms': 'malay', 'ml': 'malayalam', 'mt': 'maltese', 'mi': 'maori', 'mr': 'marathi', 'mn': 'mongolian', 'my': 'myanmar (burmese)', 'ne': 'nepali', 'no': 'norwegian', 'or': 'odia', 'ps': 'pashto', 'fa': 'persian', 'pl': 'polish', 'pt': 'portuguese', 'pa': 'punjabi', 'ro': 'romanian', 'ru': 'russian', 'sm': 'samoan', 'gd': 'scots gaelic', 'sr': 'serbian', 'st': 'sesotho', 'sn': 'shona', 'sd': 'sindhi', 'si': 'sinhala', 'sk': 'slovak', 'sl': 'slovenian', 'so': 'somali', 'es': 'spanish', 'su': 'sundanese', 'sw': 'swahili', 'sv': 'swedish', 'tg': 'tajik', 'ta': 'tamil', 'te': 'telugu', 'th': 'thai', 'tr': 'turkish', 'uk': 'ukrainian', 'ur': 'urdu', 'ug': 'uyghur', 'uz': 'uzbek', 'vi': 'vietnamese', 'cy': 'welsh', 'xh': 'xhosa', 'yi': 'yiddish', 'yo': 'yoruba', 'zu': 'zulu'}

See, all the languages are returned in a python dictionary. Each language has a unique key name(short-name). When we will command our program, we will use those key names instead of the language name.

Translate your first language

We found the total number of languages googletrans offers. Now time to translate a text from one language to another. As an example, I will use a Spanish text here which will be translated into the English language.

By default the target language is English but for the rest, we need to specify the language name. Here is the code for you.

Code


from googletrans import Translator

# Spanish
text = "Gracias traductor de Google"

# Instance of Translator class
translator = Translator()

# Detect Language
langType = translator.detect(text)

# Translating the text
result = translator.translate(text)

# Output in compact form
print(langType)
print(result)

# Printing the output in a convenient way
print(f"\nDetected language: {langType.lang}")
print(f"Translated text: {result.text}")

Output

Detected(lang=es, confidence=1)
Translated(src=es, dest=en, text=Thanks Google translator, pronunciation=Thanks Google translator, extra_data="{'translat...")

Detected language: es
Translated text: Thanks Google translator

As you can see, we have two additional results with our desired answer which are the detected language(the one we used for translation) and the probability of being that language.

Mention the language

Suppose you want to translate a text from English to French(key name: 'fr'), this code will help you.

Code


from googletrans import Translator

# English
text = "Thank you Google Translate"

# Instance of Translator class
translator = Translator()

# Translating the text
result = translator.translate(text, src='en', dest='fr')

print(f"Translated text: {result.text}")

Output

Translated text: Merci Google Traduction

See, I have specified two more parameters in the translate() method. 'src' represents the source language we use for translation which is optional(the program will detect the language automatically), lastly 'dest' represents the language to translate the source text into (target language).

The few examples we've seen so far will help you better understand the Google Translate application I've built, but you must know about the Tkinter library and its working principle. 

If you don't, check out some other projects made using this library. Hope they help you a lot. Some examples are below.

👉A GUI YouTube Video Downloader using Python Tkinter

👉An Advanced Alarm Clock using Python Tkinter

👉Countdown Timer in Python: with Start and Pause Function

The Translator Application

I divided the whole Source Code into three python files. I highly recommend you, create a separate folder for this project and those three python files will be present there.

I have set a logo on this application to make it look more prestigious. That image file must be represented in that folder. You will get that image from below download button.

google translate application using googletrans and tkinter library in python

Download the Logo

Let's get straight to the code now.

The Driver Program

This is the core program that uses object oriented programming to create a beautiful user interface and perform all translation-related tasks.

app.py


import data as d
import custom as cs
from tkinter import *
from PIL import ImageTk, Image
from googletrans import Translator
from tkinter import ttk, messagebox

class GoogleTranslate:
def __init__(self, root):
# Window Settings
self.window = root
self.window.geometry("900x540")
self.window.title('Google Translate')
self.window.resizable(width = False, height = False)
self.window.configure(bg="white")

# A Frame: For showing the GoogleTranslate Logo
self.frame = Frame(self.window,width=300, height=60)
self.frame.pack()
self.frame.place(x=20, y=20)
# Calling the function for showing the Logo
self.DisplayLogo()

# ========Header Buttons========
# About Button
aboutBtn = Button(self.window, text="About", \
font=(cs.font2, 8, 'bold'), bg=cs.color2, \
fg="white", width=5, command=self.About)
aboutBtn.place(x=800, y=20)

# Exit Button
exitBtn = Button(self.window, text="Exit", \
font=(cs.font2, 8, 'bold'), bg=cs.color2, \
fg="white", width=5, command=self.Exit)
exitBtn.place(x=800, y=60)
# ==============================

self.MainWindow()

# Function for displaying the logo image
def DisplayLogo(self):
# Opening the image
image = Image.open('GoogleTranslate.png')
# Resizing the image
resized_image = image.resize((300, 60))

self.img1 = ImageTk.PhotoImage(resized_image)
# A Label Widget to display the Image
label = Label(self.frame, bg=cs.color1, image=self.img1)
label.pack()

# The Main Window: Containing other widgets
def MainWindow(self):
# String Variable
self.currLang = StringVar()
self.currLang.set("Not Detected")
# Label: For showing the name of detected language
self.detectedLang = Label(self.window, \
textvariable=self.currLang, font=(cs.font3, 20), \
bg=cs.color1)
self.detectedLang.place(x=160, y=130)

# Combobox: To select the language to be translated
text = StringVar()
self.toLang = ttk.Combobox(self.window, textvariable=text, \
font=(cs.font1, 15))
self.toLang['values'] = d.lang_list
self.toLang.current(0)
self.toLang.place(x=550, y=130)

self.fromText_Box = Text(self.window, bg=cs.color3,
font=(cs.font1, 15), height=9, width=34)
self.fromText_Box.place(x=80, y=190)

self.toText_Box = Text(self.window, bg=cs.color3, \
relief=GROOVE, font=(cs.font1, 15), height=9, width=34)
self.toText_Box.place(x=480, y=190)

translateBtn = Button(self.window, text="Translate", \
font=(cs.font2, 14, "bold"), bg=cs.color4, fg=cs.color1, \
command=self.Translator)
translateBtn.place(x=385, y=430)

def Translator(self):
try:
fromText = self.fromText_Box.get("1.0", "end-1c")

# instance of Translator class
translator = Translator()

dest_lang = self.toLang.get()

if dest_lang == '':
messagebox.showwarning("Nothing has chosen!", \
"Please Select a Language")
else:
if fromText != '':
langType = translator.detect(fromText)

# Translating the text
result = translator.translate(fromText, dest=dest_lang)

self.currLang.set(d._languages[langType.lang.lower()])

self.toText_Box.delete("1.0", END)

self.toText_Box.insert(INSERT, result.text)
except Exception as es:
messagebox.showerror("Error!", f"Error due to {es}")

# For About Button
def About(self):
messagebox.showinfo("Google Translate - Python", \
"Developed by Subhankar Rakshit\n~PySeek")
# For Exit Button: All window will be closed
def Exit(self):
self.window.destroy()

if __name__ == "__main__":
# Instance of Tk Class
root = Tk()
# Object of GoogleTranslator Class
obj = GoogleTranslate(root)
root.mainloop()

Data File

This program file stores each language name and corresponding key name in the Python list and Python dictionary.

data.py


languages = {'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': 'he', \
'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', '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': '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', 'telugu': 'te', 'thai': 'th', \
'turkish': 'tr', 'ukrainian': 'uk', 'urdu': 'ur', \
'uyghur': 'ug', 'uzbek': 'uz', 'vietnamese': 'vi', \
'welsh': 'cy', 'xhosa': 'xh', 'yiddish': 'yi', \
'yoruba': 'yo', 'zulu': 'zu'}

lang_list = ['afrikaans', 'albanian', 'amharic', 'arabic', \
'armenian', 'azerbaijani', 'basque', 'belarusian', 'bengali', \
'bosnian', 'bulgarian', 'catalan', 'cebuano', 'chichewa', \
'chinese (simplified)', 'chinese (traditional)', 'corsican', \
'croatian', 'czech', 'danish', 'dutch', 'english', 'esperanto', \
'estonian', 'filipino', 'finnish', 'french', 'frisian', \
'galician', 'georgian', 'german', 'greek', 'gujarati', \
'haitian creole', 'hausa', 'hawaiian', 'hebrew', 'hindi', \
'hmong', 'hungarian', 'icelandic', 'igbo', 'indonesian', \
'irish', 'italian', 'japanese', 'javanese', 'kannada', 'kazakh', \
'khmer', 'korean', 'kurdish (kurmanji)', 'kyrgyz', 'lao', 'latin', \
'latvian', 'lithuanian', 'luxembourgish', 'macedonian', \
'malagasy', 'malay', 'malayalam', 'maltese', 'maori', \
'marathi', 'mongolian', 'myanmar (burmese)', 'nepali', \
'norwegian', 'odia', 'pashto', 'persian', 'polish', 'portuguese', \
'punjabi', 'romanian', 'russian', 'samoan', 'scots gaelic', \
'serbian', 'sesotho', 'shona', 'sindhi', 'sinhala', 'slovak', \
'slovenian', 'somali', 'spanish', 'sundanese', 'swahili', \
'swedish', 'tajik', 'tamil', 'telugu', 'thai', 'turkish', \
'ukrainian', 'urdu', 'uyghur', 'uzbek', 'vietnamese', 'welsh', \
'xhosa', 'yiddish', 'yoruba', 'zulu']

_languages = {'af': 'afrikaans', 'sq': 'albanian', \
'am': 'amharic', 'ar': 'arabic', 'hy': 'armenian', \
'az': 'azerbaijani', 'eu': 'basque', 'be': 'belarusian', \
'bn': 'bengali', 'bs': 'bosnian', 'bg': 'bulgarian', \
'ca': 'catalan', 'ceb': 'cebuano', 'ny': 'chichewa', \
'zh-cn': 'chinese (simplified)', 'zh-tw': 'chinese (traditional)', \
'co': 'corsican', 'hr': 'croatian', 'cs': 'czech', \
'da': 'danish', 'nl': 'dutch', 'en': 'english', 'eo': 'esperanto', \
'et': 'estonian', 'tl': 'filipino', 'fi': 'finnish', \
'fr': 'french', 'fy': 'frisian', 'gl': 'galician', 'ka': \
'georgian', 'de': 'german', 'el': 'greek', 'gu': 'gujarati', \
'ht': 'haitian creole', 'ha': 'hausa', 'haw': 'hawaiian', \
'iw': 'hebrew', 'he': 'hebrew', 'hi': 'hindi', 'hmn': 'hmong', \
'hu': 'hungarian', 'is': 'icelandic', 'ig': 'igbo', \
'id': 'indonesian', 'ga': 'irish', 'it': 'italian', \
'ja': 'japanese', 'jw': 'javanese', 'kn': 'kannada', \
'kk': 'kazakh', 'km': 'khmer', 'ko': 'korean', \
'ku': 'kurdish (kurmanji)', 'ky': 'kyrgyz', 'lo': 'lao', \
'la': 'latin', 'lv': 'latvian', 'lt': 'lithuanian', \
'lb': 'luxembourgish', 'mk': 'macedonian', 'mg': 'malagasy', \
'ms': 'malay', 'ml': 'malayalam', 'mt': 'maltese', \
'mi': 'maori', 'mr': 'marathi', 'mn': 'mongolian', \
'my': 'myanmar (burmese)', 'ne': 'nepali', 'no': 'norwegian', \
'or': 'odia', 'ps': 'pashto', 'fa': 'persian', 'pl': 'polish', \
'pt': 'portuguese', 'pa': 'punjabi', 'ro': 'romanian', \
'ru': 'russian', 'sm': 'samoan', 'gd': 'scots gaelic', \
'sr': 'serbian', 'st': 'sesotho', 'sn': 'shona', 'sd': 'sindhi', \
'si': 'sinhala', 'sk': 'slovak', 'sl': 'slovenian', \
'so': 'somali', 'es': 'spanish', 'su': 'sundanese', \
'sw': 'swahili', 'sv': 'swedish', 'tg': 'tajik', 'ta': 'tamil', \
'te': 'telugu', 'th': 'thai', 'tr': 'turkish', 'uk': 'ukrainian', \
'ur': 'urdu', 'ug': 'uyghur', 'uz': 'uzbek', 'vi': 'vietnamese', \
'cy': 'welsh', 'xh': 'xhosa', 'yi': 'yiddish', 'yo': 'yoruba', \
'zu': 'zulu'}

Customization File

This file contains some Tkinter font types and color options used in various widgets in the driver program.

custom.py


# ========Customization========
# Color options
color1 = "white"
color2 = "dodger blue"
color3 = "gray95"
color4 = "green2"
# Font options
font1 = "times new roman"
font2 = "Kokila"
font3 = "Helvetica"
# =============================

Watch the entire video to understand how the application works.

Summary

In this tutorial, we build a Language Translator application using Python. Tkinter and googletrans libraries helped us a lot in this work. It looks almost like the original Google Translate.

First, we learned how the googletrans library works and then built the application. I divided the whole Source Code into three python files and tried to declare a single comment line before each line of the code so that it becomes easy to understand.

Do use this beautiful Translator App and Share Your Opinion below. It would be an inspiration for me. For any problems or errors, don't hesitate to leave your comment below. You'll get an immediate response.

Thanks for reading!💙

PySeek

Subhankar Rakshit

Meet Subhankar Rakshit, a Computer Science postgraduate (M.Sc.) and the creator of PySeek. Subhankar is a programmer, specializes in Python language. With a several years of experience under his belt, he has developed a deep understanding of software development. He enjoys writing blogs on various topics related to Computer Science, Python Programming, and Software Development.

Post a Comment (0)
Previous Post Next Post