import streamlit as st from translate import Translator, LANGUAGES # Streamlit app st.title("Text Translator") # Input text user_text = st.text_area("Enter the text you want to translate:") # Target language selection with a unique key st.subheader("Select the target language for translation:") target_language = st.selectbox("Select a target language:", [lang for lang in LANGUAGES.values()]) # Get the language code for the selected target language selected_target_language_code = next(code for code, lang in LANGUAGES.items() if lang == target_language) # Initialize the translator translator = Translator(to_lang=selected_target_language_code) # Translate the text when the user clicks the "Translate" button if st.button("Translate"): try: # Translate the input text to the selected target language translated_text = translator.translate(user_text) # Get the full name of the translated language translated_language = next(lang for code, lang in LANGUAGES.items() if code == selected_target_language_code) st.success(f"Translated text ({translated_language}): {translated_text}") except Exception as e: st.error(f"Translation failed: {str(e)}")