merge-catalogs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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')
  6. def merge_message(msg, frontend_msg):
  7. non_js_locations = [
  8. (fn, lineno) for fn, lineno in msg.locations
  9. if not fn.endswith(JS_EXTENSIONS)
  10. ]
  11. if (not msg.user_comments or not non_js_locations) \
  12. and frontend_msg.user_comments:
  13. msg.user_comments = frontend_msg.usr_comments
  14. msg.locations = non_js_locations + frontend_msg.locations
  15. @click.command()
  16. @click.argument('locale')
  17. def cli(locale):
  18. catalog_file = 'src/sentry/locale/%s/LC_MESSAGES/django.po' % locale
  19. frontend_file = 'build/javascript.po'
  20. if not os.path.isfile(frontend_file):
  21. return
  22. with open(catalog_file) as f:
  23. catalog = read_po(f, locale)
  24. with open(frontend_file) as f:
  25. frontend = read_po(f, locale)
  26. for frontend_msg in frontend:
  27. if frontend_msg.id == '':
  28. continue
  29. msg = catalog.get(frontend_msg.id)
  30. # If the message is not yet in the catalog, then insert it as
  31. # such.
  32. if msg is None:
  33. msg = frontend_msg
  34. # Otherwise merge the record. Locations are merged by removing
  35. # all javascript locations and inserting new ones.
  36. else:
  37. merge_message(msg, frontend_msg)
  38. catalog[msg.id] = msg
  39. with open(catalog_file, 'w') as f:
  40. write_po(f, catalog)
  41. if __name__ == '__main__':
  42. cli()