Language detection with built-in AI

Published: September 24, 2024, Last updated: May 20, 2025

Explainer Web Extensions Chrome Status Intent
GitHub Behind a flag Chrome 137 beta Behind a flag Chrome 137 beta View Intent to Ship

Before translating text from one language to another, you must first determine what language is used in the given text. Previously, translation required uploading the text to a cloud service, performing the translation on the server, then downloading the results.

The Language Detector API works client-side, which means you can protect user privacy. While it's possible to ship a specific library which does this, it would require additional resources to download.

When to use language detection

The Language Detector API is primarily useful in the following scenarios:

  • Determine the language of input text, so it can be translated.
  • Determine the language of input text, so the correct model can be loaded for language-specific tasks, such as toxicity detection.
  • Determine the language of input text, so it can be labeled correctly, for example, in online social networking sites.
  • Determine the language of input text, so an app's interface can be adjusted accordingly. For example, on a Belgian site to only show the interface relevant to users who speak French.

Get started

The Language Detector API is available from Chrome 138 stable. Run feature detection to see if the browser supports the Language Detector API.

if ('LanguageDetector' in self) {
  // The Language Detector API is available.
}

Review the hardware requirements

The Language Detector and Translator APIs work on desktop only in Chrome.

The Prompt API, Summarizer API, Writer API, and Rewriter API work in Chrome when the following conditions are met:

  • Operating system: Windows 10 or 11; macOS 13+ (Ventura and onwards); or Linux. Chrome for Android, iOS, and ChromeOS are not yet supported by our APIs backed by Gemini Nano.
  • Storage: At least 22 GB on the volume that contains your Chrome profile.
  • GPU: Strictly more than 4 GB of VRAM.
  • Network: Unlimited data or an unmetered connection.
  • GPU: Strictly more than 4 GB of VRAM.
  • Network: Unlimited data or an unmetered connection.

These requirements exist for you in your development process and your users who work with the features you build.

Model download

Language detection depends on a model that is fine-tuned for the specific task of detecting languages. While the API is built in the browser, the model is downloaded on-demand the first time a site tries to use the API. In Chrome, this model is very small by comparison with other models. It might already be present, as this model is used by other Chrome features.

To see if the model is ready to use, call the asynchronous LanguageDetector.availability() function and inspect the resulting promise. There are three possible responses:

  • "unavailable": The requested options are not supported or the model cannot be prompted.
  • "downloadable": The request is supported, but additional downloads are needed before you can create a session. These downloads may include the language model or fine-tuning.
  • "downloading": The request is supported and a download is ongoing, which must be completed before creating a session..
  • "available": The request is supported, and you can create a session.

To trigger the download and instantiate the language detector, call the asynchronous LanguageDetector.create() function. If the response to availability() was 'downloadable' or 'downloading', it's best practice to listen for download progress, so you can inform the user in case the download takes time.

The following example demonstrates how to initialize the language detector.

const availability = await LanguageDetector.availability();

let detector;
if (availability === 'unavailable') {
  // The language detector isn't usable.
  return;
}
if (availability === 'available') {
  // The language detector can immediately be used.
  detector = await LanguageDetector.create();
} else {
  // The language detector can be used after model download.
  detector = await LanguageDetector.create({
    monitor(m) {
      m.addEventListener('downloadprogress', (e) => {
        console.log(`Downloaded ${e.loaded * 100}%`);
      });
    },
  });
  await detector.ready;
}

Run the language detector

The Language Detector API uses a ranking model to determine which language is most likely used in a given piece of text. Ranking is a type of machine learning, where the objective is to order a list of items. In this case, the Language Detector API ranks languages from highest to lowest probability.

The detect() function can return either the first result, the likeliest answer, or iterate over the ranked candidates with the level of confidence. This is returned as a list of {detectedLanguage, confidence} objects. The confidence level is expressed as a value between 0.0 (lowest confidence) and 1.0 (highest confidence).

const someUserText = 'Hallo und herzlich willkommen!';
const results = await detector.detect(someUserText);
for (const result of results) {
  // Show the full list of potential languages with their likelihood, ranked
  // from most likely to least likely. In practice, one would pick the top
  // language(s) that cross a high enough threshold.
  console.log(result.detectedLanguage, result.confidence);
}
// (Output truncated):
// de 0.9993835687637329
// en 0.00038279531872831285
// nl 0.00010798392031574622
// ...

API playground

Experiment with the Language Detector API in our API playground. Enter text written in different languages in the textarea.

Standardization effort

The Chrome team requested feedback from the W3C Technical Architecture Group and asked Mozilla and WebKit for their standards positions.

Share your feedback

We want to see what you're building with the Language Detector API. Share your websites and web applications with us on X, YouTube, and LinkedIn.

If you have feedback on Chrome's implementation, file a Chromium bug.