Translator App using Python, Streamlit, and Deep Translator

 Introduction:

This project guides you through building a language translation web application using Python, Streamlit, and the deep_translator library. Translate text between various languages with ease.

Project Prerequisites:

  • Python: Ensure Python is installed. Download from: Python Official Website
  • Streamlit: Install the Streamlit library:
pip install streamlit
  • Deep Translator: Install the deep_translator library:
pip install deep_translator

Getting Started:

Step 1: Install the Required Libraries
Open your terminal or CLI and install the libraries:

pip install streamlit deep_translator

Step 2: Create a New Python Script
Open your text editor or IDE and create a new Python file (e.g., translator_app.py).

Step 3: Write the Code
Copy and paste the following code into your script:

import streamlit as st
from deep_translator import GoogleTranslator
st.title("Translator App")
# Get text input
text = st.text_area("Enter text to translate:")
# Select target language
target_language = st.selectbox("Select target language", ["es", "fr", "de", "ja", "ko", "zh-CN"])
if st.button("Translate"):
if text:
# Translate using deep_translator
translated = GoogleTranslator(source='auto', target=target_language).translate(text)
st.write("Translation:")
st.write(translated)
else:
st.warning("Please enter some text.")

Step 4: Run the Application
Save the script. In your terminal, navigate to where you saved the file and run:

streamlit run translator_app.py

Step 5: Use the Translator App
The translator app will open in your browser. Enter the text you want to translate, select the target language from the dropdown menu, and click “Translate”. The translated text will be displayed below.

Guide:

  • st.title(): Sets the title of your app.
  • st.text_area(): Creates a text input area for the user.
  • st.selectbox(): Creates a dropdown menu to select the target language.
  • GoogleTranslator(source='auto', target=target_language): Creates a GoogleTranslator object.
  • source='auto' automatically detects the source language.
  • target=target_language sets the target language.
  • .translate(text): Translates the input text.
  • st.write(): Displays the translated text.

Conclusion:
This translator app provides a simple way to translate text between different languages. The deep_translator library offers more stability and language options compared to other libraries. You can further enhance this app by:

  • Adding more languages to the target_language selection.
  • Implementing source language selection.
  • Adding error handling to gracefully handle translation errors or network issues.
  • Improving the user interface with different layouts and styling options.
  • Exploring other translation providers supported by deep_translator (Microsoft Translator, DeepL, etc.).

Advertisement