└── Translation.py /Translation.py: -------------------------------------------------------------------------------- 1 | #PythonGeeks GUIDE TO TRANSLATE LANGUAGES USING Google Translate API 2 | import googletrans 3 | from googletrans import Translator 4 | from tkinter import * 5 | from tkinter import messagebox 6 | 7 | #Since default options are allowed, we check for 8 | #explicitly given source and destination languages 9 | def translate_function(): 10 | #check if the source and target languages are empty 11 | if (len(src_entry.get("1.0","end-1c"))>1): 12 | src_v = src_entry.get("1.0","end-1c").lower() 13 | src_v =src_v.replace(" ","") 14 | else: 15 | src_v = None 16 | 17 | if (len(dest_entry.get("1.0","end-1c"))>1): 18 | dest_v = dest_entry.get("1.0","end-1c").lower() 19 | dest_v =dest_v.replace(" ","") 20 | else: 21 | dest_v = None 22 | #Check if the text is empty. If so, prompt user to key it 23 | if (len(text_entry.get("1.0","end-1c"))<=1): 24 | messagebox.showerror(message="Enter valid text") 25 | else: 26 | #Send the parameters based on user input provided 27 | text_v = text_entry.get("1.0","end-1c") 28 | if (not src_v) & (not dest_v): 29 | translated_text = translator_object.translate(text_v) 30 | elif (not src_v): 31 | translated_text = translator_object.translate(text_v,dest=dest_v) 32 | elif (not dest_v): 33 | translated_text = translator_object.translate(text_v,src=src_v) 34 | else: 35 | translated_text = translator_object.translate(text_v,src=src_v,dest=dest_v) 36 | #Display translated text on a prompt 37 | messagebox.showinfo(message = "TRANSLATED TEXT: "+translated_text.text) 38 | 39 | #Function to clear the text boxes 40 | def clear(): 41 | dest_entry.delete("1.0","end-1c") 42 | src_entry.delete("1.0","end-1c") 43 | text_entry.delete("1.0","end-1c") 44 | 45 | 46 | #Invoke call to class to view a window 47 | window = Tk() 48 | #Set dimensions of window and title 49 | window.geometry("500x300") 50 | window.title("Language Translator") 51 | 52 | #Import the Translator class which will read the input and translate 53 | #Default translation is done by detection of input and to english 54 | translator_object = Translator() 55 | #Title of the app 56 | title_label = Label(window, text="Language Translator",font=("Gayathri", 12)).pack() 57 | #Read inputs 58 | #Text input 59 | text_label = Label(window, text="Text to translate:").place(x=10,y=20) 60 | text_entry = Text(window, width=40, height=5,font=("Ubuntu Mono",12)) 61 | text_entry.place(x=130,y=20) 62 | #Source language input 63 | src_label = Label(window, text="Source language (empty: auto-detect):").place(x=10,y=120) 64 | src_entry = Text(window, width=20,height=1,font=("Ubuntu Mono",12)) 65 | src_entry.place(x=275,y=120) 66 | #Destination input 67 | dest_label = Label(window, text="Target language (empty: english-default):").place(x=10,y=150) 68 | dest_entry = Text(window, width=20,height=1,font=("Ubuntu Mono",12)) 69 | dest_entry.place(x=300,y=150) 70 | #Translate function and clear function activated through buttons 71 | button1 = Button(window,text='Translate', bg = 'Turquoise',command=translate_function).place(x=160,y=190) 72 | button2 = Button(window,text='Clear', bg = 'Turquoise',command=clear).place(x=270,y=190) 73 | #close the app 74 | window.mainloop() 75 | 76 | 77 | 78 | --------------------------------------------------------------------------------