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
pip install streamlit deep-translatorStep 1: Create the Script
Create translator_app.py and paste the following:
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
streamlit run translator_app.pyStep 3: Use the App
.txt file.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
What to Try Next
PyMuPDF or python-docx.Common Errors & Fixes
if not text.strip(): st.warning(...) before calling the translator."Hindi") where a code ("hi") is expected. Build the name→code map once at startup.try/except and retry once after a short sleep."en"); check the selectbox defaults.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.