package main
import (
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/i18n/gi18n"
"github.com/gogf/gf/v2/os/gctx"
"github.com/gogf/gf/v2/util/gconv"
)
func main() {
type User struct {
Name string `v:"required#ReuiredUserName"`
Type int `v:"required#ReuiredUserType"`
}
var (
ctx = gctx.New()
data = g.Map{
"name": "john",
}
user = User{}
ctxEn = gi18n.WithLanguage(ctx, "en")
ctxCh = gi18n.WithLanguage(ctx, "zh-CN")
)
if err := gconv.Scan(data, &user); err != nil {
panic(err)
}
// 英文
if err := g.Validator().Assoc(data).Data(user).Run(ctxEn); err != nil {
g.Dump(err.String())
}
// 中文
if err := g.Validator().Assoc(data).Data(user).Run(ctxCh); err != nil {
g.Dump(err.String())
}
}