diff --git a/web/controller/base.go b/web/controller/base.go index a9659bd2..674a195d 100644 --- a/web/controller/base.go +++ b/web/controller/base.go @@ -1,9 +1,12 @@ package controller import ( - "github.com/gin-gonic/gin" "net/http" + "x-ui/logger" + "x-ui/web/locale" "x-ui/web/session" + + "github.com/gin-gonic/gin" ) type BaseController struct { @@ -12,7 +15,7 @@ type BaseController struct { func (a *BaseController) checkLogin(c *gin.Context) { if !session.IsLogin(c) { if isAjax(c) { - pureJsonMsg(c, false, I18n(c, "pages.login.loginAgain")) + pureJsonMsg(c, false, I18nWeb(c, "pages.login.loginAgain")) } else { c.Redirect(http.StatusTemporaryRedirect, c.GetString("base_path")) } @@ -22,11 +25,13 @@ func (a *BaseController) checkLogin(c *gin.Context) { } } -func I18n(c *gin.Context, name string) string { - anyfunc, _ := c.Get("I18n") - i18n, _ := anyfunc.(func(key string, params ...string) (string, error)) - - message, _ := i18n(name) - - return message +func I18nWeb(c *gin.Context, name string, params ...string) string { + anyfunc, funcExists := c.Get("I18n") + if !funcExists { + logger.Warning("I18n function not exists in gin context!") + return "" + } + i18nFunc, _ := anyfunc.(func(i18nType locale.I18nType, key string, keyParams ...string) string) + msg := i18nFunc(locale.Web, name, params...) + return msg } diff --git a/web/web.go b/web/web.go index ba3d04cc..f1e7dcde 100644 --- a/web/web.go +++ b/web/web.go @@ -185,10 +185,10 @@ func (s *Server) initRouter() (*gin.Engine, error) { } // Apply locale middleware for i18n - webI18nFunc := func(key string, params ...string) string { + i18nWebFunc := func(key string, params ...string) string { return locale.I18n(locale.Web, key, params...) } - engine.FuncMap["i18n"] = webI18nFunc + engine.FuncMap["i18n"] = i18nWebFunc engine.Use(locale.LocalizerMiddleware()) // set static files and template