Identify the language of text with confidence scores. Useful for routing multilingual content or determining if translation is needed.
POST /translate/v1/detect
import Casedev from 'casedev';
const client = new Casedev({ apiKey: 'sk_case_YOUR_API_KEY' });
const result = await client.translate.v1.detect({
q: 'Bonjour, comment allez-vous?'
});
const detection = result.data.detections[0][0];
console.log(detection.language); // "fr"
console.log(detection.confidence); // 0.98
{
"data": {
"detections": [
[
{
"language": "fr",
"confidence": 0.98,
"isReliable": true
}
]
]
}
}
Parameters
| Parameter | Type | Required | Description |
|---|
q | string or string[] | Yes | Text to detect language for. Can be a single string or array for batch detection. |
Response
The response contains an array of detections. Each detection includes:
| Field | Type | Description |
|---|
language | string | Detected language code (ISO 639-1) |
confidence | number | Confidence score from 0 to 1 |
isReliable | boolean | Whether the detection is considered reliable |
Batch detection
Detect languages for multiple texts in a single request:
const result = await client.translate.v1.detect({
q: [
'Hello, how are you?',
'Hola, como estas?',
'Bonjour, comment ca va?',
'Guten Tag, wie geht es Ihnen?'
]
});
result.data.detections.forEach((detection, i) => {
console.log(`Text ${i + 1}: ${detection[0].language}`);
});
// Text 1: en
// Text 2: es
// Text 3: fr
// Text 4: de
Examples
Route content by language
async function routeByLanguage(text: string) {
const result = await client.translate.v1.detect({ q: text });
const lang = result.data.detections[0][0].language;
switch (lang) {
case 'en':
return processEnglish(text);
case 'es':
return processSpanish(text);
default:
// Translate to English first
const translated = await client.translate.v1.translate({
q: text,
target: 'en',
source: lang
});
return processEnglish(translated.data.translations[0].translatedText);
}
}
Filter by language confidence
const result = await client.translate.v1.detect({
q: foreignText
});
const detection = result.data.detections[0][0];
if (detection.confidence > 0.9 && detection.isReliable) {
console.log(`Confident detection: ${detection.language}`);
} else {
console.log('Low confidence, may need manual review');
}
Identify mixed-language documents
// Split document into paragraphs and detect each
const paragraphs = document.split('\n\n');
const result = await client.translate.v1.detect({
q: paragraphs
});
const languages = new Set(
result.data.detections.map(d => d[0].language)
);
if (languages.size > 1) {
console.log('Mixed-language document detected');
console.log('Languages found:', [...languages].join(', '));
}
Pricing: $0.05 per 1,000 characters, same as translation.