1
0
mirror of https://github.com/hrfee/jfa-go.git synced 2024-12-22 17:10:10 +00:00

lang: add patchable notifications to common

This commit is contained in:
Harvey Tindall 2023-06-16 18:29:49 +01:00
parent a1af6e3892
commit 2fc2f1ddb3
Signed by: hrfee
GPG Key ID: BBC65952848FB1A2
3 changed files with 38 additions and 9 deletions

View File

@ -28,6 +28,7 @@ type commonLangs map[string]commonLang
type commonLang struct { type commonLang struct {
Meta langMeta `json:"meta"` Meta langMeta `json:"meta"`
Strings langSection `json:"strings"` Strings langSection `json:"strings"`
Notifications langSection `json:"notifications"`
} }
type adminLang struct { type adminLang struct {
@ -57,6 +58,7 @@ type userLang struct {
notificationsJSON string notificationsJSON string
ValidationStrings map[string]quantityString `json:"validationStrings"` ValidationStrings map[string]quantityString `json:"validationStrings"`
validationStringsJSON string validationStringsJSON string
JSON string
} }
type pwrLangs map[string]pwrLang type pwrLangs map[string]pwrLang

View File

@ -111,7 +111,8 @@ func (st *Storage) loadLangSetup(filesystems ...fs.FS) error {
if err != nil { if err != nil {
return err 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 fname != "en-us.json" {
if lang.Meta.Fallback != "" { if lang.Meta.Fallback != "" {
fallback, ok := st.lang.Setup[lang.Meta.Fallback] fallback, ok := st.lang.Setup[lang.Meta.Fallback]

View File

@ -164,7 +164,7 @@ func (st *Storage) loadLang(filesystems ...fs.FS) (err error) {
// from a list of other sources in a preferred order. // from a list of other sources in a preferred order.
// languages to patch from should be in decreasing priority, // languages to patch from should be in decreasing priority,
// E.g: If to = fr-be, from = [fr-fr, en-us]. // 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 { if *to == nil {
*to = langSection{} *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) { func patchLang(to *langSection, from ...*langSection) {
if *to == nil { if *to == nil {
*to = langSection{} *to = langSection{}
@ -329,7 +348,8 @@ func (st *Storage) loadLangAdmin(filesystems ...fs.FS) error {
if err != nil { if err != nil {
return err 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 fname != "en-us.json" {
if lang.Meta.Fallback != "" { if lang.Meta.Fallback != "" {
fallback, ok := st.lang.Admin[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 { if err != nil {
return err 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 fname != "en-us.json" {
if lang.Meta.Fallback != "" { if lang.Meta.Fallback != "" {
fallback, ok := st.lang.User[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 { if err != nil {
return err return err
} }
userJSON, err := json.Marshal(lang)
if err != nil {
return err
}
lang.notificationsJSON = string(notifications) lang.notificationsJSON = string(notifications)
lang.validationStringsJSON = string(validationStrings) lang.validationStringsJSON = string(validationStrings)
lang.JSON = string(userJSON)
st.lang.User[index] = lang st.lang.User[index] = lang
return nil return nil
} }
@ -506,7 +532,7 @@ func (st *Storage) loadLangPWR(filesystems ...fs.FS) error {
if err != nil { if err != nil {
return err return err
} }
st.lang.Common.patchCommon(&lang.Strings, index) st.lang.Common.patchCommonStrings(&lang.Strings, index)
if fname != "en-us.json" { if fname != "en-us.json" {
if lang.Meta.Fallback != "" { if lang.Meta.Fallback != "" {
fallback, ok := st.lang.PasswordReset[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 { if err != nil {
return err return err
} }
st.lang.Common.patchCommon(&lang.Strings, index) st.lang.Common.patchCommonStrings(&lang.Strings, index)
if fname != "en-us.json" { if fname != "en-us.json" {
if lang.Meta.Fallback != "" { if lang.Meta.Fallback != "" {
fallback, ok := st.lang.Email[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 { if err != nil {
return err return err
} }
st.lang.Common.patchCommon(&lang.Strings, index) st.lang.Common.patchCommonStrings(&lang.Strings, index)
if fname != "en-us.json" { if fname != "en-us.json" {
if lang.Meta.Fallback != "" { if lang.Meta.Fallback != "" {
fallback, ok := st.lang.Telegram[lang.Meta.Fallback] fallback, ok := st.lang.Telegram[lang.Meta.Fallback]