fix(middleware): init translation messages (#7778)

* fix(middleware): init translation messages

* revert change

* refactor: split loop in separate function

* add imports to ensure init of fs
This commit is contained in:
Silvan 2024-04-16 14:08:18 +02:00 committed by GitHub
parent 386addc718
commit 9bcfa12be2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 58 additions and 33 deletions

View File

@ -3,58 +3,78 @@ package i18n
import (
"encoding/json"
"io"
"io/fs"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/BurntSushi/toml"
"github.com/nicksnyder/go-i18n/v2/i18n"
"github.com/zitadel/logging"
"golang.org/x/text/language"
"sigs.k8s.io/yaml"
"github.com/zitadel/zitadel/internal/domain"
"github.com/zitadel/zitadel/internal/zerrors"
)
const i18nPath = "/i18n"
func newBundle(dir http.FileSystem, defaultLanguage language.Tag, allowedLanguages []language.Tag) (*i18n.Bundle, error) {
var translationMessages = map[Namespace]map[language.Tag]*i18n.MessageFile{
ZITADEL: make(map[language.Tag]*i18n.MessageFile),
LOGIN: make(map[language.Tag]*i18n.MessageFile),
NOTIFICATION: make(map[language.Tag]*i18n.MessageFile),
}
func init() {
for ns := range translationMessages {
loadTranslationsFromNamespace(ns)
}
}
func newBundle(ns Namespace, defaultLanguage language.Tag, allowedLanguages []language.Tag) (*i18n.Bundle, error) {
bundle := i18n.NewBundle(defaultLanguage)
bundle.RegisterUnmarshalFunc("yaml", func(data []byte, v interface{}) error { return yaml.Unmarshal(data, v) })
bundle.RegisterUnmarshalFunc("json", json.Unmarshal)
bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal)
i18nDir, err := dir.Open(i18nPath)
if err != nil {
return nil, zerrors.ThrowNotFound(err, "I18N-MnXRie", "path not found")
}
defer i18nDir.Close()
files, err := i18nDir.Readdir(0)
if err != nil {
return nil, zerrors.ThrowNotFound(err, "I18N-Gew23", "cannot read dir")
}
for _, file := range files {
fileLang, _ := strings.CutSuffix(file.Name(), filepath.Ext(file.Name()))
if err = domain.LanguageIsAllowed(false, allowedLanguages, language.Make(fileLang)); err != nil {
for lang, file := range translationMessages[ns] {
if err := domain.LanguageIsAllowed(false, allowedLanguages, lang); err != nil {
continue
}
if err := addFileFromFileSystemToBundle(dir, bundle, file); err != nil {
return nil, zerrors.ThrowNotFoundf(err, "I18N-ZS2AW", "cannot append file %s to Bundle", file.Name())
}
bundle.MustAddMessages(lang, file.Messages...)
}
return bundle, nil
}
func addFileFromFileSystemToBundle(dir http.FileSystem, bundle *i18n.Bundle, file os.FileInfo) error {
f, err := dir.Open("/i18n/" + file.Name())
if err != nil {
return err
func loadTranslationsFromNamespace(ns Namespace) {
dir := LoadFilesystem(ns)
i18nDir, err := dir.Open(i18nPath)
logging.WithFields("namespace", ns).OnError(err).Panic("unable to open translation files")
defer i18nDir.Close()
files, err := i18nDir.Readdir(0)
logging.WithFields("namespace", ns).OnError(err).Panic("unable to read translation files")
for _, file := range files {
loadTranslationsFromFile(ns, file, dir)
}
defer f.Close()
content, err := io.ReadAll(f)
if err != nil {
return err
}
_, err = bundle.ParseMessageFileBytes(content, file.Name())
return err
}
func loadTranslationsFromFile(ns Namespace, fileInfo fs.FileInfo, dir http.FileSystem) {
file, err := dir.Open("/i18n/" + fileInfo.Name())
logging.WithFields("namespace", ns, "file", fileInfo.Name()).OnError(err).Panic("unable to open translation file")
defer file.Close()
content, err := io.ReadAll(file)
logging.WithFields("namespace", ns, "file", fileInfo.Name()).OnError(err).Panic("unable to read translation file")
unmarshaler := map[string]i18n.UnmarshalFunc{
"yaml": func(data []byte, v interface{}) error { return yaml.Unmarshal(data, v) },
"json": json.Unmarshal,
"toml": toml.Unmarshal,
}
messageFile, err := i18n.ParseMessageFileBytes(content, fileInfo.Name(), unmarshaler)
logging.WithFields("namespace", ns, "file", fileInfo.Name()).OnError(err).Panic("unable to parse translation file")
fileLang, _ := strings.CutSuffix(fileInfo.Name(), filepath.Ext(fileInfo.Name()))
lang := language.Make(fileLang)
translationMessages[ns][lang] = messageFile
}

View File

@ -5,6 +5,11 @@ import (
"github.com/rakyll/statik/fs"
"github.com/zitadel/logging"
// ensure fs is setup
_ "github.com/zitadel/zitadel/internal/api/ui/login/statik"
_ "github.com/zitadel/zitadel/internal/notification/statik"
_ "github.com/zitadel/zitadel/internal/statik"
)
var zitadelFS, loginFS, notificationFS http.FileSystem

View File

@ -51,7 +51,7 @@ func newTranslator(ns Namespace, defaultLanguage language.Tag, allowedLanguages
if len(t.allowedLanguages) == 0 {
t.allowedLanguages = SupportedLanguages()
}
t.bundle, err = newBundle(LoadFilesystem(ns), defaultLanguage, t.allowedLanguages)
t.bundle, err = newBundle(ns, defaultLanguage, t.allowedLanguages)
if err != nil {
return nil, err
}