& 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 situation | What to do | Why |
|---|---|---|
Your page shows R&D correctly | Keep the value as plain text and let the output step escape it once | A second escape turns R&D into R&D, which the browser displays as a literal & |
You see & on the page | Check the stage the string came from before converting it again | Decoding until nothing changes can destroy text you meant to display, such as <div> |
| Your input is raw HTML | Parse it and translate the text nodes | The translator needs readable text, not markup |
| Your input is untrusted | Don't decode it and insert it as HTML | Embedded tags can become active again |
| You're ready to ship | Test with regression samples in an HTML test file | Tags, 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.escapeconverts characters that have meaning in HTML into entities.html.unescapeparses 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&amp;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&D <Guide> "2026"'
assert html.unescape(encoded) == plain
assert html.unescape(double_encoded) == encoded
assert html.unescape(double_encoded) != plain
untrusted = '<img src=x onerror=alert(1)>'
decoded = html.unescape(untrusted)
safe_as_text = html.escape(decoded, quote=True)
assert decoded.startswith('<img')
assert safe_as_text.startswith('<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 input | What you want to get | What to check first |
|---|---|---|
| Original HTML | Translatable text | Use a parser to locate the text nodes |
| DOM text | Target-language text | Don't decode it again unconditionally |
| Translated plain text | Text visible on the page | Let the template perform text escaping |
| Allowed rich text | A safe display fragment | Use 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 <div> 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;-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
| Function | What it does | Risk when misused |
|---|---|---|
html.escape | Converts HTML-meaningful characters into entities | Escaping twice shows literal entities such as & |
html.unescape | Parses entities back into characters | Decoding 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 & 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 & 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 <div>. 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


