# One line of AI that fixes your accessibility — demo

Companion code for the blog post [_One line of AI that fixes your accessibility_](https://www.khriztianmoreno.dev/blog/language-detector-lang-attribute-a11y) (also in [Spanish](https://www.khriztianmoreno.dev/es/blog/language-detector-lang-attribute-a11y)), the first post in a series on Chrome's built-in AI task APIs.

A comment thread on an `<html lang="en">` page, with comments in English, Spanish, Portuguese, French, German, and one that's too short to call. One click detects each comment's language on-device with the [Language Detector API](https://developer.chrome.com/docs/ai/language-detection) and sets the `lang` attribute on it, so screen readers switch to the right voice ([WCAG 3.1.2 Language of Parts](https://www.w3.org/WAI/WCAG22/Understanding/language-of-parts.html)). No server, no API key, and no text leaves the device.

There's no build step, like the other demos. Tailwind loads from its browser CDN; everything else is plain ES modules.

## Files

- [`lang-detector.js`](./lang-detector.js) is the reusable part, and what the post walks through:
  - `getAvailability()` wraps `LanguageDetector.availability()` and adds `'unsupported'` for browsers without the API.
  - `getLanguageDetector({ onProgress })` creates one shared detector, reporting download progress through `monitor`.
  - `getReadyLanguageDetector()` returns that detector only if it already exists or the model is already on the device. It never starts a download, so it's safe outside a click (the post's React hook uses it).
  - `annotateLang(element, detector)` detects and calls `applyLang()`, which sets `element.lang` only when the top result isn't `und` and clears the threshold. Otherwise it removes `lang`, so the element inherits the page's language.
  - `observeUgc(root, detector)` annotates every `[data-ugc]` element now, and new ones as a `MutationObserver` sees them added.
  - `destroyLanguageDetector()` frees the model when you're done.
- [`main.js`](./main.js) is the UI: availability badge, download progress, the threshold slider (it re-applies the decision to stored results without re-running the model), the "post a comment" form, and the live markup view.
- [`index.html`](./index.html) and [`style.css`](./style.css) are the page.

## Running it

ES modules need HTTP, not `file://`:

```bash
npx serve .
# or
python3 -m http.server
```

Open it in desktop Chrome 138 or later, where the Language Detector API is stable. It doesn't work on mobile. If the model isn't on the device yet, the button reads **Download model & detect**: `create()` needs a user click to start that download.

In browsers without the API, the page says so and still renders every comment. They just inherit `lang="en"`.

## What to look for

- Each comment gets a badge: `lang="es"` plus its confidence when the attribute was set, or `kept page lang` when the top result was below the threshold (try the `ok 👍` comment, or drag the slider up).
- **Ranked results** shows the raw `detect()` output. The last entry is always `und`, the confidence that the text isn't in any language the model knows.
- **Resulting markup** is read from the DOM after every change: that's what assistive technology gets.
- Post a comment in another language after running detection: the `MutationObserver` annotates it on its own.
- To hear the difference, read the thread with a screen reader before and after (NVDA with **Automatic language switching** enabled, or VoiceOver) with a voice installed for each language.
