merge-catalogs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #!/usr/bin/env python
  2. import os
  3. import click
  4. from babel.messages.pofile import read_po, write_po
  5. JS_EXTENSIONS = (".js", ".jsx", ".ts", ".tsx")
  6. def merge_message(msg, frontend_msg):
  7. non_js_locations = [
  8. (fn, lineno) for fn, lineno in msg.locations if not fn.endswith(JS_EXTENSIONS)
  9. ]
  10. if (not msg.user_comments or not non_js_locations) and frontend_msg.user_comments:
  11. msg.user_comments = frontend_msg.usr_comments
  12. msg.locations = non_js_locations + frontend_msg.locations
  13. @click.command()
  14. @click.argument("locale")
  15. def cli(locale):
  16. catalog_file = "src/sentry/locale/%s/LC_MESSAGES/django.po" % locale
  17. frontend_file = "build/javascript.po"
  18. if not os.path.isfile(frontend_file):
  19. return
  20. with open(catalog_file) as f:
  21. catalog = read_po(f, locale)
  22. with open(frontend_file) as f:
  23. frontend = read_po(f, locale)
  24. for frontend_msg in frontend:
  25. if frontend_msg.id == "":
  26. continue
  27. msg = catalog.get(frontend_msg.id)
  28. # If the message is not yet in the catalog, then insert it as
  29. # such.
  30. if msg is None:
  31. msg = frontend_msg
  32. # Otherwise merge the record. Locations are merged by removing
  33. # all javascript locations and inserting new ones.
  34. else:
  35. merge_message(msg, frontend_msg)
  36. catalog[msg.id] = msg
  37. with open(catalog_file, "wb") as f:
  38. write_po(f, catalog)
  39. if __name__ == "__main__":
  40. cli()