fix validator translations map read and write
This commit is contained in:
parent
5dab1f64cd
commit
c495e57d2d
|
@ -3,6 +3,7 @@ package global
|
|||
import (
|
||||
"blog/pkg/logger"
|
||||
"blog/pkg/setting"
|
||||
ut "github.com/go-playground/universal-translator"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -12,4 +13,6 @@ var (
|
|||
Logger *logger.Logger
|
||||
JWTSetting *setting.JWT
|
||||
EmailSetting *setting.EmailSettingS
|
||||
ZhTrans ut.Translator
|
||||
EnTrans ut.Translator
|
||||
)
|
||||
|
|
|
@ -1,38 +1,22 @@
|
|||
package middleware
|
||||
|
||||
import (
|
||||
"blog/global"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gin-gonic/gin/binding"
|
||||
"github.com/go-playground/locales/en"
|
||||
"github.com/go-playground/locales/zh"
|
||||
"github.com/go-playground/locales/zh_Hant_TW"
|
||||
ut "github.com/go-playground/universal-translator"
|
||||
"github.com/go-playground/validator/v10"
|
||||
enTranslations "github.com/go-playground/validator/v10/translations/en"
|
||||
zhTranslations "github.com/go-playground/validator/v10/translations/zh"
|
||||
)
|
||||
|
||||
func Translations() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
uni := ut.New(en.New(), zh.New(), zh_Hant_TW.New())
|
||||
|
||||
locale := c.GetHeader("locale")
|
||||
trans, _ := uni.GetTranslator(locale)
|
||||
v, ok := binding.Validator.Engine().(*validator.Validate)
|
||||
if ok {
|
||||
switch locale {
|
||||
case "zh":
|
||||
_ = zhTranslations.RegisterDefaultTranslations(v, trans)
|
||||
break
|
||||
c.Set("trans", global.ZhTrans)
|
||||
case "en":
|
||||
_ = enTranslations.RegisterDefaultTranslations(v, trans)
|
||||
break
|
||||
c.Set("trans", global.EnTrans)
|
||||
default:
|
||||
_ = zhTranslations.RegisterDefaultTranslations(v, trans)
|
||||
break
|
||||
c.Set("trans", global.ZhTrans)
|
||||
}
|
||||
c.Set("trans", trans)
|
||||
}
|
||||
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
|
24
main.go
24
main.go
|
@ -7,6 +7,14 @@ import (
|
|||
"blog/pkg/logger"
|
||||
"blog/pkg/setting"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gin-gonic/gin/binding"
|
||||
"github.com/go-playground/locales/en"
|
||||
"github.com/go-playground/locales/zh"
|
||||
"github.com/go-playground/locales/zh_Hant_TW"
|
||||
ut "github.com/go-playground/universal-translator"
|
||||
"github.com/go-playground/validator/v10"
|
||||
enTranslations "github.com/go-playground/validator/v10/translations/en"
|
||||
zhTranslations "github.com/go-playground/validator/v10/translations/zh"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"gopkg.in/natefinch/lumberjack.v2"
|
||||
"log"
|
||||
|
@ -72,6 +80,22 @@ func setupSetting() error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
uni := ut.New(en.New(), zh.New(), zh_Hant_TW.New())
|
||||
global.ZhTrans, _ = uni.GetTranslator("zh")
|
||||
global.EnTrans, _ = uni.GetTranslator("en")
|
||||
v, ok := binding.Validator.Engine().(*validator.Validate)
|
||||
if ok {
|
||||
err = zhTranslations.RegisterDefaultTranslations(v, global.ZhTrans)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = enTranslations.RegisterDefaultTranslations(v, global.EnTrans)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
global.JWTSetting.Expire *= time.Second
|
||||
global.ServerSetting.ReadTimeout *= time.Second
|
||||
global.ServerSetting.WriteTimeout *= time.Second
|
||||
|
|
Loading…
Reference in New Issue
Block a user