mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2025-07-01 12:32:09 +00:00
create LocalizerMiddleware func
This commit is contained in:
parent
4865754b3d
commit
95e0d9a468
1 changed files with 19 additions and 0 deletions
|
@ -6,6 +6,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"x-ui/logger"
|
"x-ui/logger"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/nicksnyder/go-i18n/v2/i18n"
|
"github.com/nicksnyder/go-i18n/v2/i18n"
|
||||||
"github.com/pelletier/go-toml/v2"
|
"github.com/pelletier/go-toml/v2"
|
||||||
"golang.org/x/text/language"
|
"golang.org/x/text/language"
|
||||||
|
@ -82,6 +83,24 @@ func I18n(i18nType I18nType, key string, params ...string) string {
|
||||||
return msg
|
return msg
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func LocalizerMiddleware() gin.HandlerFunc {
|
||||||
|
return func(c *gin.Context) {
|
||||||
|
var lang string
|
||||||
|
|
||||||
|
if cookie, err := c.Request.Cookie("lang"); err == nil {
|
||||||
|
lang = cookie.Value
|
||||||
|
} else {
|
||||||
|
lang = c.GetHeader("Accept-Language")
|
||||||
|
}
|
||||||
|
|
||||||
|
LocalizerWeb = i18n.NewLocalizer(i18nBundle, lang)
|
||||||
|
|
||||||
|
c.Set("localizer", LocalizerWeb)
|
||||||
|
c.Set("I18n", I18n)
|
||||||
|
c.Next()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func parseTranslationFiles(i18nFS embed.FS, i18nBundle *i18n.Bundle) error {
|
func parseTranslationFiles(i18nFS embed.FS, i18nBundle *i18n.Bundle) error {
|
||||||
err := fs.WalkDir(i18nFS, "translation",
|
err := fs.WalkDir(i18nFS, "translation",
|
||||||
func(path string, d fs.DirEntry, err error) error {
|
func(path string, d fs.DirEntry, err error) error {
|
||||||
|
|
Loading…
Reference in a new issue