From c495e57d2dc756beb68a902ca036a3e75f39d46c Mon Sep 17 00:00:00 2001 From: xing Date: Thu, 3 Jun 2021 17:03:43 +0800 Subject: [PATCH] fix validator translations map read and write --- global/setting.go | 3 +++ internal/middleware/translations.go | 36 ++++++++--------------------- main.go | 24 +++++++++++++++++++ 3 files changed, 37 insertions(+), 26 deletions(-) diff --git a/global/setting.go b/global/setting.go index 7e8443b..1aadc53 100644 --- a/global/setting.go +++ b/global/setting.go @@ -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 ) diff --git a/internal/middleware/translations.go b/internal/middleware/translations.go index e98f968..a61b4a5 100644 --- a/internal/middleware/translations.go +++ b/internal/middleware/translations.go @@ -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 - 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() } } diff --git a/main.go b/main.go index 8dcaa73..c3982be 100644 --- a/main.go +++ b/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