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