find-good-catalogs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/usr/bin/env python
  2. import json # noqa: S003
  3. import os
  4. import click
  5. from babel.messages.pofile import read_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(catalog_file, "w") as f:
  38. json.dump({"supported_locales": sorted(rv)}, f, indent=2)
  39. f.write("\n")
  40. if __name__ == "__main__":
  41. cli()