AI / ChatGPT
AI Consultancy

Python Script for Text Translation with Multiple Languages

Posted on October 29, 2023 in AI,ChatGPT,ChatGPT-3.5,languages,Projects,software,Sound,Sound by Stefaan Meeuws

The Python Script is now:

You can add a wide range of languages to the menu for both source and target languages. Here's an extended version of your code with additional languages added to the menu:

import tkinter as tk
from tkinter import filedialog
from googletrans import Translator
from gtts import gTTS
import playsound

def translate_text(text, source_lang, target_lang):
    translator = Translator()
    translated = translator.translate(text, src=source_lang, dest=target_lang)
    return translated.text

def open_file():
    file_path = filedialog.askopenfilename(filetypes=[("Text Files", "*.txt")])
    if file_path:
        with open(file_path, "r", encoding="utf-8") as file:
            source_text.delete(1.0, tk.END)  # Clear existing text

def translate_and_speak():
    source_language = source_language_var.get()
    target_language = target_language_var.get()
    source_text_str = source_text.get(1.0, tk.END)
    translated_text_str = translate_text(source_text_str, source_language, target_language)
    translated_text.delete(1.0, tk.END)
    translated_text.insert(tk.END, translated_text_str)
    speech = gTTS(text=translated_text_str, lang=target_language, slow=False, tld="")"TTSpeech.mp3")

# Create the main window
root = tk.Tk()
root.title("Text Translator")

# Create menu system
menu = tk.Menu(root)

file_menu = tk.Menu(menu)
menu.add_cascade(label="File", menu=file_menu)
file_menu.add_command(label="Open", command=open_file)

language_menu = tk.Menu(menu)
menu.add_cascade(label="Language", menu=language_menu)

source_language_var = tk.StringVar()
target_language_var = tk.StringVar()

source_menu = tk.Menu(language_menu)
language_menu.add_cascade(label="Source", menu=source_menu)
target_menu = tk.Menu(language_menu)
language_menu.add_cascade(label="Target", menu=target_menu)

# Define language codes (You can extend this list)
languages = [
    ("English", "en"),
    ("French", "fr"),
    ("German", "de"),
    ("Spanish", "es"),
    ("Italian", "it"),
    ("Portuguese", "pt"),
    ("Dutch", "nl"),
    ("Russian", "ru"),
    ("Japanese", "ja"),
    ("Korean", "ko"),
    ("Simplified Chinese", "zh-CN"),
    ("Arabic", "ar"),
    ("Hindi", "hi"),
    ("Turkish", "tr"),
    ("Swedish", "sv"),
    ("Polish", "pl"),
    # Add more languages here

for language, code in languages:
    source_menu.add_radiobutton(label=language, variable=source_language_var, value=code)
    target_menu.add_radiobutton(label=language, variable=target_language_var, value=code)

# Create text fields
source_text = tk.Text(root, width=40, height=10)
source_text.grid(row=0, column=0, padx=5, pady=5)

translated_text = tk.Text(root, width=40, height=10)
translated_text.grid(row=0, column=1, padx=5, pady=5)

# Create Translate button
translate_button = tk.Button(root, text="Translate", command=translate_and_speak)
translate_button.grid(row=1, column=0, columnspan=2, pady=5)

# Start the GUI application

In this code, I've added several additional languages to the menu. You can continue to add more languages to the `languages` list as needed.

Comments on 'Python Script for Text Translation with Multiple Languages' (0)

Leave a Reply