翻譯器初始化
package valid
//gin > 1.4.0
//將驗(yàn)證器錯(cuò)誤翻譯成中文
import (
"github.com/gin-gonic/gin/binding"
"github.com/go-playground/locales/zh"
ut "github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
translation "github.com/go-playground/validator/v10/translations/zh"
)
var (
trans ut.Translator
)
func init() {
trans, _ = ut.New(zh.New()).GetTranslator("zh")
translation.RegisterDefaultTranslations(binding.Validator.Engine().(*validator.Validate), trans)
}
func Error(err error) (ret string) {
if validationErrors, ok := err.(validator.ValidationErrors); !ok {
return err.Error()
} else {
for _, e := range validationErrors {
ret += e.Translate(trans) + ";"
}
}
return ret
}
使用