From 4cc93bac68cecc75da7f7ded1b37b7f09666a6fa Mon Sep 17 00:00:00 2001 From: Matt Westcott Date: Thu, 14 Oct 2021 15:05:36 +0100 Subject: [PATCH] Add script for finding translated strings with broken placeholders --- scripts/check-translation-strings.py | 22 ++++++++++++++++++++++ setup.py | 3 +++ 2 files changed, 25 insertions(+) create mode 100644 scripts/check-translation-strings.py diff --git a/scripts/check-translation-strings.py b/scripts/check-translation-strings.py new file mode 100644 index 0000000000..9017b8b25c --- /dev/null +++ b/scripts/check-translation-strings.py @@ -0,0 +1,22 @@ +import re + +from pathlib import Path + +import polib + + +placeholder_regexp = re.compile(r'\{[^\}]*?\}') + +for path in Path(__file__).parent.resolve().parent.rglob('LC_MESSAGES/*.po'): + po = polib.pofile(path) + for entry in po: + if not entry.msgstr: + continue # ignore untranslated strings + + expected_placeholders = set(placeholder_regexp.findall(entry.msgid)) + actual_placeholders = set(placeholder_regexp.findall(entry.msgstr)) + if expected_placeholders != actual_placeholders: + print("Invalid string at %s line %d:" % (path, entry.linenum)) + print("\toriginal string %r has placeholders: %r" % (entry.msgid, expected_placeholders)) + print("\ttranslated string %r has placeholders: %r" % (entry.msgstr, actual_placeholders)) + print() diff --git a/setup.py b/setup.py index 0d8d29f06f..bfe7e8fa4b 100755 --- a/setup.py +++ b/setup.py @@ -68,6 +68,9 @@ testing_extras = [ # django-taggit 1.3.0 made changes to verbose_name which affect migrations; # the test suite migrations correspond to >=1.3.0 'django-taggit>=1.3.0,<2.0', + + # for validating string formats in .po translation files + 'polib>=1.1,<2.0', ] # Documentation dependencies