forked from ory/fosite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
i18n_helper.go
45 lines (39 loc) · 1.43 KB
/
i18n_helper.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
package fosite
import (
"github.com/pkg/errors"
"golang.org/x/text/language"
"github.com/ory/fosite/i18n"
"github.com/ory/x/errorsx"
)
// AddLocalizerToErr augments the error object with the localizer
// based on the language set in the requester object. This is primarily
// required for response writers like introspection that do not take in
// the requester in the Write* function that produces the translated
// message.
// See - WriteIntrospectionError, for example.
func AddLocalizerToErr(catalog i18n.MessageCatalog, err error, requester Requester) error {
return AddLocalizerToErrWithLang(catalog, getLangFromRequester(requester), err)
}
// AddLocalizerToErrWithLang augments the error object with the localizer
// based on the language passed in. This is primarily
// required for response writers like introspection that do not take in
// the requester in the Write* function that produces the translated
// message.
// See - WriteIntrospectionError, for example.
func AddLocalizerToErrWithLang(catalog i18n.MessageCatalog, lang language.Tag, err error) error {
var e RFC6749Error
if errors.As(err, &e) {
return e.WithLocalizer(catalog, lang)
} else if errors.As(errorsx.Cause(err), &e) {
return e.WithLocalizer(catalog, lang)
}
return err
}
func getLangFromRequester(requester Requester) language.Tag {
lang := language.English
g11nContext, ok := requester.(G11NContext)
if ok {
lang = g11nContext.GetLang()
}
return lang
}