Translating Text Inside HTML iframe and srcdoc Attributes: What Works and What Fails
Translate an iframe by localizing the document it loads through src, or by localizing the embedded HTML in srcdoc. The outer page can translate the frame’s title, but it cannot change the language inside a third-party frame unless the provider offers a language setting or a localized embed.
Treat the frame as a separate document even when its entire source fits in one attribute. It needs its own language metadata, and srcdoc needs two levels of escaping: one for the inner HTML and one for the outer attribute. A localized page can otherwise have a French frame label, English content, and a link broken by an unescaped quote.
Choose the right translation surface
| What you want to translate | Where to make the change | What won’t work |
|---|---|---|
| The text in a frame you host | Publish a translated document and point src at it. | Setting lang="fr" on the outer iframe does not translate the loaded page. |
| The text in an inline frame | Translate the inner HTML, then serialize it into srcdoc. | Running a text-node extractor on the outer page alone won’t find the paragraphs inside the attribute. |
| A widget supplied by another site | Use the provider’s documented locale setting or localized embed URL in src. | Adding an arbitrary ?lang=fr query parameter does nothing unless the provider supports it. |
| The frame’s label | Translate title on the outer iframe. | Changing the label doesn’t change the text or the document <title> inside the frame. |
Publish a localized document for src
For content you control, give each locale its own URL. The parent page chooses that URL and supplies a label that makes sense in the surrounding page. This version looks localized during a markup review but still loads the English document:
<iframe
src="/en/embed/shipping/"
lang="fr"
title="Délais de livraison"
></iframe>
The browser requests /en/embed/shipping/ regardless of the language declared on the frame element. The French label can make the mismatch less obvious to a reviewer, so inspect the content inside the frame rather than checking only the outer source.
<iframe
src="/fr/embed/livraison/"
title="Délais de livraison"
width="640"
height="260"
></iframe>
This version points to a French route, but the URL alone doesn’t guarantee a French response. Check the final URL after redirects and verify that the deployed route doesn’t silently fall back to English. Test the height at narrow widths because the translated copy may wrap onto more lines.
<!doctype html>
<html lang="fr">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Délais de livraison</title>
</head>
<body>
<h1>Délais de livraison</h1>
<p>La livraison prend de deux à quatre jours ouvrables.</p>
<p><a href="/fr/aide/livraison/">Consulter les options de livraison</a></p>
</body>
</html>
The child document declares its own language so assistive technology can handle pronunciation independently of the parent. For a right-to-left translation, set dir="rtl" on the child’s html element and test its layout inside the frame. Stylesheets in the parent don’t style the child’s headings or paragraphs.
Never translate an embed URL, identifier, or configuration key as prose. Change a URL only when routing to a real localized page or using a locale option the provider documents.
Localize srcdoc before escaping it
srcdoc is an attribute whose value is itself an HTML document. The outer parser decodes the attribute value, then the inner parser reads that value as markup. Use the decoded inner HTML as your translation input, not the entity-heavy attribute as it appears in the outer file.
- Extract the inner document from
srcdoc, or keep it as a separate source template and inline it during the build. - Translate text nodes and user-facing attributes such as
altandaria-label. Keep markup, identifiers, and placeholders intact; set the targetlangon the innerhtml. - Serialize the translated inner HTML through your template engine’s normal attribute escaping. If you assign the value with
frame.srcdoc = translatedHtml, pass the inner HTML directly; there is no outer HTML attribute to encode in that assignment.
<iframe
title="Horaires de retrait"
sandbox=""
srcdoc="<!doctype html>
<html lang="fr">
<head><meta charset="utf-8"><title>Horaires de retrait</title></head>
<body>
<p>Retrait le samedi & le dimanche.</p>
<p><a href="/fr/retrait/">Voir les horaires</a></p>
</body>
</html>"
></iframe>
The " sequences become quotes in the inner source without closing the outer attribute. The outer parser turns &amp; into &, and the inner parser renders that as a single &. Verify both the visible ampersand and the link: if the translator or build escapes the same layer twice, your output can show entity text or literal tags.
A srcdoc document has the address about:srcdoc, but relative URLs normally resolve against the embedding document’s base URL. A link like href="hours/" can therefore resolve differently when the same frame is inserted on a different page. On a single-origin site, a deliberate root-relative path such as /fr/retrait/ avoids depending on the parent’s path; verify the resolved URL if the parent sets a base element.
Never treat HTML escaping as sanitization. Escaping protects the outer attribute syntax; it does not make the decoded inner HTML safe to render.
For static content that doesn’t need scripts, sandbox="" is a useful default. It also gives the child an opaque origin, so parent JavaScript cannot read the child DOM even though the HTML came from the parent’s own attribute. If translators, a CMS, or an external service can supply markup, sanitize that markup before embedding it as well. Don’t add permissions just to make a test harness pass.
Verify what the browser actually loaded
In browser developer tools, select the iframe’s execution context in the console and run the following checks there, not in the parent page. This checks the child directly without requiring the parent’s JavaScript to read across an origin boundary.
console.table({
address: document.URL,
language: document.documentElement.lang,
direction: getComputedStyle(document.documentElement).direction,
heading: document.querySelector("h1")?.textContent.trim(),
firstLink: document.querySelector("a[href]")?.href
});
For a srcdoc frame, expect about:srcdoc; for a hosted frame, check the final document URL. Compare the reported language with the actual copy, and verify that the resolved link points to the expected locale. An absent heading is normal for the inline example above, so inspect its paragraph text as well.
Use HTML Translate to compare the extracted child HTML across locales before you serialize it back into the iframe attribute.
Also test with a keyboard and at a narrow viewport. Confirm that the frame label identifies its purpose, translated content fits or scrolls as intended, and links behave correctly under the iframe’s sandbox settings. For email, make the localized content or a localized link available in the message itself rather than relying on a frame that many clients will strip or block.
Common mistakes and troubleshooting
- The label is translated, but the content is not. The outer
titlechanged whilesrcstill points to the source locale. Correct the route and inspect the final response after redirects. - Changing
srchas no effect. Ifsrcdocis present, it takes precedence in browsers that support it, even when its value is empty. Remove the attribute to usesrc. - Tags are visible as text, or the frame markup ends early. Escaping happened at the wrong layer. Parse the outer
srcdocattribute first, then inspect its decoded value: it should contain markup like<p>, not the literal entity sequence<p>. - The widget ignores the locale. A guessed
?lang=frparameter is not an integration. Use the provider’s documented setting and verify whether the setting controls widget labels, embedded content, or both. - The parent can’t inspect the child. A cross-origin
srcor an opaque sandbox origin prevents ordinary parent DOM access. Inspect the child in developer tools or use a provider-supportedpostMessageintegration where available. - The localized frame is blank. Read the browser console. The parent’s
frame-srcpolicy can block the load, while the child’sframe-ancestorspolicy orX-Frame-Optionscan block embedding; changinglangor the URL’s locale won’t fix either policy.
HTML File Translation Questions
Can both src and srcdoc appear on one iframe?
Yes. Browsers that support srcdoc use it in preference to src. The latter can serve browsers that don’t support srcdoc, but it isn’t a fallback for empty, broken, or untranslated inline content in a modern browser.
Does translating the parent page translate embedded content?
Not reliably. A tool may translate the outer title without processing the separate document or parsing srcdoc as HTML. Localize the child independently and verify what the browser renders.
Can JavaScript change the language of a third-party iframe?
Only through a mechanism the provider supports, such as a locale-specific src or a documented message API. Parent JavaScript cannot rewrite a cross-origin child’s DOM.
Should the frame and the parent have the same language?
Not necessarily. Translate the outer title for readers of the parent page and set the child document’s lang to its actual content language. If a French page embeds a recording available only in English, say so in the surrounding French copy.
Should search-critical localized copy live only inside an iframe?
No. Put essential copy in the localized parent document so the page is useful on its own. A child route loaded through src is a separate resource; don’t assume its text will be indexed as part of the parent or that its locale signals will apply to the parent URL.