trans.go 727 Bytes
Newer Older
kzkzzzz's avatar
kzkzzzz committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
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)
}