Blame scripts/sanitize_po_files.py

Packit 6f3914
#!/usr/bin/python3
Packit 6f3914
Packit 6f3914
# script to get rid of
Packit 6f3914
# "'msgid' and 'msgstr' entries do not both begin/end with '\n'"
Packit 6f3914
# error messages during .po to .mo files conversion via msgfmt tool
Packit 6f3914
#
Packit 6f3914
# 'polib' module is needed for run: https://pypi.python.org/pypi/polib
Packit 6f3914
#
Packit 6f3914
# usage: python3 sanitize_po_files.py [po_file...]
Packit 6f3914
#
Packit 6f3914
# in order to update translations from zanata, do:
Packit 6f3914
#  * cmake .
Packit 6f3914
#  * make gettext-update
Packit 6f3914
#  * git add po/*.po
Packit 6f3914
#  * ./scripts/sanitize_po_files.py po/*.po
Packit 6f3914
#  * git commit -m "zanata update"
Packit 6f3914
Packit 6f3914
import polib
Packit 6f3914
import re
Packit 6f3914
import sys
Packit 6f3914
Packit 6f3914
Packit 6f3914
def sanitize_po_file(po_file):
Packit 6f3914
    print("Processing", po_file)
Packit 6f3914
    po = polib.pofile(po_file)
Packit 6f3914
    for entry in po:
Packit 6f3914
        msgid_without_indents = entry.msgid.strip()
Packit 6f3914
        msgstr_without_indents = entry.msgstr.strip()
Packit 6f3914
        entry.msgstr = entry.msgid.replace(
Packit 6f3914
            msgid_without_indents, msgstr_without_indents)
Packit 6f3914
        if re.match(r"^\s+$", entry.msgstr):
Packit 6f3914
            entry.msgstr = ""
Packit 6f3914
Packit 6f3914
        if entry.msgid_plural:
Packit 6f3914
            msgid_plural_without_indents = entry.msgid_plural.strip()
Packit 6f3914
            for i in entry.msgstr_plural.keys():
Packit 6f3914
                msgstr_plural_without_indents = entry.msgstr_plural[i].strip()
Packit 6f3914
                entry.msgstr_plural[i] = entry.msgid_plural.replace(
Packit 6f3914
                    msgid_plural_without_indents,
Packit 6f3914
                    msgstr_plural_without_indents)
Packit 6f3914
                if re.match(r"^\s+$", entry.msgstr_plural[i]):
Packit 6f3914
                    entry.msgstr_plural[i] = ""
Packit 6f3914
    po.save()
Packit 6f3914
Packit 6f3914
Packit 6f3914
if __name__ == "__main__":
Packit 6f3914
    for po_file in sys.argv[1:]:
Packit 6f3914
        sanitize_po_file(po_file)