Language Detection
Discuse identifies the language of a message so you can apply the right moderation rules before acting on it. Send text to POST https://api.discuse.com/api/v2/check with check_language enabled, and read the detected language code from results.language.language. Add expected_language to flag content that is not in the language you require.
What is language detection for?
Detecting language up front lets you:
- Apply language-specific moderation thresholds
- Route content to the right reviewers or support team
- Enforce a community language policy
- Filter or localize feeds by language
How do I detect a language?
curl -X POST https://api.discuse.com/api/v2/check \
-H "Content-Type: application/json" \
-H "X-API-Key: YOUR_API_KEY" \
-d '{
"content": {
"text": "Bonjour, comment allez-vous aujourd'\''hui?"
},
"settings": {
"check_language": true
}
}'
Response format
{
"has_violations": false,
"cached": false,
"results": {
"language": {
"language": "fr",
"confidence": 0.99,
"hit": false
}
}
}
The detected language code is in results.language.language. When expected_language enforcement is on, detected and expected are also populated (see below).
What fields does the language result return?
| Field | Type | Meaning |
|---|---|---|
language |
string | Detected language code (e.g. en, fr, es) |
confidence |
number | Detection confidence (0.0–1.0) |
expected |
string | The enforced language code, when expected_language is set |
detected |
string | Detected code (alias of language, populated during enforcement) |
hit |
bool | True when the detected language does not match expected |
delete_only |
bool | When true, the message should be deleted without further punishment |
error |
string | Present only when detection failed |
Which languages are supported?
Discuse returns ISO-style language codes. Commonly detected languages include:
| Code | Language | Code | Language |
|---|---|---|---|
en |
English | de |
German |
es |
Spanish | fr |
French |
it |
Italian | pt |
Portuguese |
nl |
Dutch | pl |
Polish |
ru |
Russian | uk |
Ukrainian |
zh |
Chinese | ja |
Japanese |
ko |
Korean | ar |
Arabic |
hi |
Hindi | tr |
Turkish |
Read the value from results.language.language directly rather than hardcoding a fixed list.
How do I enforce a specific language?
Set expected_language to the code you require. When the detected language differs, language.hit is true and expected/detected are filled in:
Request:
{
"content": { "text": "Hola, cómo estás?" },
"settings": {
"check_language": true,
"expected_language": "en"
}
}
Response:
{
"has_violations": true,
"message": "Content is not in expected language",
"results": {
"hits": true,
"language": {
"language": "es",
"detected": "es",
"expected": "en",
"confidence": 0.97,
"hit": true
}
}
}
Use cases
English-only forums
async function validatePost(post) {
const result = await fetch('https://api.discuse.com/api/v2/check', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-API-Key': process.env.DISCUSE_API_KEY
},
body: JSON.stringify({
content: { text: post.content },
settings: { check_language: true, expected_language: 'en' }
})
}).then(r => r.json());
if (result.results.language.hit) {
return {
approved: false,
reason: 'Posts must be in English',
detected_language: result.results.language.language
};
}
return { approved: true };
}
Multi-language routing
async function routeContent(content) {
const result = await checkLanguage(content.text);
const language = result.results.language.language;
const moderatorQueue = {
en: 'english-moderation',
es: 'spanish-moderation',
fr: 'french-moderation',
de: 'german-moderation',
default: 'general-moderation'
};
const queue = moderatorQueue[language] || moderatorQueue.default;
await addToQueue(queue, content);
return { queued: true, language };
}
Combining with content moderation
Run language detection alongside sentiment and spam in one request:
{
"content": {
"text": "User message in any language"
},
"settings": {
"check_language": true,
"check_sentiment": true,
"check_spam": true
}
}
The detected language can then drive your per-language sentiment thresholds:
const LANGUAGE_THRESHOLDS = {
en: { toxicity: 0.7, profanity: 0.6 },
de: { toxicity: 0.6, profanity: 0.5 },
es: { toxicity: 0.7, profanity: 0.7 },
default: { toxicity: 0.7, profanity: 0.6 }
};
async function moderateContent(text) {
const result = await checkText(text); // check_language + check_sentiment
const language = result.results.language.language;
const thresholds = LANGUAGE_THRESHOLDS[language] || LANGUAGE_THRESHOLDS.default;
const sentiment = result.results.sentiment;
if (sentiment.toxicity > thresholds.toxicity) {
return { action: 'block', reason: 'toxic_content' };
}
if (sentiment.profanity > thresholds.profanity) {
return { action: 'flag', reason: 'profanity' };
}
return { action: 'allow' };
}
Best practices
Account for short text
Detection is less reliable on very short strings. Skip the check below a minimum length:
async function smartLanguageCheck(text) {
if (text.length < 20) {
return { language: 'unknown', confidence: 0 };
}
const result = await checkLanguage(text);
return result.results.language;
}
Cache results
async function getLanguageWithCache(text, contentId) {
const cached = await cache.get(`lang:${contentId}`);
if (cached) return JSON.parse(cached);
const result = await checkLanguage(text);
const language = result.results.language;
await cache.set(`lang:${contentId}`, JSON.stringify(language), 'EX', 3600);
return language;
}
Usage limits
Language detection draws from your text-analysis quota:
| Plan | Monthly Analyses |
|---|---|
| Basic | 1,000 |
| Gold | 5,000 |
| Platinum | 15,000 |
| Ultimate | 30,000 |
Cached responses do not count against your quota.
Integration examples
Node.js
const checkLanguage = async (text, expectedLanguage = null) => {
const settings = { check_language: true };
if (expectedLanguage) settings.expected_language = expectedLanguage;
const response = await fetch('https://api.discuse.com/api/v2/check', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-API-Key': process.env.DISCUSE_API_KEY
},
body: JSON.stringify({ content: { text }, settings })
});
return response.json();
};
Python
import os
import requests
def check_language(text, expected_language=None):
settings = {'check_language': True}
if expected_language:
settings['expected_language'] = expected_language
response = requests.post(
'https://api.discuse.com/api/v2/check',
headers={
'Content-Type': 'application/json',
'X-API-Key': os.environ['DISCUSE_API_KEY']
},
json={'content': {'text': text}, 'settings': settings}
)
return response.json()
Next steps
- Text Analysis - combine with sentiment and spam detection
- Spam Detection - classify promotional and scam content
- Quick Start Guide - get your first API key