2
0

check-translation-strings.py 966 B

1234567891011121314151617181920212223242526
  1. import re
  2. from pathlib import Path
  3. import polib
  4. placeholder_regexp = re.compile(r"\{[^\}]*?\}")
  5. for path in Path(__file__).parent.resolve().parent.rglob("LC_MESSAGES/*.po"):
  6. po = polib.pofile(path)
  7. for entry in po:
  8. if not entry.msgstr:
  9. continue # ignore untranslated strings
  10. expected_placeholders = set(placeholder_regexp.findall(entry.msgid))
  11. actual_placeholders = set(placeholder_regexp.findall(entry.msgstr))
  12. if expected_placeholders != actual_placeholders:
  13. print("Invalid string at %s line %d:" % (path, entry.linenum)) # noqa: T201
  14. print( # noqa: T201
  15. "\toriginal string %r has placeholders: %r"
  16. % (entry.msgid, expected_placeholders)
  17. )
  18. print( # noqa: T201
  19. "\ttranslated string %r has placeholders: %r"
  20. % (entry.msgstr, actual_placeholders)
  21. )
  22. print() # noqa: T201