DEV Community

Alexey D
Alexey D

Posted on

Detect Text Language in Python — Free API, 55 Languages & Confidence Score

How to Detect Text Language in Python — 55 Languages, Free API

Language detection sounds simple until you try to build it yourself. Rule-based heuristics fail on short text. Running a full NLP model on your server is overkill for a lookup operation. And most free libraries top out at 20–30 languages.

This tutorial shows how to detect the language of any text in Python and JavaScript using a free API that supports 55+ languages and returns a confidence score so you can decide what to do with low-confidence results.

The API: Language Detector Pro

👉 Language Detector Pro on RapidAPI

  • 55+ languages detected
  • Confidence score (0.0 – 1.0)
  • ISO 639-1 language codes
  • Works on short strings (tweets, user inputs, form fields)
  • Batch detection endpoint

Sample Response

\json
{
\"text\": \"Bonjour le monde\",
\"language\": \"French\",
\"language_code\": \"fr\",
\"confidence\": 0.98
}
\
\

Python Example

\`python
import requests

def detect_language(text):
url = \"https://language-detector-pro.p.rapidapi.com/detect\"
headers = {
\"X-RapidAPI-Key\": \"YOUR_KEY\",
\"Content-Type\": \"application/json\"
}
r = requests.post(url, json={\"text\": text}, headers=headers)
data = r.json()
return {
\"language\": data[\"language\"],
\"code\": data[\"language_code\"],
\"confidence\": data[\"confidence\"]
}

print(detect_language(\"Bonjour le monde\"))

{\"language\": \"French\", \"code\": \"fr\", \"confidence\": 0.98}

print(detect_language(\"Привет мир\"))

{\"language\": \"Russian\", \"code\": \"ru\", \"confidence\": 0.99}

print(detect_language(\"こんにちは世界\"))

{\"language\": \"Japanese\", \"code\": \"ja\", \"confidence\": 0.97}

`\

JavaScript Example

\javascript
async function detectLanguage(text) {
const res = await fetch(\"https://language-detector-pro.p.rapidapi.com/detect\", {
method: \"POST\",
headers: {
\"X-RapidAPI-Key\": \"YOUR_KEY\",
\"Content-Type\": \"application/json\"
},
body: JSON.stringify({ text })
});
const data = await res.json();
console.log(\
\${data.language} (\${data.language_code}) — confidence: \${data.confidence}`);
return data;
}

detectLanguage(\"Hola mundo\");
// Spanish (es) — confidence: 0.99
`\

Batch Detection

\`python
texts = [\"Bonjour le monde\", \"Привет мир\", \"Hello world\", \"مرحبا بالعالم\"]

r = requests.post(
\"https://language-detector-pro.p.rapidapi.com/batch\",
json={\"texts\": texts},
headers={\"X-RapidAPI-Key\": \"YOUR_KEY\", \"Content-Type\": \"application/json\"}
)

for item in r.json():
print(f\"{item['language_code']}: {item['text'][:30]:<30} conf:{item['confidence']:.2f}\")
`\

Supported Languages (55+)

English, Spanish, French, German, Italian, Portuguese, Russian, Arabic, Chinese, Japanese, Korean, Dutch, Polish, Turkish, Swedish, Norwegian, Danish, Finnish, Hebrew, Hindi, Bengali, Vietnamese, Thai, Indonesian, and more.

Practical Use Cases

  • Content moderation — route text to the right language moderator
  • User onboarding — auto-detect locale and switch UI language
  • Support tickets — auto-assign to agents by language
  • Search engines — apply the right stemmer per language

Free Tier

Free tier available — no credit card required.

👉 Subscribe here: Language Detector Pro on RapidAPI


Also check out: Phone Validator Pro | Email Validator Pro

Top comments (0)