From 04eb3cde92e8f65301e528a19bd78151aa6c4119 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toma=C5=BE=20Jerman?= Date: Mon, 7 Nov 2022 14:41:28 +0100 Subject: [PATCH] Fix improper resource translation management for automation buttons --- compose/service/locale.go | 2 +- compose/types/page.go | 100 +++++++++++++++++++++++--------------- 2 files changed, 61 insertions(+), 41 deletions(-) diff --git a/compose/service/locale.go b/compose/service/locale.go index e4c2caaec..559e82b61 100644 --- a/compose/service/locale.go +++ b/compose/service/locale.go @@ -356,7 +356,7 @@ func (svc resourceTranslationsManager) pageBlockButtons(tag language.Tag, res *t for j, auxBtn := range bb { btn := auxBtn.(map[string]interface{}) - bContentID := uint64(1) + bContentID := uint64(0) if aux, ok := btn["buttonID"]; ok { bContentID = cast.ToUint64(aux) } diff --git a/compose/types/page.go b/compose/types/page.go index 33bc88bf4..a2bcaab64 100644 --- a/compose/types/page.go +++ b/compose/types/page.go @@ -222,31 +222,40 @@ func (p *Page) decodeTranslations(tt locale.ResourceTranslationIndex) { switch block.Kind { case "Automation": bb, _ := block.Options["buttons"].([]interface{}) - for j, auxBtn := range bb { - btn := auxBtn.(map[string]interface{}) + p.decodeRecordListButtons(tt, bb, blockID) - buttonID := uint64(0) - if aux, ok := btn["buttonID"]; ok { - buttonID = cast.ToUint64(aux) - } - buttonID = locale.ContentID(buttonID, j) - - rpl := strings.NewReplacer( - "{{blockID}}", strconv.FormatUint(blockID, 10), - "{{buttonID}}", strconv.FormatUint(buttonID, 10), - ) - - if aux = tt.FindByKey(rpl.Replace(LocaleKeyPagePageBlockBlockIDButtonButtonIDLabel.Path)); aux != nil { - btn["label"] = aux.Msg - } - } + case "RecordList": + bb, _ := block.Options["selectionButtons"].([]interface{}) + p.decodeRecordListButtons(tt, bb, blockID) case "Content": if aux = tt.FindByKey(rpl.Replace(LocaleKeyPagePageBlockBlockIDContentBody.Path)); aux != nil { block.Options["body"] = aux.Msg } } + } +} +func (p *Page) decodeRecordListButtons(tt locale.ResourceTranslationIndex, bb []interface{}, blockID uint64) { + var aux *locale.ResourceTranslation + + for j, auxBtn := range bb { + btn := auxBtn.(map[string]interface{}) + + buttonID := uint64(0) + if aux, ok := btn["buttonID"]; ok { + buttonID = cast.ToUint64(aux) + } + buttonID = locale.ContentID(buttonID, j) + + rpl := strings.NewReplacer( + "{{blockID}}", strconv.FormatUint(blockID, 10), + "{{buttonID}}", strconv.FormatUint(buttonID, 10), + ) + + if aux = tt.FindByKey(rpl.Replace(LocaleKeyPagePageBlockBlockIDButtonButtonIDLabel.Path)); aux != nil { + btn["label"] = aux.Msg + } } } @@ -281,31 +290,12 @@ func (p *Page) encodeTranslations() (out locale.ResourceTranslationSet) { switch block.Kind { case "Automation": bb, _ := block.Options["buttons"].([]interface{}) - for j, auxBtn := range bb { - btn := auxBtn.(map[string]interface{}) + out = append(out, p.encodeRecordListButtons(bb, blockID)...) - if _, ok := btn["label"]; !ok { - continue - } + case "RecordList": + bb, _ := block.Options["selectionButtons"].([]interface{}) + out = append(out, p.encodeRecordListButtons(bb, blockID)...) - buttonID := uint64(0) - if aux, ok := btn["buttonID"]; ok { - buttonID = cast.ToUint64(aux) - } - buttonID = locale.ContentID(buttonID, j) - - rpl := strings.NewReplacer( - "{{blockID}}", strconv.FormatUint(blockID, 10), - "{{buttonID}}", strconv.FormatUint(buttonID, 10), - ) - - out = append(out, &locale.ResourceTranslation{ - Resource: p.ResourceTranslation(), - Key: rpl.Replace(LocaleKeyPagePageBlockBlockIDButtonButtonIDLabel.Path), - Msg: btn["label"].(string), - }) - - } case "Content": body, _ := block.Options["body"].(string) out = append(out, &locale.ResourceTranslation{ @@ -319,6 +309,36 @@ func (p *Page) encodeTranslations() (out locale.ResourceTranslationSet) { return } +func (p *Page) encodeRecordListButtons(bb []interface{}, blockID uint64) (out locale.ResourceTranslationSet) { + for j, auxBtn := range bb { + btn := auxBtn.(map[string]interface{}) + + if _, ok := btn["label"]; !ok { + continue + } + + buttonID := uint64(0) + if aux, ok := btn["buttonID"]; ok { + buttonID = cast.ToUint64(aux) + } + buttonID = locale.ContentID(buttonID, j) + + rpl := strings.NewReplacer( + "{{blockID}}", strconv.FormatUint(blockID, 10), + "{{buttonID}}", strconv.FormatUint(buttonID, 10), + ) + + out = append(out, &locale.ResourceTranslation{ + Resource: p.ResourceTranslation(), + Key: rpl.Replace(LocaleKeyPagePageBlockBlockIDButtonButtonIDLabel.Path), + Msg: btn["label"].(string), + }) + + } + + return +} + // FindByHandle finds page by it's handle func (set PageSet) FindByHandle(handle string) *Page { for i := range set {