blob: e038e0a0d35a1db895480b7dbc6ffd160956a822 [file] [log] [blame]
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package middleware
import (
type tagKey struct{}
var matcher = language.NewMatcher(message.DefaultCatalog.Languages())
// Language is a middleware that provides browser i18n information to handlers,
// in the form of a
func Language(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
tag, _ := language.MatchStrings(matcher, r.Header.Get("Accept-Language"))
h.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), tagKey{}, tag)))
// LanguageTag returns the language.Tag from the context, or language.English if none is set.
func LanguageTag(ctx context.Context) language.Tag {
if tag, ok := ctx.Value(tagKey{}).(language.Tag); ok {
return tag
return language.English