-
Notifications
You must be signed in to change notification settings - Fork 5
/
context.go
44 lines (36 loc) · 843 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
39
40
41
42
43
44
package t
import "context"
type (
typeLang struct{}
typeDomain struct{}
)
var (
keyLang = typeLang{}
keyDomain = typeDomain{}
)
func SetCtxLocale(ctx context.Context, lang string) context.Context {
return context.WithValue(ctx, keyLang, lang)
}
func SetCtxDomain(ctx context.Context, domain string) context.Context {
return context.WithValue(ctx, keyDomain, domain)
}
func GetCtxLocale(ctx context.Context) (string, bool) {
v := ctx.Value(keyLang)
lang, ok := v.(string)
return lang, ok
}
func GetCtxDomain(ctx context.Context) (string, bool) {
v := ctx.Value(keyDomain)
domain, ok := v.(string)
return domain, ok
}
func WithContext(ctx context.Context) *Translations {
t := Global()
if lang, ok := GetCtxLocale(ctx); ok {
t = t.L(lang)
}
if domain, ok := GetCtxDomain(ctx); ok {
t = t.D(domain)
}
return t
}