bricks: Index | Files

package locale

import ""

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


Package Files

cfg.go context.go http.go locale.go strategy.go


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

None is no timezone or language


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

func ContextTransfer Uses

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 Uses

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

Handler builds new Middleware

func WithLocale Uses

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

WithLocale creates a new context with the passed locale

type Locale Uses

type Locale struct {
    // contains filtered or unexported fields

Locale contains the preferred language and timezone of the request

func FromCtx Uses

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

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

func FromRequest Uses

func FromRequest(r *http.Request) *Locale

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

func NewLocale Uses

func NewLocale(acceptLanguage, acceptTimezone string) *Locale

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

func (Locale) HasLanguage Uses

func (l Locale) HasLanguage() bool

HasTimezone returns true if the language is defined, false otherwise

func (Locale) HasTimezone Uses

func (l Locale) HasTimezone() bool

HasTimezone returns true if the timezone is defined, false otherwise

func (Locale) Language Uses

func (l Locale) Language() string

Language of the locale

func (Locale) Location Uses

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

Location based of the locale timezone

func (Locale) Now Uses

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 Uses

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 Uses

func (l Locale) Timezone() string

Timezone of the locale

type Middleware Uses

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 Uses

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

ServeHTTP adds the locale to the request context

type Strategy Uses

type Strategy func(ctx context.Context) *Locale

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

func NewContextStrategy Uses

func NewContextStrategy() Strategy

NewContextStrategy returns a strategy that takes the locale form the request

func NewFallbackStrategy Uses

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 Uses

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 Uses

func NewDefaultFallbackStrategy() *StrategyList

NewDefaultFallbackStrategy returns a strategy list configured via environment

func (*StrategyList) Locale Uses

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

Locale executes all strategies and returns the new locale

func (*StrategyList) PushBack Uses

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

PushBack inserts the passed strategies at the back of list

func (*StrategyList) PushFront Uses

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

PushFront inserts a passed strategies at the front of list

Package locale imports 7 packages (graph) and is imported by 3 packages. Updated 2020-04-17. Refresh now. Tools for package owners.