lang_sample_text.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/bin/env python3
  2. """
  3. lang-sample-text
  4. Adds sample text for a given language using the specified UDHR translation.
  5. Usage:
  6. lang-sample-text -l ./languages/en.textproto ./udhr_translations/en.xml
  7. """
  8. from gflanguages import LoadLanguages, languages_public_pb2
  9. from gftools.util.google_fonts import ReadProto, WriteProto
  10. from gflanguages.udhr import Udhr
  11. from lxml import etree
  12. import os
  13. import re
  14. import argparse
  15. def main(argv=None):
  16. parser = argparse.ArgumentParser(
  17. description="Update UDHR sample text for a given language"
  18. )
  19. parser.add_argument(
  20. "-l",
  21. "--lang",
  22. help="Language proto file to update",
  23. required=True,
  24. )
  25. parser.add_argument(
  26. "-u",
  27. "--udhr",
  28. help="Path to UDHR translation (XML)",
  29. required=True,
  30. )
  31. args = parser.parse_args(argv)
  32. language = ReadProto(languages_public_pb2.LanguageProto(), args.lang)
  33. udhr_data = etree.parse(args.udhr)
  34. head = udhr_data.getroot()
  35. for name, value in head.attrib.items():
  36. if re.search(r"\{.*\}lang", name):
  37. bcp47 = value.replace("-", "_")
  38. udhr = Udhr(
  39. key=head.get("key"),
  40. iso639_3=head.get("iso639-3"),
  41. iso15924=head.get("iso15924"),
  42. bcp47=bcp47,
  43. direction=head.get("dir"),
  44. ohchr=None,
  45. stage=4,
  46. loc=None,
  47. name=head.get("n"),
  48. )
  49. udhr.Parse(udhr_data)
  50. language.sample_text.MergeFrom(udhr.GetSampleTexts())
  51. WriteProto(language, args.lang)
  52. if __name__ == "__main__":
  53. main()