2020-07-08 09:48:11 +02:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2022-07-07 14:58:00 +02:00
|
|
|
"github.com/zitadel/logging"
|
2020-07-08 09:48:11 +02:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
2023-12-05 12:12:01 +01:00
|
|
|
"github.com/zitadel/zitadel/internal/i18n"
|
2022-04-27 01:01:45 +02:00
|
|
|
_ "github.com/zitadel/zitadel/internal/statik"
|
2023-01-19 16:50:05 +01:00
|
|
|
"github.com/zitadel/zitadel/internal/telemetry/tracing"
|
2020-07-08 09:48:11 +02:00
|
|
|
)
|
|
|
|
|
2022-04-25 11:16:36 +02:00
|
|
|
func TranslationHandler() func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
|
2020-07-08 09:48:11 +02:00
|
|
|
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
|
|
|
|
resp, err := handler(ctx, req)
|
2023-01-19 16:50:05 +01:00
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
2020-07-09 15:02:03 +02:00
|
|
|
if loc, ok := resp.(localizers); ok && resp != nil {
|
2023-12-05 12:12:01 +01:00
|
|
|
translator, translatorError := getTranslator(ctx)
|
2023-01-19 16:50:05 +01:00
|
|
|
if translatorError != nil {
|
|
|
|
return resp, err
|
|
|
|
}
|
2020-07-08 09:48:11 +02:00
|
|
|
translateFields(ctx, loc, translator)
|
|
|
|
}
|
2020-08-28 09:44:43 +02:00
|
|
|
if err != nil {
|
2023-12-05 12:12:01 +01:00
|
|
|
translator, translatorError := getTranslator(ctx)
|
2023-01-19 16:50:05 +01:00
|
|
|
if translatorError != nil {
|
|
|
|
return resp, err
|
|
|
|
}
|
2020-08-28 09:44:43 +02:00
|
|
|
err = translateError(ctx, err, translator)
|
|
|
|
}
|
2020-07-08 09:48:11 +02:00
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
}
|
2023-12-05 12:12:01 +01:00
|
|
|
|
|
|
|
func getTranslator(ctx context.Context) (*i18n.Translator, error) {
|
|
|
|
translator, err := i18n.NewZitadelTranslator(authz.GetInstance(ctx).DefaultLanguage())
|
|
|
|
if err != nil {
|
|
|
|
logging.New().WithError(err).Error("could not load translator")
|
|
|
|
}
|
|
|
|
return translator, err
|
|
|
|
}
|