-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
38 lines (30 loc) · 950 Bytes
/
context.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
package i18n
import (
"context"
"net/http"
)
type contextKey int
// Context keys
const (
ContextKeyTranslator contextKey = iota
)
// ContextTranslator returns the translator used for the context.
func ContextTranslator(c context.Context) *Translator {
v := c.Value(ContextKeyTranslator)
if t, ok := v.(*Translator); ok {
return t
}
return nil
}
// RequestTranslator returns the translator used for the HTTP request.
func RequestTranslator(r *http.Request) *Translator {
return ContextTranslator(r.Context())
}
// ContextWithTranslator returns the context with the translator t set.
func ContextWithTranslator(c context.Context, t *Translator) context.Context {
return context.WithValue(c, ContextKeyTranslator, t)
}
// RequestWithTranslator returns the HTTP request with the translator t set.
func RequestWithTranslator(r *http.Request, t *Translator) *http.Request {
return r.WithContext(ContextWithTranslator(r.Context(), t))
}