find-good-catalogs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #!/usr/bin/env python
  2. import os
  3. import json
  4. import click
  5. from babel.messages.pofile import read_po
  6. MINIMUM = 80
  7. def is_translated(msg):
  8. if isinstance(msg.string, basestring):
  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%%' % (
  35. locale,
  36. pct,
  37. ), err=True)
  38. if pct >= MINIMUM and locale not in rv:
  39. rv.append(locale)
  40. with open(catalog_file, 'w') as f:
  41. json.dump({
  42. 'supported_locales': sorted(rv)
  43. }, f, indent=2)
  44. f.write('\n')
  45. if __name__ == '__main__':
  46. cli()