コード例 #1
0
def check_messages(filename, report_empty=False):
    """
    Checks messages in `filename` in various ways:

    * Translations must have the same slots as the English.

    * Messages can't have astral characters in them.

    If `report_empty` is True, will also report empty translation strings.

    Returns the problems, a list of tuples. Each is a description, a msgid, and
    then zero or more translations.

    """
    problems = []
    pomsgs = polib.pofile(filename)
    for msg in pomsgs:
        # Check for characters Javascript can't support.
        # https://code.djangoproject.com/ticket/21725
        if astral(msg.msgstr):
            problems.append(("Non-BMP char", msg.msgid, msg.msgstr))

        if is_format_message(msg):
            # LONG_DATE_FORMAT, etc, have %s etc in them, and that's ok.
            continue

        if msg.msgid_plural:
            # Plurals: two strings in, N strings out.
            source = msg.msgid + " | " + msg.msgid_plural
            translation = " | ".join(
                v for k, v in sorted(msg.msgstr_plural.items()))
            empty = any(not t.strip() for t in msg.msgstr_plural.values())
        else:
            # Singular: just one string in and one string out.
            source = msg.msgid
            translation = msg.msgstr
            empty = not msg.msgstr.strip()

        if empty:
            if report_empty:
                problems.append(("Empty translation", source))
        else:
            id_tags = tags_in_string(source)
            tx_tags = tags_in_string(translation)

            # Check if tags don't match
            if id_tags != tx_tags:
                id_has = ", ".join(sorted(f'"{t}"' for t in id_tags - tx_tags))
                tx_has = ", ".join(sorted(f'"{t}"' for t in tx_tags - id_tags))
                if id_has and tx_has:
                    diff = f"{id_has} vs {tx_has}"
                elif id_has:
                    diff = f"{id_has} missing"
                else:
                    diff = f"{tx_has} added"
                problems.append(("Different tags in source and translation",
                                 source, translation, diff))

    return problems
コード例 #2
0
ファイル: validate.py プロジェクト: wkoha/i18n-tools
def check_messages(filename, report_empty=False):
    """
    Checks messages in `filename` in various ways:

    * Translations must have the same slots as the English.

    * Messages can't have astral characters in them.

    If `report_empty` is True, will also report empty translation strings.

    Returns the problems, a list of tuples. Each is a description, a msgid, and
    then zero or more translations.

    """
    problems = []
    pomsgs = polib.pofile(filename)
    for msg in pomsgs:
        # Check for characters Javascript can't support.
        # https://code.djangoproject.com/ticket/21725
        if astral(msg.msgstr):
            problems.append(("Non-BMP char", msg.msgid, msg.msgstr))

        if is_format_message(msg):
            # LONG_DATE_FORMAT, etc, have %s etc in them, and that's ok.
            continue

        if msg.msgid_plural:
            # Plurals: two strings in, N strings out.
            source = msg.msgid + " | " + msg.msgid_plural
            translation = " | ".join(v for k, v in sorted(msg.msgstr_plural.items()))
            empty = any(not t.strip() for t in msg.msgstr_plural.values())
        else:
            # Singular: just one string in and one string out.
            source = msg.msgid
            translation = msg.msgstr
            empty = not msg.msgstr.strip()

        if empty:
            if report_empty:
                problems.append(("Empty translation", source))
        else:
            id_tags = tags_in_string(source)
            tx_tags = tags_in_string(translation)

            # Check if tags don't match
            if id_tags != tx_tags:
                id_has = u", ".join(u'"{}"'.format(t) for t in id_tags - tx_tags)
                tx_has = u", ".join(u'"{}"'.format(t) for t in tx_tags - id_tags)
                if id_has and tx_has:
                    diff = u"{} vs {}".format(id_has, tx_has)
                elif id_has:
                    diff = u"{} missing".format(id_has)
                else:
                    diff = u"{} added".format(tx_has)
                problems.append((
                    "Different tags in source and translation",
                    source,
                    translation,
                    diff
                ))

    return problems