diff --git a/.gitignore b/.gitignore index 68b78d9..cfafa89 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +*.pem .idea/ /cacert-idp /static diff --git a/Makefile b/Makefile index c44c870..a35a670 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ GOFILES = $(wildcard */*.go) TEMPLATES = $(wildcard ui/templates/*.gohtml) -TRANSLATIONS = $(wildcard active.*.toml) +TRANSLATIONS = $(wildcard translations/active.*.toml) RESOURCES = ui/css ui/images ui/js all: cacert-idp @@ -18,8 +18,9 @@ go.sum: go.mod go mod tidy translations: $(TRANSLATIONS) $(GOFILES) - goi18n extract . - goi18n merge active.*.toml + cd translations ; \ + goi18n extract .. ; \ + goi18n merge active.*.toml ; \ if translate.*.toml 2>/dev/null; then \ echo "missing translations"; \ goi18n merge active.*.toml translate.*.toml; \ diff --git a/cmd/idp.go b/cmd/idp.go index 5319b08..601949f 100644 --- a/cmd/idp.go +++ b/cmd/idp.go @@ -147,9 +147,10 @@ func main() { csrf.MaxAge(DefaultCSRFMaxAge)) errorMiddleware, err := handlers.ErrorHandling( - context.Background(), logger, ui.Templates, + bundle, + catalog, ) if err != nil { logger.Fatalf("could not initialize request error handling: %v", err) diff --git a/handlers/error.go b/handlers/error.go index 1d003e0..0584d51 100644 --- a/handlers/error.go +++ b/handlers/error.go @@ -130,9 +130,10 @@ func (w *errorResponseWriter) Write(content []byte) (int, error) { } func ErrorHandling( - handlerContext context.Context, logger *log.Logger, templateFS fs.FS, + bundle *i18n.Bundle, + messageCatalog *services.MessageCatalog, ) (func(http.Handler) http.Handler, error) { errorTemplates, err := template.ParseFS( templateFS, @@ -143,16 +144,6 @@ func ErrorHandling( return nil, fmt.Errorf("could not parse templates: %w", err) } - bundle, err := services.GetI18nBundle(handlerContext) - if err != nil { - return nil, fmt.Errorf("could not get i18n bundle: %w", err) - } - - messageCatalog, err := services.GetMessageCatalog(handlerContext) - if err != nil { - return nil, fmt.Errorf("could not get message catalog: %w", err) - } - return func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { errorBucket := &ErrorBucket{ diff --git a/services/i18n.go b/services/i18n.go index 2e88470..d3f40d7 100644 --- a/services/i18n.go +++ b/services/i18n.go @@ -18,12 +18,13 @@ limitations under the License. package services import ( - "context" "errors" "fmt" log "github.com/sirupsen/logrus" + "git.cacert.org/oidc_idp/translations" + "github.com/BurntSushi/toml" "github.com/nicksnyder/go-i18n/v2/i18n" "golang.org/x/text/language" @@ -107,13 +108,6 @@ func AddMessages(catalog *MessageCatalog) error { return nil } -type contextKey int - -const ( - ctxI18nBundle contextKey = iota - ctxI18nCatalog -) - type MessageCatalog struct { messages map[string]*i18n.Message logger *log.Logger @@ -227,10 +221,16 @@ func InitI18n(logger *log.Logger, languages []string) (*i18n.Bundle, *MessageCat bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal) for _, lang := range languages { - _, err := bundle.LoadMessageFile(fmt.Sprintf("active.%s.toml", lang)) + bundleName := fmt.Sprintf("active.%s.toml", lang) + + bundleBytes, err := translations.Bundles.ReadFile(bundleName) if err != nil { - logger.Warnf("message bundle %s.toml not found", lang) + logger.Warnf("message bundle %s not found", bundleName) + + continue } + + bundle.MustParseMessageFileBytes(bundleBytes, bundleName) } catalog := initMessageCatalog(logger) @@ -247,19 +247,3 @@ func initMessageCatalog(logger *log.Logger) *MessageCatalog { return &MessageCatalog{messages: messages, logger: logger} } - -func GetI18nBundle(ctx context.Context) (*i18n.Bundle, error) { - if b, ok := ctx.Value(ctxI18nBundle).(*i18n.Bundle); ok { - return b, nil - } - - return nil, errors.New("context value is not a Bundle") -} - -func GetMessageCatalog(ctx context.Context) (*MessageCatalog, error) { - if c, ok := ctx.Value(ctxI18nCatalog).(*MessageCatalog); ok { - return c, nil - } - - return nil, errors.New("context value is not a MessageCatalog") -} diff --git a/active.de.toml b/translations/active.de.toml similarity index 100% rename from active.de.toml rename to translations/active.de.toml diff --git a/active.en.toml b/translations/active.en.toml similarity index 100% rename from active.en.toml rename to translations/active.en.toml diff --git a/translations/translations.go b/translations/translations.go new file mode 100644 index 0000000..2db9e61 --- /dev/null +++ b/translations/translations.go @@ -0,0 +1,23 @@ +/* +Copyright 2020-2023 CAcert Inc. +SPDX-License-Identifier: Apache-2.0 + +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 + + http://www.apache.org/licenses/LICENSE-2.0 + +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 translations + +import "embed" + +//go:embed active.*.toml +var Bundles embed.FS diff --git a/ui/templates/base.gohtml b/ui/templates/base.gohtml index 9ed1a5d..7687259 100644 --- a/ui/templates/base.gohtml +++ b/ui/templates/base.gohtml @@ -32,7 +32,7 @@