middleware

package
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 30, 2026 License: MIT Imports: 3 Imported by: 0

README

i18n/middleware

HTTP middleware for Accept-Language locale detection.

This is a separate sub-package to avoid pulling net/http into WASM and TinyGo builds.

Install

go get github.com/0verkilll/i18n/middleware

Usage

import (
    i18n "github.com/0verkilll/i18n"
    "github.com/0verkilll/i18n/middleware"
)

t, _ := i18n.NewWithFS("locales", "en-US")

handler := middleware.LocaleFromRequest(t)(yourMux)

// In handlers:
func handleRequest(w http.ResponseWriter, r *http.Request) {
    tr := middleware.TranslatorFromContext(r.Context())
    fmt.Fprintln(w, tr.Translate("greeting"))
}

API

  • LocaleFromRequest(t *i18n.Translator) func(http.Handler) http.Handler — Wraps a handler to detect locale from Accept-Language header and store a ContextTranslator in the request context.
  • TranslatorFromContext(ctx context.Context) *i18n.ContextTranslator — Retrieves the ContextTranslator from the request context. Returns nil if not present.

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

func LocaleFromContext(ctx context.Context) string

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

func LocaleFromRequest(t *i18n.Translator) func(http.Handler) http.Handler

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL