How to Fix Garbled Characters (Mojibake) After Translating an HTML File (UTF-8 Guide)
Use a consistent UTF-8 pipeline: declare <meta charset="UTF-8">, serve the file with a UTF-8 HTTP charset, and read and write its bytes as UTF-8. Mojibake such as café usually means UTF-8 bytes were decoded using a different encoding, then saved as text; adding a declaration after that has happened won’t restore the original characters.
Diagnose the problem before changing content: inspect the response headers and the file’s actual bytes, then identify whether the damage is only a decoding mismatch or whether text was already corrupted and saved. Repair only the latter with a reversible conversion, keep the original file, and verify representative characters in both the browser and the delivered response.
Tell an encoding declaration from an encoding conversion
A charset declaration tells a parser how to interpret bytes. It doesn’t transform those bytes. If the file contains the UTF-8 bytes for é but a tool reads them as Windows-1252, the tool may display é. If it then saves that display as UTF-8, the saved file contains the literal mojibake characters. The browser can correctly read that new UTF-8 file and still show the wrong text.
Use a minimal example to distinguish a bad declaration from already-damaged text:
<!-- Broken: the declaration doesn't match the saved bytes. -->
<meta charset="ISO-8859-1">
<p>Café</p>
<!-- Fixed for an HTML file saved as UTF-8, if the text itself is correct. -->
<meta charset="UTF-8">
<p>Café</p>
Changing the first declaration only helps when the underlying bytes already represent the intended characters in the declared encoding. If the source text is literally Café, the corrected declaration will faithfully display that wrong text. Check the file contents in an editor that lets you inspect or reopen the file with a specified encoding before deciding whether conversion is needed.
Never treat changing
<meta charset>as a repair for text that has already been decoded incorrectly and saved; it changes parser behavior, not the characters in the file.
Check the file and the HTTP response separately
Encoding can go wrong at more than one boundary. A translated file may be valid UTF-8 on disk but served with a conflicting HTTP charset, or it may already contain mojibake even though both the file and response claim UTF-8. Check the exact file that is published, not just a local preview or the source template.
python - <<'PY'
from pathlib import Path
path = Path("translated.html")
raw = path.read_bytes()
print("First bytes:", raw[:16].hex(" "))
print("UTF-8 BOM:", raw.startswith(b"\xef\xbb\xbf"))
try:
text = raw.decode("utf-8-sig")
except UnicodeDecodeError as exc:
print("Not valid UTF-8:", exc)
else:
print("Valid UTF-8.")
for marker in ("Ã", "Â", "â€"):
if marker in text:
print(f"Possible mojibake marker: {marker!r}")
PY
This check reads bytes before decoding, so it can distinguish invalid UTF-8 from text that is valid UTF-8 but may already be corrupted. The markers are clues, not proof: they can occur legitimately in some languages or content. Review the reported lines and compare them with the source before attempting a repair.
Then inspect the published response in your browser’s network panel or with a command-line client:
curl -I https://example.com/translated.html
Look for a response header such as Content-Type: text/html; charset=UTF-8. A conflicting server header can override or confuse the document declaration, while a correct header cannot repair text that was saved incorrectly; verify both the header and the visible characters.
Set UTF-8 at the document and server layers
For HTML, place the charset declaration near the start of <head>, before content that could be interpreted under another encoding. The HTML encoding declaration must be fully contained within the first 1024 bytes of the document. Configure the web server or hosting platform to send the same encoding, and make sure your editor and build process save the file as UTF-8.
<!doctype html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Café et pâtisserie</title>
</head>
<body>
<p>Crème brûlée, déjà vu, Ελληνικά, 日本語.</p>
</body>
</html>
The declaration and sample text make a useful smoke test because they include accented Latin letters and characters from other scripts. The lang attribute identifies the document language; it does not set the character encoding. Confirm the saved file is UTF-8 and that the response header agrees with the declaration.
| Layer | What it controls | Correct example | What it cannot fix |
|---|---|---|---|
| HTML document | How a browser identifies the document’s character encoding | <meta charset="UTF-8"> | Text already saved as café |
| HTTP response | The encoding announced by the server for the response | Content-Type: text/html; charset=UTF-8 | Invalid bytes or incorrect characters in the file |
| Saved file | The actual byte sequence on disk or in the deployed asset | UTF-8 bytes for Café | A mismatched server configuration by itself |
| Editor or translation pipeline | How bytes become text and how edited text is written back | decode("utf-8") followed by UTF-8 output | Characters already replaced with � |
Repair text only when the mojibake is already saved
If valid UTF-8 text contains a pattern such as café, and the known failure was UTF-8 decoded as Windows-1252 or Latin-1, a controlled reverse conversion may recover it. This is not a universal “fix mojibake” operation: the wrong source encoding can corrupt correct text, punctuation, or names. Keep the original and write to a new file so the result can be reviewed before publication.
Never run a blind whole-file replacement or encoding conversion on translated content. Confirm the specific decode mistake first, and retain an untouched copy of the input.
The following script deliberately requires you to choose the encoding that was used incorrectly. It reads an input file as UTF-8, reverses that mistaken text decoding, and writes the recovered result as UTF-8 to a different path.
#!/usr/bin/env python3
"""Reverse a known UTF-8-as-single-byte decoding mistake."""
import argparse
from pathlib import Path
import sys
def repair_text(text: str, mistaken_encoding: str) -> str:
"""Recover UTF-8 bytes previously decoded using another encoding."""
return text.encode(mistaken_encoding).decode("utf-8")
def main() -> int:
parser = argparse.ArgumentParser(
description="Repair a known mojibake conversion into a new UTF-8 file."
)
parser.add_argument("input", type=Path, help="UTF-8 file containing mojibake")
parser.add_argument("output", type=Path, help="new UTF-8 output file")
parser.add_argument(
"--from-encoding",
choices=("cp1252", "latin-1"),
required=True,
help="encoding mistakenly used to decode the original UTF-8 bytes",
)
args = parser.parse_args()
if args.input.resolve() == args.output.resolve():
parser.error("input and output must be different files")
try:
text = args.input.read_text(encoding="utf-8-sig")
repaired = repair_text(text, args.from_encoding)
args.output.write_text(repaired, encoding="utf-8", newline="")
except (OSError, UnicodeError) as exc:
print(f"Repair failed: {exc}", file=sys.stderr)
return 1
print(f"Wrote repaired UTF-8 file: {args.output}")
return 0
if __name__ == "__main__":
raise SystemExit(main())
The conversion succeeds only if the text can be encoded with the selected single-byte codec and those bytes form valid UTF-8; otherwise, it stops instead of silently substituting characters. The output is a separate UTF-8 file, but successful conversion still needs a human review because a wrong choice can produce plausible-looking yet incorrect text.
Run it with the encoding that matches the documented failure, then compare the output with the source translation:
python repair_mojibake.py translated.html repaired.html --from-encoding cp1252
# Use this only if the mistaken decoder was Latin-1 instead.
python repair_mojibake.py translated.html repaired.html --from-encoding latin-1
Don’t try both options on production content and select whichever looks more familiar without checking the original. Review the diff, confirm names and punctuation, and keep the original until the repaired file has passed the same publishing checks as any other translation.
Prevent encoding damage in translation workflows
Keep text as Unicode inside the translation process and specify UTF-8 at every file boundary. Avoid decoding HTML into a legacy encoding before sending it to a translator or a build step. When a tool exports translated HTML, inspect the output encoding rather than assuming the input setting was preserved.
# Python: read and write an HTML file explicitly as UTF-8.
from pathlib import Path
source_path = Path("source.html")
output_path = Path("translated.html")
html = source_path.read_text(encoding="utf-8")
# Pass `html` through the translation step without a legacy-encoding conversion.
translated_html = html
output_path.write_text(translated_html, encoding="utf-8", newline="")
Explicit encoding prevents the runtime from relying on machine or locale defaults, which can differ across build environments. In a real translation step, preserve markup and placeholders while changing only translatable text; then check the output bytes and browser rendering instead of treating successful file writing as proof of correct characters.
Validate the repaired page before publishing
Test more than one layer: open the exact deployed URL, inspect its response charset, and check characters that were previously affected. Include content from the target locale, punctuation such as curly quotes or an em dash, and any names or merge fields that must remain exact. A browser may display a local file differently from the server response, so local preview alone is not sufficient.
Use HTML Translate to compare locale output and sanity-check that translated characters and markup survived the export.
Review a diff against the original translation, verify that the document still has one appropriate charset declaration, and check that no replacement character � has appeared. If the response is wrong despite a correct file, fix the server’s Content-Type configuration; don’t compensate by changing the text or adding HTML entities.
Pre-publish encoding checks
- Open the deployed page and confirm that the affected characters render correctly.
- Confirm the saved file is valid UTF-8 and that the server returns
charset=UTF-8. - Review the repaired text against the source translation, especially names, punctuation, and legal wording.
- Search for likely mojibake patterns and for the replacement character
�; investigate matches rather than replacing them automatically. - Check that the HTML declaration appears early in
<head>and that translation placeholders and markup remain intact. - Keep the original file and publish only the reviewed output.
Common Mojibake Troubleshooting
- The page still shows
éafter adding UTF-8 metadata. The text may already be mojibake saved as UTF-8; inspect the file contents and apply a reverse conversion only if the original decoding mistake is known. - The local file looks right but the published page does not. The server may send a conflicting charset; inspect the response’s
Content-Typeand configure it to use UTF-8. - The file fails a UTF-8 decoder. It may have been saved in another encoding or contain mixed bytes; identify the actual encoding before converting, and don’t force decoding with replacement characters.
- Some punctuation becomes
’or–. This often points to UTF-8 bytes decoded as Windows-1252; confirm that history, then test--from-encoding cp1252on a copy. - Characters display as
�. A previous tool may have replaced undecodable bytes, which usually loses the original information; restore from a clean source or translation export rather than trying to reverse the replacement. - Only one editor shows the problem. Its file-encoding interpretation may differ from the browser’s; reopen the file explicitly as UTF-8 and check the raw bytes before saving again.
- The repair script errors while converting. The selected codec may be wrong or the damage may not fit this specific conversion; stop, preserve the input, and investigate the pipeline instead of broadening the conversion blindly.
HTML File Translation Questions
Does <meta charset="UTF-8"> fix mojibake by itself?
No. It tells the browser how to interpret the HTML bytes. It can resolve a declaration mismatch, but it cannot turn already-saved text such as café back into café.
Should translated HTML use UTF-8 without a byte-order mark?
UTF-8 does not require a byte-order mark. A UTF-8 BOM may be accepted, but consistency matters more: use a known UTF-8 encoding for the file, a matching HTML declaration, and a matching server response.
Can HTML entities repair corrupted characters?
Entities can represent intended characters, such as é for é, but they don’t identify or reverse an encoding mistake. Use entities only when they’re appropriate for the content, not as a blanket mojibake repair.
How can a file be valid UTF-8 and still display garbled text?
The wrong characters may have been saved as ordinary UTF-8 text. For example, café is valid Unicode and can be encoded as valid UTF-8, even though it was likely produced by an earlier decoding error.
Is Windows-1252 the same as Latin-1 for repairing mojibake?
No. They overlap for many characters but differ in the byte range commonly used for punctuation. Choose the codec that matches the actual mistaken decoding; using the wrong one can fail or alter content.
What if the text contains � instead of mojibake?
The replacement character often means a decoder discarded information it couldn’t interpret. The original bytes may no longer be recoverable from that text, so use a clean source file or request a fresh translation export.
The script and sample files from this article are on GitHub: repair_mojibake.