import streamlit as st from googletrans import Translator, LANGUAGES # Streamlit app st.title("Text Translator") # Input text user_text = st.text_area("Enter the text you want to translate:") # Language selection st.subheader("Select the target language for translation:") # Create a list of language codes and names for selection language_options = {lang: code for code, lang in LANGUAGES.items()} selected_language = st.selectbox("Select a language:", list(language_options.keys())) # Initialize the translator translator = Translator() # Translate the text when the user clicks the "Translate" button if st.button("Translate"): try: # Translate the text to the selected language translated_text = translator.translate(user_text, dest=language_options[selected_language]).text st.success(f"Translated text: {translated_text}") except Exception as e: st.error(f"Translation failed: {str(e)}")