πβ¨The Power of Language Detection and Translationβ¦ With Code!
Imagine youβre traveling the worldβ¦ chatting with localsβ¦ and suddenlyβ¦ youβre faced with a text in a language you canβt decipher π². Whatβ¦
πβ¨The Power of Language Detection and Translationβ¦ With Code!

Imagine youβre traveling the worldβ¦ chatting with localsβ¦ and suddenlyβ¦ youβre faced with a text in a language you canβt decipher π². What if there was an app that could tell you what language it isβ¦ and magically translate it into English for you in seconds? π
Wellβ¦ the solution is here! Enter our Language Detection and Translation App powered by Python, Streamlit, and a sprinkle of magic from Google Translate. Let me take you on a journey through how you can build this powerful appβ¦ step-by-step! π
Why is Language Detection So Cool? π§
Language is the key to understanding the worldβ¦ but with thousands of languages spoken, how can we detect them accurately? π€ Thatβs where langdetect comes in handy! This Python library guesses the language of any text by analyzing patterns and probabilitiesβ¦ like a detective on a mission! π΅οΈββοΈ
The Ingredients Youβll Needβ¦ π²
To whip up this awesome app, youβll need:
- Streamlit: Our simple yet elegant app builder π οΈ
- Langdetect: The detective that figures out what language youβre dealing with π
- Google Translate: The magical wand that translates any text into English
Letβs Get Cooking! π¨βπ³
Hereβs the recipeβ¦ I mean, the codeβ¦ for the Language Detection and Translation App:
import streamlit as st
from langdetect import detect, detect_langs, DetectorFactory
from langdetect.lang_detect_exception import LangDetectException
import pycountry
from googletrans import Translator
# Ensure consistent results by fixing the seed of the detector
DetectorFactory.seed = 0
# Initialize the Google Translator
translator = Translator()
# Helper function to get language name from code
def get_language_name(lang_code):
# Handle specific cases for zh-cn and zh-tw
if lang_code == "zh-cn":
return "Chinese (Simplified)"
elif lang_code == "zh-tw":
return "Chinese (Traditional)"
try:
return pycountry.languages.get(alpha_2=lang_code).name
except AttributeError:
return "Unknown language"
# Streamlit App
st.title("Language Detection and Translation App")
st.write("This app detects the language of the given text and translates it to English.")
# Input text area
text = st.text_area("Enter text to detect its language and translate to English:")
if text:
try:
# Detect the language of the text
language_code = detect(text)
language_name = get_language_name(language_code)
st.subheader("Detected Language:")
st.write(f"Language: {language_name} (Code: {language_code})")
# Translation to English
translation = translator.translate(text, dest='en')
st.subheader("Translation to English:")
st.write(translation.text)
except LangDetectException:
st.error("Couldn't detect the language. Please enter a longer text.")
except Exception as e:
st.error(f"An error occurred: {str(e)}")
Code Flow Diagram π
To make things even clearer, hereβs a visual diagram of how the app works! πΌοΈ
ββββββββββββββββββββββββββββββββ
β User Inputs Text β
ββββββββββββββββ¬ββββββββββββββββ
β
βββββββββββββΌβββββββββββββ
β Language Detection β
β (detect function) β
βββββββββββββ¬βββββββββββββ
β
ββββββββββββββββΌβββββββββββββββ
β Identify Language Code β
β (e.g., 'en' for English) β
ββββββββββββββββ¬βββββββββββββββ
β
βββββββββββββββΌββββββββββββββββ
β Get Full Language Name β
β (e.g., 'English', 'Spanish') β
βββββββββββββββ¬ββββββββββββββββ
β
βββββββββββββββΌββββββββββββββββ
β Translate Text to English β
β (Google Translate API) β
βββββββββββββββ¬ββββββββββββββββ
β
βββββββββββββββΌββββββββββββββββ
β Display Results in Streamlit β
β (Detected Language + Trans.) β
βββββββββββββββββββββββββββββββ
How it Worksβ¦ π
- Language Detection: Once you enter a textβ¦ our app uses langdetect to identify the language by analyzing the patterns and structure. It returns the language code (like βesβ for Spanish or βfrβ for French) π§.
- Translation: After detecting the languageβ¦ the app uses Google Translate to convert the text into English! Itβs as simple as typing, waiting a second, and voilΓ ! β¨ Your mystery text is transformedβ¦ and you can finally understand what it says!
Letβs Put it to the Test! β‘
Donβt just take my word for itβ¦ Try the live app yourself! π Language Detection and Translation App πβ¨
Test it out with a variety of languages and see the magic in action!
Error-Proof? You Bet! πͺ
And what happens if you enter too short of a text? Our app will kindly tell youβ¦ βOops, we couldnβt detect the language! Try a longer sentenceβ¦β π
λ©νλ°μ΄ν°
- post_id
- 240dfb22e0bc
- slug
- the-power-of-language-detection-and-translation-with-code-240dfb22e0bc
- url
- https://medium.com/@wl8380/the-power-of-language-detection-and-translation-with-code-240dfb22e0bc
- canonical_url
- https://medium.com/@wl8380/the-power-of-language-detection-and-translation-with-code-240dfb22e0bc
- author_url
- https://medium.com/@wl8380
- status
- ok
- fetched_at
- 2026-08-06 05:25:01