in python/moz/l10n/formats/xliff/parse_trans_unit.py [0:0]
def parse_trans_unit(unit: etree._Element, is_xcode: bool) -> Entry[Message]:
id = unit.attrib.get("id", None)
if id is None:
raise ValueError(f'Missing "id" attribute for <trans-unit>: {unit}')
meta = attrib_as_metadata(unit, None, ("id",))
if unit.text and not unit.text.isspace():
raise ValueError(f"Unexpected text in <trans-unit>: {unit.text}")
target = None
note = None
seen: dict[str, int] = defaultdict(int)
for el in unit:
if isinstance(el, etree._Comment):
meta.append(Metadata("comment()", el.text))
else:
name = pretty_name(el, el.tag)
if name == "target":
if target:
raise ValueError(f"Duplicate <target> in <trans-unit> {id}: {unit}")
target = el
meta += attrib_as_metadata(el, "target")
elif name == "note" and note is None and el.text:
note = el
note_attrib = attrib_as_metadata(el, "note")
if note_attrib:
meta += note_attrib
elif el != unit[-1]:
# If there are elements after this <note>,
# add a marker for its relative position.
meta.append(Metadata("note", ""))
seen[name] += 1
else:
idx = seen[name] + 1
base = f"{name}[{idx}]" if idx > 1 else name
meta += element_as_metadata(el, base, True)
seen[name] = idx
if el.tail and not el.tail.isspace():
raise ValueError(f"Unexpected text in <trans-unit>: {el.tail}")
comment = "" if note is None else note.text or ""
msg = PatternMessage(
[] if target is None else list(parse_pattern(target, is_xcode))
)
return Entry((id,), msg, comment, meta)