Add resource translations for page layouts

This commit is contained in:
Tomaž Jerman
2023-04-07 12:39:26 +02:00
committed by Jože Fortun
parent 0a2a11d9fd
commit 3a329511a4
7 changed files with 163 additions and 55 deletions
+11 -3
View File
@@ -98,11 +98,19 @@ pageLayout: {
}
locale: {
skipSvc: true
// extended: true
extended: true
keys: {
// @todo
name: {
path: ["meta", "name"]
}
description: {
path: ["meta", "description"]
}
actionLabel: {
path: ["config", "actions", {part: "actionID", var: true}, "label"]
customHandler: true
}
}
}
+37
View File
@@ -41,6 +41,7 @@ type (
Module(ctx context.Context, namespaceID uint64, id uint64) (locale.ResourceTranslationSet, error)
Namespace(ctx context.Context, id uint64) (locale.ResourceTranslationSet, error)
Page(ctx context.Context, namespaceID uint64, id uint64) (locale.ResourceTranslationSet, error)
PageLayout(ctx context.Context, namespaceID uint64, pageID uint64, id uint64) (locale.ResourceTranslationSet, error)
Upsert(context.Context, locale.ResourceTranslationSet) error
Locale() locale.Resource
@@ -272,6 +273,42 @@ func (svc resourceTranslationsManager) Page(ctx context.Context, namespaceID uin
return append(out, tmp...), err
}
func (svc resourceTranslationsManager) PageLayout(ctx context.Context, namespaceID uint64, pageID uint64, id uint64) (locale.ResourceTranslationSet, error) {
var (
err error
out locale.ResourceTranslationSet
res *types.PageLayout
)
res, err = svc.loadPageLayout(ctx, svc.store, namespaceID, pageID, id)
if err != nil {
return nil, err
}
var k types.LocaleKey
for _, tag := range svc.locale.Tags() {
k = types.LocaleKeyPageLayoutMetaName
out = append(out, &locale.ResourceTranslation{
Resource: res.ResourceTranslation(),
Lang: tag.String(),
Key: k.Path,
Msg: svc.locale.TResourceFor(tag, res.ResourceTranslation(), k.Path),
})
k = types.LocaleKeyPageLayoutMetaDescription
out = append(out, &locale.ResourceTranslation{
Resource: res.ResourceTranslation(),
Lang: tag.String(),
Key: k.Path,
Msg: svc.locale.TResourceFor(tag, res.ResourceTranslation(), k.Path),
})
}
tmp, err := svc.pageLayoutExtended(ctx, res)
return append(out, tmp...), err
}
func updateTranslations(ctx context.Context, ac localeAccessControl, lsvc ResourceTranslationsManagerService, tt ...*locale.ResourceTranslation) error {
if lsvc == nil || lsvc.Locale() == nil || lsvc.Locale().Default() == nil {
// gracefully handle partial initializations
+29 -45
View File
@@ -180,51 +180,6 @@ func (svc resourceTranslationsManager) pageExtended(ctx context.Context, res *ty
)
for _, tag := range svc.locale.Tags() {
// Button translations
// if res.Config.Buttons != nil {
// out = append(out, &locale.ResourceTranslation{
// Resource: res.ResourceTranslation(),
// Lang: tag.String(),
// Key: types.LocaleKeyPageRecordToolbarNewLabel.Path,
// Msg: svc.locale.TResourceFor(tag, res.ResourceTranslation(), types.LocaleKeyPageRecordToolbarNewLabel.Path),
// })
// out = append(out, &locale.ResourceTranslation{
// Resource: res.ResourceTranslation(),
// Lang: tag.String(),
// Key: types.LocaleKeyPageRecordToolbarEditLabel.Path,
// Msg: svc.locale.TResourceFor(tag, res.ResourceTranslation(), types.LocaleKeyPageRecordToolbarEditLabel.Path),
// })
// out = append(out, &locale.ResourceTranslation{
// Resource: res.ResourceTranslation(),
// Lang: tag.String(),
// Key: types.LocaleKeyPageRecordToolbarSubmitLabel.Path,
// Msg: svc.locale.TResourceFor(tag, res.ResourceTranslation(), types.LocaleKeyPageRecordToolbarSubmitLabel.Path),
// })
// out = append(out, &locale.ResourceTranslation{
// Resource: res.ResourceTranslation(),
// Lang: tag.String(),
// Key: types.LocaleKeyPageRecordToolbarDeleteLabel.Path,
// Msg: svc.locale.TResourceFor(tag, res.ResourceTranslation(), types.LocaleKeyPageRecordToolbarDeleteLabel.Path),
// })
// out = append(out, &locale.ResourceTranslation{
// Resource: res.ResourceTranslation(),
// Lang: tag.String(),
// Key: types.LocaleKeyPageRecordToolbarCloneLabel.Path,
// Msg: svc.locale.TResourceFor(tag, res.ResourceTranslation(), types.LocaleKeyPageRecordToolbarCloneLabel.Path),
// })
// out = append(out, &locale.ResourceTranslation{
// Resource: res.ResourceTranslation(),
// Lang: tag.String(),
// Key: types.LocaleKeyPageRecordToolbarBackLabel.Path,
// Msg: svc.locale.TResourceFor(tag, res.ResourceTranslation(), types.LocaleKeyPageRecordToolbarBackLabel.Path),
// })
// }
for i, block := range res.Blocks {
pbContentID := locale.ContentID(block.BlockID, i)
rpl := strings.NewReplacer(
@@ -279,6 +234,31 @@ func (svc resourceTranslationsManager) pageExtended(ctx context.Context, res *ty
return
}
func (svc resourceTranslationsManager) pageLayoutExtended(ctx context.Context, res *types.PageLayout) (out locale.ResourceTranslationSet, err error) {
var (
k types.LocaleKey
)
for _, tag := range svc.locale.Tags() {
for i, action := range res.Config.Actions {
acContentID := locale.ContentID(action.ActionID, i)
rpl := strings.NewReplacer(
"{{actionID}}", strconv.FormatUint(acContentID, 10),
)
k = types.LocaleKeyPageLayoutConfigActionsActionIDLabel
out = append(out, &locale.ResourceTranslation{
Resource: res.ResourceTranslation(),
Lang: tag.String(),
Key: rpl.Replace(k.Path),
Msg: svc.locale.TResourceFor(tag, res.ResourceTranslation(), rpl.Replace(k.Path)),
})
}
}
return
}
func (svc resourceTranslationsManager) chartExtended(_ context.Context, res *types.Chart) (out locale.ResourceTranslationSet, err error) {
var (
yAxisLabelK = types.LocaleKeyChartYAxisLabel
@@ -393,6 +373,10 @@ func (svc resourceTranslationsManager) loadPage(ctx context.Context, s store.Sto
return loadPage(ctx, s, namespaceID, pageID)
}
func (svc resourceTranslationsManager) loadPageLayout(ctx context.Context, s store.Storer, namespaceID, pageID, pageLayoutID uint64) (res *types.PageLayout, err error) {
return loadPageLayout(ctx, s, namespaceID, pageID, pageLayoutID)
}
func (svc resourceTranslationsManager) loadChart(ctx context.Context, s store.Storer, namespaceID, chartID uint64) (*types.Chart, error) {
return loadChart(ctx, s, namespaceID, chartID)
}
+8 -6
View File
@@ -37,6 +37,8 @@ type (
}
pageLayoutAccessController interface {
CanManageResourceTranslations(ctx context.Context) bool
// @todo...
}
@@ -215,9 +217,9 @@ func (svc pageLayout) Create(ctx context.Context, new *types.PageLayout) (*types
return err
}
// if err = updateTranslations(ctx, svc.ac, svc.locale, new.EncodeTranslations()...); err != nil {
// return
// }
if err = updateTranslations(ctx, svc.ac, svc.locale, new.EncodeTranslations()...); err != nil {
return
}
if err = label.Create(ctx, s, new); err != nil {
return
@@ -346,9 +348,9 @@ func (svc pageLayout) updater(ctx context.Context, s store.Storer, ns *types.Nam
}
}
// if err = updateTranslations(ctx, svc.ac, svc.locale, res.EncodeTranslations()...); err != nil {
// return
// }
if err = updateTranslations(ctx, svc.ac, svc.locale, res.EncodeTranslations()...); err != nil {
return
}
if changes&pageLayoutLabelsChanged > 0 {
if err = label.Update(ctx, s, res); err != nil {
+28
View File
@@ -60,6 +60,9 @@ var (
LocaleKeyPagePageBlockBlockIDDescription = LocaleKey{Path: "pageBlock.{{blockID}}.description"}
LocaleKeyPagePageBlockBlockIDButtonButtonIDLabel = LocaleKey{Path: "pageBlock.{{blockID}}.button.{{buttonID}}.label"}
LocaleKeyPagePageBlockBlockIDContentBody = LocaleKey{Path: "pageBlock.{{blockID}}.content.body"}
LocaleKeyPageLayoutMetaName = LocaleKey{Path: "meta.name"}
LocaleKeyPageLayoutMetaDescription = LocaleKey{Path: "meta.description"}
LocaleKeyPageLayoutConfigActionsActionIDLabel = LocaleKey{Path: "config.actions.{{actionID}}.label"}
)
// ResourceTranslation returns string representation of Locale resource for Chart by calling ChartResourceTranslation fn
@@ -390,10 +393,35 @@ func PageLayoutResourceTranslationTpl() string {
}
func (r *PageLayout) DecodeTranslations(tt locale.ResourceTranslationIndex) {
var aux *locale.ResourceTranslation
if aux = tt.FindByKey(LocaleKeyPageLayoutMetaName.Path); aux != nil {
r.Meta.Name = aux.Msg
}
if aux = tt.FindByKey(LocaleKeyPageLayoutMetaDescription.Path); aux != nil {
r.Meta.Description = aux.Msg
}
r.decodeTranslations(tt)
}
func (r *PageLayout) EncodeTranslations() (out locale.ResourceTranslationSet) {
out = locale.ResourceTranslationSet{}
out = append(out, &locale.ResourceTranslation{
Resource: r.ResourceTranslation(),
Key: LocaleKeyPageLayoutMetaName.Path,
Msg: locale.SanitizeMessage(r.Meta.Name),
})
out = append(out, &locale.ResourceTranslation{
Resource: r.ResourceTranslation(),
Key: LocaleKeyPageLayoutMetaDescription.Path,
Msg: locale.SanitizeMessage(r.Meta.Description),
})
out = append(out, r.encodeTranslations()...)
return out
}
+47
View File
@@ -3,8 +3,11 @@ package types
import (
"database/sql/driver"
"encoding/json"
"strconv"
"strings"
"time"
"github.com/cortezaproject/corteza/server/pkg/locale"
"github.com/cortezaproject/corteza/server/pkg/sql"
"github.com/cortezaproject/corteza/server/pkg/filter"
@@ -67,6 +70,11 @@ type (
Placement string
Meta any
// Warning: value of this field is now handled via resource-translation facility
// struct field is kept for the convenience for now since it allows us
// easy encoding/decoding of the outgoing/incoming values
Label string `json:"label"`
// Kind and Params specify the action's behavior and the parameters it
// can use for execution
Kind string
@@ -103,6 +111,45 @@ func (m PageLayout) Clone() *PageLayout {
return c
}
func (p *PageLayout) decodeTranslations(tt locale.ResourceTranslationIndex) {
var aux *locale.ResourceTranslation
// @note not doing blocks because they are simply copied from the page's index
for i, action := range p.Config.Actions {
actionID := locale.ContentID(action.ActionID, i)
rpl := strings.NewReplacer(
"{{actionID}}", strconv.FormatUint(actionID, 10),
)
if aux = tt.FindByKey(rpl.Replace(LocaleKeyPagePageBlockBlockIDTitle.Path)); aux != nil {
p.Config.Actions[i].Label = aux.Msg
}
}
}
func (p *PageLayout) encodeTranslations() (out locale.ResourceTranslationSet) {
out = make(locale.ResourceTranslationSet, 0, 8)
// @note not doing blocks because they are simply copied from the page's index
// Actions
for i, action := range p.Config.Actions {
actionID := locale.ContentID(action.ActionID, i)
rpl := strings.NewReplacer(
"{{actionID}}", strconv.FormatUint(actionID, 10),
)
out = append(out, &locale.ResourceTranslation{
Resource: p.ResourceTranslation(),
Key: rpl.Replace(LocaleKeyPageLayoutConfigActionsActionIDLabel.Path),
Msg: action.Label,
})
}
return
}
// FindByHandle finds pageLayout by it's handle
func (set PageLayoutSet) FindByHandle(handle string) *PageLayout {
for i := range set {
+3 -1
View File
@@ -74,5 +74,7 @@ func (r *ComposePageLayout) EncodeTranslations() ([]*ResourceTranslation, error)
res, ref, pp := r.ResourceTranslationParts()
out = append(out, NewResourceTranslation(systemTypes.FromLocale(rr), res, ref, pp...))
return out, nil
tmp, err := r.encodeTranslations()
return append(out, tmp...), err
}