From b5b73bd1076c46e7c04fcb5ea71718b114307422 Mon Sep 17 00:00:00 2001 From: andig Date: Sun, 2 Oct 2022 19:42:26 +0200 Subject: [PATCH] Templates: simplify language handling --- util/templates/template.go | 7 +------ util/templates/template_types.go | 34 ++++++++++++++++---------------- 2 files changed, 18 insertions(+), 23 deletions(-) diff --git a/util/templates/template.go b/util/templates/template.go index 0cafda7c4..729bd9a99 100644 --- a/util/templates/template.go +++ b/util/templates/template.go @@ -108,11 +108,6 @@ func (t *Template) Title() string { return t.title } -// return a language specific product title -func (t *Template) ProductTitle(p Product) string { - return strings.TrimSpace(fmt.Sprintf("%s %s", p.Brand, p.Description.String(t.Lang))) -} - // return the language specific product titles func (t *Template) Titles(lang string) []string { t.Lang = lang @@ -127,7 +122,7 @@ func (t *Template) Titles(lang string) []string { // set the language specific product titles func (t *Template) resolveTitles() { for _, p := range t.Products { - t.titles = append(t.titles, t.ProductTitle(p)) + t.titles = append(t.titles, p.Title(t.Lang)) } } diff --git a/util/templates/template_types.go b/util/templates/template_types.go index 0f887698b..cfb34010f 100644 --- a/util/templates/template_types.go +++ b/util/templates/template_types.go @@ -2,6 +2,7 @@ package templates import ( "bufio" + "fmt" "reflect" "strings" ) @@ -95,16 +96,17 @@ type TextLanguage struct { } func (t *TextLanguage) String(lang string) string { - if t.Generic != "" { - return t.Generic - } - switch lang { - case "de": + switch { + case lang == "de" && t.DE != "": return t.DE - case "en": + case lang == "en" && t.EN != "": return t.EN + default: + if t.Generic != "" { + return t.Generic + } + return t.DE } - return t.DE } func (t *TextLanguage) set(lang, value string) { @@ -120,19 +122,13 @@ func (t *TextLanguage) set(lang, value string) { // Shorten reduces help texts to one line and adds ... func (t *TextLanguage) Shorten(lang string) { - help := t.String(lang) - if help == "" { - return - } - - scanner := bufio.NewScanner(strings.NewReader(help)) - - var line int var short string + help := t.String(lang) + scanner := bufio.NewScanner(strings.NewReader(help)) + for scanner.Scan() { - line++ - if line == 1 { + if short == "" { short = scanner.Text() } else { short += "..." @@ -283,6 +279,10 @@ type Product struct { Description TextLanguage // product name } +func (p Product) Title(lang string) string { + return strings.TrimSpace(fmt.Sprintf("%s %s", p.Brand, p.Description.String(lang))) +} + // TemplateDefinition contains properties of a device template type TemplateDefinition struct { Template string