Skip to content
Content Generation & Placeholders

Language detection

The language detection API names the language of a text and returns a two-letter code. Needs 100 characters; detailed=1 adds alternatives and margin.

GET https://api2.globus.studio/v2/language

Reference

Parameters

Name Type Required Description
text string yes Text, minimum 100 characters after punctuation and digits are removed.
detailed bool no Add up to five alternatives, confidence and margin. Default: false.
format string no json or jsonp. Default: json.
callback string no JSONP callback name. Default: callback.

Example request

curl -s "https://api2.globus.studio/v2/language?text=ThisisasimpleEnglishsentenceaboutAPIsanddeveloperswhobuildwebservicesforotherteamseveryday.&format=json"
Open documentation

Language detection

Language detection names the language of a text and returns its two-letter code. Send a paragraph, get back english, en, and the length of the text that was actually analysed.

The language detection API covers 26 languages with Latin, Cyrillic, Hebrew and Arabic scripts, including English, Russian, Ukrainian, German, French, Spanish, Portuguese, Italian, Dutch, Danish, Swedish, Norwegian, Finnish, Romanian, Catalan, Estonian, Albanian and Hebrew.

What the language detection API returns

{
    "success": true,
    "language": "english",
    "code": "en",
    "name": "English",
    "text_length": 108
}

text_length counts characters after punctuation and digits are removed. That is the same text the model sees, so it explains a rejected request better than the raw input length.

With detailed=1 the response also carries up to five alternatives, the winner’s confidence and the margin to the runner-up.

{
    "language": "english",
    "code": "en",
    "confidence": 14.62,
    "margin": 2.61,
    "alternatives": [
        {"language": "english", "code": "en", "confidence": 14.62},
        {"language": "danish", "code": "da", "confidence": 12.01},
        {"language": "portuguese", "code": "pt", "confidence": 10}
    ]
}

confidence is an n-gram similarity index from 0 to 100, not a probability. Read margin instead: the detector treats a lead under 0.5 points as ambiguous, and a lead above two points on a full paragraph is a solid answer.

Why the minimum is 100 characters

The detector builds a profile from character sequences and compares it against language models. A sentence or two is not enough data: the profile stays close to several languages at once.

The floor is measured, not guessed. An English sentence of 58 characters came back as Danish. The same text expanded to 109 characters came back as English. Shorter input produced confident wrong answers. A detector that answers wrong with confidence is worse than one that refuses.

The language detection API rejects text under 100 characters with Text too short for detection (min 100 chars) and the cleaned length. If a caller has only a headline, it should collect the surrounding paragraphs before asking.

What it does not do

Language detection does not translate. It does not tell you the script, the encoding or the country. It does not detect mixed-language pages reliably: a Russian page with English code samples can land on either language depending on the balance.

It does not verify that the text is correct in that language either. A machine translation reads like the target language to a character model and still fails a reader. For that problem, use readability metrics with the same language and look at the sentence statistics.

A similarity index of 14 looks low next to a probability and is not one. It measures how much of the text fits a language profile, and the absolute value depends on the length of the paragraph. Compare candidates inside one response, not across requests. The margin between first and second place is the number that answers “did it really win”.

Where detection earns its place

Three jobs fit the endpoint well:

  • route incoming text to the right queue: a support form in three languages, sorted before a human opens it;
  • check a translation before publishing: the page should come back as the language you meant;
  • tag a corpus you inherited: thousands of short documents with no metadata.

The code fits BCP 47 for these languages, so it can be stored next to a locale or used in a lang attribute.

Where it should not be used

Do not gate access on a language detection API result. A user who writes in another language would be locked out of a page they can read. Do not use it to decide whether a message is abusive, and do not treat the result as evidence about a person’s country. A language is a property of the text, not of the author.

The endpoint accepts GET, POST and PUT, and every parameter can arrive in the query string or a JSON body. A large paragraph is easier to send in a body, where length limits and encoding are not a concern.

For the length and complexity of the same text, pair language detection with readability metrics. For dummy text in a chosen length while building a layout, the text placeholder API is quicker than writing filler text by hand.

Further reading: RFC 5646, Tags for Identifying Languages.

Custom work

Need an API that does not exist yet?

Describe the task and the response format. We will estimate the endpoint, the integration and the maintenance.

Start a conversation