Documentation ¶
Overview ¶
Package language provides HTTP middleware for parsing language from HTTP request and passing it via context.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( Und = language.Und Afrikaans = language.Afrikaans Amharic = language.Amharic Arabic = language.Arabic ModernStandardArabic = language.ModernStandardArabic Azerbaijani = language.Azerbaijani Bulgarian = language.Bulgarian Bengali = language.Bengali Catalan = language.Catalan Czech = language.Czech Danish = language.Danish German = language.German Greek = language.Greek English = language.English AmericanEnglish = language.AmericanEnglish BritishEnglish = language.BritishEnglish Spanish = language.Spanish EuropeanSpanish = language.EuropeanSpanish LatinAmericanSpanish = language.LatinAmericanSpanish Estonian = language.Estonian Persian = language.Persian Finnish = language.Finnish Filipino = language.Filipino French = language.French CanadianFrench = language.CanadianFrench Gujarati = language.Gujarati Hebrew = language.Hebrew Hindi = language.Hindi Croatian = language.Croatian Hungarian = language.Hungarian Armenian = language.Armenian Indonesian = language.Indonesian Icelandic = language.Icelandic Italian = language.Italian Japanese = language.Japanese Georgian = language.Georgian Kazakh = language.Kazakh Khmer = language.Khmer Kannada = language.Kannada Korean = language.Korean Kirghiz = language.Kirghiz Lao = language.Lao Lithuanian = language.Lithuanian Latvian = language.Latvian Macedonian = language.Macedonian Malayalam = language.Malayalam Mongolian = language.Mongolian Marathi = language.Marathi Malay = language.Malay Burmese = language.Burmese Nepali = language.Nepali Dutch = language.Dutch Norwegian = language.Norwegian Punjabi = language.Punjabi Polish = language.Polish Portuguese = language.Portuguese BrazilianPortuguese = language.BrazilianPortuguese EuropeanPortuguese = language.EuropeanPortuguese Romanian = language.Romanian Russian = language.Russian Sinhala = language.Sinhala Slovak = language.Slovak Slovenian = language.Slovenian Albanian = language.Albanian Serbian = language.Serbian SerbianLatin = language.SerbianLatin Swedish = language.Swedish Swahili = language.Swahili Tamil = language.Tamil Telugu = language.Telugu Thai = language.Thai Turkish = language.Turkish Ukrainian = language.Ukrainian Urdu = language.Urdu Uzbek = language.Uzbek Vietnamese = language.Vietnamese Chinese = language.Chinese SimplifiedChinese = language.SimplifiedChinese TraditionalChinese = language.TraditionalChinese Zulu = language.Zulu )
Functions ¶
func Equal ¶ added in v0.2.0
Equal compares language tags by base ISO 639 language code.
Example ¶
package main import ( "fmt" "github.com/muonsoft/language" textlanguage "golang.org/x/text/language" ) func main() { fmt.Println(language.Equal(language.English, language.English)) fmt.Println(language.Equal(language.English, language.Russian)) fmt.Println(language.Equal(textlanguage.MustParse("ru"), textlanguage.MustParse("ru-RU"))) }
Output: true false true
Types ¶
type Middleware ¶
type Middleware struct {
// contains filtered or unexported fields
}
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.
func NewMiddleware ¶
func NewMiddleware(next http.Handler, options ...MiddlewareOption) *Middleware
NewMiddleware creates middleware for parsing language from Accept-Language header or a cookie and passing its value via context.
By default Middleware uses only English language and Accept-Language header as source.
To set up supported languages list use SupportedLanguages option.
To set up sources of language value use ReadFromCookie and ReadFromAcceptHeader options. Order of sources should be preserved.
func (*Middleware) ServeHTTP ¶
func (middleware *Middleware) ServeHTTP(writer http.ResponseWriter, request *http.Request)
Example (ReadFromAcceptLanguageHeader) ¶
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 (ReadFromCookie) ¶
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
type MiddlewareOption ¶
type MiddlewareOption func(middleware *Middleware)
MiddlewareOption is used to set up Middleware.
func ReadFromAcceptHeader ¶
func ReadFromAcceptHeader() MiddlewareOption
ReadFromAcceptHeader can be used to set up middleware to read language value from Accept-Language header.
func ReadFromCookie ¶
func ReadFromCookie(name string) MiddlewareOption
ReadFromCookie can be used to set up middleware to read language value from cookie with given name.
func SupportedLanguages ¶
func SupportedLanguages(tags ...Tag) MiddlewareOption
SupportedLanguages is used to set up list of supported languages. See language.NewMatcher() for details.