Files
2024-12-24 00:38:41 +08:00

29 lines
700 B
Go

package i18n
import (
"net/http"
"golang.org/x/text/language"
)
type I18nMiddleware struct {
supportTags []language.Tag
localizationFiles []string
}
func NewI18nMiddleware(supportTags []language.Tag, localizationFiles []string) *I18nMiddleware {
return &I18nMiddleware{
supportTags: supportTags,
localizationFiles: localizationFiles,
}
}
func (m *I18nMiddleware) Handle(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
lang := r.Header.Get(defaultLangHeaderKey)
langTag := MatchCurrentLanguageTag(lang, m.supportTags)
bundle := NewBundle(langTag, m.localizationFiles...)
next(w, withRequest(r, langTag, bundle))
}
}