-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.go
151 lines (125 loc) · 3.53 KB
/
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package l10n
import (
"github.com/pkg/errors"
"github.com/xelaj/errs"
"github.com/iafan/Plurr/go/plurr"
)
// Context holds a localization context
// (a combination of a language, a corresponding Plurr formatter,
// and a link back to parent localization Pool object)
type Context struct {
pool *Pool
lang string
plurr *plurr.Plurr
}
// GetLanguage returns the current language of the context.
func (lc *Context) Lang() string {
return lc.lang
}
// Tr returns a translated version of the string
func (lc *Context) Tr(key string) string {
res, err := lc.tr(key)
if err != nil {
return key
}
return res
}
func (lc *Context) TrWithError(key string) (string, error) {
return lc.tr(key)
}
// Trf returns a formatted version of the string
func (lc *Context) Trf(key string, params plurr.Params) (string, error) {
translated, err := lc.tr(key)
if err != nil {
return "", errors.Wrap(err, "translating")
}
s, err := lc.plurr.Format(translated, params)
if err != nil {
return "", errors.New(key + ": " + err.Error())
}
return s, nil
}
func (lc *Context) Strf(key string, params plurr.Params) string {
r, err := lc.Trf(key, params)
if err != nil {
return lc.Tr(key)
}
return r
}
// tr исключительно достает из пула только сами переводы, дополнением занимаются публичные функции
// tr будет стараться искать альтернативные ключи
func (lc *Context) tr(key string) (string, error) {
err := lc.pool.LoadResource(lc.lang)
if err != nil {
return key, errors.Wrap(err, "loading resource")
}
resource := lc.pool.Resources[lc.lang]
s, ok := resource[key]
if ok {
return s, nil
}
alternativeLocale := ""
for _, locale := range lc.pool.locales {
if locale.Code == lc.lang {
alternativeLocale = locale.Extends
}
}
if alternativeLocale == "" {
return key, errs.NotFound("key", key)
}
ctx, err := lc.pool.GetContext(alternativeLocale)
if err != nil {
return key, errors.Wrap(err, "getting context")
}
return ctx.tr(key)
}
func (lc *Context) Message(key string) MessageCode {
t := Translator(lc)
return ImplicitCtxMessage(&t, key)
}
func (lc *Context) MessageWithParams(key string) MessageWithParamsCode {
t := Translator(lc)
return ImplicitCtxMessageWithParams(&t, key)
}
func (lc *Context) MustAll(items []string) error {
errorsMultiple := &errs.MultipleErrors{}
for _, item := range items {
_, err := lc.TrWithError(item)
errorsMultiple.Add(err)
}
return errorsMultiple.Normalize()
}
type MsgCodeReturner interface {
Code() string
}
type codeGetter uint8
type MessageCode func(privateParamss ...interface{}) string
func ImplicitCtxMessage(lc *Translator, key string) MessageCode {
return func(privateParams ...interface{}) string {
if len(privateParams) > 0 {
if _, ok := privateParams[0].(codeGetter); !ok {
panic("do not use additional parameters")
}
return key
}
return (*lc).Tr(key)
}
}
func (m MessageCode) Code() string {
return m(codeGetter(0))
}
type MessageWithParamsCode func(params map[string]interface{}, privateParams ...interface{}) string
func ImplicitCtxMessageWithParams(lc *Translator, key string) MessageWithParamsCode {
return func(params map[string]interface{}, privateParams ...interface{}) string {
if len(privateParams) > 0 {
if _, ok := privateParams[0].(codeGetter); !ok {
panic("do not use additional parameters")
}
return key
}
return (*lc).Strf(key, params)
}
}
func (m MessageWithParamsCode) Code() string {
return m(nil, codeGetter(0))
}