langmiddleware

package module
v0.0.0-...-ca7266c Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2019 License: MIT Imports: 4 Imported by: 0

README

Lang Middleware

The Lang Middleware provide a simple way to obtain the prefed language of the client.

How to use

Initialize a new middleware using one of the function provided.

  • NewCookieOnly returns a new LangMiddleware configured to use only informations from cookie named
  • NewHeaderOnly returns a new LangMiddleware configured to use only informations from accept-language header
  • NewHeaderAndCookie returns a new LangMiddleware configured to use informations from both cookie and accept-language header

Then use the Extractor() function to get the http handler.

## Example

package main

import (
	"net/http"
	"github.com/go-chi/chi"
	"github.com/jderail/langmiddleware"
)

func main() {

	lang, _ := langmiddleware.NewCookieOnly("en", []string{"en", "fr"}, "lang-cookie")
	r := chi.NewRouter()
	r.Use(lang.Extractor())
	r.Get("/", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte(r.Context().Value(langmiddleware.LangContextKey).(string)))
	})
	http.ListenAndServe(":3000", r)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrHeaderParsing is returned by ParseLangHeader when accept language header is not conform
	ErrHeaderParsing = errors.New("An error occurred while parsing accept-language header")
	// ErrEmptyHeader is returned by ParseLangHeader when no tags are provided in header
	ErrEmptyHeader = errors.New("Accept-language header is empty")
)
View Source
var LangContextKey = &contextKey{"lang"}

LangContextKey is the key used to store Lang in context

Functions

This section is empty.

Types

type LangMiddleware

type LangMiddleware struct {
	CookieName         string
	SupportedLanguages []string
	DefaultLanguage    string
	// contains filtered or unexported fields
}

LangMiddleware

func NewCookieAndHeader

func NewCookieAndHeader(defaultLang string, supportedLanguages []string, cookieName string) (*LangMiddleware, error)

NewCookieAndHeader returns a new LangMiddleware with a configuration to use informations from both cookie and accept-language header

func NewCookieOnly

func NewCookieOnly(defaultLang string, supportedLanguages []string, cookieName string) (*LangMiddleware, error)

NewCookieOnly returns a new LangMiddleware with a configuration to use only informations from cookie named

func NewHeaderOnly

func NewHeaderOnly(defaultLang string, supportedLanguages []string) (*LangMiddleware, error)

NewHeaderOnly returns a new LangMiddleware with a configuration to use only informations from accept-language header

func (*LangMiddleware) Extractor

func (i *LangMiddleware) Extractor() func(http.Handler) http.Handler

Extractor return http handler to use as middleware

type LangSource

type LangSource int

LangSource can be used to select possible source of language

const (
	// Cookie indicates that only cookie will be used to extract language informations
	Cookie LangSource = 1 << iota
	// Header indicates that only header will be used to extract language informations
	Header
	// HeaderAndCookie indicates that both header and cookie will be used to extract language informations
	HeaderAndCookie = Cookie | Header
)

Jump to

Keyboard shortcuts

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