summaryrefslogtreecommitdiffstats
path: root/debian/check_translations
blob: 4343e33f1123c3a63586868cfabdaad78cd81c28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/python3

# Author: Benjamin Drung <bdrung@ubuntu.com>

"""Check translation .po files for inconsistencies like identical translations."""


import argparse
import collections
import logging
import os
import sys

import polib

LOG_FORMAT = "%(name)s %(levelname)s: %(message)s"
__script_name__ = os.path.basename(sys.argv[0]) if __name__ == "__main__" else __name__


def check_duplicate_translations(po_file: polib.POFile) -> int:
    """Check if different msgids have the identical translation."""
    msgstr_to_msgids = collections.defaultdict(set)
    for entry in po_file:
        if entry.msgstr == "":
            continue
        msgstr_to_msgids[entry.msgstr].add(entry.msgid)

    # Drop single translations
    duplicate_translations = {
        msgstr: msgids for msgstr, msgids in msgstr_to_msgids.items() if len(msgids) > 1
    }
    if not duplicate_translations:
        return 0

    logger = logging.getLogger(__script_name__)
    for msgstr, msgids in duplicate_translations.items():
        logger.warning(
            "Following IDs have the identical '%s' translation '%s': %s",
            po_file.metadata["Language"],
            msgstr,
            ", ".join(sorted(msgids)),
        )
    return 1


def check_translation(po_filename: str) -> int:
    """Check translation .po file for issues."""
    po_file = polib.pofile(po_filename)
    return check_duplicate_translations(po_file)


def main() -> int:
    """Check translation .po files for inconsistencies like identical translations."""
    parser = argparse.ArgumentParser()
    parser.add_argument("po_file", nargs="*")
    args = parser.parse_args()
    logging.basicConfig(format=LOG_FORMAT, level=logging.INFO)

    failures = 0
    for po_filename in args.po_file:
        failures += check_translation(po_filename)
    return failures


if __name__ == "__main__":
    sys.exit(main())