Back to

Package locale

Latest Go to latest

The latest major version is .

Published: 2 days ago | License: MIT | Module:


The http locale package helps to transport and use the localization information in a microservice landscape. It enables the propagation of the locale information using contexts.

Two important aspects of localization are part of this package the language (RFC 7231, section 5.3.5: Accept-Language) and Timezone (RFC 7808).

In order to get the Timezone information the package defines a new HTTP header "Accept-Timezone" to present times with the requested preference



const (
	HeaderAcceptLanguage = "Accept-Language"
	HeaderAcceptTimezone = "Accept-Timezone"
const None = ""

None is no timezone or language


var ErrNoTimezone = errors.New("no timezone given")

func ContextTransfer

func ContextTransfer(sourceCtx context.Context, targetCtx context.Context) context.Context

ContextTransfer sources the locale from the sourceCtx and returns a new context based on the targetCtx

func Handler

func Handler() func(http.Handler) http.Handler

Handler builds new Middleware

func WithLocale

func WithLocale(ctx context.Context, locale *Locale) context.Context

WithLocale creates a new context with the passed locale

type Locale

type Locale struct {
	// contains filtered or unexported fields

Locale contains the preferred language and timezone of the request

func FromCtx

func FromCtx(ctx context.Context) (*Locale, bool)

FromCtx returns the locale from the context. The returned locale is always not nil.

func FromRequest

func FromRequest(r *http.Request) *Locale

FromRequest creates a locale based on the accept headers from the given request.

func NewLocale

func NewLocale(acceptLanguage, acceptTimezone string) *Locale

NewLocale creates a new locale based on the passed accepted and language and timezone

func (Locale) HasLanguage

func (l Locale) HasLanguage() bool

HasTimezone returns true if the language is defined, false otherwise

func (Locale) HasTimezone

func (l Locale) HasTimezone() bool

HasTimezone returns true if the timezone is defined, false otherwise

func (Locale) Language

func (l Locale) Language() string

Language of the locale

func (Locale) Location

func (l Locale) Location() (*time.Location, error)

Location based of the locale timezone

func (Locale) Now

func (l Locale) Now() time.Time

Now returns the current time with the set timezone or local time if timezone is not set

func (Locale) Request

func (l Locale) Request(r *http.Request) *http.Request

Request returns the passed request with added accept headers. The request is returned for convenience.

func (Locale) Timezone

func (l Locale) Timezone() string

Timezone of the locale

type Middleware

type Middleware struct {
	// contains filtered or unexported fields

Middleware takes the accept lang and timezone info and stores them in the context

func (Middleware) ServeHTTP

func (m Middleware) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP adds the locale to the request context

type Strategy

type Strategy func(ctx context.Context) *Locale

Strategy defines a function that returns a Locale based on the passed Context

func NewContextStrategy

func NewContextStrategy() Strategy

NewContextStrategy returns a strategy that takes the locale form the request

func NewFallbackStrategy

func NewFallbackStrategy(lang, timezone string) Strategy

NewContextStrategy returns a strategy that defines a static fallback language and timezone. If only lang or timezone fallback should be defined as a fallback, the None value may be used.

type StrategyList

type StrategyList struct {
	// contains filtered or unexported fields

StrategyList has a list of strategies that are evaluated to find the correct user locale

func NewDefaultFallbackStrategy

func NewDefaultFallbackStrategy() *StrategyList

NewDefaultFallbackStrategy returns a strategy list configured via environment

func (*StrategyList) Locale

func (s *StrategyList) Locale(ctx context.Context) *Locale

Locale executes all strategies and returns the new locale

func (*StrategyList) PushBack

func (s *StrategyList) PushBack(strategies ...Strategy)

PushBack inserts the passed strategies at the back of list

func (*StrategyList) PushFront

func (s *StrategyList) PushFront(strategies ...Strategy)

PushFront inserts a passed strategies at the front of list

Package Files

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier