-
Notifications
You must be signed in to change notification settings - Fork 0
/
i18n.go
49 lines (40 loc) · 1.24 KB
/
i18n.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
44
45
46
47
48
49
package main
import (
"context"
"net/http"
"golang.org/x/text/language"
"golang.org/x/text/message"
)
//go:generate gotext -srclang=en update -out=catalog.go -lang=en,ar,bn,zh,fr,ht,ko,pl,ru,es,ur
// func init() {
// l := display.Languages(language.English)
// for _, t := range tags {
// b, _ := t.Base()
// fmt.Printf("lang %s base:%s %s name:%s\n", t.String(), b.String(), b.ISO3(), l.Name(b))
// }
// }
type i18nMiddleware struct {
Matcher language.Matcher
Handler http.Handler
}
func newI18nMiddleware(h http.Handler) *i18nMiddleware {
return &i18nMiddleware{
Matcher: language.NewMatcher(message.DefaultCatalog.Languages()),
Handler: h,
}
}
const contextKey = "lang"
func Printer(ctx context.Context) *message.Printer {
return ctx.Value(contextKey).(*message.Printer)
}
func (t *i18nMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) {
query := r.Form.Get("hl")
cookie, _ := r.Cookie("lang")
accept := r.Header.Get("Accept-Language")
fallback := "en"
tag, _ := language.MatchStrings(t.Matcher, query, cookie.String(), accept, fallback)
// log.Printf("%#v %s", tag, tag.String())
printer := message.NewPrinter(tag)
ctx := context.WithValue(r.Context(), contextKey, printer)
t.Handler.ServeHTTP(w, r.WithContext(ctx))
}