package contactcheck import ( "errors" "strings" "unicode" "github.com/abadojack/whatlanggo" ) const ( minMessageRunes = 20 maxMessageRunes = 8000 maxNameRunes = 120 // Reject non-English only when the detector is fairly confident. englishRejectConfidence = 0.80 ) var ( ErrNotEnglish = errors.New("message must be in English") ErrTooShort = errors.New("message too short") ErrTooLong = errors.New("message too long") ErrName = errors.New("name too long") ) // ValidateEnglish checks script and language for name + message combined. func ValidateEnglish(name, message string) error { msg := strings.TrimSpace(message) nMsg := len([]rune(msg)) if nMsg < minMessageRunes { return ErrTooShort } if nMsg > maxMessageRunes { return ErrTooLong } if len([]rune(strings.TrimSpace(name))) > maxNameRunes { return ErrName } combined := strings.TrimSpace(name) + "\n\n" + msg info := whatlanggo.Detect(combined) // Block clearly non-Latin scripts (CJK, Cyrillic, etc.). if info.Script != nil && info.Script != unicode.Latin { return ErrNotEnglish } // Allow uncertain detection; block only confident non-English. if info.Lang != whatlanggo.Eng && info.IsReliable() { return ErrNotEnglish } if info.Lang != whatlanggo.Eng && info.Confidence >= englishRejectConfidence { return ErrNotEnglish } return nil }