-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplates.go
More file actions
76 lines (60 loc) · 1.7 KB
/
Copy pathtemplates.go
File metadata and controls
76 lines (60 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright (c) Kopexa GmbH
// SPDX-License-Identifier: BUSL-1.1
package comms
import (
"embed"
"io/fs"
"path/filepath"
"text/template"
"github.com/Masterminds/sprig/v3"
"github.com/rs/zerolog/log"
)
const (
defaultTemplatesDir = "templates"
// DefaultLanguage is the fallback language when a template is not found
// in the requested language.
DefaultLanguage = "en"
)
var (
//go:embed templates/en/* templates/de/*
files embed.FS
templates map[string]*template.Template
)
func init() {
templates = make(map[string]*template.Template)
// Read language directories
langDirs, err := fs.ReadDir(files, defaultTemplatesDir)
if err != nil {
log.Panic().Err(err).Msg("could not read template directories")
}
for _, langDir := range langDirs {
if !langDir.IsDir() {
continue
}
lang := langDir.Name()
langPath := filepath.Join(defaultTemplatesDir, lang)
templateFiles, err := fs.ReadDir(files, langPath)
if err != nil {
log.Error().Err(err).Str("lang", lang).Msg("could not read language template directory")
continue
}
for _, file := range templateFiles {
if file.IsDir() {
continue
}
// Key format: "en/verify-email.txt"
key := filepath.Join(lang, file.Name())
templates[key] = parseTemplate(key)
}
}
}
func parseTemplate(name string) *template.Template {
// ParseFS names templates by their base filename only, not the full path.
// So we need to use the base name for template.New() as well.
baseName := filepath.Base(name)
tmpl, err := template.New(baseName).Funcs(sprig.FuncMap()).ParseFS(files, filepath.Join(defaultTemplatesDir, name))
if err != nil {
log.Fatal().Err(err).Str("template", name).Msg("could not parse template")
}
return tmpl
}