demo · v138

Confidence ranking

The API doesn’t just return one language — it ranks every candidate with a confidence score. The thoughtful product surface is to set a threshold and fall back to “language not detected” below it, rather than confidently mislabel a 20-character message.

On-device model. First call may download the language pack. If the API isn’t available (older Chrome, no model), the demo uses a tiny built-in heuristic so the UI still works for illustration.
checking LanguageDetector availability…
0.50
awaiting…

the call

const detector = await LanguageDetector.create();
const results = await detector.detect(text);
// results is a ranked array:
// [{ detectedLanguage: "fr", confidence: 0.81 },
//  { detectedLanguage: "en", confidence: 0.14 },
//  { detectedLanguage: "und", confidence: 0.05 }]
const top = results[0];
if (top.confidence < 0.5) { /* show "language unknown" fallback */ }

This shape mirrors browser hint negotiation: ranked candidates plus a confidence score, so product code can decide its own threshold. The motivating use case from the explainer: chat apps showing “Translate?” only when confident the message is in a different language than the user’s preference.

see also