diff options
-rw-r--r-- | main.go | 12 | ||||
-rw-r--r-- | middleware/i18n.go | 9 |
2 files changed, 17 insertions, 4 deletions
@@ -1,7 +1,7 @@ package main import ( - "context" + "errors" "flag" "fmt" "log" @@ -69,4 +69,14 @@ func setUserLanguage(w http.ResponseWriter, r *http.Request) { Value: loc, MaxAge: math.MaxInt32, }) + + if c, err := r.Cookie("redirect"); errors.Is(err, http.ErrNoCookie) { + http.Redirect(w, r, "/", http.StatusFound) + } else { + http.SetCookie(w, &http.Cookie{ + Name: "redirect", + MaxAge: -1, + }) + http.Redirect(w, r, c.Value, http.StatusFound) + } } diff --git a/middleware/i18n.go b/middleware/i18n.go index 5e33636..a30a9f6 100644 --- a/middleware/i18n.go +++ b/middleware/i18n.go @@ -28,12 +28,15 @@ func I18n(next http.Handler) http.Handler { } } - used := cmp.Or(p, i18n.DefaultPrinter) - ctx := context.WithValue(r.Context(), PrinterKey, used) + ctx := context.WithValue( + r.Context(), PrinterKey, cmp.Or(p, i18n.DefaultPrinter)) if p == pZero { + http.SetCookie(w, &http.Cookie{ + Name: "redirect", + Value: r.URL.Path, + }) templates.Root(nil, templates.SetLanguage()).Render(ctx, w) - /* TODO: Redirect the user back to where they came from */ } else { next.ServeHTTP(w, r.WithContext(ctx)) } |