# Import necessary libraries import streamlit as st from deep_translator import GoogleTranslator # Function to translate text and provide pronunciation def translate_text(text, target_language): """ Translate text to the target language and provide its pronunciation. :param text: Input text to be translated. :param target_language: Language code ('en' for English, 'ur' for Urdu, 'zh-cn' for Chinese, etc.) :return: Translated text and pronunciation. """ try: # Translate the text using deep-translator translated = GoogleTranslator(source='auto', target=target_language).translate(text) # Provide a placeholder for pronunciation pronunciation = translated # For simplicity, use translated text as pronunciation return translated, pronunciation except Exception as e: return f"Error: {str(e)}", "" # App UI st.title("Language Translator and Pronunciation Tool 🌍🗣️") st.markdown(""" This app helps you translate text between different languages and provides a pronunciation guide. 1. Select the translation mode (input and output language pair). 2. Enter text to translate. 3. View the translated text and pronunciation. """) # Translation modes language_options = { "English to Urdu 🇺🇸➡️🇵🇰": "ur", "Urdu to English 🇵🇰➡️🇺🇸": "en", "English to Chinese 🇺🇸➡️🇨🇳": "zh-cn", "English to Italian 🇺🇸➡️🇮🇹": "it", "English to German 🇺🇸➡️🇩🇪": "de", "English to Japanese 🇺🇸➡️🇯🇵": "ja", "English to Korean 🇺🇸➡️🇰🇷": "ko" } # Select translation mode choice = st.selectbox("Select translation mode", options=list(language_options.keys())) # Text input text = st.text_area("Enter text to translate:", placeholder="Type your text here...") # Translate button if st.button("Translate"): if not text.strip(): st.warning("Please enter text to translate!") else: target_language = language_options.get(choice, None) if target_language: translated_text, pronunciation = translate_text(text, target_language) st.subheader("Translated Text") st.success(translated_text) st.subheader("Pronunciation") st.info(pronunciation) else: st.error("Invalid translation mode selected!")