'Synchronize the translations to the terms to translate, after the
English value of a field is modified. The algorithm tries to match
existing translations to the terms to translate, provided the distance
between modified strings is not too large. It allows to not retranslate
data where a typo has been fixed in the English value.'

@api.model
def _sync_terms_translations(self, field, records):

    if (not callable(field.translate)):
        return
    trans = self.env['ir.translation']
    outdated = trans
    discarded = trans
    for record in records:
        value = record[field.name]
        terms = set(field.get_trans_terms(value))
        record_trans = trans.search([('type', '=', 'model'), ('name', '=', ('%s,%s' % (field.model_name, field.name))), ('res_id', '=', record.id)])
        if (not terms):
            discarded += record_trans
            continue
        for trans in record_trans:
            if (trans.src == trans.value):
                discarded += trans
            elif (trans.src not in terms):
                matches = get_close_matches(trans.src, terms, 1, 0.9)
                if matches:
                    trans.write({'src': matches[0], 'state': trans.state})
                else:
                    outdated += trans
    outdated.write({'state': 'to_translate'})
    discarded.unlink()
