Skip to content

Instantly share code, notes, and snippets.

@flodolo
Created September 25, 2025 14:34
Show Gist options
  • Save flodolo/fe2b5cba8a9018ec883c2a80dc305a22 to your computer and use it in GitHub Desktop.
Save flodolo/fe2b5cba8a9018ec883c2a80dc305a22 to your computer and use it in GitHub Desktop.
Test android parsing
<!DOCTYPE resources>
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<string name="single_quote">They\'re great</string>
<string name="double_quotes">They are \"great\"</string>
<string name="both_quotes">They\'re really \"great\"</string>
</resources>
from moz.l10n.formats import Format
from moz.l10n.model import Entry
from moz.l10n.resource import parse_resource
from moz.l10n.message import serialize_message
with open("strings.xml", "rb") as f:
content = f.read()
def print_res(res):
for section in res.sections:
for entry in section.entries:
if isinstance(entry, Entry):
entry_id = ".".join(section.id + entry.id)
print(
f"ID: {entry_id}, Value: {entry.value}, Serialized: {serialize_message(res.format, entry.value)}"
)
print("\n----\nParsing Android resource with literal quotes\n----\n")
res = parse_resource(input=Format.android, source=content, android_literal_quotes=True)
print_res(res)
print("\n----\nParsing Android resource without literal quotes\n----\n")
res = parse_resource(input=Format.android, source=content, android_literal_quotes=False)
print_res(res)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment