Templates: simplify language handling

This commit is contained in:
andig 2022-10-02 19:42:26 +02:00
parent 3af110652f
commit b5b73bd107
2 changed files with 18 additions and 23 deletions

View file

@ -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))
}
}

View file

@ -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