Finds the text on a page that needs translating, and writes translations back onto the page's own nodes. Native does language detection, translation, batching and ordering. Nothing runs until native sends a message.
Ships in its own bundles, not the main ones: apple-page-translation (Sources/ContentScopeScripts/dist/pageTranslation.js, page world) and android-page-translation (build/android/pageTranslation.js). Native injects the bundle when the user translates a page, the same way it injects contentScope.js (same placeholders, same messaging setup). It's a platform-specific feature: native decides when to use it, so it runs without a privacy-config entry and on sites where the user turned protections off. Setting pageTranslation to disabled in remote config turns it off. extract and apply always reply, even when a unit or the page's DOM throws.
Apple native must fill $CONTENT_SCOPE_JSON$ (the config as a JavaScript string literal) in every Apple bundle, isolated and Duck.ai ones included. That is the C-S-S 18.0.0 change, not specific to this feature, but this feature needs it.
Native sends subscriptions; the feature replies with notifications.
| Native sends | Feature replies | Params |
|---|---|---|
extract {} |
extractResult |
{ units: Unit[] } |
apply { sourceLanguage, targetLanguage, updates: [{ id, text } or { id, tree }] } |
applyResult |
{ count }: updates written |
| — | unitsAdded |
{ units: Unit[] }: content that appeared after extract (not sent yet) |
Unit = { id, text, html, vp }:
html: false: plain text. Translate it as plain text and send the result as text; it's written as text.html: true: an HTML fragment. Translate it with TranslateKit as HTML with htmlAsJSONTree, and send TranslateKit's JSON tree, unchanged, as tree.vp: 1: the unit is in the viewport. Translate these first.extract needs no languages, so native can detect the page's language from the units it returns. apply carries sourceLanguage (the detected page language) and targetLanguage (the language translated into), both BCP 47. extract reads the page as it is, so a second one on a translated page returns translated text.
extract and apply also take an optional string requestId, which comes back unchanged in the reply. Native can use it to drop a late reply to a request it cancelled. Without it, nothing is echoed.
To show the original, native reloads the page. It also reloads before translating again, for example into another language.
p, li, h1 or td. A block inside an inline element (<li><a><div>…) counts as a block.script, style, noscript, template, textarea, code, kbd, samp, var, math, svg, iframe, embed, object, translate="no", .notranslate and editable content.lang other than sourceLanguage is still extracted, but apply doesn't write it: a unit inside such an element is skipped (not counted), and such an element inside a unit comes back untouched while the rest of the sentence is translated. The <html lang> is ignored for this check, and an empty sourceLanguage turns it off.display: none, the hidden attribute, visibility: hidden, opacity: 0, closed <details>) is extracted too, with vp: 0, so it's translated by the time someone opens it. A display: none element is treated as block or inline by its tag's usual display.<select> goes as an empty tag inside its sentence and comes back untouched. Each <option> with text is a plain-text unit of its own. Before an option's text changes, it gets an explicit value (its current one) if it had none, so the form still submits the same value.title, aria-label, aria-description, aria-roledescription and aria-valuetext on any element, alt on img and area, label on track, optgroup and option, abbr on th, and content on <meta name="description"> and <meta name="keywords">. Never on form controls (input, select, textarea, button): Autofill reads their attributes. The <title> isn't translated.Use <code data-t="0"></code> to check.script, style and template inside a unit aren't sent at all. They, and comments, stay before or after the unit's text, as they were.<tag data-t="N">, with no other attributes. N is the element's index within the unit.<span>A</span>iwa…) is wrapped in <div>…</div>, and must come back as a single div. Whitespace and invisible formatting characters (such as bidi marks) outside the div are accepted; the formatting characters are kept.The script never parses HTML. TranslateKit parses the engine's output into a JSON tree of {"text"} and {"tag", "attrs", "children"} nodes, and the script reads that with JSON.parse. So pages that enforce Trusted Types need no policy. A tree with tags or attributes we didn't send, or TranslateKit's null for malformed HTML, is rejected and the unit stays as it is.
The translation is then merged onto the original nodes. Elements are moved, never recreated, so listeners, form values and framework-managed nodes survive. Text goes into the existing text nodes.
Once anything is written, <html lang> is set to targetLanguage.
accrued {count} spaceships), that node is removed, so later updates to it don't show. react.html covers this with a test marked as an expected failure.From injected/:
npm run build && npx playwright test integration-test/page-translation.spec.js --project apple-page-translation --project android-page-translation --reporter list
npx jasmine --config=unit-test/config.json --filter="page-translation parse"
Fixtures are in integration-test/test-pages/page-translation/pages/. Each one lists its expected units in the spec's FIXTURES, and runs the extract and apply checks. Apply uses a fake translator that marks each text run as ‹…›.