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
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user