}
// 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 {
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)
})
}
}