2020-07-08 07:48:11 +00:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-08-28 07:44:43 +00:00
|
|
|
"errors"
|
2020-07-08 07:48:11 +00:00
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/i18n"
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2020-07-08 07:48:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type localizers interface {
|
|
|
|
Localizers() []Localizer
|
|
|
|
}
|
|
|
|
type Localizer interface {
|
|
|
|
LocalizationKey() string
|
|
|
|
SetLocalizedMessage(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
func translateFields(ctx context.Context, object localizers, translator *i18n.Translator) {
|
|
|
|
if translator == nil || object == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, field := range object.Localizers() {
|
|
|
|
field.SetLocalizedMessage(translator.LocalizeFromCtx(ctx, field.LocalizationKey(), nil))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-28 07:44:43 +00:00
|
|
|
func translateError(ctx context.Context, err error, translator *i18n.Translator) error {
|
|
|
|
if translator == nil || err == nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-12-08 14:30:55 +00:00
|
|
|
caosErr := new(zerrors.ZitadelError)
|
2020-08-28 07:44:43 +00:00
|
|
|
if errors.As(err, &caosErr) {
|
|
|
|
caosErr.SetMessage(translator.LocalizeFromCtx(ctx, caosErr.GetMessage(), nil))
|
|
|
|
}
|
2021-07-05 08:58:58 +00:00
|
|
|
return err
|
2020-08-28 07:44:43 +00:00
|
|
|
}
|