From 2fc2f1ddb33cff5646e0bd1eca7e04aa772d61fc Mon Sep 17 00:00:00 2001 From: Harvey Tindall Date: Fri, 16 Jun 2023 18:29:49 +0100 Subject: [PATCH] lang: add patchable notifications to common --- lang.go | 6 ++++-- setup.go | 3 ++- storage.go | 38 ++++++++++++++++++++++++++++++++------ 3 files changed, 38 insertions(+), 9 deletions(-) diff --git a/lang.go b/lang.go index 0691417..1cf3fcd 100644 --- a/lang.go +++ b/lang.go @@ -26,8 +26,9 @@ func (ls *adminLangs) getOptions() [][2]string { type commonLangs map[string]commonLang type commonLang struct { - Meta langMeta `json:"meta"` - Strings langSection `json:"strings"` + Meta langMeta `json:"meta"` + Strings langSection `json:"strings"` + Notifications langSection `json:"notifications"` } type adminLang struct { @@ -57,6 +58,7 @@ type userLang struct { notificationsJSON string ValidationStrings map[string]quantityString `json:"validationStrings"` validationStringsJSON string + JSON string } type pwrLangs map[string]pwrLang diff --git a/setup.go b/setup.go index 3ccaf5b..4e67dab 100644 --- a/setup.go +++ b/setup.go @@ -111,7 +111,8 @@ func (st *Storage) loadLangSetup(filesystems ...fs.FS) error { if err != nil { return err } - st.lang.Common.patchCommon(&lang.Strings, index) + st.lang.Common.patchCommonStrings(&lang.Strings, index) + st.lang.Common.patchCommonNotifications(&lang.Notifications, index) if fname != "en-us.json" { if lang.Meta.Fallback != "" { fallback, ok := st.lang.Setup[lang.Meta.Fallback] diff --git a/storage.go b/storage.go index f487847..1626cb5 100644 --- a/storage.go +++ b/storage.go @@ -164,7 +164,7 @@ func (st *Storage) loadLang(filesystems ...fs.FS) (err error) { // from a list of other sources in a preferred order. // languages to patch from should be in decreasing priority, // E.g: If to = fr-be, from = [fr-fr, en-us]. -func (common *commonLangs) patchCommon(to *langSection, from ...string) { +func (common *commonLangs) patchCommonStrings(to *langSection, from ...string) { if *to == nil { *to = langSection{} } @@ -183,6 +183,25 @@ func (common *commonLangs) patchCommon(to *langSection, from ...string) { } } +func (common *commonLangs) patchCommonNotifications(to *langSection, from ...string) { + if *to == nil { + *to = langSection{} + } + for n, ev := range (*common)[from[len(from)-1]].Notifications { + if v, ok := (*to)[n]; !ok || v == "" { + i := 0 + for i < len(from)-1 { + ev, ok = (*common)[from[i]].Notifications[n] + if ok && ev != "" { + break + } + i++ + } + (*to)[n] = ev + } + } +} + func patchLang(to *langSection, from ...*langSection) { if *to == nil { *to = langSection{} @@ -329,7 +348,8 @@ func (st *Storage) loadLangAdmin(filesystems ...fs.FS) error { if err != nil { return err } - st.lang.Common.patchCommon(&lang.Strings, index) + st.lang.Common.patchCommonStrings(&lang.Strings, index) + st.lang.Common.patchCommonNotifications(&lang.Notifications, index) if fname != "en-us.json" { if lang.Meta.Fallback != "" { fallback, ok := st.lang.Admin[lang.Meta.Fallback] @@ -415,7 +435,8 @@ func (st *Storage) loadLangUser(filesystems ...fs.FS) error { if err != nil { return err } - st.lang.Common.patchCommon(&lang.Strings, index) + st.lang.Common.patchCommonStrings(&lang.Strings, index) + st.lang.Common.patchCommonNotifications(&lang.Notifications, index) if fname != "en-us.json" { if lang.Meta.Fallback != "" { fallback, ok := st.lang.User[lang.Meta.Fallback] @@ -445,8 +466,13 @@ func (st *Storage) loadLangUser(filesystems ...fs.FS) error { if err != nil { return err } + userJSON, err := json.Marshal(lang) + if err != nil { + return err + } lang.notificationsJSON = string(notifications) lang.validationStringsJSON = string(validationStrings) + lang.JSON = string(userJSON) st.lang.User[index] = lang return nil } @@ -506,7 +532,7 @@ func (st *Storage) loadLangPWR(filesystems ...fs.FS) error { if err != nil { return err } - st.lang.Common.patchCommon(&lang.Strings, index) + st.lang.Common.patchCommonStrings(&lang.Strings, index) if fname != "en-us.json" { if lang.Meta.Fallback != "" { fallback, ok := st.lang.PasswordReset[lang.Meta.Fallback] @@ -582,7 +608,7 @@ func (st *Storage) loadLangEmail(filesystems ...fs.FS) error { if err != nil { return err } - st.lang.Common.patchCommon(&lang.Strings, index) + st.lang.Common.patchCommonStrings(&lang.Strings, index) if fname != "en-us.json" { if lang.Meta.Fallback != "" { fallback, ok := st.lang.Email[lang.Meta.Fallback] @@ -679,7 +705,7 @@ func (st *Storage) loadLangTelegram(filesystems ...fs.FS) error { if err != nil { return err } - st.lang.Common.patchCommon(&lang.Strings, index) + st.lang.Common.patchCommonStrings(&lang.Strings, index) if fname != "en-us.json" { if lang.Meta.Fallback != "" { fallback, ok := st.lang.Telegram[lang.Meta.Fallback]