└── New.py /New.py: -------------------------------------------------------------------------------- 1 | from tkinter import * 2 | from tkinter import filedialog 3 | from googletrans import Translator 4 | import pyttsx3 5 | from gtts import gTTS 6 | import os 7 | 8 | # GUI Creation 9 | window = Tk() 10 | window.title("Language Translator ___RSP___") 11 | window.geometry('640x480+100+100') 12 | window.configure(bg='#FDC3DC') 13 | 14 | # Initializing Translator and Speech Engine 15 | translator = Translator() 16 | engine = pyttsx3.init() 17 | engine.setProperty("rate", 150) 18 | 19 | Post_lang_Choice = StringVar() 20 | # Setting a default Language for Translation 21 | Post_lang_Choice.set('english') 22 | # Creating a dictionary for the set of Languages available 23 | lang = {'af': 'afrikaans', 'sq': 'albanian', 'am': 'amharic', 'ar': 'arabic', 'hy': 'armenian', 'az': 'azerbaijani', 24 | 'eu': 'basque', 'be': 'belarusian', 'bn': 'bengali', 'bs': 'bosnian', 'bg': 'bulgarian', 'ca': 'catalan', 25 | 'ceb': 'cebuano', 'ny': 'chichewa', 'zh-cn': 'chinese (simplified)', 'zh-tw': 'chinese (traditional)', 26 | 'co': 'corsican', 'hr': 'croatian', 'cs': 'czech', 'da': 'danish', 'nl': 'dutch', 'en': 'english', 27 | 'eo': 'esperanto', 'et': 'estonian', 'tl': 'filipino', 'fi': 'finnish', 'fr': 'french', 'fy': 'frisian', 28 | 'gl': 'galician', 'ka': 'georgian', 'de': 'german', 'el': 'greek', 'gu': 'gujarati', 'ht': 'haitian creole', 29 | 'ha': 'hausa', 'haw': 'hawaiian', 'iw': 'hebrew', 'he': 'hebrew', 'hi': 'hindi', 'hmn': 'hmong', 30 | 'hu': 'hungarian', 'is': 'icelandic', 'ig': 'igbo', 'id': 'indonesian', 'ga': 'irish', 'it': 'italian', 31 | 'ja': 'japanese', 'jw': 'javanese', 'kn': 'kannada', 'kk': 'kazakh', 'km': 'khmer', 'ko': 'korean', 32 | 'ku': 'kurdish (kurmanji)', 'ky': 'kyrgyz', 'lo': 'lao', 'la': 'latin', 'lv': 'latvian', 'lt': 'lithuanian', 33 | 'lb': 'luxembourgish', 'mk': 'macedonian', 'mg': 'malagasy', 'ms': 'malay', 'ml': 'malayalam', 'mt': 'maltese', 34 | 'mi': 'maori', 'mr': 'marathi', 'mn': 'mongolian', 'my': 'myanmar (burmese)', 'ne': 'nepali', 'no': 'norwegian', 35 | 'or': 'odia', 'ps': 'pashto', 'fa': 'persian', 'pl': 'polish', 'pt': 'portuguese', 'pa': 'punjabi', 36 | 'ro': 'romanian', 'ru': 'russian', 'sm': 'samoan', 'gd': 'scots gaelic', 'sr': 'serbian', 'st': 'sesotho', 37 | 'sn': 'shona', 'sd': 'sindhi', 'si': 'sinhala', 'sk': 'slovak', 'sl': 'slovenian', 'so': 'somali', 38 | 'es': 'spanish', 'su': 'sundanese', 'sw': 'swahili', 'sv': 'swedish', 'tg': 'tajik', 'ta': 'tamil', 39 | 'te': 'telugu', 'th': 'thai', 'tr': 'turkish', 'uk': 'ukrainian', 'ur': 'urdu', 'ug': 'uyghur', 'uz': 'uzbek', 40 | 'vi': 'vietnamese', 'cy': 'welsh', 'xh': 'xhosa', 'yi': 'yiddish', 'yo': 'yoruba', 'zu': 'zulu'} 41 | 42 | # Putting language values (not the symbol) into the list 43 | languages = [langs for langs in lang.values()] 44 | 45 | 46 | # Function for Exception of non entry of text 47 | def exc(): 48 | print("Please Enter text first") 49 | engine.say("Please Enter text first") 50 | engine.runAndWait() 51 | 52 | 53 | # Function for Selecting the text File 54 | def TF(): 55 | print("Selecting File....") 56 | text_file = filedialog.askopenfilename(initialdir=r"C:\Users\nandh\OneDrive\Desktop\Minor project", 57 | title="Select a text File to be translated", 58 | filetypes=(("text files", "*.txt"), ("all files", "*.*"))) 59 | print(text_file) 60 | 61 | with open(text_file) as text: 62 | contents = text.read() 63 | print(contents) 64 | Input_TextBox.delete('1.0', END) 65 | Input_TextBox.insert(END, contents) 66 | text.close() 67 | 68 | 69 | # Function for Pronouncing the text using pyttsx3 70 | def Pronounce(): 71 | try: 72 | # Extract language code 73 | key_list = list(lang.keys()) 74 | val_list = list(lang.values()) 75 | end_lan = Post_lang_Choice.get() 76 | end_position = val_list.index(end_lan) 77 | ep = key_list[end_position] 78 | 79 | # Translate the text 80 | trans = translator.translate(Input_TextBox.get("1.0", END), dest=ep) 81 | 82 | # Get pronunciation or fallback to translated text 83 | pronunciation_text = trans.pronunciation if trans.pronunciation else trans.text 84 | print("Pronunciation:", pronunciation_text) 85 | 86 | # Speak the text 87 | engine.say(pronunciation_text) 88 | engine.runAndWait() 89 | except Exception as e: 90 | print("Error in pronunciation:", e) 91 | exc() 92 | 93 | 94 | # Function for Pronouncing the text using gTTS 95 | def Pronounce_with_gTTS(): 96 | try: 97 | # Extract language code 98 | key_list = list(lang.keys()) 99 | val_list = list(lang.values()) 100 | end_lan = Post_lang_Choice.get() 101 | end_position = val_list.index(end_lan) 102 | ep = key_list[end_position] 103 | 104 | # Translate the text 105 | trans = translator.translate(Input_TextBox.get("1.0", END), dest=ep) 106 | 107 | # Generate speech using gTTS 108 | tts = gTTS(text=trans.text, lang=ep) 109 | tts.save("output.mp3") 110 | os.system("start output.mp3") # Use "start" on Windows, "open" on macOS, or "xdg-open" on Linux 111 | except Exception as e: 112 | print("Error in pronunciation:", e) 113 | exc() 114 | 115 | 116 | # Function for Translating the text 117 | def Translate(): 118 | try: 119 | # Extract language code 120 | key_list = list(lang.keys()) 121 | val_list = list(lang.values()) 122 | end_lan = Post_lang_Choice.get() 123 | end_position = val_list.index(end_lan) 124 | ep = key_list[end_position] 125 | print(ep) 126 | 127 | # Translate the text 128 | trans = translator.translate(Input_TextBox.get("1.0", END), dest=ep) 129 | print(trans) 130 | Translated_TextBox.config(state=NORMAL) 131 | Translated_TextBox.delete('1.0', END) 132 | Translated_TextBox.insert(END, trans.text) 133 | Translated_TextBox.config(state=DISABLED) 134 | except Exception as e: 135 | print(e) 136 | exc() 137 | 138 | 139 | # Function to clear the text boxes 140 | def clear(): 141 | Input_TextBox.delete('1.0', END) # Clear input text 142 | Translated_TextBox.config(state=NORMAL) 143 | Translated_TextBox.delete('1.0', END) # Clear translated text 144 | Translated_TextBox.config(state=DISABLED) 145 | 146 | 147 | # Creating OptionMenu, Labels and Buttons on the Window for ease 148 | Post_lang_Menu = OptionMenu(window, Post_lang_Choice, *languages) 149 | Post_lang_Menu.config(bg='#EAB543') 150 | Post_lang_Label = Label(window, text="Choose Translate Language", 151 | font=('System', 12)) 152 | Post_lang_Label.grid(row=0, column=0, padx=10, pady=10, sticky=W) 153 | Post_lang_Menu.grid(row=0, column=1, padx=10, pady=10, sticky=W) 154 | 155 | Input_lang_Label = Label(window, text="Enter Text below", 156 | font=('System', 16), bg='#FD7272') 157 | Input_lang_Label.grid(row=1, column=0, padx=10, pady=10, sticky=W) 158 | 159 | # Create a Text widget and Scrollbar for input 160 | input_frame = Frame(window) 161 | input_frame.grid(row=2, column=0, columnspan=2, padx=10, pady=5) 162 | 163 | input_scrollbar = Scrollbar(input_frame) 164 | input_scrollbar.pack(side=RIGHT, fill=Y) 165 | 166 | Input_TextBox = Text(input_frame, width=60, height=10, font=('Georgia', 14), yscrollcommand=input_scrollbar.set) 167 | Input_TextBox.pack(side=LEFT, fill=BOTH, expand=True) 168 | input_scrollbar.config(command=Input_TextBox.yview) 169 | 170 | # Create a Text widget and Scrollbar for output 171 | output_frame = Frame(window) 172 | output_frame.grid(row=3, column=0, columnspan=2, padx=10, pady=5) 173 | 174 | output_scrollbar = Scrollbar(output_frame) 175 | output_scrollbar.pack(side=RIGHT, fill=Y) 176 | 177 | Translated_TextBox = Text(output_frame, width=60, height=10, font=('Georgia', 14), yscrollcommand=output_scrollbar.set, state=DISABLED) 178 | Translated_TextBox.pack(side=LEFT, fill=BOTH, expand=True) 179 | output_scrollbar.config(command=Translated_TextBox.yview) 180 | 181 | # Creating Buttons 182 | translate_button = Button(window, text="Translate", command=Translate, bg='#6AB04C', font=('System', 12)) 183 | translate_button.grid(row=4, column=0, padx=10, pady=10, sticky=W) 184 | 185 | clear_button = Button(window, text="Clear", command=clear, bg='#F8C471', font=('System', 12)) 186 | clear_button.grid(row=4, column=1, padx=10, pady=10, sticky=W) 187 | 188 | file_button = Button(window, text="Select File", command=TF, bg='#FF6F61', font=('System', 12)) 189 | file_button.grid(row=5, column=0, padx=10, pady=10, sticky=W) 190 | 191 | pronounce_button = Button(window, text="Pronounce", command=Pronounce, bg='#B5EAD7', font=('System', 12)) 192 | pronounce_button.grid(row=5, column=1, padx=10, pady=10, sticky=W) 193 | 194 | pronounce_gtts_button = Button(window, text="Pronounce with gTTS", command=Pronounce_with_gTTS, bg='#F6C6C6', font=('System', 12)) 195 | pronounce_gtts_button.grid(row=6, column=0, columnspan=2, padx=10, pady=10, sticky=W) 196 | 197 | window.mainloop() 198 | 199 | --------------------------------------------------------------------------------