-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
43 lines (36 loc) · 1.04 KB
/
parse.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package i18n
import (
"net/http"
"golang.org/x/text/language"
)
// A Parser is a funcion that returns a list of accepted languages according to a request variable (header, URL...), most preferred first.
type Parser func(*http.Request) []language.Tag
// ParseAcceptLanguage parses the Accept-Language header.
func ParseAcceptLanguage(r *http.Request) []language.Tag {
t, _, err := language.ParseAcceptLanguage(r.Header.Get("Accept-Language"))
if err != nil {
return nil
}
return t
}
// ParseCookie parses the LocaleFieldName cookie.
func ParseCookie(r *http.Request) []language.Tag {
c, err := r.Cookie(LocaleFieldName)
if err != nil {
return nil
}
t, err := language.Parse(c.Value)
if err != nil {
return nil
}
return []language.Tag{t}
}
// ParseFormValue parses the LocaleFieldName form value.
func ParseFormValue(r *http.Request) []language.Tag {
t, err := language.Parse(r.FormValue(LocaleFieldName))
if err != nil {
return nil
}
return []language.Tag{t}
}
// TODO: Parsers for location, GeoIP, subdomain, TLD, request path.