{"slug": "build-a-multilingual-react-editor-with-chromes-built-in-ai", "title": "Build a Multilingual React Editor with Chrome’s Built-In AI", "summary": "A developer published a walkthrough for building a multilingual React and TypeScript editor on Chrome's built-in Language Detector and Translator APIs, keeping translation entirely on-device. The design separates language detection from translation, probes pair availability on selection changes, supports cancellation and stale-result protection, and requires a distinct acceptance action before a translation is used. The adapter creates and destroys a translator per request and never falls back to a cloud model when the APIs are unavailable.", "body_md": "A translation can look polished and still be dangerous to apply. The application may preserve the words while quietly losing the most useful review artifact: what the person actually wrote.\n\nConsider a synthetic event brief: “Please keep Room 204 available until Friday. The budget is $250. Do not confirm the booking until Maya approves.” The room, amount, deadline, and condition all matter. A translation belongs beside that source so someone can inspect the result before using it.\n\nThis walkthrough builds a safer interaction in React and TypeScript with Chrome’s Language Detector and Translator APIs. The companion lab includes a manual source-language selector, an availability check for the selected pair, cancellation, stale-result protection, and a separate acceptance action. It never submits a booking or sends the brief to a cloud model.\n\nLanguage detection returns candidates. A short message, a proper name, or mixed-language text may not provide enough evidence for a reliable automatic choice. Display the leading candidates and let the person confirm the source language.\n\nDo not treat the detector’s confidence as a score for the eventual translation. These are different operations with different failure modes. A confident identification of English says nothing about whether a particular translation preserves a conditional deadline.\n\nThe interface uses two actions: **Detect language**, then **Translate on this device**. That separation also gives translation a fresh user interaction when model preparation requires it.\n\n*Figure 1. Detection informs the language choice. Acceptance remains a separate application action.*\n\nFeature detection answers whether an API is exposed. Availability answers a more specific question about the requested operation.\n\n```\nif (typeof Translator === \"undefined\") {\n  setAvailable(\"Translator API is not exposed here.\");\n} else {\n  Translator.availability({\n    sourceLanguage: source,\n    targetLanguage: target,\n  }).then(\n    value => setAvailable(`Selected pair: ${value}.`),\n    () => setAvailable(\"Availability check failed.\")\n  );\n}\n```\n\nRun this probe when the language selection changes. In the full React component, an effect cleanup prevents an older probe from updating the label after a newer selection. A slow probe changes the label to “Readiness is still unknown”; a timeout does not prove that the API is unsupported.\n\nAn unavailable feature should leave the editor useful. Keep the source text editable and offer manual copying. Do not silently turn a local translation action into a server request.\n\nChrome’s current documentation describes these APIs on desktop and notes that language-pack availability reporting protects information about previously used pairs. Consequently, an availability label should guide preparation rather than become a detailed inventory of the person’s installed languages.\n\nThe adapter receives the exact source text and language pair. It owns one translator and releases it after use.\n\n```\nasync function translateText(\n  text: string,\n  sourceLanguage: string,\n  targetLanguage: string,\n  signal: AbortSignal\n) {\n  signal.throwIfAborted();\n  const translator = await Translator.create({\n    sourceLanguage, targetLanguage, signal,\n  });\n  try {\n    return await translator.translate(text, { signal });\n  } finally {\n    translator.destroy();\n  }\n}\n```\n\nCall this directly from the translation handler. Avoid placing an unrelated asynchronous operation before creation and assuming transient user activation will still exist afterward. The lab performs its readiness probe separately and catches creation errors in the handler.\n\nThe complete adapter also checks for missing APIs and empty input and reports download progress. Progress describes preparation; it is not an estimate of translation accuracy or remaining generation time.\n\nWhile translation runs, the person might change “Friday” to “Thursday” or select another language. The earlier response must not appear as a translation of the new text.\n\nThe lab invalidates the pending request whenever the source text or either language changes. A request identity check prevents a late response from becoming current even if cancellation does not immediately stop the underlying work. A completed suggestion is cleared by the same changes.\n\nThis is especially important at acceptance time. A button that applies an old suggestion after the original has changed can undo the person’s latest work. Keep the acceptance handler tied to the current suggestion, and invalidate it as soon as its inputs change.\n\nRendering the suggestion as ordinary React text keeps it from becoming executable HTML. If the product later adds Markdown, its rendering policy needs a separate review.\n\nThe companion’s deterministic tests cover superseding and cancelling requests. Those checks establish which result may be displayed; they cannot establish whether the translation reads naturally or preserves meaning.\n\nUse a small review set before calling the feature useful:\n\n| Input case | What to inspect | \n|---|---|\n| Room number and budget | Identifiers and amounts remain correct | \n| “Do not confirm until approved” | Negation and prerequisite remain intact | \n| Short ambiguous phrase | Manual source selection remains available | \n| Mixed languages | Candidate uncertainty is visible | \n| Unsupported pair | Original remains editable; no cloud request occurs | \n| Source edited during generation | Old output cannot become the current suggestion | \n\nAsk a fluent reviewer to compare the original and translation. A reverse translation can reveal something worth investigating, but it is not an independent ground truth and can repeat the first model’s mistake.\n\nFrom the supplied `lab` directory, run `npm ci` and `npm run dev`. Open the printed localhost address in a compatible browser. The native action buttons use real browser APIs; they do not return canned translations when support is missing.\n\nThe code is typechecked against the pinned Chromium AI declarations. Translation quality still requires a native model run and language review; no accuracy percentage is claimed here. Record the browser version, language pair, input, and actual output when conducting that review.\n\nA useful translation feature lets someone recover from uncertainty without losing the message they meant to send. Which parts of your application’s user-written text must survive translation exactly, and which need a human judgment about meaning?", "url": "https://wpnews.pro/news/build-a-multilingual-react-editor-with-chromes-built-in-ai", "canonical_source": "https://dev.to/gde/build-a-multilingual-react-editor-with-chromes-built-in-ai-4863", "published_at": "2026-11-22 14:10:00+00:00", "updated_at": "2026-11-22 14:13:07.352361+00:00", "lang": "en", "topics": ["ai-tools", "developer-tools", "natural-language-processing", "ai-products"], "entities": ["Google", "Chrome", "React", "TypeScript"], "also_reported_by": [], "takeaway": "Separate language detection from translation, probe Translator.availability for the specific source-target pair on every selection change, and create then destroy a translator per request with an AbortSignal to block stale results.", "recommendation": {"advice": "Probe Translator.availability per language pair and create a fresh translator per request with an AbortSignal, destroying it in a finally block so stale or cancelled results never reach the user.", "playbook": "agent-architecture", "playbook_url": "https://wpnews.pro/playbooks/agent-architecture"}, "alternates": {"html": "https://wpnews.pro/news/build-a-multilingual-react-editor-with-chromes-built-in-ai", "markdown": "https://wpnews.pro/news/build-a-multilingual-react-editor-with-chromes-built-in-ai.md", "text": "https://wpnews.pro/news/build-a-multilingual-react-editor-with-chromes-built-in-ai.txt", "jsonld": "https://wpnews.pro/news/build-a-multilingual-react-editor-with-chromes-built-in-ai.jsonld"}}