mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-12 01:47:33 +00:00
chore: move the go code into a subfolder
This commit is contained in:
80
apps/api/internal/i18n/bundle.go
Normal file
80
apps/api/internal/i18n/bundle.go
Normal file
@@ -0,0 +1,80 @@
|
||||
package i18n
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"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"
|
||||
)
|
||||
|
||||
const i18nPath = "/i18n"
|
||||
|
||||
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)
|
||||
|
||||
for lang, file := range translationMessages[ns] {
|
||||
if err := domain.LanguageIsAllowed(false, allowedLanguages, lang); err != nil {
|
||||
continue
|
||||
}
|
||||
bundle.MustAddMessages(lang, file.Messages...)
|
||||
}
|
||||
|
||||
return bundle, nil
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
53
apps/api/internal/i18n/fs.go
Normal file
53
apps/api/internal/i18n/fs.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package i18n
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"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
|
||||
|
||||
type Namespace string
|
||||
|
||||
const (
|
||||
ZITADEL Namespace = "zitadel"
|
||||
LOGIN Namespace = "login"
|
||||
NOTIFICATION Namespace = "notification"
|
||||
)
|
||||
|
||||
func LoadFilesystem(ns Namespace) http.FileSystem {
|
||||
var err error
|
||||
defer func() {
|
||||
if err != nil {
|
||||
logging.WithFields("namespace", ns).OnError(err).Panic("unable to get namespace")
|
||||
}
|
||||
}()
|
||||
switch ns {
|
||||
case ZITADEL:
|
||||
if zitadelFS != nil {
|
||||
return zitadelFS
|
||||
}
|
||||
zitadelFS, err = fs.NewWithNamespace(string(ns))
|
||||
return zitadelFS
|
||||
case LOGIN:
|
||||
if loginFS != nil {
|
||||
return loginFS
|
||||
}
|
||||
loginFS, err = fs.NewWithNamespace(string(ns))
|
||||
return loginFS
|
||||
case NOTIFICATION:
|
||||
if notificationFS != nil {
|
||||
return notificationFS
|
||||
}
|
||||
notificationFS, err = fs.NewWithNamespace(string(ns))
|
||||
return notificationFS
|
||||
}
|
||||
return nil
|
||||
}
|
51
apps/api/internal/i18n/languages.go
Normal file
51
apps/api/internal/i18n/languages.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package i18n
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/text/language"
|
||||
)
|
||||
|
||||
var supportedLanguages []language.Tag
|
||||
|
||||
func SupportedLanguages() []language.Tag {
|
||||
if supportedLanguages == nil {
|
||||
panic("supported languages not loaded")
|
||||
}
|
||||
return supportedLanguages
|
||||
}
|
||||
|
||||
func SupportLanguages(languages ...language.Tag) {
|
||||
supportedLanguages = languages
|
||||
}
|
||||
|
||||
func MustLoadSupportedLanguagesFromDir() {
|
||||
var err error
|
||||
defer func() {
|
||||
if err != nil {
|
||||
panic("failed to load supported languages: " + err.Error())
|
||||
}
|
||||
}()
|
||||
if supportedLanguages != nil {
|
||||
return
|
||||
}
|
||||
i18nDir, err := LoadFilesystem(LOGIN).Open(i18nPath)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer func() {
|
||||
err = errors.Join(err, i18nDir.Close())
|
||||
}()
|
||||
files, err := i18nDir.Readdir(0)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
supportedLanguages = make([]language.Tag, 0, len(files))
|
||||
for _, file := range files {
|
||||
lang := language.Make(strings.TrimSuffix(file.Name(), ".yaml"))
|
||||
if lang != language.Und {
|
||||
supportedLanguages = append(supportedLanguages, lang)
|
||||
}
|
||||
}
|
||||
}
|
177
apps/api/internal/i18n/translator.go
Normal file
177
apps/api/internal/i18n/translator.go
Normal file
@@ -0,0 +1,177 @@
|
||||
package i18n
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"github.com/grpc-ecosystem/go-grpc-middleware/util/metautils"
|
||||
"github.com/nicksnyder/go-i18n/v2/i18n"
|
||||
"github.com/zitadel/logging"
|
||||
"golang.org/x/text/language"
|
||||
|
||||
"github.com/zitadel/zitadel/internal/api/authz"
|
||||
http_util "github.com/zitadel/zitadel/internal/api/http"
|
||||
)
|
||||
|
||||
type Translator struct {
|
||||
bundle *i18n.Bundle
|
||||
cookieName string
|
||||
cookieHandler *http_util.CookieHandler
|
||||
preferredLanguages []string
|
||||
allowedLanguages []language.Tag
|
||||
}
|
||||
|
||||
type TranslatorConfig struct {
|
||||
DefaultLanguage language.Tag
|
||||
CookieName string
|
||||
}
|
||||
|
||||
type Message struct {
|
||||
ID string
|
||||
Text string
|
||||
}
|
||||
|
||||
// NewZitadelTranslator translates to all supported languages, as the ZITADEL texts are not customizable.
|
||||
func NewZitadelTranslator(defaultLanguage language.Tag) (*Translator, error) {
|
||||
return newTranslator(ZITADEL, defaultLanguage, SupportedLanguages(), "")
|
||||
}
|
||||
|
||||
func NewNotificationTranslator(defaultLanguage language.Tag, allowedLanguages []language.Tag) (*Translator, error) {
|
||||
return newTranslator(NOTIFICATION, defaultLanguage, allowedLanguages, "")
|
||||
}
|
||||
|
||||
func NewLoginTranslator(defaultLanguage language.Tag, allowedLanguages []language.Tag, cookieName string) (*Translator, error) {
|
||||
return newTranslator(LOGIN, defaultLanguage, allowedLanguages, cookieName)
|
||||
}
|
||||
|
||||
func newTranslator(ns Namespace, defaultLanguage language.Tag, allowedLanguages []language.Tag, cookieName string) (*Translator, error) {
|
||||
t := new(Translator)
|
||||
var err error
|
||||
t.allowedLanguages = allowedLanguages
|
||||
if len(t.allowedLanguages) == 0 {
|
||||
t.allowedLanguages = SupportedLanguages()
|
||||
}
|
||||
t.bundle, err = newBundle(ns, defaultLanguage, t.allowedLanguages)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
t.cookieHandler = http_util.NewCookieHandler()
|
||||
t.cookieName = cookieName
|
||||
return t, nil
|
||||
}
|
||||
|
||||
func (t *Translator) SupportedLanguages() []language.Tag {
|
||||
return t.allowedLanguages
|
||||
}
|
||||
|
||||
// AddMessages adds messages to the translator for the given language tag.
|
||||
// If the tag is not in the allowed languages, the messages are not added.
|
||||
func (t *Translator) AddMessages(tag language.Tag, messages ...Message) error {
|
||||
if len(messages) == 0 {
|
||||
return nil
|
||||
}
|
||||
var isAllowed bool
|
||||
for _, allowed := range t.allowedLanguages {
|
||||
if allowed == tag {
|
||||
isAllowed = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !isAllowed {
|
||||
return nil
|
||||
}
|
||||
i18nMessages := make([]*i18n.Message, len(messages))
|
||||
for i, message := range messages {
|
||||
i18nMessages[i] = &i18n.Message{
|
||||
ID: message.ID,
|
||||
Other: message.Text,
|
||||
}
|
||||
}
|
||||
return t.bundle.AddMessages(tag, i18nMessages...)
|
||||
}
|
||||
|
||||
func (t *Translator) LocalizeFromRequest(r *http.Request, id string, args map[string]interface{}) string {
|
||||
return localize(t.localizerFromRequest(r), id, args)
|
||||
}
|
||||
|
||||
func (t *Translator) LocalizeFromCtx(ctx context.Context, id string, args map[string]interface{}) string {
|
||||
return localize(t.localizerFromCtx(ctx), id, args)
|
||||
}
|
||||
|
||||
func (t *Translator) Localize(id string, args map[string]interface{}, langs ...string) string {
|
||||
return localize(t.localizer(langs...), id, args)
|
||||
}
|
||||
|
||||
func (t *Translator) LocalizeWithoutArgs(id string, langs ...string) string {
|
||||
return localize(t.localizer(langs...), id, map[string]interface{}{})
|
||||
}
|
||||
|
||||
func (t *Translator) Lang(r *http.Request) language.Tag {
|
||||
matcher := language.NewMatcher(t.allowedLanguages)
|
||||
tag, _ := language.MatchStrings(matcher, t.langsFromRequest(r)...)
|
||||
return tag
|
||||
}
|
||||
|
||||
func (t *Translator) SetLangCookie(w http.ResponseWriter, r *http.Request, lang language.Tag) {
|
||||
t.cookieHandler.SetCookie(w, t.cookieName, r.Host, lang.String())
|
||||
}
|
||||
|
||||
func (t *Translator) localizerFromRequest(r *http.Request) *i18n.Localizer {
|
||||
return t.localizer(t.langsFromRequest(r)...)
|
||||
}
|
||||
|
||||
func (t *Translator) localizerFromCtx(ctx context.Context) *i18n.Localizer {
|
||||
return t.localizer(t.langsFromCtx(ctx)...)
|
||||
}
|
||||
|
||||
func (t *Translator) localizer(langs ...string) *i18n.Localizer {
|
||||
return i18n.NewLocalizer(t.bundle, langs...)
|
||||
}
|
||||
|
||||
func (t *Translator) langsFromRequest(r *http.Request) []string {
|
||||
langs := t.preferredLanguages
|
||||
if r != nil {
|
||||
lang, err := t.cookieHandler.GetCookieValue(r, t.cookieName)
|
||||
if err == nil {
|
||||
langs = append(langs, lang)
|
||||
}
|
||||
langs = append(langs, r.Header.Get("Accept-Language"))
|
||||
}
|
||||
return langs
|
||||
}
|
||||
|
||||
func (t *Translator) langsFromCtx(ctx context.Context) []string {
|
||||
langs := t.preferredLanguages
|
||||
if ctx != nil {
|
||||
ctxData := authz.GetCtxData(ctx)
|
||||
if ctxData.PreferredLanguage != language.Und.String() {
|
||||
langs = append(langs, authz.GetCtxData(ctx).PreferredLanguage)
|
||||
}
|
||||
langs = append(langs, getAcceptLanguageHeader(ctx))
|
||||
}
|
||||
return langs
|
||||
}
|
||||
|
||||
func (t *Translator) SetPreferredLanguages(langs ...string) {
|
||||
t.preferredLanguages = langs
|
||||
}
|
||||
|
||||
func getAcceptLanguageHeader(ctx context.Context) string {
|
||||
acceptLanguage := metautils.ExtractIncoming(ctx).Get("accept-language")
|
||||
if acceptLanguage != "" {
|
||||
return acceptLanguage
|
||||
}
|
||||
return metautils.ExtractIncoming(ctx).Get("grpcgateway-accept-language")
|
||||
}
|
||||
|
||||
func localize(localizer *i18n.Localizer, id string, args map[string]interface{}) string {
|
||||
s, err := localizer.Localize(&i18n.LocalizeConfig{
|
||||
MessageID: id,
|
||||
TemplateData: args,
|
||||
})
|
||||
if err != nil {
|
||||
logging.WithFields("id", id, "args", args).WithError(err).Warnf("missing translation")
|
||||
return id
|
||||
}
|
||||
return s
|
||||
}
|
Reference in New Issue
Block a user