v157 · built-in ai · android

Language Detector API on Android

Detect the language of a piece of text on-device, with confidence scores. This page probes both routes the platform exposes — LanguageDetector.availability() and the older translation.canDetect() — reports the raw values it gets, and labels every result with whether it came from the model or from the page's own fallback.

Built-in AI APIs need Chrome with the relevant flag or origin trial, and the model downloads once per profile. Nothing here claims the model is ready when it is not: the probe below shows the value the browser actually returned, and when it is not a usable value the page says so and falls back to its own heuristic detector.

concepts

Five hands-on pages for the Android-specific questions: is the model on this phone, what does it cost to get it, what exactly does detect() return, how does it refuse, and how does it compose with the Translator API on Android (v161). None of them downloads a model unless you press a button that says it will.

  1. Availability & expected languages

    The basic call. Read the raw LanguageDetector.availability() value with and without expectedInputLanguages, sweep your navigator.languages one tag at a time, and see Chromium's supported-language list and the RangeError for invalid tags.

  2. Download on a constrained device

    An inspection tool that combines connection type, Save-Data, deviceMemory, storage and online state with availability to decide: detect now, ask first, or wait. The download only starts on your tap, with a <progress> bar fed by downloadprogress, plus cancel, retry and destroy().

  3. Confidence inspector

    The raw ranked array from detect(), checked against Chromium's rules (descending order, trailing und, sum of 1, iw→he), plus a length ramp showing how short phone messages lose confidence, and inputQuota / measureInputUsage().

  4. Failure & policy gates

    Every refusal run for real: invalid tag, unsupported language, aborted signal, an iframe denied language-detector, no user gesture, secure-context and worker exposure, and what we found (and didn't find) about enterprise policy.

  5. Detect-then-translate composer

    A practical phone-first chat reply box: detect the language you wrote in, then translate with Translator only if the pack is already installed. Low-confidence or missing-model messages are sent untranslated and labelled. Enter-to-send is IME-safe.

what the browser reports

checking…

detect a language

No detection run yet.

the code

// Probe first — never assume.
const availability = typeof LanguageDetector?.availability === "function"
  ? await LanguageDetector.availability()
  : "unknown";
const usable = ["available", "downloadable", "downloading"].includes(availability);

if (usable) {
  const detector = await LanguageDetector.create();
  const results = await detector.detect("Bonjour le monde");
  // [{ detectedLanguage: "fr", confidence: 0.99 }, ...]
}

// The older surface, still worth probing for compatibility:
const canDetect = typeof translation?.canDetect === "function"
  ? await translation.canDetect()
  : "unknown";

what is Android-specific here

The API surface is not Android-only: LanguageDetector is the same JavaScript object wherever it is exposed. The Android listing tracks when the on-device model becomes available on Android builds, which is why this page is laid out for a phone viewport first and why it reports model availability instead of assuming it. On any platform the honest sequence is the same: probe, then use the model only if the probe said it is usable, and label results that came from your own fallback.

see also