こんな感じで変換しました
// target struct
type User struct {
Email string `validate:"required,email" ja:"メールアドレス"`
}
// validator
func (Validator) Validate(target interface{}) error {
translator := ja.New()
uni := ut.New(translator, translator)
trans, found := uni.GetTranslator("ja")
if !found {
return nil
}
validate := cValidator.New()
// タグを日本語に変換
validate.RegisterTagNameFunc(func(fld reflect.StructField) string {
fieldName := fld.Tag.Get("ja")
if fieldName == "-" {
return ""
}
return fieldName
})
err := jatranslations.RegisterDefaultTranslations(validate, trans)
if err != nil {
return err
}
err = validate.Struct(target)
var result error
for _, e := range err.(cValidator.ValidationErrors) {
result = multierror.Append(result, errors.New(e.Translate(trans)))
}
return result
}
コメントを残す