Middleware detect-lang disabled if lang=X is set in the session storage
- commit
- 76277bdaaaf3048cf288376d4d917f5727e13fce
- author
- bnema · · 2023-09-04 19:36 UTC
- parent
- 249d4fc
- signature
- unsigned
4 files · +31 -0
M go.mod +4 -0
| @@ -17,6 +17,9 @@ require ( | |||
| 17 | 17 | github.com/docker/go-units v0.5.0 // indirect | |
| 18 | 18 | github.com/gogo/protobuf v1.3.2 // indirect | |
| 19 | 19 | github.com/golang-jwt/jwt v3.2.2+incompatible // indirect | |
| 20 | + | github.com/gorilla/context v1.1.1 // indirect | |
| 21 | + | github.com/gorilla/securecookie v1.1.1 // indirect | |
| 22 | + | github.com/gorilla/sessions v1.2.1 // indirect | |
| 20 | 23 | github.com/opencontainers/go-digest v1.0.0 // indirect | |
| 21 | 24 | github.com/opencontainers/image-spec v1.0.2 // indirect | |
| 22 | 25 | github.com/pkg/errors v0.9.1 // indirect | |
| @@ -26,6 +29,7 @@ require ( | |||
| 26 | 29 | ) | |
| 27 | 30 | ||
| 28 | 31 | require ( | |
| 32 | + | github.com/labstack/echo-contrib v0.15.0 | |
| 29 | 33 | github.com/labstack/gommon v0.4.0 // indirect | |
| 30 | 34 | github.com/mattn/go-colorable v0.1.13 // indirect | |
| 31 | 35 | github.com/mattn/go-isatty v0.0.19 // indirect | |
| @@ -17,6 +17,9 @@ require ( | |||||
| 17 | github.com/docker/go-units v0.5.0 // indirect | 17 | github.com/docker/go-units v0.5.0 // indirect | ||
| 18 | github.com/gogo/protobuf v1.3.2 // indirect | 18 | github.com/gogo/protobuf v1.3.2 // indirect | ||
| 19 | github.com/golang-jwt/jwt v3.2.2+incompatible // indirect | 19 | github.com/golang-jwt/jwt v3.2.2+incompatible // indirect | ||
| 20 | + | github.com/gorilla/context v1.1.1 // indirect | |||
| 21 | + | github.com/gorilla/securecookie v1.1.1 // indirect | |||
| 22 | + | github.com/gorilla/sessions v1.2.1 // indirect | |||
| 20 | github.com/opencontainers/go-digest v1.0.0 // indirect | 23 | github.com/opencontainers/go-digest v1.0.0 // indirect | ||
| 21 | github.com/opencontainers/image-spec v1.0.2 // indirect | 24 | github.com/opencontainers/image-spec v1.0.2 // indirect | ||
| 22 | github.com/pkg/errors v0.9.1 // indirect | 25 | github.com/pkg/errors v0.9.1 // indirect | ||
| @@ -26,6 +29,7 @@ require ( | |||||
| 26 | ) | 29 | ) | ||
| 27 | 30 | ||||
| 28 | require ( | 31 | require ( | ||
| 32 | + | github.com/labstack/echo-contrib v0.15.0 | |||
| 29 | github.com/labstack/gommon v0.4.0 // indirect | 33 | github.com/labstack/gommon v0.4.0 // indirect | ||
| 30 | github.com/mattn/go-colorable v0.1.13 // indirect | 34 | github.com/mattn/go-colorable v0.1.13 // indirect | ||
| 31 | github.com/mattn/go-isatty v0.0.19 // indirect | 35 | github.com/mattn/go-isatty v0.0.19 // indirect | ||
M go.sum +8 -0 generated file: collapsed
M internal/http/middlewares/detect-lang.go +8 -0
| @@ -3,6 +3,7 @@ package middlewares | |||
| 3 | 3 | import ( | |
| 4 | 4 | "strings" | |
| 5 | 5 | ||
| 6 | + | "github.com/labstack/echo-contrib/session" | |
| 6 | 7 | "github.com/labstack/echo/v4" | |
| 7 | 8 | ) | |
| 8 | 9 | ||
| @@ -19,6 +20,13 @@ func LanguageDetectionMiddleware(next echo.HandlerFunc) echo.HandlerFunc { | |||
| 19 | 20 | } | |
| 20 | 21 | ||
| 21 | 22 | func detectCurrentLanguage(c echo.Context) string { | |
| 23 | + | // Check if the lang key is set in the session storage | |
| 24 | + | sess, _ := session.Get("session", c) // Assuming you have a session named "session" | |
| 25 | + | if langValue, ok := sess.Values["lang"]; ok { | |
| 26 | + | return langValue.(string) // Return the lang value from the session storage | |
| 27 | + | } | |
| 28 | + | ||
| 29 | + | // If lang key is not set, proceed with "Accept-Language" header detection | |
| 22 | 30 | header := c.Request().Header.Get("Accept-Language") | |
| 23 | 31 | if strings.Contains(header, "fr") { | |
| 24 | 32 | return "fr" | |
| @@ -3,6 +3,7 @@ package middlewares | |||||
| 3 | import ( | 3 | import ( | ||
| 4 | "strings" | 4 | "strings" | ||
| 5 | 5 | ||||
| 6 | + | "github.com/labstack/echo-contrib/session" | |||
| 6 | "github.com/labstack/echo/v4" | 7 | "github.com/labstack/echo/v4" | ||
| 7 | ) | 8 | ) | ||
| 8 | 9 | ||||
| @@ -19,6 +20,13 @@ func LanguageDetectionMiddleware(next echo.HandlerFunc) echo.HandlerFunc { | |||||
| 19 | } | 20 | } | ||
| 20 | 21 | ||||
| 21 | func detectCurrentLanguage(c echo.Context) string { | 22 | func detectCurrentLanguage(c echo.Context) string { | ||
| 23 | + | // Check if the lang key is set in the session storage | |||
| 24 | + | sess, _ := session.Get("session", c) // Assuming you have a session named "session" | |||
| 25 | + | if langValue, ok := sess.Values["lang"]; ok { | |||
| 26 | + | return langValue.(string) // Return the lang value from the session storage | |||
| 27 | + | } | |||
| 28 | + | ||||
| 29 | + | // If lang key is not set, proceed with "Accept-Language" header detection | |||
| 22 | header := c.Request().Header.Get("Accept-Language") | 30 | header := c.Request().Header.Get("Accept-Language") | ||
| 23 | if strings.Contains(header, "fr") { | 31 | if strings.Contains(header, "fr") { | ||
| 24 | return "fr" | 32 | return "fr" | ||
M logs/app.log +11 -0
| @@ -48,3 +48,14 @@ | |||
| 48 | 48 | {"level":"warn","time":"2023-09-04T14:51:10+02:00","message":"Server will shutdown in 5 seconds press ENTER to cancel"} | |
| 49 | 49 | {"level":"info","time":"2023-09-04T14:51:15+02:00","message":"Server shutdown"} | |
| 50 | 50 | {"level":"info","time":"2023-09-04T17:18:16+02:00","message":"Server is listening on port 1323"} | |
| 51 | + | {"level":"info","time":"2023-09-04T21:36:24+02:00","message":"Server is listening on port 1323"} | |
| 52 | + | {"level":"info","time":"2023-09-04T21:36:24+02:00","message":"Server is listening on port 1323"} | |
| 53 | + | {"level":"fatal","error":"listen tcp :1323: bind: address already in use","time":"2023-09-04T21:36:24+02:00","message":"Failed to start the server"} | |
| 54 | + | {"level":"info","time":"2023-09-04T21:36:24+02:00","message":"Server is listening on port 1323"} | |
| 55 | + | {"level":"fatal","error":"listen tcp :1323: bind: address already in use","time":"2023-09-04T21:36:24+02:00","message":"Failed to start the server"} | |
| 56 | + | {"level":"info","time":"2023-09-04T21:36:25+02:00","message":"Server is listening on port 1323"} | |
| 57 | + | {"level":"fatal","error":"listen tcp :1323: bind: address already in use","time":"2023-09-04T21:36:25+02:00","message":"Failed to start the server"} | |
| 58 | + | {"level":"info","time":"2023-09-04T21:36:25+02:00","message":"Server is listening on port 1323"} | |
| 59 | + | {"level":"fatal","error":"listen tcp :1323: bind: address already in use","time":"2023-09-04T21:36:25+02:00","message":"Failed to start the server"} | |
| 60 | + | {"level":"info","time":"2023-09-04T21:36:25+02:00","message":"Server is listening on port 1323"} | |
| 61 | + | {"level":"fatal","error":"listen tcp :1323: bind: address already in use","time":"2023-09-04T21:36:25+02:00","message":"Failed to start the server"} | |
| @@ -48,3 +48,14 @@ | |||||
| 48 | {"level":"warn","time":"2023-09-04T14:51:10+02:00","message":"Server will shutdown in 5 seconds press ENTER to cancel"} | 48 | {"level":"warn","time":"2023-09-04T14:51:10+02:00","message":"Server will shutdown in 5 seconds press ENTER to cancel"} | ||
| 49 | {"level":"info","time":"2023-09-04T14:51:15+02:00","message":"Server shutdown"} | 49 | {"level":"info","time":"2023-09-04T14:51:15+02:00","message":"Server shutdown"} | ||
| 50 | {"level":"info","time":"2023-09-04T17:18:16+02:00","message":"Server is listening on port 1323"} | 50 | {"level":"info","time":"2023-09-04T17:18:16+02:00","message":"Server is listening on port 1323"} | ||
| 51 | + | {"level":"info","time":"2023-09-04T21:36:24+02:00","message":"Server is listening on port 1323"} | |||
| 52 | + | {"level":"info","time":"2023-09-04T21:36:24+02:00","message":"Server is listening on port 1323"} | |||
| 53 | + | {"level":"fatal","error":"listen tcp :1323: bind: address already in use","time":"2023-09-04T21:36:24+02:00","message":"Failed to start the server"} | |||
| 54 | + | {"level":"info","time":"2023-09-04T21:36:24+02:00","message":"Server is listening on port 1323"} | |||
| 55 | + | {"level":"fatal","error":"listen tcp :1323: bind: address already in use","time":"2023-09-04T21:36:24+02:00","message":"Failed to start the server"} | |||
| 56 | + | {"level":"info","time":"2023-09-04T21:36:25+02:00","message":"Server is listening on port 1323"} | |||
| 57 | + | {"level":"fatal","error":"listen tcp :1323: bind: address already in use","time":"2023-09-04T21:36:25+02:00","message":"Failed to start the server"} | |||
| 58 | + | {"level":"info","time":"2023-09-04T21:36:25+02:00","message":"Server is listening on port 1323"} | |||
| 59 | + | {"level":"fatal","error":"listen tcp :1323: bind: address already in use","time":"2023-09-04T21:36:25+02:00","message":"Failed to start the server"} | |||
| 60 | + | {"level":"info","time":"2023-09-04T21:36:25+02:00","message":"Server is listening on port 1323"} | |||
| 61 | + | {"level":"fatal","error":"listen tcp :1323: bind: address already in use","time":"2023-09-04T21:36:25+02:00","message":"Failed to start the server"} | |||