-
Notifications
You must be signed in to change notification settings - Fork 0
/
locale.go
89 lines (71 loc) · 2.1 KB
/
locale.go
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
77
78
79
80
81
82
83
84
85
86
87
88
89
package locale
import (
"bytes"
"fmt"
"text/template"
)
type (
// Map is is an alias of map[string][string]. It is used to store template strings.
Map map[string]string
// LangMap is a KV map of multiple language templates.
LangMap map[string]Map
// TemplatePayload is an alias of any possible unvalidated template payload.
TemplatePayload interface{}
// RenderOption describes the function signature of optional Render options
RenderOption func(c *renderConfig)
// Locale provides methods to access the lang map KV e.g. Render templates
Locale struct {
defaultLang string
langMap LangMap
templatePool *template.Template
}
// renderConfig is an internal struct to hold optional Render options
renderConfig struct {
langCode string
payload TemplatePayload
}
)
// NewLocale validates the lang map and returns a new instance of Locale.
func NewLocale(langMap LangMap, defaultLang string) (*Locale, error) {
if err := validate(langMap, defaultLang); err != nil {
return nil, err
}
baseTmpl, err := parseAndLoadTemplates(langMap)
if err != nil {
return nil, err
}
return &Locale{
defaultLang: defaultLang,
langMap: langMap,
templatePool: baseTmpl,
}, nil
}
// WithLangCode is an optional functional option for Render to set the language code.
func WithLangCode(langCode string) RenderOption {
return func(c *renderConfig) {
c.langCode = langCode
}
}
// WithPayload is an optional functional option for Render to set the template payload.
func WithPayload(payload TemplatePayload) RenderOption {
return func(c *renderConfig) {
c.payload = payload
}
}
// Render accepts a temeplate key and other optional functional options to Render a specific template
func (l *Locale) Render(tmplKey string, opts ...RenderOption) (string, error) {
c := &renderConfig{
langCode: l.defaultLang,
}
for _, opt := range opts {
opt(c)
}
if _, ok := l.langMap[c.langCode]; ok {
var buf bytes.Buffer
if err := l.templatePool.ExecuteTemplate(&buf, fmt.Sprintf("%s_%s", tmplKey, c.langCode), c.payload); err != nil {
return "", err
}
return buf.String(), nil
}
return "", nil
}