get-translator-credits.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import re
  2. import subprocess
  3. from collections import defaultdict
  4. from babel import Locale
  5. authors_by_locale = defaultdict(set)
  6. file_listing = subprocess.Popen(
  7. "find ./wagtail -iname *.po", shell=True, stdout=subprocess.PIPE
  8. )
  9. for file_listing_line in file_listing.stdout:
  10. filename = file_listing_line.strip()
  11. # extract locale string from filename
  12. locale = re.search(r"locale/(\w+)/LC_MESSAGES", str(filename)).group(1)
  13. if locale == "en":
  14. continue
  15. # read author list from each file
  16. with open(filename) as f:
  17. has_found_translators_heading = False
  18. for line in f:
  19. line = line.strip()
  20. if line.startswith("#"):
  21. if has_found_translators_heading:
  22. author_match = re.match(r"\# (.*), [\d\-]+", line)
  23. if not author_match:
  24. break
  25. author = author_match.group(1)
  26. authors_by_locale[locale].add(author)
  27. elif line.startswith("# Translators:"):
  28. has_found_translators_heading = True
  29. else:
  30. if has_found_translators_heading:
  31. break
  32. else:
  33. raise Exception("No 'Translators:' heading found in %s" % filename)
  34. LANGUAGE_OVERRIDES = {
  35. "tet": "Tetum",
  36. "ht": "Haitian",
  37. "dv": "Divehi",
  38. }
  39. def get_language_name(locale_string):
  40. try:
  41. return LANGUAGE_OVERRIDES[locale_string]
  42. except KeyError:
  43. return Locale.parse(locale_string).english_name
  44. language_names = [
  45. (get_language_name(locale_string), locale_string)
  46. for locale_string in authors_by_locale.keys()
  47. ]
  48. language_names.sort()
  49. for language_name, locale in language_names:
  50. print(f"{language_name} - {locale}") # noqa: T201
  51. print("-----") # noqa: T201
  52. for author in sorted(authors_by_locale[locale]):
  53. print(author.replace("@", ".")) # noqa: T201
  54. print("") # noqa: T201