find-good-catalogs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #!/usr/bin/env python
  2. import json # noqa: S003
  3. import os
  4. import click
  5. from babel.messages.pofile import read_po, write_po
  6. MINIMUM = 80
  7. def is_translated(msg):
  8. if isinstance(msg.string, bytes):
  9. return bool(msg.string)
  10. for item in msg.string:
  11. if not item:
  12. return False
  13. return True
  14. @click.command()
  15. @click.argument("catalog_file", type=click.Path())
  16. def cli(catalog_file):
  17. # Read the old ones back. Once we are in, we will never go.
  18. with open(catalog_file) as f:
  19. rv = json.load(f)["supported_locales"]
  20. base = "src/sentry/locale"
  21. for locale in os.listdir(base):
  22. fn = os.path.join(base, locale, "LC_MESSAGES", "django.po")
  23. if not os.path.isfile(fn):
  24. continue
  25. total_count = 0
  26. translated_count = 0
  27. with open(fn) as f:
  28. catalog = read_po(f)
  29. for msg in catalog:
  30. total_count += 1
  31. if is_translated(msg):
  32. translated_count += 1
  33. pct = translated_count / float(total_count) * 100
  34. click.echo("% -7s % 2d%%" % (locale, pct), err=True)
  35. if pct >= MINIMUM and locale not in rv:
  36. rv.append(locale)
  37. with open(fn, "wb") as fileobj:
  38. write_po(catalog=catalog, fileobj=fileobj, no_location=True)
  39. with open(catalog_file, "w") as f:
  40. json.dump({"supported_locales": sorted(rv)}, f, indent=2)
  41. f.write("\n")
  42. if __name__ == "__main__":
  43. cli()