From 3e746c3b57ff2b8e16dcde888dc413d1ff8b56a6 Mon Sep 17 00:00:00 2001 From: Denis Arh Date: Wed, 15 Sep 2021 20:49:59 +0200 Subject: [PATCH] Improve resource-translation locale codegen Do NOT use key (string) as a fallback in case of a missing translation. Return empty string --- automation/types/locale.gen.go | 30 ++---- compose/types/locale.gen.go | 94 ++++++++----------- .../templates/gocode/locale/types.go.tpl | 26 ++--- pkg/locale/locale.go | 2 +- 4 files changed, 60 insertions(+), 92 deletions(-) diff --git a/automation/types/locale.gen.go b/automation/types/locale.gen.go index f15e49196..5ed7253c3 100644 --- a/automation/types/locale.gen.go +++ b/automation/types/locale.gen.go @@ -72,38 +72,28 @@ func (r *Workflow) DecodeTranslations(tt locale.ResourceTranslationIndex) { var aux *locale.ResourceTranslation if aux = tt.FindByKey(LocaleKeyWorkflowName.Path); aux != nil { r.Meta.Name = aux.Msg - } else { - r.Meta.Name = LocaleKeyWorkflowName.Path } if aux = tt.FindByKey(LocaleKeyWorkflowDescription.Path); aux != nil { r.Meta.Description = aux.Msg - } else { - r.Meta.Description = LocaleKeyWorkflowDescription.Path } } func (r *Workflow) EncodeTranslations() (out locale.ResourceTranslationSet) { - out = locale.ResourceTranslationSet{ - { + out = locale.ResourceTranslationSet{} + if r.Meta.Name != "" { + out = append(out, &locale.ResourceTranslation{ Resource: r.ResourceTranslation(), Key: LocaleKeyWorkflowName.Path, - Msg: firstOkString(r.Meta.Name, LocaleKeyWorkflowName.Path), - }, - { + Msg: r.Meta.Name, + }) + } + if r.Meta.Description != "" { + out = append(out, &locale.ResourceTranslation{ Resource: r.ResourceTranslation(), Key: LocaleKeyWorkflowDescription.Path, - Msg: firstOkString(r.Meta.Description, LocaleKeyWorkflowDescription.Path), - }, + Msg: r.Meta.Description, + }) } return out } - -func firstOkString(ss ...string) string { - for _, s := range ss { - if s != "" { - return s - } - } - return "" -} diff --git a/compose/types/locale.gen.go b/compose/types/locale.gen.go index c662c6f45..94d835813 100644 --- a/compose/types/locale.gen.go +++ b/compose/types/locale.gen.go @@ -131,18 +131,17 @@ func (r *Chart) DecodeTranslations(tt locale.ResourceTranslationIndex) { var aux *locale.ResourceTranslation if aux = tt.FindByKey(LocaleKeyChartName.Path); aux != nil { r.Name = aux.Msg - } else { - r.Name = LocaleKeyChartName.Path } } func (r *Chart) EncodeTranslations() (out locale.ResourceTranslationSet) { - out = locale.ResourceTranslationSet{ - { + out = locale.ResourceTranslationSet{} + if r.Name != "" { + out = append(out, &locale.ResourceTranslation{ Resource: r.ResourceTranslation(), Key: LocaleKeyChartName.Path, - Msg: firstOkString(r.Name, LocaleKeyChartName.Path), - }, + Msg: r.Name, + }) } return out @@ -178,19 +177,18 @@ func (r *ModuleField) DecodeTranslations(tt locale.ResourceTranslationIndex) { var aux *locale.ResourceTranslation if aux = tt.FindByKey(LocaleKeyModuleFieldLabel.Path); aux != nil { r.Label = aux.Msg - } else { - r.Label = LocaleKeyModuleFieldLabel.Path } r.decodeTranslationsValidatorError(tt) } func (r *ModuleField) EncodeTranslations() (out locale.ResourceTranslationSet) { - out = locale.ResourceTranslationSet{ - { + out = locale.ResourceTranslationSet{} + if r.Label != "" { + out = append(out, &locale.ResourceTranslation{ Resource: r.ResourceTranslation(), Key: LocaleKeyModuleFieldLabel.Path, - Msg: firstOkString(r.Label, LocaleKeyModuleFieldLabel.Path), - }, + Msg: r.Label, + }) } out = append(out, r.encodeTranslationsValidatorError()...) @@ -228,20 +226,19 @@ func (r *Module) DecodeTranslations(tt locale.ResourceTranslationIndex) { var aux *locale.ResourceTranslation if aux = tt.FindByKey(LocaleKeyModuleName.Path); aux != nil { r.Name = aux.Msg - } else { - r.Name = LocaleKeyModuleName.Path } r.decodeTranslations(tt) } func (r *Module) EncodeTranslations() (out locale.ResourceTranslationSet) { - out = locale.ResourceTranslationSet{ - { + out = locale.ResourceTranslationSet{} + if r.Name != "" { + out = append(out, &locale.ResourceTranslation{ Resource: r.ResourceTranslation(), Key: LocaleKeyModuleName.Path, - Msg: firstOkString(r.Name, LocaleKeyModuleName.Path), - }, + Msg: r.Name, + }) } out = append(out, r.encodeTranslations()...) @@ -279,38 +276,37 @@ func (r *Namespace) DecodeTranslations(tt locale.ResourceTranslationIndex) { var aux *locale.ResourceTranslation if aux = tt.FindByKey(LocaleKeyNamespaceName.Path); aux != nil { r.Name = aux.Msg - } else { - r.Name = LocaleKeyNamespaceName.Path } if aux = tt.FindByKey(LocaleKeyNamespaceSubtitle.Path); aux != nil { r.Meta.Subtitle = aux.Msg - } else { - r.Meta.Subtitle = LocaleKeyNamespaceSubtitle.Path } if aux = tt.FindByKey(LocaleKeyNamespaceDescription.Path); aux != nil { r.Meta.Description = aux.Msg - } else { - r.Meta.Description = LocaleKeyNamespaceDescription.Path } } func (r *Namespace) EncodeTranslations() (out locale.ResourceTranslationSet) { - out = locale.ResourceTranslationSet{ - { + out = locale.ResourceTranslationSet{} + if r.Name != "" { + out = append(out, &locale.ResourceTranslation{ Resource: r.ResourceTranslation(), Key: LocaleKeyNamespaceName.Path, - Msg: firstOkString(r.Name, LocaleKeyNamespaceName.Path), - }, - { + Msg: r.Name, + }) + } + if r.Meta.Subtitle != "" { + out = append(out, &locale.ResourceTranslation{ Resource: r.ResourceTranslation(), Key: LocaleKeyNamespaceSubtitle.Path, - Msg: firstOkString(r.Meta.Subtitle, LocaleKeyNamespaceSubtitle.Path), - }, - { + Msg: r.Meta.Subtitle, + }) + } + if r.Meta.Description != "" { + out = append(out, &locale.ResourceTranslation{ Resource: r.ResourceTranslation(), Key: LocaleKeyNamespaceDescription.Path, - Msg: firstOkString(r.Meta.Description, LocaleKeyNamespaceDescription.Path), - }, + Msg: r.Meta.Description, + }) } return out @@ -346,42 +342,32 @@ func (r *Page) DecodeTranslations(tt locale.ResourceTranslationIndex) { var aux *locale.ResourceTranslation if aux = tt.FindByKey(LocaleKeyPageTitle.Path); aux != nil { r.Title = aux.Msg - } else { - r.Title = LocaleKeyPageTitle.Path } if aux = tt.FindByKey(LocaleKeyPageDescription.Path); aux != nil { r.Description = aux.Msg - } else { - r.Description = LocaleKeyPageDescription.Path } r.decodeTranslations(tt) } func (r *Page) EncodeTranslations() (out locale.ResourceTranslationSet) { - out = locale.ResourceTranslationSet{ - { + out = locale.ResourceTranslationSet{} + if r.Title != "" { + out = append(out, &locale.ResourceTranslation{ Resource: r.ResourceTranslation(), Key: LocaleKeyPageTitle.Path, - Msg: firstOkString(r.Title, LocaleKeyPageTitle.Path), - }, - { + Msg: r.Title, + }) + } + if r.Description != "" { + out = append(out, &locale.ResourceTranslation{ Resource: r.ResourceTranslation(), Key: LocaleKeyPageDescription.Path, - Msg: firstOkString(r.Description, LocaleKeyPageDescription.Path), - }, + Msg: r.Description, + }) } out = append(out, r.encodeTranslations()...) return out } - -func firstOkString(ss ...string) string { - for _, s := range ss { - if s != "" { - return s - } - } - return "" -} diff --git a/pkg/codegen-v3/assets/templates/gocode/locale/types.go.tpl b/pkg/codegen-v3/assets/templates/gocode/locale/types.go.tpl index 2c2c50abc..2612b74e4 100644 --- a/pkg/codegen-v3/assets/templates/gocode/locale/types.go.tpl +++ b/pkg/codegen-v3/assets/templates/gocode/locale/types.go.tpl @@ -88,8 +88,6 @@ func (r *{{ .Resource }}) DecodeTranslations(tt locale.ResourceTranslationIndex) {{- if not .Custom }} if aux = tt.FindByKey(LocaleKey{{ $Resource }}{{coalesce (export .Name) (export .Path) }}.Path); aux != nil { r.{{ .Field }} = aux.Msg - } else { - r.{{ .Field }} = LocaleKey{{ $Resource }}{{coalesce (export .Name) (export .Path) }}.Path } {{- end}} {{- end}} @@ -107,17 +105,20 @@ func (r *{{ .Resource }}) DecodeTranslations(tt locale.ResourceTranslationIndex) } func (r *{{ .Resource }}) EncodeTranslations() (out locale.ResourceTranslationSet) { - out = locale.ResourceTranslationSet{ + out = locale.ResourceTranslationSet{} {{- range .Locale.Keys}} {{- if not .Custom }} - { + if r.{{ .Field }} != "" { + out = append(out, &locale.ResourceTranslation{ Resource: r.ResourceTranslation(), Key: LocaleKey{{ $Resource }}{{coalesce (export .Name) (export .Path) }}.Path, - Msg: firstOkString(r.{{ .Field }}, LocaleKey{{ $Resource }}{{coalesce (export .Name) (export .Path) }}.Path), - }, -{{- end}} -{{- end}} + Msg: r.{{ .Field }}, + }) } + +{{- end}} +{{- end}} + {{range .Locale.Keys}} {{- if and .Custom .CustomHandler }} out = append(out, r.encodeTranslations{{export .CustomHandler}}()...) @@ -132,12 +133,3 @@ func (r *{{ .Resource }}) EncodeTranslations() (out locale.ResourceTranslationSe } {{- end }} - -func firstOkString(ss ...string) string { - for _, s := range ss { - if s != "" { - return s - } - } - return "" -} diff --git a/pkg/locale/locale.go b/pkg/locale/locale.go index f96bb4362..557a9b912 100644 --- a/pkg/locale/locale.go +++ b/pkg/locale/locale.go @@ -99,7 +99,7 @@ func (l *Language) tResource(ns, key string, rr ...string) string { return l.extends.tResource(ns, key, rr...) } - return key + return "" } // resourceTranslations returns all resource translations for the specified resource