Catch Missing {name} Placeholders Before Translations Ship
“Welcome, {name}” After being translated into another language, the variable names are translated along with it, and the page typically only reports an error wh
Translated strings can break in quiet ways. A translator may rename a variable such as {name}, and the page then shows no error until real values are filled in. This guide shows how to catch those problems at translation delivery, before the text goes live. It covers how to extract fields from a format string and check their names, occurrence counts, and format specifications, instead of only counting curly braces.
The examples use synthetic samples and were verified in an isolated environment. They do not guarantee results for every real file or product, so keep the original copies of important files and check the output against your own acceptance criteria.
You will learn which format grammar to target, how to handle escaped braces and nested format specifications, and where a strict check belongs in your release process.
Can you catch missing placeholders before a translation ships? The short answer
Yes: extract the named fields from each format string and compare them with the source, rather than counting braces. The table below shows how to handle common situations.
| Your situation | What to do | Why |
|---|---|---|
Templates use Python str.format syntax | Use the parser described in this guide | It extracts named fields, including nested ones in format specifications |
Templates contain {{name}} | Treat it as literal text, not a variable | Doubled braces display as {name} and are not fields |
| A translator changed a variable name | Reject the translation and restore the field name | The name is part of the contract, and the page would fail at runtime |
| A decimal format changed | Flag it for review under your localization rules | Format specifications are protected by default |
| Templates use JavaScript, ICU MessageFormat, or another system | Write a parser for that grammar | This guide's parser does not cover other placeholder systems |
Why placeholder errors slip through
A translated sentence can read smoothly and still break the page. When a variable name is translated, the code that fills in the value can no longer find it, and the error usually appears only after real values are inserted.
That is why the check belongs during translation delivery, not at the end of the release. Verify the identity, occurrence count, and format specification of each field.
Choose the format grammar first
This guide's code handles only Python str.format style placeholders. It does not cover JavaScript templates, ICU MessageFormat, or other placeholder systems.
Before you write any checker, decide which grammar your templates use. Nested structures, escaping rules, and pluralization conventions differ between systems, and no single regular expression can support all of them. This example requires named fields and rejects automatically numbered fields, such as {} or {0}.
Escaped braces are not variables
In str.format, {{name}} displays the text {name}. It is not a field, so your checker should not count it as one.
Format specifications can also contain nested fields. For example, {value:{width}} includes width as a field inside the specification. The example reads these specifications recursively and keeps a count of each field, including duplicates.
from string import Formatter
from collections import Counter
def contract(text, depth=0):
if depth > 5:
raise ValueError("nested format budget")
result = Counter()
for literal, field, spec, conversion in Formatter().parse(text):
if field is None:
continue
if not field or field != field.strip():
raise ValueError("named field without surrounding whitespace required")
result[(field, conversion, spec)] += 1
if spec:
result.update(contract(spec, depth + 1))
return result
assert contract("Hi {name}, total {amount:.2f}") == contract("{name}:金额 {amount:.2f}")
assert contract("{{name}}") == Counter()
assert contract("{name} {name}") != contract("{name}")
assert contract("{value:{width}}")[("width", None, "")] == 1
assert contract("{amount:.2f}") != contract("{amount:.0f}")
for bad in ["Hi {name", "{ name}", "{}"]:
try:
contract(bad)
except ValueError:
pass
else:
raise AssertionError("invalid template accepted")
Keep the checker strict on purpose
This contract is intentionally strict. Format specifications are treated as protected content, so changes to the number of decimal places are flagged.
Some localization work does require changing number formatting. In that case, add a separate rule for it rather than turning off all placeholder checks.
The nesting depth limit is an example budget, not a requirement of the Python language specification.
Use the table below to decide what to do with each kind of change.
| Change | Result in this example | Proofreading action |
|---|---|---|
| Field moved to a different position | Accepted | Check that the reading order still sounds natural |
| A field appears one fewer time than in the original | Rejected | Check whether a sentence or phrase was accidentally dropped |
| Variable name was translated | Rejected | Restore the original field name |
| Decimal format changed | Flagged as an inconsistency | Confirm it against your localization rules |
Treat templates as untrusted input
This code only parses a template. It does not call format on your input, and it is not a safe sandbox for running templates.
When you render templates later, restrict which fields are available, which objects can be accessed, and how output is escaped. Pay particular attention to escaping rules on HTML pages.
Add a check between translation and deployment
When your materials contain template content, use OmniDoc (product entry point) and first list which fields must stay unchanged. After translation, run the structural check and the linguistic proofreading as two separate steps. A sentence that reads well does not prove that the variable contracts still hold.
For the standard reference on these formats, see Python string and Formatter.
🎯 Which approach fits you?
If your templates use Python str.format
Use the named-field parser in this guide and reject numbered fields.
If your templates include doubled braces
Treat {{name}} as literal text. It displays as {name} and should not be counted as a variable.
If your localization changes number formats
Add a separate rule for decimal changes so you can review them without disabling the placeholder checks.
❓ FAQ
1. Why not just count the curly braces?
Counting braces cannot tell you whether the same field names survived translation or whether format specifications changed. Extracting the fields can.
2. Does this check work for JavaScript or ICU MessageFormat templates?
No. This guide's code handles only Python str.format style placeholders, so other systems need their own parser.
3. Is {{name}} treated as a variable?
No. Doubled braces display as literal braces, so the checker should not count them as fields.
4. Should translators be allowed to change decimal formats?
Only if your localization rules allow it. The example flags those changes so you can confirm them, rather than turning off the placeholder checks.
📝 Summary
Placeholder errors often stay hidden until real values are filled in, so check them during translation delivery. Extract named fields, compare their identities, counts, and format specifications, and keep the proofreading step separate. Try your own templates before you rely on the result.
Try it now: upload a document and get it translated with the layout intact


