,

How To Make Your Own Google Translator App With Python Tkinter & Googletrans

How To Make Your Own Google Translator App With Python Tkinter & Googletrans


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:

  1. Import necessary libraries:
    • tkinter for creating the GUI
    • ttk for themed widgets
    • Translator from googletrans for translation
    • Image and ImageTk from PIL for handling images
  2. Define a function translate_text to perform translation:
    • Get the text to translate from the source text input (source_text)
    • Use googletrans to translate the text to the selected target language (target_lang)
    • Update the translated text output (translated_text) with the translated text
  3. Create the main window (root) and set its title to “Google Translator”.
  4. Create a label (source_label) and a text input widget (source_text) for entering text to translate.
  5. Create a label (target_label) and a dropdown (target_lang) for selecting the target language.
  6. Create a button (translate_button) that, when clicked, calls the translate_text function to perform the translation.
  7. Create a label (translated_label) for displaying “Translated text:” and a text widget (translated_text) for displaying the translated text.
  8. Load and resize the Google logo image (google_logo), and display it using a label (logo_label).
  9. Create an exit button (exit_button) that closes the application when clicked.
  10. Start the main event loop (root.mainloop()) to run the application.

Play with Tinkter

This code provides a basic example of how to create a translation application using Tkinter and Google Translate.

Output:

Google translator with the help of Python

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

  1. Afrikaans – af
  2. Albanian – sq
  3. Amharic – am
  4. Arabic – ar
  5. Armenian – hy
  6. Azerbaijani – az
  7. Basque – eu
  8. Belarusian – be
  9. Bengali – bn
  10. Bosnian – bs
  11. Bulgarian – bg
  12. Catalan – ca
  13. Cebuano – ceb
  14. Chichewa – ny
  15. Chinese (Simplified) – zh-CN
  16. Chinese (Traditional) – zh-TW
  17. Corsican – co
  18. Croatian – hr
  19. Czech – cs
  20. Danish – da
  21. Dutch – nl
  22. English – en
  23. Esperanto – eo
  24. Estonian – et
  25. Filipino – tl
  26. Finnish – fi
  27. French – fr
  28. Frisian – fy
  29. Galician – gl
  30. Georgian – ka
  31. German – de
  32. Greek – el
  33. Gujarati – gu
  34. Haitian Creole – ht
  35. Hausa – ha
  36. Hawaiian – haw
  37. Hebrew – iw
  38. Hindi – hi
  39. Hmong – hmn
  40. Hungarian – hu
  41. Icelandic – is
  42. Igbo – ig
  43. Indonesian – id
  44. Irish – ga
  45. Italian – it
  46. Japanese – ja
  47. Javanese – jw
  48. Kannada – kn
  49. Kazakh – kk
  50. Khmer – km
  51. Kinyarwanda – rw
  52. Korean – ko
  53. Kurdish (Kurmanji) – ku
  54. Kyrgyz – ky
  55. Lao – lo
  56. Latin – la
  57. Latvian – lv
  58. Lithuanian – lt
  59. Luxembourgish – lb
  60. Macedonian – mk
  61. Malagasy – mg
  62. Malay – ms
  63. Malayalam – ml
  64. Maltese – mt
  65. Maori – mi
  66. Marathi – mr
  67. Mongolian – mn
  68. Myanmar (Burmese) – my
  69. Nepali – ne
  70. Norwegian – no
  71. Odia (Oriya) – or
  72. Pashto – ps
  73. Persian – fa
  74. Polish – pl
  75. Portuguese – pt
  76. Punjabi – pa
  77. Romanian – ro
  78. Russian – ru
  79. Samoan – sm
  80. Scots Gaelic – gd
  81. Serbian – sr
  82. Sesotho – st
  83. Shona – sn
  84. Sindhi – sd
  85. Sinhala – si
  86. Slovak – sk
  87. Slovenian – sl
  88. Somali – so
  89. Spanish – es
  90. Sundanese – su
  91. Swahili – sw
  92. Swedish – sv
  93. Tajik – tg
  94. Tamil – ta
  95. Tatar – tt
  96. Telugu – te
  97. Thai – th
  98. Turkish – tr
  99. Turkmen – tk
  100. Ukrainian – uk
  101. Urdu – ur
  102. Uyghur – ug
  103. Uzbek – uz
  104. Vietnamese – vi
  105. Welsh – cy
  106. Xhosa – xh
  107. 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.

Author

Sona Avatar

Written by

Leave a Reply

Trending

CodeMagnet

Your Magnetic Resource, For Coding Brilliance

Programming Languages

Web Development

Data Science and Visualization

Career Section

<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-4205364944170772"
     crossorigin="anonymous"></script>