@duckduckgo/content-scope-scripts
    Preparing search index...

    Page Translation

    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.
    • Ids are never reused while the page is open.

    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.

    • The innermost block element that holds text, such as a p, li, h1 or td. A block inside an inline element (<li><a><div>…) counts as a block.
    • In a container that also holds blocks, each run of text and inline elements between those blocks is one unit.
    • Floated elements count as inline, so a drop cap stays inside its paragraph.
    • A unit needs at least one letter.
    • Never translated: script, style, noscript, template, textarea, code, kbd, samp, var, math, svg, iframe, embed, object, translate="no", .notranslate and editable content.
    • Content marked with a 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.
    • Hidden content (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.
    • A <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.
    • Attributes are plain-text units, after the content units: 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.
    • Inside a unit, a never-translated element is sent as an empty tag and comes back untouched: 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.
    • A unit without child elements is sent as plain text.
    • Otherwise each element becomes <tag data-t="N">, with no other attributes. N is the element's index within the unit.
    • A unit that starts with an element (a drop cap: <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.
    • Soft hyphens are removed.

    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.

    • A tag the engine returns more than once: the original goes to the occurrence with the most text (the first on a tie), and the others get copies built only from copies, without ids. Original nodes never move into a copy.
    • A tag returned empty: stays empty. The engine empties a tag when it moves the text elsewhere, so putting the original back would double it. Elements inside it are put back.
    • A tag the engine dropped: put back at the end of its container.
    • An id we didn't send, or a tag that would end up inside itself: its text is kept, no element is created or moved.
    • A tag inside an element left untouched (another language, or sent empty) that the engine places elsewhere: dropped there. It stays inside that element, which already shows its words.

    Once anything is written, <html lang> is set to targetLanguage.

    • Adjacent text nodes in one element are merged when written back. On React-style pages that render data into its own text node (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.
    • When the engine drops a tag, the element is put back with its original-language text, which then shows next to the translation.

    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 ‹…›.