get-translator-credits.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. }
  38. def get_language_name(locale_string):
  39. try:
  40. return LANGUAGE_OVERRIDES[locale_string]
  41. except KeyError:
  42. return Locale.parse(locale_string).english_name
  43. language_names = [
  44. (get_language_name(locale_string), locale_string)
  45. for locale_string in authors_by_locale.keys()
  46. ]
  47. language_names.sort()
  48. for language_name, locale in language_names:
  49. print(f"{language_name} - {locale}") # noqa: T201
  50. print("-----") # noqa: T201
  51. for author in sorted(authors_by_locale[locale]):
  52. print(author.replace("@", ".")) # noqa: T201
  53. print("") # noqa: T201