mirror of
https://github.com/go-eagle/eagle.git
synced 2025-10-18 15:00:37 +08:00
40 lines
1.0 KiB
Go
40 lines
1.0 KiB
Go
package middleware
|
|
|
|
// nolint
|
|
import (
|
|
en_translations "github.com/go-playground/validator/v10/translations/en"
|
|
zh_translations "github.com/go-playground/validator/v10/translations/zh"
|
|
|
|
"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"
|
|
"github.com/go-playground/universal-translator"
|
|
"github.com/go-playground/validator/v10"
|
|
)
|
|
|
|
var uni = ut.New(en.New(), zh.New(), zh_Hant_TW.New())
|
|
|
|
// Translations .
|
|
func Translations() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
locale := c.GetHeader("locale")
|
|
trans, _ := uni.GetTranslator(locale)
|
|
v, ok := binding.Validator.Engine().(*validator.Validate)
|
|
if ok {
|
|
switch locale {
|
|
case "zh":
|
|
_ = zh_translations.RegisterDefaultTranslations(v, trans)
|
|
case "en":
|
|
_ = en_translations.RegisterDefaultTranslations(v, trans)
|
|
default:
|
|
_ = zh_translations.RegisterDefaultTranslations(v, trans)
|
|
}
|
|
c.Set("trans", trans)
|
|
}
|
|
|
|
c.Next()
|
|
}
|
|
}
|