package unity import ( "fmt" "github.com/gin-gonic/gin/binding" "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" enTranslations "github.com/go-playground/validator/v10/translations/en" chTranslations "github.com/go-playground/validator/v10/translations/zh" ) var Trans ut.Translator func Translator(local string) (err error) { if v, ok := binding.Validator.Engine().(*validator.Validate); ok { zhT := zh.New() enT := en.New() uni := ut.New(enT, zhT, enT) var o bool Trans, o = uni.GetTranslator(local) if !o { return fmt.Errorf("uni.GetTranslator(%s) failed", local) } //register translate // 注册翻译器 switch local { case "en": err = enTranslations.RegisterDefaultTranslations(v, Trans) case "zh": err = chTranslations.RegisterDefaultTranslations(v, Trans) default: err = enTranslations.RegisterDefaultTranslations(v, Trans) } return } return }