# README
Language package for Golang
Package language provides HTTP middleware for parsing language from HTTP request and passing it via context.
How to install
Run the following command to install the package:
go get -u github.com/muonsoft/language
Example of reading language from Accept-Language header
package main
import (
"fmt"
"net/http"
"net/http/httptest"
"github.com/muonsoft/language"
)
func main() {
h := http.HandlerFunc(func (writer http.ResponseWriter, request *http.Request) {
tag := language.FromContext(request.Context())
fmt.Println("language:", tag)
})
m := language.NewMiddleware(h, language.SupportedLanguages(language.English, language.Russian))
r := httptest.NewRequest(http.MethodGet, "/", nil)
r.Header.Set("Accept-Language", "ru")
w := httptest.NewRecorder()
m.ServeHTTP(w, r)
// Output: language: ru
}
Example of reading language from Cookie
package main
import (
"fmt"
"net/http"
"net/http/httptest"
"github.com/muonsoft/language"
)
func main() {
h := http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
tag := language.FromContext(request.Context())
fmt.Println("language:", tag)
})
m := language.NewMiddleware(
h,
language.SupportedLanguages(language.English, language.Russian),
language.ReadFromCookie("lang"),
)
r := httptest.NewRequest(http.MethodGet, "/", nil)
r.AddCookie(&http.Cookie{Name: "lang", Value: "ru"})
w := httptest.NewRecorder()
m.ServeHTTP(w, r)
// Output: language: ru
}
# Functions
Equal compares language tags by base ISO 639 language code.
FromContext returns language tag.
NewMiddleware creates middleware for parsing language from Accept-Language header or a cookie and passing its value via context.
ReadFromAcceptHeader can be used to set up middleware to read language value from Accept-Language header.
ReadFromCookie can be used to set up middleware to read language value from cookie with given name.
SupportedLanguages is used to set up list of supported languages.
WithContext adds language tag to context.
# Variables
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
# Structs
Middleware is used to parse language from Accept-Language header or custom cookie from current request and pass best matching language via context to next http.Handler.
# Type aliases
MiddlewareOption is used to set up Middleware.
Tag is an alias for language.Tag.