1
0
forked from lxh/go-wxhelper
go-wxhelper/common/validator/validator.go
2024-07-04 14:10:46 +08:00

57 lines
1.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package validator
import (
"errors"
"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"
zhTranslations "github.com/go-playground/validator/v10/translations/zh"
"log"
"strings"
)
var (
uni *ut.UniversalTranslator
validate *validator.Validate
trans ut.Translator
)
// Init
// @description: 初始化验证器
func Init() {
//注册翻译器
zhTranslator := zh.New()
uni = ut.New(zhTranslator, zhTranslator)
trans, _ = uni.GetTranslator("zh")
//获取gin的校验器
validate = binding.Validator.Engine().(*validator.Validate)
//注册翻译器
err := zhTranslations.RegisterDefaultTranslations(validate, trans)
if err != nil {
log.Panicf("注册翻译器失败:%v", err)
}
}
// Translate
// @description: 翻译错误信息
// @param err
// @return error
func Translate(err error) error {
errorMsg := make([]string, 0)
var ves validator.ValidationErrors
ok := errors.As(err, &ves)
if !ok {
return err
}
for _, e := range ves {
errorMsg = append(errorMsg, e.Translate(trans))
}
return errors.New(strings.Join(errorMsg, ""))
}