package validate import ( "github.com/go-playground/locales/en" "github.com/go-playground/locales/zh" ut "github.com/go-playground/universal-translator" "github.com/go-playground/validator/v10" zhTranslations "github.com/go-playground/validator/v10/translations/zh" ) var ( vt *validator.Validate tr ut.Translator ) func newTranslate(v *validator.Validate) ut.Translator { zhT := zh.New() enT := en.New() uni := ut.New(enT, zhT) trans, _ := uni.GetTranslator("zh") _ = zhTranslations.RegisterDefaultTranslations(v, trans) return trans } func init() { vt = validator.New() tr = newTranslate(vt) } func GrtTrans() ut.Translator { return tr } func Struct(data interface{}) error { return vt.Struct(data) }