& on a Translated Page? You May Have Decoded Twice

The page displays a string of &, the first reaction is often to decode it once more. But the problem may be just the opposite: the same piece of content has

Double-escaped text on a translated page usually comes from a value that was already escaped at one stage and then escaped again by the template. If you are translating HTML, placing text into a template, or seeing & where you expected &, this guide shows how to find that stage and stop converting there. It is written for developers and content editors who handle HTML, translated strings and templates in Python.

You will learn how escaping depends on the output boundary, how to name the data at each stage of your pipeline, and why repeatedly decoding a string can damage text you meant to show. The examples use only Python's standard library, so you can try them without extra tools.

Before you rely on any code here, note that the samples are synthetic. Keep your original copies and check results against your own requirements before you process important documents.

Why does & appear on a translated page? The short answer

Most often the text was escaped twice, so the fix is to find out which stage of your pipeline already escaped it and to stop converting at that point.

Your situationWhat to doWhy
Your page shows R&D correctlyKeep the value as plain text and let the output step escape it onceA second escape turns R&D into R&D, which the browser displays as a literal &
You see & on the pageCheck the stage the string came from before converting it againDecoding until nothing changes can destroy text you meant to display, such as <div>
Your input is raw HTMLParse it and translate the text nodesThe translator needs readable text, not markup
Your input is untrustedDon't decode it and insert it as HTMLEmbedded tags can become active again
You're ready to shipTest with regression samples in an HTML test fileTags, product names and escape layers can all break even when the English looks right

Why this happens: escaping depends on the output boundary

Two functions from Python's html module do opposite jobs:

  • html.escape converts characters that have meaning in HTML into entities.
  • html.unescape parses entities back into characters.

They are not cleaning steps you can swap freely. The translator usually needs semantic text, while the template needs a result that follows the rules of its current context.

Take the product name R&D:

  • As displayed text, it is fine.
  • In HTML source, it is usually written as R&D.
  • Escaped a second time, it becomes R&D, and the browser shows the intermediate entity & as literal text.

Decoding untrusted content and inserting it directly as HTML works the other way round: it can re-activate tags that were embedded in the content.

Run a small experiment with three states

import html

plain = 'R&D <Guide> "2026"'
encoded = html.escape(plain, quote=True)
double_encoded = html.escape(encoded, quote=True)

assert encoded == 'R&amp;D &lt;Guide&gt; &quot;2026&quot;'
assert html.unescape(encoded) == plain
assert html.unescape(double_encoded) == encoded
assert html.unescape(double_encoded) != plain

untrusted = '&lt;img src=x onerror=alert(1)&gt;'
decoded = html.unescape(untrusted)
safe_as_text = html.escape(decoded, quote=True)
assert decoded.startswith('<img')
assert safe_as_text.startswith('&lt;img')

This example uses only the standard library, and it does not run HTML or scripts. The last two lines describe output "as text," which is neither a secure HTML sanitization method nor enough to make arbitrary output safe inside JavaScript, CSS or URLs. Each context needs its own rules.

Identify the data state at each stage

Start by working out which of these you have:

  • raw HTML
  • DOM text
  • a plain string ready to insert into the page

Then decide whether any further conversion is needed.

To keep track of this in code, use separate names for each stage, such as source_html, plain_text, translated_text and rendered_html. Avoid reusing one generic name like content for several types. If your project allows it, wrap each stage in a lightweight data class so a function can accept only plain text. Naming won't replace testing, but it helps reviewers spot wiring errors, such as passing final HTML back into the translator.

Current inputWhat you want to getWhat to check first
Original HTMLTranslatable textUse a parser to locate the text nodes
DOM textTarget-language textDon't decode it again unconditionally
Translated plain textText visible on the pageLet the template perform text escaping
Allowed rich textA safe display fragmentUse a well-defined cleaning strategy

Don't decode until the string stops changing

Repeated decoding can look like a way to handle multi-level entities, but it will break code that you meant to display. A string such as &lt;div&gt; may be a deliberate literal, not a tag that was missed. Decide whether to parse another level based on your input protocol, not on how the string looks.

Links, attribute values and code blocks may also need different translation rules from the body text. A more useful check than comparing source-file strings is a set of regression samples that include angle brackets, quotation marks, ampersands and entity literals. Check both the parsed text and what the browser displays.

Build a small acceptance test

When you translate a document, you can put these samples into an HTML test file and confirm that tag examples and product names in the translated text still appear as expected. You can run this check with the OmniDoc (Product Entry) document translation entry point. For a useful acceptance test, check:

  • Meaning: the translated content says what the original said.
  • Escape layers: each value is escaped once, and no &amp;amp;-style artefacts appear.
  • Code protection: tags, code and product names survive translation intact.

Don't treat "the English has been translated" as the only sign of success.

📊 At a glance

FunctionWhat it doesRisk when misused
html.escapeConverts HTML-meaningful characters into entitiesEscaping twice shows literal entities such as &amp;
html.unescapeParses entities back into charactersDecoding untrusted content and inserting it as HTML can re-activate tags

🎯 Which approach fits you?

If you are translating raw HTML

Parse the markup and translate the text nodes, so tags and attributes stay out of the translator's input.

If you are inserting translated text into a template

Pass plain text and let the template escape it once.

If you see &amp; where you expected &

Check whether the value was already escaped before you convert it again. Don't decode it in a loop.

If your content is untrusted

Don't decode it and insert it as HTML. Use a defined cleaning strategy for any rich text you need to allow.

❓ FAQ

1. Why does my translated page show &amp; instead of &?

The text was most likely escaped twice, so the browser displays the entity as literal text. Find the stage that already escaped it and stop converting there.

2. Should I keep decoding HTML until the string no longer changes?

No. Repeated decoding can destroy text you meant to display, such as &lt;div&gt;. Decide how many levels to parse based on your input format.

3. What is the difference between html.escape and html.unescape?

html.escape turns HTML-meaningful characters into entities, and html.unescape parses entities back into characters. They are not interchangeable cleaning steps. The Python html module documentation describes both.

4. How can I check a translated page before I publish it?

Put regression samples with angle brackets, quotation marks, ampersands and entity literals into a test HTML file. Check the parsed text and the browser display, not just the source strings.

📝 Summary

Double escaping usually happens when a value is escaped at one stage and then escaped again by the template. Name each data stage, convert only at the boundary where it is needed, and test with samples before you publish.

Try it now: upload a document and get it translated with the layout intact

Updated 2026-11-29