Documentation
¶
Overview ¶
Package middleware provides net/http middleware for locale detection and translator injection. It reads the Accept-Language header from incoming requests, derives a per-request locale-scoped translator, and stores it in the request context for downstream handlers.
This package is separated from the core i18n library so that users who do not need HTTP middleware do not pay the cost of importing net/http. Import it as:
import "github.com/0verkilll/i18n/middleware"
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func LocaleFromContext ¶
LocaleFromContext returns the raw detected locale string stored in the request context by LocaleFromRequest. Returns an empty string if no locale was detected (e.g., the request had no Accept-Language header) or if the middleware did not run on this context.
func LocaleFromRequest ¶
LocaleFromRequest returns an http.Handler middleware that detects the locale from the Accept-Language header and stores a locale-scoped ContextTranslator in the request context.
CONCURRENCY: this middleware does NOT mutate the shared Translator. Each request gets its own lightweight ContextTranslator carrying its detected locale, via i18n.Translator.WithLocaleContext. This makes it safe to use a single shared Translator across many concurrent HTTP requests with different Accept-Language headers — the previous behaviour of calling t.SetLocale raced between requests and could serve the wrong language to an unrelated request.
Downstream handlers retrieve the translator with TranslatorFromContext (or read the raw locale with LocaleFromContext).
If the Accept-Language header is empty or unparseable, the translator falls back to the shared Translator's current locale.
Usage:
translator, _ := i18n.New(
i18n.WithFileSystemLoader("locales"),
i18n.WithDefaultLocale("en-US"),
)
mux := http.NewServeMux()
mux.Handle("/", middleware.LocaleFromRequest(translator)(myHandler))
func TranslatorFromContext ¶
func TranslatorFromContext(ctx context.Context) *i18n.ContextTranslator
TranslatorFromContext retrieves the ContextTranslator stored in the request context by LocaleFromRequest. Returns nil if no translator is present.
The returned ContextTranslator is scoped to the request's detected locale; it is safe to use concurrently with other requests that may have detected different locales.
func TranslatorFromRequest ¶
func TranslatorFromRequest(r *http.Request) *i18n.ContextTranslator
TranslatorFromRequest is a convenience wrapper around TranslatorFromContext that accepts an *http.Request directly. Returns nil if no translator is present in the request context.
Types ¶
This section is empty.