This repository has been archived on 2024-11-28. You can view files and clone it, but cannot push or open issues or pull requests.
Files
schisandra-cloud-album/middleware/i18n.go
2024-08-12 22:05:59 +08:00

30 lines
695 B
Go

package middleware
import (
"github.com/BurntSushi/toml"
ginI18n "github.com/gin-contrib/i18n"
"github.com/gin-gonic/gin"
"golang.org/x/text/language"
)
func I18n() gin.HandlerFunc {
return ginI18n.Localize(
ginI18n.WithBundle(&ginI18n.BundleCfg{
RootPath: "i18n/language/",
AcceptLanguage: []language.Tag{language.Chinese, language.English},
DefaultLanguage: language.Chinese,
UnmarshalFunc: toml.Unmarshal,
FormatBundleFile: "toml",
}),
ginI18n.WithGetLngHandle(
func(context *gin.Context, defaultLng string) string {
lang := context.GetHeader("Accept-Language")
if lang == "" {
return defaultLng
}
return lang
},
),
)
}