From c0200345aca5ea9269b4181e616650d877aa3b03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toma=C5=BE=20Jerman?= Date: Wed, 23 Mar 2022 16:36:34 +0100 Subject: [PATCH] Add back-end support for boolean label res. translations --- compose/module-field.cue | 4 ++++ compose/service/locale.go | 32 +++++++++++++++++++++++++++++++ compose/types/locale.gen.go | 5 +++++ compose/types/module_field.go | 36 +++++++++++++++++++++++++++++++++++ 4 files changed, 77 insertions(+) diff --git a/compose/module-field.cue b/compose/module-field.cue index 46ef3b388..6abbe9fd4 100644 --- a/compose/module-field.cue +++ b/compose/module-field.cue @@ -46,6 +46,10 @@ moduleField: schema.#resource & { path: ["meta", "options", {part: "value", var: true}, "text"] customHandler: true } + optionsBoolLabels: { + path: ["meta", "bool", {part: "value", var: true}, "label"] + customHandler: true + } } } } diff --git a/compose/service/locale.go b/compose/service/locale.go index 010f546b6..b60e723ec 100644 --- a/compose/service/locale.go +++ b/compose/service/locale.go @@ -71,6 +71,12 @@ func (svc resourceTranslationsManager) moduleExtended(ctx context.Context, res * return nil, err } out = append(out, set...) + + set, err = svc.moduleFieldBoolHandler(ctx, tag, f) + if err != nil { + return nil, err + } + out = append(out, set...) } } @@ -141,6 +147,32 @@ func (svc resourceTranslationsManager) moduleFieldOptionsHandler(ctx context.Con return out, nil } +func (svc resourceTranslationsManager) moduleFieldBoolHandler(ctx context.Context, tag language.Tag, f *types.ModuleField) (locale.ResourceTranslationSet, error) { + if f.Kind != "Bool" { + return nil, nil + } + + out := make(locale.ResourceTranslationSet, 0, 2) + + trKey := strings.NewReplacer("{{value}}", "true").Replace(types.LocaleKeyModuleFieldMetaBoolValueLabel.Path) + out = append(out, &locale.ResourceTranslation{ + Resource: f.ResourceTranslation(), + Lang: tag.String(), + Key: trKey, + Msg: svc.locale.TResourceFor(tag, f.ResourceTranslation(), trKey), + }) + + trKey = strings.NewReplacer("{{value}}", "false").Replace(types.LocaleKeyModuleFieldMetaBoolValueLabel.Path) + out = append(out, &locale.ResourceTranslation{ + Resource: f.ResourceTranslation(), + Lang: tag.String(), + Key: trKey, + Msg: svc.locale.TResourceFor(tag, f.ResourceTranslation(), trKey), + }) + + return out, nil +} + func (svc resourceTranslationsManager) pageExtended(ctx context.Context, res *types.Page) (out locale.ResourceTranslationSet, err error) { var ( k types.LocaleKey diff --git a/compose/types/locale.gen.go b/compose/types/locale.gen.go index b79003d3f..51e9e09c2 100644 --- a/compose/types/locale.gen.go +++ b/compose/types/locale.gen.go @@ -39,6 +39,7 @@ var ( LocaleKeyModuleFieldMetaHintEdit = LocaleKey{Path: "meta.hint.edit"} LocaleKeyModuleFieldExpressionValidatorValidatorIDError = LocaleKey{Path: "expression.validator.{{validatorID}}.error"} LocaleKeyModuleFieldMetaOptionsValueText = LocaleKey{Path: "meta.options.{{value}}.text"} + LocaleKeyModuleFieldMetaBoolValueLabel = LocaleKey{Path: "meta.bool.{{value}}.label"} LocaleKeyNamespaceName = LocaleKey{Path: "name"} LocaleKeyNamespaceMetaSubtitle = LocaleKey{Path: "meta.subtitle"} LocaleKeyNamespaceMetaDescription = LocaleKey{Path: "meta.description"} @@ -148,6 +149,8 @@ func (r *ModuleField) DecodeTranslations(tt locale.ResourceTranslationIndex) { r.decodeTranslationsMetaOptionsValueText(tt) + r.decodeTranslationsMetaBoolValueLabel(tt) + } func (r *ModuleField) EncodeTranslations() (out locale.ResourceTranslationSet) { @@ -171,6 +174,8 @@ func (r *ModuleField) EncodeTranslations() (out locale.ResourceTranslationSet) { out = append(out, r.encodeTranslationsMetaOptionsValueText()...) + out = append(out, r.encodeTranslationsMetaBoolValueLabel()...) + return out } diff --git a/compose/types/module_field.go b/compose/types/module_field.go index 9e929e7a0..7979a39d2 100644 --- a/compose/types/module_field.go +++ b/compose/types/module_field.go @@ -155,6 +155,21 @@ func (f *ModuleField) decodeTranslationsMetaOptionsValueText(tt locale.ResourceT } } +// Decodes translations and modifies options +func (f *ModuleField) decodeTranslationsMetaBoolValueLabel(tt locale.ResourceTranslationIndex) { + if f.Kind != "Bool" { + return + } + + var aux *locale.ResourceTranslation + if aux = tt.FindByKey(strings.NewReplacer("{{value}}", "true").Replace(LocaleKeyModuleFieldMetaBoolValueLabel.Path)); aux != nil { + f.setOptionKey(aux.Msg, "trueLabel") + } + if aux = tt.FindByKey(strings.NewReplacer("{{value}}", "false").Replace(LocaleKeyModuleFieldMetaBoolValueLabel.Path)); aux != nil { + f.setOptionKey(aux.Msg, "falseLabel") + } +} + func (m *ModuleField) encodeTranslationsExpressionValidatorValidatorIDError() (out locale.ResourceTranslationSet) { out = make(locale.ResourceTranslationSet, 0, 3) @@ -261,6 +276,27 @@ func (f *ModuleField) encodeTranslationsMetaOptionsValueText() (out locale.Resou return } +func (m *ModuleField) encodeTranslationsMetaBoolValueLabel() (out locale.ResourceTranslationSet) { + if m.Kind != "Bool" { + return + } + + out = make(locale.ResourceTranslationSet, 0, 3) + out = append(out, &locale.ResourceTranslation{ + Resource: m.ResourceTranslation(), + Key: strings.NewReplacer("{{value}}", "true").Replace(LocaleKeyModuleFieldMetaBoolValueLabel.Path), + Msg: m.Options.String("trueLabel"), + }) + + out = append(out, &locale.ResourceTranslation{ + Resource: m.ResourceTranslation(), + Key: strings.NewReplacer("{{value}}", "false").Replace(LocaleKeyModuleFieldMetaBoolValueLabel.Path), + Msg: m.Options.String("falseLabel"), + }) + + return +} + func (f ModuleField) Clone() *ModuleField { return &f }