> ## Documentation Index
> Fetch the complete documentation index at: https://docs.case.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Detect Language

> Detect the language of text

Identify the language of text with confidence scores. Useful for routing multilingual content or determining if translation is needed.

```bash title="Endpoint" theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
POST /translate/v1/detect
```

<CodeGroup>
  ```bash title="cURL" theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
  curl -X POST https://api.case.dev/translate/v1/detect \
    -H "Authorization: Bearer sk_case_YOUR_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "q": "Bonjour, comment allez-vous?"
    }'
  ```

  ```bash title="CLI" theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
  casedev translate:v1 detect --q "Bonjour, comment allez-vous?"
  ```

  ```typescript title="Typescript" theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
  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
  ```

  ```python title="Python" theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
  import casedev

  client = casedev.Casedev(api_key='sk_case_YOUR_API_KEY')

  result = client.translate.v1.detect(
      q='Bonjour, comment allez-vous?'
  )

  detection = result.data.detections[0][0]
  print(detection.language)   # "fr"
  print(detection.confidence) # 0.98
  ```

  ```go title="Go" theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
  result, _ := client.Translate.V1.Detect(ctx, casedev.TranslateV1DetectParams{
  	Q: casedev.F("Bonjour, comment allez-vous?"),
  })
  fmt.Println(result.Data.Detections[0][0].Language)
  ```
</CodeGroup>

```json title="Response" theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
{
  "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:

<CodeGroup>
  ```bash title="CLI" theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
  casedev translate:v1 detect --q "Bonjour, comment allez-vous?"
  ```

  ```typescript title="Typescript" theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
  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
  ```

  ```python title="Python" theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
  result = client.translate.v1.detect(
      q=[
          'Hello, how are you?',
          'Hola, como estas?',
          'Bonjour, comment ca va?',
          'Guten Tag, wie geht es Ihnen?'
      ]
  )

  for i, detection in enumerate(result.data.detections):
      print(f'Text {i + 1}: {detection[0].language}')
  # Text 1: en
  # Text 2: es
  # Text 3: fr
  # Text 4: de
  ```

  ```go title="Go" theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
  result, _ := client.Translate.V1.Detect(ctx, casedev.TranslateV1DetectParams{
  	Q: casedev.F("Bonjour, comment allez-vous?"),
  })
  fmt.Println(result.Data.Detections[0][0].Language)
  ```
</CodeGroup>

## Examples

### Route content by language

<CodeGroup>
  ```bash title="CLI" theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
  # Detect the language
  casedev translate:v1 detect --q "Your text here"

  # Then translate if needed
  casedev translate:v1 translate --q "Your text here" --target en --source fr
  ```

  ```typescript title="Typescript" theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
  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);
    }
  }
  ```

  ```python title="Python" theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
  async def route_by_language(text: str):
      result = client.translate.v1.detect(q=text)
      lang = result.data.detections[0][0].language

      if lang == 'en':
          return process_english(text)
      elif lang == 'es':
          return process_spanish(text)
      else:
          # Translate to English first
          translated = client.translate.v1.translate(
              q=text,
              target='en',
              source=lang
          )
          return process_english(translated.data.translations[0].translated_text)
  ```

  ```go title="Go" theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
  // Detect the language
  detectResult, _ := client.Translate.V1.Detect(ctx, casedev.TranslateV1DetectParams{
  	Q: casedev.F(text),
  })
  lang := detectResult.Data.Detections[0][0].Language

  switch lang {
  case "en":
  	processEnglish(text)
  case "es":
  	processSpanish(text)
  default:
  	// Translate to English first
  	translated, _ := client.Translate.V1.Translate(ctx, casedev.TranslateV1TranslateParams{
  		Q:      casedev.F(text),
  		Target: casedev.F("en"),
  		Source: casedev.F(lang),
  	})
  	processEnglish(translated.Data.Translations[0].TranslatedText)
  }
  ```
</CodeGroup>

### Filter by language confidence

<CodeGroup>
  ```bash title="CLI" theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
  casedev translate:v1 detect --q "Text to check confidence for"
  ```

  ```typescript title="Typescript" theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
  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');
  }
  ```

  ```python title="Python" theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
  result = client.translate.v1.detect(q=foreign_text)

  detection = result.data.detections[0][0]

  if detection.confidence > 0.9 and detection.is_reliable:
      print(f'Confident detection: {detection.language}')
  else:
      print('Low confidence, may need manual review')
  ```

  ```go title="Go" theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
  result, _ := client.Translate.V1.Detect(ctx, casedev.TranslateV1DetectParams{
  	Q: casedev.F(foreignText),
  })
  detection := result.Data.Detections[0][0]

  if detection.Confidence > 0.9 && detection.IsReliable {
  	fmt.Printf("Confident detection: %s\n", detection.Language)
  } else {
  	fmt.Println("Low confidence, may need manual review")
  }
  ```
</CodeGroup>

### Identify mixed-language documents

<CodeGroup>
  ```bash title="CLI" theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
  # Detect each paragraph separately
  casedev translate:v1 detect --q "First paragraph text"
  casedev translate:v1 detect --q "Second paragraph text"
  ```

  ```typescript title="Typescript" theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
  // 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(', '));
  }
  ```

  ```python title="Python" theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
  # Split document into paragraphs and detect each
  paragraphs = document.split('\n\n')

  result = client.translate.v1.detect(q=paragraphs)

  languages = set(d[0].language for d in result.data.detections)

  if len(languages) > 1:
      print('Mixed-language document detected')
      print('Languages found:', ', '.join(languages))
  ```

  ```go title="Go" theme={"theme":{"light":"github-light","dark":"one-dark-pro"}}
  // Split document into paragraphs and detect each
  paragraphs := strings.Split(document, "\n\n")

  result, _ := client.Translate.V1.Detect(ctx, casedev.TranslateV1DetectParams{
  	Q: casedev.F(paragraphs),
  })

  languages := make(map[string]bool)
  for _, d := range result.Data.Detections {
  	languages[d[0].Language] = true
  }

  if len(languages) > 1 {
  	fmt.Println("Mixed-language document detected")
  }
  ```
</CodeGroup>

<Info>
  **Pricing:** \$0.05 per 1,000 characters, same as translation.
</Info>
