Gate a translation update on its placeholders

Agent buildEasy12 min

Your task

Your product ships in 12 languages, and a broken placeholder in a translation crashes the screen at render time. An agent proposes translation updates and may write the safe ones to the locale files itself.

Write gate_translation_update(source_string, proposed_translation): the check every proposed update passes through.

  • source_string (str): the source text, with placeholders in braces such as {name}.
  • proposed_translation (str): the proposed translation.

It returns one of the values in the rules below. Apply them in order. The first one that matches decides:

  1. Either string has malformed braces: "reject_malformed". Braces are well formed when, reading left to right, every { is closed by a } before the next {. A } must not appear without an open {, and no { may be left open at the end. So {a{b}} is malformed.
  2. The translation is empty: "route_to_human".
  3. The placeholder names differ: "reject_placeholder_mismatch". A placeholder's name is the text between { and }. Compare the names as a multiset: order does not matter, but counts do, so {a} {a} and {a} differ.
  4. The translation is exactly equal to the source: "route_to_human", because it was never translated.
  5. Otherwise: "accept".

Examples

gate_translation_update(source_string: str, proposed_translation: str) -> str
Example 1
Inputgate_translation_update("Hello {name}, you have {count} new messages", "Hola {name}, tienes {count} mensajes nuevos")
Output"accept"
Example 2
Inputgate_translation_update("Hello {name}", "Bonjour {nom}")
Output"reject_placeholder_mismatch"
Example 3
Inputgate_translation_update("Save changes", "")
Output"route_to_human"