diff options
author | Thomas Voss <mail@thomasvoss.com> | 2025-07-30 02:25:39 +0200 |
---|---|---|
committer | Thomas Voss <mail@thomasvoss.com> | 2025-07-30 02:25:39 +0200 |
commit | 219364a5aa31dd5815627f41c770f53f91b1a9af (patch) | |
tree | bd02a30730fb338e6e48f64eb80c47d191da619a /src/templates.go | |
parent | f3a324d9eb72c0f00d952a8d46e0487805fa969d (diff) |
Do work on migrating the collecting/crh page
Diffstat (limited to 'src/templates.go')
-rw-r--r-- | src/templates.go | 50 |
1 files changed, 45 insertions, 5 deletions
diff --git a/src/templates.go b/src/templates.go index c2a40a5..d6b82b8 100644 --- a/src/templates.go +++ b/src/templates.go @@ -28,11 +28,13 @@ var ( errorTmpl *template.Template templates map[string]*template.Template funcmap = map[string]any{ - "locales": i18n.Locales, - "map": templateMakeMap, - "safe": asHTML, - "toUpper": strings.ToUpper, - "tuple": templateMakeTuple, + "ifElse": ifElse, + "locales": i18n.Locales, + "map": templateMakeMap, + "safe": asHTML, + "toUpper": strings.ToUpper, + "tuple": templateMakeTuple, + "makeHeaderWithTranslations": makeHeaderWithTranslations, } ) @@ -121,6 +123,44 @@ func includeIfExists(tmpl *template.Template) template.FuncMap { } } +func ifElse(b bool, x, y any) any { + if b { + return x + } + return y +} + +func makeHeaderWithTranslations(tag string, text string, + translations ...[]any) template.HTML { + var bob strings.Builder + bob.WriteByte('<') + bob.WriteString(tag) + bob.WriteByte('>') + bob.WriteString(text) + + /* TODO: Assert that the pairs are [2]string */ + for _, pair := range translations { + if text == pair[1] { + continue + } + bob.WriteString(`<br><span class="translation"`) + if pair[0].(string) != "" { + bob.WriteString(` lang="`) + bob.WriteString(pair[0].(string)) + bob.WriteString(`">`) + } else { + bob.WriteByte('>') + } + bob.WriteString(pair[1].(string)) + bob.WriteString("</span>") + } + + bob.WriteString("</") + bob.WriteString(tag) + bob.WriteByte('>') + return template.HTML(bob.String()) +} + func (td templateData) Get(fmt string, args ...map[string]any) template.HTML { return template.HTML(td.Printer.Get(fmt, args...)) } |