What goes wrong
{count} itemsbecomes{Anzahl} Artikel: the variable name was translated and now matches nothing.%sor%ddisappear or are duplicated.- An HTML tag is dropped, renamed or left unclosed, which breaks the layout.
- ICU plural syntax such as
{n, plural, one {...} other {...}}is partly translated and no longer parses.
What the API does
Glossferry instructs the model to keep placeholders such as {name}, %s and :id and to leave HTML and XML tags unchanged while translating the text between them. That is a strong default, but the output is generated text, so it is a best effort and not a guarantee.
A defensive approach
- Separate text from code. Do not send code, identifiers, URLs or large attribute values.
- Send fragments. Translate a paragraph or list item at a time, not a whole HTML document.
- Validate after translation. Compare the placeholders and tags of source and result.
- Fall back safely. If validation fails, keep the source text or retry once, and log the key for review.
- Test. Keep a small set of tricky strings as regression tests.
// Compare placeholders and HTML tags as sorted lists (order may change with word order)
const placeholders = (s) => (s.match(/\{\{[^}]+\}\}|\{[^}]+\}|%[sd]|:[A-Za-z_]\w*/g) ?? []).sort().join('|');
const tags = (s) => (s.match(/<\/?[a-z][a-z0-9-]*/gi) ?? []).map((t) => t.toLowerCase()).sort().join(',');
export const isSafe = (source, translated) =>
placeholders(source) === placeholders(translated) && tags(source) === tags(translated);
// usage
const out = isSafe(src, translated) ? translated : src; // keep the source text and flag it for review
Sorting the lists matters: a translation may legitimately move a tag or a placeholder to another position when the word order changes, so compare what is present, not where it sits.
function isSafe(string $source, string $translated): bool
{
$tokens = function (string $s): string {
preg_match_all('/\{\{[^}]+\}\}|\{[^}]+\}|%[sd]|:[A-Za-z_]\w*|<\/?[a-z][a-z0-9-]*/i', $s, $m);
$list = array_map('strtolower', $m[0]);
sort($list);
return implode('|', $list);
};
return $tokens($source) === $tokens($translated);
}
Limits of this check
- It does not prove the sentence reads well. Review important screens with a native speaker.
- A colon followed by a word, such as
Note:text, can look like a:placeholder. Tune the pattern to your own formats. - ICU plurals need an ICU-aware parser to validate fully.