✨ add i18n support
This commit is contained in:
28
common/i18n/middleware.go
Normal file
28
common/i18n/middleware.go
Normal file
@@ -0,0 +1,28 @@
|
||||
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))
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user