2021-09-11 11:37:31 +00:00
|
|
|
/*
|
2024-05-12 08:20:06 +00:00
|
|
|
Copyright CAcert Inc.
|
2023-07-29 15:46:33 +00:00
|
|
|
SPDX-License-Identifier: Apache-2.0
|
2021-09-11 11:37:31 +00:00
|
|
|
|
2023-07-29 15:46:33 +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
|
2021-09-11 11:37:31 +00:00
|
|
|
|
2023-07-29 15:46:33 +00:00
|
|
|
https://www.apache.org/licenses/LICENSE-2.0
|
2021-09-11 11:37:31 +00:00
|
|
|
|
2023-07-29 15:46:33 +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.
|
2021-09-11 11:37:31 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
2023-07-29 15:46:33 +00:00
|
|
|
"errors"
|
2021-09-11 11:37:31 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/nicksnyder/go-i18n/v2/i18n"
|
|
|
|
"golang.org/x/text/language"
|
|
|
|
|
2023-07-29 15:46:33 +00:00
|
|
|
"code.cacert.org/cacert/oidc-demo-app/translations"
|
|
|
|
|
2021-09-11 11:37:31 +00:00
|
|
|
"github.com/BurntSushi/toml"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2023-07-29 15:46:33 +00:00
|
|
|
func AddMessages(catalog *MessageCatalog) {
|
2021-09-11 11:37:31 +00:00
|
|
|
messages := make(map[string]*i18n.Message)
|
2023-07-30 14:48:26 +00:00
|
|
|
messages["AuthenticatedAs"] = &i18n.Message{
|
|
|
|
ID: "AuthenticatedAs",
|
|
|
|
Other: "The identity provider authenticated your identity as {{ .Name }}" +
|
|
|
|
" with the email address {{ .Email }}.",
|
|
|
|
}
|
2023-08-03 14:46:28 +00:00
|
|
|
messages["GreetingAnonymous"] = &i18n.Message{
|
|
|
|
ID: "GreetingAnonymous",
|
|
|
|
Other: "Hello",
|
|
|
|
}
|
|
|
|
messages["GreetingAuthenticated"] = &i18n.Message{
|
|
|
|
ID: "GreetingAuthenticated",
|
|
|
|
Other: "Hello {{ .Name }}",
|
2021-09-11 11:37:31 +00:00
|
|
|
}
|
|
|
|
messages["IndexTitle"] = &i18n.Message{
|
|
|
|
ID: "IndexTitle",
|
|
|
|
Other: "Welcome to the Demo application",
|
|
|
|
}
|
2023-08-03 14:46:28 +00:00
|
|
|
messages["LoginLabel"] = &i18n.Message{
|
|
|
|
ID: "LoginLabel",
|
|
|
|
Description: "A label on a login button or link",
|
|
|
|
Other: "Login",
|
|
|
|
}
|
2021-09-11 11:37:31 +00:00
|
|
|
messages["LogoutLabel"] = &i18n.Message{
|
|
|
|
ID: "LogoutLabel",
|
|
|
|
Description: "A label on a logout button or link",
|
|
|
|
Other: "Logout",
|
|
|
|
}
|
|
|
|
messages["IndexIntroductionText"] = &i18n.Message{
|
|
|
|
ID: "IndexIntroductionText",
|
2023-08-03 14:46:28 +00:00
|
|
|
Other: "This is a public resource.",
|
|
|
|
}
|
|
|
|
messages["IndexNavLabel"] = &i18n.Message{
|
|
|
|
ID: "IndexNavLabel",
|
|
|
|
Description: "Label for the index page in the top navigation",
|
|
|
|
Other: "Welcome",
|
|
|
|
}
|
|
|
|
messages["ProtectedIntroductionText"] = &i18n.Message{
|
|
|
|
ID: "ProtectedIntroductionText",
|
|
|
|
Other: "This is an authorization protected resource.",
|
|
|
|
}
|
|
|
|
messages["ProtectedNavLabel"] = &i18n.Message{
|
|
|
|
ID: "ProtectedNavLabel",
|
|
|
|
Description: "Label for the protected resource page in the top navigation",
|
|
|
|
Other: "Protected area",
|
2021-09-11 11:37:31 +00:00
|
|
|
}
|
|
|
|
|
2023-07-29 15:46:33 +00:00
|
|
|
catalog.AddMessages(messages)
|
|
|
|
}
|
2021-09-11 11:37:31 +00:00
|
|
|
|
|
|
|
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-07-29 15:46:33 +00:00
|
|
|
func (m *MessageCatalog) LookupErrorMessage(
|
|
|
|
tag string,
|
|
|
|
field string,
|
|
|
|
value interface{},
|
|
|
|
localizer *i18n.Localizer,
|
|
|
|
) string {
|
2023-07-29 16:34:40 +00:00
|
|
|
fieldTag := fmt.Sprintf("%s-%s", field, tag)
|
2023-07-29 18:36:04 +00:00
|
|
|
|
2023-07-29 16:34:40 +00:00
|
|
|
message, ok := m.messages[fieldTag]
|
2021-09-11 11:37:31 +00:00
|
|
|
if !ok {
|
2023-07-29 16:34:40 +00:00
|
|
|
m.logger.WithField("field_tag", fieldTag).Info("no specific error message for field and tag")
|
2023-07-29 15:46:33 +00:00
|
|
|
|
2021-09-11 11:37:31 +00:00
|
|
|
message, ok = m.messages[tag]
|
|
|
|
if !ok {
|
2023-07-29 16:34:40 +00:00
|
|
|
m.logger.WithField("tag", tag).Info("no specific error message for tag")
|
2023-07-29 15:46:33 +00:00
|
|
|
|
2021-09-11 11:37:31 +00:00
|
|
|
message, ok = m.messages["unknown"]
|
|
|
|
if !ok {
|
2023-07-29 16:34:40 +00:00
|
|
|
m.logger.Warn("no default translation found")
|
2023-07-29 15:46:33 +00:00
|
|
|
|
2021-09-11 11:37:31 +00:00
|
|
|
return tag
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
translation, err := localizer.Localize(&i18n.LocalizeConfig{
|
|
|
|
DefaultMessage: message,
|
|
|
|
TemplateData: map[string]interface{}{
|
|
|
|
"Value": value,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
2023-07-29 16:34:40 +00:00
|
|
|
m.logger.WithError(err).Error("localization failed")
|
2023-07-29 15:46:33 +00:00
|
|
|
|
2021-09-11 11:37:31 +00:00
|
|
|
return tag
|
|
|
|
}
|
2023-07-29 15:46:33 +00:00
|
|
|
|
2021-09-11 11:37:31 +00:00
|
|
|
return translation
|
|
|
|
}
|
|
|
|
|
2023-07-29 15:46:33 +00:00
|
|
|
func (m *MessageCatalog) LookupMessage(
|
|
|
|
id string,
|
|
|
|
templateData map[string]interface{},
|
|
|
|
localizer *i18n.Localizer,
|
|
|
|
) string {
|
2021-09-11 11:37:31 +00:00
|
|
|
if message, ok := m.messages[id]; ok {
|
|
|
|
translation, err := localizer.Localize(&i18n.LocalizeConfig{
|
|
|
|
DefaultMessage: message,
|
|
|
|
TemplateData: templateData,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2023-07-29 15:46:33 +00:00
|
|
|
return m.handleLocalizeError(id, translation, err)
|
2021-09-11 11:37:31 +00:00
|
|
|
}
|
2023-07-29 15:46:33 +00:00
|
|
|
|
2021-09-11 11:37:31 +00:00
|
|
|
return translation
|
2023-07-29 15:46:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m.logger.WithField("id", id).Warn("no translation found for 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.WithError(err).WithField("message", id).Warn("message not found")
|
|
|
|
|
|
|
|
if translation != "" {
|
|
|
|
return translation
|
|
|
|
}
|
2021-09-11 11:37:31 +00:00
|
|
|
} else {
|
2023-07-29 15:46:33 +00:00
|
|
|
m.logger.WithError(err).WithField("message", id).Error("translation error")
|
2021-09-11 11:37:31 +00:00
|
|
|
}
|
2023-07-29 15:46:33 +00:00
|
|
|
|
|
|
|
return id
|
2021-09-11 11:37:31 +00:00
|
|
|
}
|
|
|
|
|
2023-07-29 15:46:33 +00:00
|
|
|
func InitI18n(logger *log.Logger, languages []string) (*i18n.Bundle, *MessageCatalog) {
|
2021-09-11 11:37:31 +00:00
|
|
|
bundle := i18n.NewBundle(language.English)
|
|
|
|
bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal)
|
2023-07-29 15:46:33 +00:00
|
|
|
|
2021-09-11 11:37:31 +00:00
|
|
|
for _, lang := range languages {
|
2023-07-29 15:46:33 +00:00
|
|
|
bundleName := fmt.Sprintf("active.%s.toml", lang)
|
|
|
|
|
|
|
|
bundleBytes, err := translations.Bundles.ReadFile(bundleName)
|
2021-09-11 11:37:31 +00:00
|
|
|
if err != nil {
|
2023-07-29 15:46:33 +00:00
|
|
|
logger.WithField("bundle", bundleName).Warn("message bundle not found")
|
|
|
|
|
|
|
|
continue
|
2021-09-11 11:37:31 +00:00
|
|
|
}
|
2023-07-29 15:46:33 +00:00
|
|
|
|
|
|
|
bundle.MustParseMessageFileBytes(bundleBytes, bundleName)
|
2021-09-11 11:37:31 +00:00
|
|
|
}
|
2023-07-29 15:46:33 +00:00
|
|
|
|
2021-09-11 11:37:31 +00:00
|
|
|
catalog := initMessageCatalog(logger)
|
2023-07-29 15:46:33 +00:00
|
|
|
|
|
|
|
return bundle, catalog
|
2021-09-11 11:37:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func initMessageCatalog(logger *log.Logger) *MessageCatalog {
|
|
|
|
messages := make(map[string]*i18n.Message)
|
|
|
|
messages["ErrorTitle"] = &i18n.Message{
|
|
|
|
ID: "ErrorTitle",
|
|
|
|
Other: "An error has occurred",
|
|
|
|
}
|
|
|
|
|
2023-07-29 15:46:33 +00:00
|
|
|
return &MessageCatalog{messages: messages, logger: logger}
|
2021-09-11 11:37:31 +00:00
|
|
|
}
|