DevelopmentNovember 01, 20243 min read

Translator App using Python, Streamlit, and Deep Translator

Build a multi-language translator with Python and Streamlit using deep-translator — translate text and entire documents across 100+ languages.

Galvan

Galvan

Founder & Creator

Introduction

This project walks you through building a multi-language translator app with Python, Streamlit, and the deep-translator library. You can translate text input — or upload an entire .txt document — across 100+ languages using Google Translate under the hood. Translation pairs naturally with text understanding — see the companion sentiment analysis app for the NLP side.

Prerequisites

  • Python 3.8+python.org
  • Libraries:
  • code
    pip install streamlit deep-translator

    Step 1: Create the Script

    Create translator_app.py and paste the following:

    code
    import streamlit as st
    from deep_translator import GoogleTranslator
    
    st.set_page_config(page_title="Translator App", page_icon="🌍")
    st.title("🌍 Multi-Language Translator")
    
    LANGUAGES = GoogleTranslator().get_supported_languages(as_dict=True)
    lang_names = list(LANGUAGES.keys())
    
    col1, col2 = st.columns(2)
    with col1:
        source_lang = st.selectbox("Source Language", ["auto"] + lang_names)
    with col2:
        target_lang = st.selectbox("Target Language", lang_names,
                                   index=lang_names.index("english"))
    
    tab1, tab2 = st.tabs(["Translate Text", "Translate Document"])
    
    with tab1:
        text_input = st.text_area("Enter text to translate:", height=150)
        if st.button("Translate Text") and text_input:
            src = "auto" if source_lang == "auto" else LANGUAGES[source_lang]
            tgt = LANGUAGES[target_lang]
            result = GoogleTranslator(source=src, target=tgt).translate(text_input)
            st.subheader("Translation:")
            st.success(result)
    
    with tab2:
        uploaded = st.file_uploader("Upload a .txt file", type=["txt"])
        if st.button("Translate Document") and uploaded:
            content = uploaded.read().decode("utf-8")
            src = "auto" if source_lang == "auto" else LANGUAGES[source_lang]
            tgt = LANGUAGES[target_lang]
            result = GoogleTranslator(source=src, target=tgt).translate(content[:5000])
            st.text_area("Translated Document:", result, height=300)
            st.download_button("Download Translation", result, file_name="translated.txt")

    Step 2: Run the App

    code
    streamlit run translator_app.py

    Step 3: Use the App

  • Pick source and target languages from the dropdowns.
  • Use the Translate Text tab for quick text translation.
  • Use the Translate Document tab to upload and translate a .txt file.
  • Click Download Translation to save the output.
  • How It Works

    The GoogleTranslator class from deep-translator does the heavy lifting: you specify a source and target language code, call .translate(text), and get a string back. The library handles the HTTP call to Google's endpoint, chunking of long text, and retries — which is why it is the go-to choice over raw requests.

    The Streamlit layer is two st.selectbox widgets for languages (populated from GoogleTranslator().get_supported_languages()), a text area, and a button. Gating the translation behind st.button("Translate") is deliberate: without it, every keystroke-triggered rerun would fire an API call, which is slow and can get rate-limited.

    Language codes are ISO 639-1 strings ("en", "hi", "fr"). Mapping friendly names to codes with a small dictionary keeps the UI clean and the API calls valid.

    Key Concepts

  • `GoogleTranslator` — Wraps Google Translate with a clean Python API.
  • `get_supported_languages()` — Returns a dict of all supported language names and codes.
  • `st.tabs()` — Creates a tabbed interface separating text and document modes.
  • `st.download_button()` — Lets users save the translated content as a file.
  • What to Try Next

  • Support PDF and DOCX files using PyMuPDF or python-docx.
  • Show a detected source language label when using the auto-detect mode.
  • Add a character counter to display the remaining translation limit.
  • Common Errors & Fixes

  • NoTranslationError / empty output — the source text is blank or only punctuation. Validate with if not text.strip(): st.warning(...) before calling the translator.
  • Invalid destination language — you passed a display name ("Hindi") where a code ("hi") is expected. Build the name→code map once at startup.
  • Random network failures — Google's endpoint occasionally throttles. Wrap the call in try/except and retry once after a short sleep.
  • Very long text gets cut — translate in chunks of ~4500 characters and join the results.
  • Translation returns the original text — source and target language codes are identical (both "en"); check the selectbox defaults.
  • App hangs on submit — the network call runs without feedback; wrap it in with st.spinner("Translating..."): so the UI stays responsive while waiting.
  • FAQ

    Is deep-translator free?

    Yes — it uses the free Google endpoint without an API key. For production volume, switch to the official Cloud Translation API.

    Can I auto-detect the source language?

    Yes — pass source="auto" and Google detects it per request.

    How do I translate a whole file?

    Read the file, split into paragraphs, translate each, and join — or render a side-by-side view with st.columns(2), a pattern shared with the quiz app's layout.

    Does it work offline?

    No — deep-translator calls Google's endpoint over the network. For offline translation you would need a local model like Argos Translate.