When I try to translate text using the googletrans module, I get an error:
httpcore._exceptions.ConnectTimeout: _ssl.c:1106: The handshake operation timed out
Here's my code:
from tkinter import *
from googletrans import Translator , LANGUAGES
root = Tk()
root.geometry("500x500")
def translate():
translator = Translator()
translated_text = translator.translate(text=entry.get(),
dest=languages_listbox.get(languages_listbox.curselection())) # Text does not get get translated and it throws an error
output_label.config(text = f"Translated Text: {translated_text}")
entry = Entry(root , font = "consolas 14")
entry.place(x = 120 , y = 0)
entry.insert(0 , "Hello world")
languages_list = []
for key, value in LANGUAGES.items():
languages_list.append(value)
languages_listbox_frame = Frame(root)
languages_listbox_frame.place(x = 120 , y = 50)
languages_listbox = Listbox(languages_listbox_frame , font = "calibri 14")
languages_listbox.grid(row = 0 , column = 0)
scrollbar = Scrollbar(languages_listbox_frame , orient = VERTICAL , command = languages_listbox.yview)
scrollbar.grid(row = 0 , column = 1 , sticky = N+S+E+W)
languages_listbox.config(yscrollcommand = scrollbar.set)
for language in languages_list:
languages_listbox.insert(END , language)
languages_listbox.select_set(0)
translate_button = Button(root , text = "Translate" , font = "comicsansms 18 bold" , command = translate)
translate_button.place(x = 160 , y = 370)
output_label = Label(root , text = "Translated Text: " , font = "comicsansms 16 bold")
output_label.place(x = 5 , y = 460)
mainloop()
My code is very simple and I can't figure out what's wrong with it.
I tried changing the network connection, but it made no difference.
Is there any way to fix this problem?
It would be great if anyone could help me out.
Well that seems very much like an API error as google does not like when you send too much request, I cannot close this question either as the answer there is not an accepted one, anyway here is an alternative using deep_translator
.
pip install deep-translator
from deep_translator import GoogleTranslator
translator = GoogleTranslator(target='en')
languages_list = []
lang_map = translator.get_supported_languages(as_dict=True) # Get in form of dict
for key, value in lang_map.items():
languages_list.append(key.title()) # Making first letter capital with title()
def translate():
lang = lang_map[languages_listbox.get(languages_listbox.curselection()[0]).lower()] # Get the corresponding language code from the dictionary
translator = GoogleTranslator(source='en',target=lang) # Create new instance with selected language
translated_text = translator.translate(entry.get()) # Translate
output_label.config(text = f"Translated Text: {translated_text}") # Update the text
Yes creating a new instance every time function is run is not a good idea, but I couldn't find another successful method to change the target language once it is initially set. It does not seem to give any error as of now, unless your source
and target
languages are both same.
On a side note, it is better to use threading
, as sometimes the API might take time to respond and it will freeze the GUI.