in python/moz/l10n/message/serialize.py [0:0]
def serialize_message(format: Format | None, msg: Message) -> str:
"""
Serialize a `Message` to its string representation.
Custom serialisers are used for `android`, `mf2`, `properties`, `webext`, and `xliff` formats.
Many formats rely on non-string message parts including an appropriate `source` attribute.
SelectMessage serialization is only supported for `mf2`.
Serializing `fluent` messages is not supported.
"""
# TODO post-py38: should be a match
if format == Format.properties:
return properties_serialize_message(msg)
elif format == Format.webext:
# Placeholders are discarded
return webext_serialize_message(msg)[0]
elif format == Format.android:
if android_serialize_message is None:
raise UnsupportedFormat("Serializing Android messages requires [xml] extra")
return android_serialize_message(msg)
elif format == Format.xliff:
if xliff_serialize_message is None:
raise UnsupportedFormat("Serializing XLIFF messages requires [xml] extra")
return xliff_serialize_message(msg)
elif format == Format.mf2:
return mf2_serialize_message(msg)
elif format == Format.fluent:
raise UnsupportedFormat("Serializing Fluent message patterns is not supported")
elif not isinstance(msg, PatternMessage) or msg.declarations:
raise ValueError(f"Unsupported message: {msg}")
else:
res = ""
for part in msg.pattern:
if isinstance(part, str):
res += part
else:
part_source = part.attributes.get("source", None)
if isinstance(part_source, str):
res += part_source
else:
raise ValueError(f"Unsupported placeholder: {part}")
return res