oidc-idp/services/i18n.go

250 lines
6.7 KiB
Go
Raw Normal View History

/*
2023-05-13 11:27:19 +00:00
Copyright 2020-2023 CAcert Inc.
SPDX-License-Identifier: Apache-2.0
2023-05-13 11:27:19 +00:00
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
2023-05-13 11:27:19 +00:00
http://www.apache.org/licenses/LICENSE-2.0
2023-05-13 11:27:19 +00:00
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package services
import (
2023-05-13 11:27:19 +00:00
"errors"
"fmt"
log "github.com/sirupsen/logrus"
2023-05-15 14:47:37 +00:00
"git.cacert.org/oidc_idp/translations"
"github.com/BurntSushi/toml"
"github.com/nicksnyder/go-i18n/v2/i18n"
"golang.org/x/text/language"
)
2023-05-13 11:27:19 +00:00
func AddMessages(catalog *MessageCatalog) error {
messages := make(map[string]*i18n.Message)
messages["unknown"] = &i18n.Message{
ID: "ErrorUnknown",
Other: "Unknown error",
}
messages["TitleRequestConsent"] = &i18n.Message{
ID: "TitleRequestConsent",
Other: "Application requests your consent",
}
messages["LabelSubmit"] = &i18n.Message{
ID: "LabelSubmit",
Other: "Submit",
}
messages["LabelConsent"] = &i18n.Message{
ID: "LabelConsent",
Other: "I hereby agree that the application may get the requested permissions.",
}
messages["IntroConsentRequested"] = &i18n.Message{
2023-05-13 11:27:19 +00:00
ID: "IntroConsentRequested",
Other: "The <strong>{{ .client }}</strong> application requested your consent for the following set of " +
"permissions:",
}
messages["IntroConsentMoreInformation"] = &i18n.Message{
2023-05-13 11:27:19 +00:00
ID: "IntroConsentMoreInformation",
Other: "You can find more information about <strong>{{ .client }}</strong> at " +
"<a href=\"{{ .clientLink }}\">its description page</a>.",
}
messages["ClaimsInformation"] = &i18n.Message{
ID: "ClaimsInformation",
Other: "In addition the application wants access to the following information:",
}
messages["WrongOrLockedUserOrInvalidPassword"] = &i18n.Message{
ID: "WrongOrLockedUserOrInvalidPassword",
Other: "You entered an invalid username or password or your account has been locked.",
}
messages["CertLoginIntroText"] = &i18n.Message{
ID: "CertLoginIntroText",
Other: "The application <strong>{{ .ClientName }}</strong> requests a login.",
}
messages["EmailChoiceText"] = &i18n.Message{
2023-05-13 11:27:19 +00:00
ID: "EmailChoiceText",
One: "You have presented a valid client certificate for the following email address:",
Other: "You have presented a valid client certificate for multiple email addresses. " +
"Please choose which one you want to present to the application:",
}
messages["LoginTitle"] = &i18n.Message{
2023-05-13 11:27:19 +00:00
ID: "LoginTitle",
Other: "Authenticate with a client certificate",
}
messages["CertLoginRequestText"] = &i18n.Message{
ID: "CertLoginRequestText",
Other: "Do you want to use the chosen identity from the certificate for authentication?",
}
messages["LabelAcceptCertLogin"] = &i18n.Message{
ID: "LabelAcceptCertLogin",
Description: "Label for a button to accept certificate login",
Other: "Yes, please use this identity",
}
messages["LabelRejectCertLogin"] = &i18n.Message{
ID: "LabelRejectCertLogin",
Description: "Label for a button to reject certificate login",
Other: "No, please send me back",
}
messages["LoginDeniedByUser"] = &i18n.Message{
ID: "LoginDeniedByUser",
Other: "Login has been denied by the user.",
}
messages["HintChooseAnIdentityForAuthentication"] = &i18n.Message{
ID: "HintChooseAnIdentityForAuthentication",
Other: "Choose an identity for authentication.",
}
2023-05-13 11:27:19 +00:00
catalog.AddMessages(messages)
return nil
}
type MessageCatalog struct {
messages map[string]*i18n.Message
logger *log.Logger
}
func (m *MessageCatalog) AddMessages(messages map[string]*i18n.Message) {
for key, value := range messages {
m.messages[key] = value
}
}
2023-05-13 11:27:19 +00:00
func (m *MessageCatalog) LookupErrorMessage(tag, field string, value interface{}, localizer *i18n.Localizer) string {
var message *i18n.Message
2023-05-13 11:27:19 +00:00
message, ok := m.messages[fmt.Sprintf("%s-%s", field, tag)]
if !ok {
m.logger.Infof("no specific error message %s-%s", field, tag)
2023-05-13 11:27:19 +00:00
message, ok = m.messages[tag]
if !ok {
m.logger.Infof("no specific error message %s", tag)
2023-05-13 11:27:19 +00:00
message, ok = m.messages["unknown"]
if !ok {
m.logger.Warnf("no default translation found")
2023-05-13 11:27:19 +00:00
return tag
}
}
}
translation, err := localizer.Localize(&i18n.LocalizeConfig{
DefaultMessage: message,
TemplateData: map[string]interface{}{
"Value": value,
},
})
if err != nil {
m.logger.Error(err)
2023-05-13 11:27:19 +00:00
return tag
}
2023-05-13 11:27:19 +00:00
return translation
}
2023-05-13 11:27:19 +00:00
func (m *MessageCatalog) LookupMessage(
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 {
2023-05-13 11:27:19 +00:00
return m.handleLocalizeError(id, translation, err)
}
2023-05-13 11:27:19 +00:00
return translation
}
2023-05-13 11:27:19 +00:00
m.logger.Warnf("no translation found for %s", id)
return id
}
2023-05-13 11:27:19 +00:00
func (m *MessageCatalog) LookupMessagePlural(
id string,
templateData map[string]interface{},
localizer *i18n.Localizer,
count int,
) string {
if message, ok := m.messages[id]; ok {
translation, err := localizer.Localize(&i18n.LocalizeConfig{
DefaultMessage: message,
TemplateData: templateData,
PluralCount: count,
})
if err != nil {
2023-05-13 11:27:19 +00:00
return m.handleLocalizeError(id, translation, err)
}
2023-05-13 11:27:19 +00:00
return translation
2023-05-13 11:27:19 +00:00
}
m.logger.Warnf("no translation found for %s", id)
return id
}
func (m *MessageCatalog) handleLocalizeError(id string, translation string, err error) string {
var messageNotFound *i18n.MessageNotFoundErr
if errors.As(err, &messageNotFound) {
m.logger.Warnf("message %s not found: %v", id, err)
if translation != "" {
return translation
}
} else {
2023-05-13 11:27:19 +00:00
m.logger.Error(err)
}
2023-05-13 11:27:19 +00:00
return id
}
2023-05-13 11:27:19 +00:00
func InitI18n(logger *log.Logger, languages []string) (*i18n.Bundle, *MessageCatalog) {
bundle := i18n.NewBundle(language.English)
bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal)
2023-05-13 11:27:19 +00:00
for _, lang := range languages {
2023-05-15 14:47:37 +00:00
bundleName := fmt.Sprintf("active.%s.toml", lang)
bundleBytes, err := translations.Bundles.ReadFile(bundleName)
if err != nil {
2023-05-15 14:47:37 +00:00
logger.Warnf("message bundle %s not found", bundleName)
continue
}
2023-05-15 14:47:37 +00:00
bundle.MustParseMessageFileBytes(bundleBytes, bundleName)
}
2023-05-13 11:27:19 +00:00
catalog := initMessageCatalog(logger)
2023-05-13 11:27:19 +00:00
return bundle, catalog
}
func initMessageCatalog(logger *log.Logger) *MessageCatalog {
messages := make(map[string]*i18n.Message)
messages["ErrorTitle"] = &i18n.Message{
ID: "ErrorTitle",
Other: "An error has occurred",
}
2023-05-13 11:27:19 +00:00
return &MessageCatalog{messages: messages, logger: logger}
}