aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/templates.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/templates.go')
-rw-r--r--src/templates.go56
1 files changed, 51 insertions, 5 deletions
diff --git a/src/templates.go b/src/templates.go
index c2a40a5..a7d7f2a 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,
+ "withTranslations": withTranslations,
}
)
@@ -43,6 +45,9 @@ func BuildTemplates(dir fs.FS) {
templates = make(map[string]*template.Template, len(ents))
for _, e := range ents {
+ if e.IsDir() {
+ continue
+ }
name := e.Name()
buildAndSetTemplate(dir, name)
if Debugp {
@@ -121,10 +126,51 @@ func includeIfExists(tmpl *template.Template) template.FuncMap {
}
}
+func ifElse(b bool, x, y any) any {
+ if b {
+ return x
+ }
+ return y
+}
+
+func withTranslations(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...))
}
+func (td templateData) GetC(fmt, ctx string, args ...map[string]any) template.HTML {
+ return template.HTML(td.Printer.GetC(fmt, ctx, args...))
+}
+
func (td templateData) GetN(fmtS, fmtP string, n int, args ...map[string]any) template.HTML {
return template.HTML(td.Printer.GetN(fmtS, fmtP, n, args...))
}