def run_checks()

in pontoon/checks/libraries/pontoon_db.py [0:0]


def run_checks(entity, original, string):
    """
    Group all checks related to the base UI that get stored in the DB
    :arg pontoon.base.models.Entity entity: Source entity
    :arg basestring original: an original string
    :arg basestring string: a translation
    """
    checks = defaultdict(list)
    resource_ext = entity.resource.format

    # Bug 1599056: Original and translation must either both end in a newline,
    # or none of them should.
    if resource_ext == "po":
        if original.endswith("\n") != string.endswith("\n"):
            checks["pErrors"].append("Ending newline mismatch")

    # Prevent empty translation submissions if not supported
    if string == "" and not entity.resource.allows_empty_translations:
        checks["pErrors"].append("Empty translations are not allowed")

    # FTL checks
    if resource_ext == "ftl" and string != "":
        translation_ast = parser.parse_entry(string)
        entity_ast = parser.parse_entry(entity.string)

        # Parse error
        if isinstance(translation_ast, ast.Junk):
            checks["pErrors"].append(translation_ast.annotations[0].message)

        # Not a localizable entry
        elif not isinstance(translation_ast, (ast.Message, ast.Term)):
            checks["pErrors"].append(
                "Translation needs to be a valid localizable entry"
            )

        # Message ID mismatch
        elif entity_ast.id.name != translation_ast.id.name:
            checks["pErrors"].append("Translation key needs to match source string key")

        # Empty translation entry warning; set here rather than pontoon_non_db.py
        # to avoid needing to parse the Fluent message twice.
        else:
            visitor = IsEmptyVisitor()
            visitor.visit(translation_ast)
            if visitor.is_empty:
                checks["pndbWarnings"].append("Empty translation")

    return checks