fix default language fallback (#7479)
* fix default language fallback * Update services/userlog/pkg/config/config.go Co-authored-by: Martin <github@diemattels.at> * Update services/notifications/pkg/config/config.go Co-authored-by: Martin <github@diemattels.at> * readme updated. local env vars removed * Update changelog/unreleased/fix-default-mail-language-fallback.md Co-authored-by: Martin <github@diemattels.at> * update readme's and envvar texts * fix changelog text --------- Co-authored-by: Roman Perekhod <rperekhod@owncloud.com> Co-authored-by: Martin <github@diemattels.at>
This commit is contained in:
co-authored by
Martin
Roman Perekhod
parent
9e81f02a25
commit
04a5ee283e
@@ -66,3 +66,7 @@ Important: For the time being, the embedded ownCloud Web frontend only supports
|
||||
* If a requested language code is not available, the service tries to fall back to the base language if available. For example, if the requested language-code `de_DE` is not available, the service tries to fall back to translations in the `de` folder.
|
||||
* If the base language `de` is also not available, the service falls back to the system's default English (`en`),
|
||||
which is the source of the texts provided by the code.
|
||||
|
||||
## Default Language
|
||||
|
||||
The default language can be defined via the `OCIS_DEFAULT_LANGUAGE` environment variable. See the `settings` service for a detailed description.
|
||||
|
||||
@@ -116,7 +116,7 @@ func Server(cfg *config.Config) *cli.Command {
|
||||
logger.Fatal().Err(err).Str("addr", cfg.Notifications.RevaGateway).Msg("could not get reva gateway selector")
|
||||
}
|
||||
valueService := settingssvc.NewValueService("com.owncloud.api.settings", grpcClient)
|
||||
svc := service.NewEventsNotifier(evts, channel, logger, gatewaySelector, valueService, cfg.ServiceAccount.ServiceAccountID, cfg.ServiceAccount.ServiceAccountSecret, cfg.Notifications.EmailTemplatePath, cfg.WebUIURL)
|
||||
svc := service.NewEventsNotifier(evts, channel, logger, gatewaySelector, valueService, cfg.ServiceAccount.ServiceAccountID, cfg.ServiceAccount.ServiceAccountSecret, cfg.Notifications.EmailTemplatePath, cfg.Notifications.DefaultLanguage, cfg.WebUIURL)
|
||||
|
||||
gr.Add(svc.Run, func(error) {
|
||||
cancel()
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
// Package config provides the service configuration.
|
||||
package config
|
||||
|
||||
import (
|
||||
@@ -31,6 +32,7 @@ type Notifications struct {
|
||||
Events Events `yaml:"events"`
|
||||
EmailTemplatePath string `yaml:"email_template_path" env:"OCIS_EMAIL_TEMPLATE_PATH;NOTIFICATIONS_EMAIL_TEMPLATE_PATH" desc:"Path to Email notification templates overriding embedded ones."`
|
||||
TranslationPath string `yaml:"translation_path" env:"OCIS_TRANSLATION_PATH,NOTIFICATIONS_TRANSLATION_PATH" desc:"(optional) Set this to a path with custom translations to overwrite the builtin translations. Note that file and folder naming rules apply, see the documentation for more details."`
|
||||
DefaultLanguage string `yaml:"default_language" env:"OCIS_DEFAULT_LANGUAGE" desc:"The default language used by services and the WebUI. If not defined, English will be used as default. See the documentation for more details."`
|
||||
RevaGateway string `yaml:"reva_gateway" env:"OCIS_REVA_GATEWAY" desc:"CS3 gateway used to look up user metadata"`
|
||||
GRPCClientTLS *shared.GRPCClientTLS `yaml:"grpc_client_tls"`
|
||||
}
|
||||
|
||||
@@ -9,9 +9,9 @@ import (
|
||||
)
|
||||
|
||||
// NewTextTemplate replace the body message template placeholders with the translated template
|
||||
func NewTextTemplate(mt MessageTemplate, locale string, translationPath string, vars map[string]string) (MessageTemplate, error) {
|
||||
func NewTextTemplate(mt MessageTemplate, locale, defaultLocale string, translationPath string, vars map[string]string) (MessageTemplate, error) {
|
||||
var err error
|
||||
t := l10n.NewTranslator(locale, translationPath)
|
||||
t := l10n.NewTranslator(locale, defaultLocale, translationPath)
|
||||
mt.Subject, err = composeMessage(t.Translate(mt.Subject), vars)
|
||||
if err != nil {
|
||||
return mt, err
|
||||
@@ -32,9 +32,9 @@ func NewTextTemplate(mt MessageTemplate, locale string, translationPath string,
|
||||
}
|
||||
|
||||
// NewHTMLTemplate replace the body message template placeholders with the translated template
|
||||
func NewHTMLTemplate(mt MessageTemplate, locale string, translationPath string, vars map[string]string) (MessageTemplate, error) {
|
||||
func NewHTMLTemplate(mt MessageTemplate, locale, defaultLocale string, translationPath string, vars map[string]string) (MessageTemplate, error) {
|
||||
var err error
|
||||
t := l10n.NewTranslator(locale, translationPath)
|
||||
t := l10n.NewTranslator(locale, defaultLocale, translationPath)
|
||||
mt.Subject, err = composeMessage(t.Translate(mt.Subject), vars)
|
||||
if err != nil {
|
||||
return mt, err
|
||||
|
||||
@@ -26,8 +26,8 @@ var (
|
||||
)
|
||||
|
||||
// RenderEmailTemplate renders the email template for a new share
|
||||
func RenderEmailTemplate(mt MessageTemplate, locale string, emailTemplatePath string, translationPath string, vars map[string]string) (*channels.Message, error) {
|
||||
textMt, err := NewTextTemplate(mt, locale, translationPath, vars)
|
||||
func RenderEmailTemplate(mt MessageTemplate, locale, defaultLocale string, emailTemplatePath string, translationPath string, vars map[string]string) (*channels.Message, error) {
|
||||
textMt, err := NewTextTemplate(mt, locale, defaultLocale, translationPath, vars)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -39,7 +39,7 @@ func RenderEmailTemplate(mt MessageTemplate, locale string, emailTemplatePath st
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
htmlMt, err := NewHTMLTemplate(mt, locale, translationPath, escapeStringMap(vars))
|
||||
htmlMt, err := NewHTMLTemplate(mt, locale, defaultLocale, translationPath, escapeStringMap(vars))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -22,11 +22,19 @@ type Translator interface {
|
||||
}
|
||||
|
||||
type translator struct {
|
||||
l *gotext.Locale
|
||||
locale *gotext.Locale
|
||||
}
|
||||
|
||||
// NewTranslator Create Translator with library path and language code and load default domain
|
||||
func NewTranslator(local string, path string) Translator {
|
||||
func NewTranslator(locale, defaultLocale string, path string) Translator {
|
||||
l := newLocate(locale, path)
|
||||
if locale != "en" && len(l.GetTranslations()) == 0 {
|
||||
l = newLocate(defaultLocale, path)
|
||||
}
|
||||
return &translator{locale: l}
|
||||
}
|
||||
|
||||
func newLocate(local string, path string) *gotext.Locale {
|
||||
var l *gotext.Locale
|
||||
if path == "" {
|
||||
filesystem, _ := fs.Sub(_translationFS, "locale")
|
||||
@@ -35,9 +43,9 @@ func NewTranslator(local string, path string) Translator {
|
||||
l = gotext.NewLocale(path, local)
|
||||
}
|
||||
l.AddDomain(_domain) // make domain configurable only if needed
|
||||
return &translator{l: l}
|
||||
return l
|
||||
}
|
||||
|
||||
func (t *translator) Translate(str string) string {
|
||||
return t.l.Get(str)
|
||||
return t.locale.Get(str)
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ func NewEventsNotifier(
|
||||
logger log.Logger,
|
||||
gatewaySelector pool.Selectable[gateway.GatewayAPIClient],
|
||||
valueService settingssvc.ValueService,
|
||||
serviceAccountID, serviceAccountSecret, emailTemplatePath, ocisURL string) Service {
|
||||
serviceAccountID, serviceAccountSecret, emailTemplatePath, defaultLanguage, ocisURL string) Service {
|
||||
|
||||
return eventsNotifier{
|
||||
logger: logger,
|
||||
@@ -54,6 +54,7 @@ func NewEventsNotifier(
|
||||
serviceAccountID: serviceAccountID,
|
||||
serviceAccountSecret: serviceAccountSecret,
|
||||
emailTemplatePath: emailTemplatePath,
|
||||
defaultLanguage: defaultLanguage,
|
||||
ocisURL: ocisURL,
|
||||
}
|
||||
}
|
||||
@@ -67,6 +68,7 @@ type eventsNotifier struct {
|
||||
valueService settingssvc.ValueService
|
||||
emailTemplatePath string
|
||||
translationPath string
|
||||
defaultLanguage string
|
||||
ocisURL string
|
||||
serviceAccountID string
|
||||
serviceAccountSecret string
|
||||
@@ -109,7 +111,7 @@ func (s eventsNotifier) render(ctx context.Context, template email.MessageTempla
|
||||
locale := s.getUserLang(ctx, usr.GetId())
|
||||
fields[granteeFieldName] = usr.GetDisplayName()
|
||||
|
||||
rendered, err := email.RenderEmailTemplate(template, locale, s.emailTemplatePath, s.translationPath, fields)
|
||||
rendered, err := email.RenderEmailTemplate(template, locale, s.defaultLanguage, s.emailTemplatePath, s.translationPath, fields)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -77,7 +77,7 @@ var _ = Describe("Notifications", func() {
|
||||
cfg := defaults.FullDefaultConfig()
|
||||
cfg.GRPCClientTLS = &shared.GRPCClientTLS{}
|
||||
ch := make(chan events.Event)
|
||||
evts := service.NewEventsNotifier(ch, tc, log.NewLogger(), gatewaySelector, vs, "", "", "", "")
|
||||
evts := service.NewEventsNotifier(ch, tc, log.NewLogger(), gatewaySelector, vs, "", "", "", "", "")
|
||||
go evts.Run()
|
||||
|
||||
ch <- ev
|
||||
@@ -275,7 +275,7 @@ var _ = Describe("Notifications X-Site Scripting", func() {
|
||||
cfg := defaults.FullDefaultConfig()
|
||||
cfg.GRPCClientTLS = &shared.GRPCClientTLS{}
|
||||
ch := make(chan events.Event)
|
||||
evts := service.NewEventsNotifier(ch, tc, log.NewLogger(), gatewaySelector, vs, "", "", "", "")
|
||||
evts := service.NewEventsNotifier(ch, tc, log.NewLogger(), gatewaySelector, vs, "", "", "", "", "")
|
||||
go evts.Run()
|
||||
|
||||
ch <- ev
|
||||
|
||||
Reference in New Issue
Block a user