]> git.earlybird.gay Git - today/commitdiff
make SetLocaleFromPath use header as fallback
authorearly <me@earlybird.gay>
Sun, 5 Jan 2025 03:47:03 +0000 (20:47 -0700)
committerearly <me@earlybird.gay>
Sun, 5 Jan 2025 03:47:03 +0000 (20:47 -0700)
localization/http.go

index 83f48609163455b46b5e7e893f56c007cd1d0f20..7608508f2859f77492074aae44dbff52dad75ee2 100644 (file)
@@ -36,7 +36,7 @@ func SetLocaleFromHeader(defaultLang language.Tag) func(next http.Handler) http.
 }
 
 // SetLocaleFromHeader sets r.Context.Value(today.Locale) to the language in
-// the path param named by "param", or to defaultLang.
+// the path param named by "param", Accept-Language, or to defaultLang.
 // To avoid confusion, consider redirecting the user if they input a param that
 // isn't valid.
 func SetLocaleFromPath(param string, defaultLang language.Tag) func(next http.Handler) http.Handler {
@@ -46,12 +46,18 @@ func SetLocaleFromPath(param string, defaultLang language.Tag) func(next http.Ha
                                next.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), Locale, lang)))
                        }
                        lang := r.PathValue(param)
-                       if lang == "" {
-                               use(defaultLang)
+                       if lang != "" {
+                               tag := language.Make(lang)
+                               use(tag)
                                return
                        }
-                       tag := language.Make(lang)
-                       use(tag)
+                       lang = r.Header.Get("Accept-Language")
+                       if lang != "" {
+                               tag := language.Make(lang)
+                               use(tag)
+                               return
+                       }
+                       use(defaultLang)
                })
        }
 }