Move Markdown parsing to i18n service

This commit is contained in:
Jan Dittberner 2023-07-24 17:59:18 +02:00
parent b0daaf956f
commit fe1914fd89
2 changed files with 40 additions and 20 deletions

View file

@ -32,7 +32,6 @@ import (
"github.com/ory/hydra-client-go/client/admin"
"github.com/ory/hydra-client-go/models"
log "github.com/sirupsen/logrus"
"github.com/yuin/goldmark"
"code.cacert.org/cacert/oidc_idp/ui"
@ -330,23 +329,13 @@ func (h *LoginHandler) renderNoEmailsInClientCertificate(w http.ResponseWriter,
}
func (h *LoginHandler) renderNoChallengeInRequest(w http.ResponseWriter, localizer *i18n.Localizer) {
trans := func(label string) string {
return h.messageCatalog.LookupMessage(label, nil, localizer)
}
buf := &bytes.Buffer{}
err := goldmark.Convert([]byte(trans("NoChallengeInRequestExplanation")), buf)
if err != nil {
h.logger.WithError(err).Error("markdown conversion failed")
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
err = h.templates[NoChallengeInRequest].Lookup("base").Execute(w, map[string]interface{}{
"Title": trans("NoChallengeInRequestTitle"),
"Explanation": template.HTML(buf.String()), //nolint:gosec
err := h.templates[NoChallengeInRequest].Lookup("base").Execute(w, map[string]interface{}{
"Title": h.messageCatalog.LookupMessage("NoChallengeInRequestTitle", nil, localizer),
"Explanation": template.HTML(h.messageCatalog.LookupMarkdownMessage( //nolint:gosec
"NoChallengeInRequestExplanation",
nil,
localizer,
)),
})
if err != nil {
h.logger.WithError(err).Error("template rendering failed")

View file

@ -18,10 +18,12 @@ limitations under the License.
package services
import (
"bytes"
"errors"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/yuin/goldmark"
"code.cacert.org/cacert/oidc_idp/translations"
@ -195,6 +197,35 @@ func (m *MessageCatalog) LookupMessage(
return id
}
func (m *MessageCatalog) LookupMarkdownMessage(
id string,
templateData map[string]interface{},
localizer *i18n.Localizer,
) string {
if message, ok := m.messages[id]; ok {
translation, err := localizer.Localize(&i18n.LocalizeConfig{
DefaultMessage: message,
TemplateData: templateData,
})
if err != nil {
return m.handleLocalizeError(id, translation, err)
}
buf := &bytes.Buffer{}
err = goldmark.Convert([]byte(translation), buf)
if err != nil {
return m.handleLocalizeError(id, translation, fmt.Errorf("markdown conversion error: %w", err))
}
return buf.String()
}
m.logger.Warnf("no translation found for %s", id)
return id
}
func (m *MessageCatalog) LookupMessagePlural(
id string,
templateData map[string]interface{},
@ -223,13 +254,13 @@ func (m *MessageCatalog) handleLocalizeError(id string, translation string, err
var messageNotFound *i18n.MessageNotFoundErr
if errors.As(err, &messageNotFound) {
m.logger.Warnf("message %s not found: %v", id, err)
m.logger.WithError(err).WithField("message", id).Warn("message not found")
if translation != "" {
return translation
}
} else {
m.logger.Error(err)
m.logger.WithError(err).WithField("message", id).Error("translation error")
}
return id