Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/MHSanaei/3x-ui.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHamidreza Ghavami <70919649+hamid-gh98@users.noreply.github.com>2023-05-20 18:16:34 +0300
committerHamidreza Ghavami <70919649+hamid-gh98@users.noreply.github.com>2023-05-20 18:16:34 +0300
commit95e0d9a468dbf569daf65abc01c18f8f0f020dc1 (patch)
treeeeef16aaa70309c75a5a45408b841fbfeebf27df /web/locale
parent4865754b3d3d2d6a3296c8b07bb69a7f0bb0c489 (diff)
create LocalizerMiddleware func
Diffstat (limited to 'web/locale')
-rw-r--r--web/locale/locale.go19
1 files changed, 19 insertions, 0 deletions
diff --git a/web/locale/locale.go b/web/locale/locale.go
index ba7a30e5..8b32d04d 100644
--- a/web/locale/locale.go
+++ b/web/locale/locale.go
@@ -6,6 +6,7 @@ import (
"strings"
"x-ui/logger"
+ "github.com/gin-gonic/gin"
"github.com/nicksnyder/go-i18n/v2/i18n"
"github.com/pelletier/go-toml/v2"
"golang.org/x/text/language"
@@ -82,6 +83,24 @@ func I18n(i18nType I18nType, key string, params ...string) string {
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 {
err := fs.WalkDir(i18nFS, "translation",
func(path string, d fs.DirEntry, err error) error {