Documentation ¶
Overview ¶
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
Index ¶
- Constants
- Variables
- func ContextTransfer(sourceCtx context.Context, targetCtx context.Context) context.Context
- func Handler() func(http.Handler) http.Handler
- func WithLocale(ctx context.Context, locale *Locale) context.Context
- type Locale
- func (l Locale) HasLanguage() bool
- func (l Locale) HasTimezone() bool
- func (l Locale) Language() string
- func (l Locale) Location() (*time.Location, error)
- func (l Locale) Now() time.Time
- func (l Locale) Request(r *http.Request) *http.Request
- func (l Locale) Serialize() string
- func (l Locale) Timezone() string
- type Middleware
- type Strategy
- type StrategyList
Constants ¶
const ( HeaderAcceptLanguage = "Accept-Language" HeaderAcceptTimezone = "Accept-Timezone" )
const None = ""
None is no timezone or language
Variables ¶
var ErrNoTimezone = errors.New("no timezone given")
Functions ¶
func ContextTransfer ¶
ContextTransfer sources the locale from the sourceCtx and returns a new context based on the targetCtx
Types ¶
type Locale ¶
type Locale struct {
// contains filtered or unexported fields
}
Locale contains the preferred language and timezone of the request
func FromRequest ¶
FromRequest creates a locale based on the accept headers from the given request.
func NewLocale ¶
NewLocale creates a new locale based on the passed accepted and language and timezone
func ParseLocale ¶ added in v0.2.4
ParseLocale parses a serialized locale
func (Locale) HasLanguage ¶
HasTimezone returns true if the language is defined, false otherwise
func (Locale) HasTimezone ¶
HasTimezone returns true if the timezone is defined, false otherwise
func (Locale) Now ¶
Now returns the current time with the set timezone or local time if timezone is not set
func (Locale) Request ¶
Request returns the passed request with added accept headers. The request is returned for convenience.
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 ¶
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 ¶
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