← Back to list

🌍✨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…

Unicorn Day Β· 2024-10-06 22:33 Β· 0 claps Β· 2.6 min read paywalled
#language-detection #translation-app #streamlit #translation #multilingual
Open on Medium β†—
Wiki topics: LNG · Linguistics & Language ✈️ · Travel

🌍✨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… πŸš€

  1. 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) 🧐.
  2. 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