a2aa8b2a76
- Created i18n package with translator and middleware - Added translation files for English (en.json) and Spanish (es.json) - Updated all HTTP handlers to use i18n for error/success messages - Added comprehensive test coverage for i18n (87%) - Updated CI workflow to use Go 1.24 - All tests passing with 50.8% total coverage
31 lines
732 B
Go
31 lines
732 B
Go
package i18n
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
|
|
"golang.org/x/text/language"
|
|
)
|
|
|
|
type contextKey string
|
|
|
|
const languageKey contextKey = "language"
|
|
|
|
func LanguageMiddleware(translator *Translator) func(http.Handler) http.Handler {
|
|
return func(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
acceptLanguage := r.Header.Get("Accept-Language")
|
|
lang := translator.GetLanguage(acceptLanguage)
|
|
ctx := context.WithValue(r.Context(), languageKey, lang)
|
|
next.ServeHTTP(w, r.WithContext(ctx))
|
|
})
|
|
}
|
|
}
|
|
|
|
func GetLanguageFromContext(ctx context.Context) language.Tag {
|
|
if lang, ok := ctx.Value(languageKey).(language.Tag); ok {
|
|
return lang
|
|
}
|
|
return language.English
|
|
}
|