...

Source file src/github.com/ory/fosite/i18n_helper.go

Documentation: github.com/ory/fosite

     1  package fosite
     2  
     3  import (
     4  	"github.com/ory/fosite/i18n"
     5  	"github.com/ory/x/errorsx"
     6  	"github.com/pkg/errors"
     7  	"golang.org/x/text/language"
     8  )
     9  
    10  // AddLocalizerToErr augments the error object with the localizer
    11  // based on the language set in the requester object. This is primarily
    12  // required for response writers like introspection that do not take in
    13  // the requester in the Write* function that produces the translated
    14  // message.
    15  // See - WriteIntrospectionError, for example.
    16  func AddLocalizerToErr(catalog i18n.MessageCatalog, err error, requester Requester) error {
    17  	return AddLocalizerToErrWithLang(catalog, getLangFromRequester(requester), err)
    18  }
    19  
    20  // AddLocalizerToErrWithLang augments the error object with the localizer
    21  // based on the language passed in. This is primarily
    22  // required for response writers like introspection that do not take in
    23  // the requester in the Write* function that produces the translated
    24  // message.
    25  // See - WriteIntrospectionError, for example.
    26  func AddLocalizerToErrWithLang(catalog i18n.MessageCatalog, lang language.Tag, err error) error {
    27  	var e RFC6749Error
    28  	if errors.As(err, &e) {
    29  		return e.WithLocalizer(catalog, lang)
    30  	} else if errors.As(errorsx.Cause(err), &e) {
    31  		return e.WithLocalizer(catalog, lang)
    32  	}
    33  	return err
    34  }
    35  
    36  func getLangFromRequester(requester Requester) language.Tag {
    37  	lang := language.English
    38  	g11nContext, ok := requester.(G11NContext)
    39  	if ok {
    40  		lang = g11nContext.GetLang()
    41  	}
    42  
    43  	return lang
    44  }
    45  

View as plain text