-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontent.go
54 lines (41 loc) · 940 Bytes
/
content.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
package elrond
import (
"bytes"
"fmt"
"text/template"
)
// Content for specified language
type Content struct {
language Language
template *template.Template
}
// Language of content
func (c *Content) Language() Language {
return c.language
}
// Parse template
func (c *Content) Parse(v interface{}) (string, error) {
var buf bytes.Buffer
err := c.template.Execute(&buf, v)
return buf.String(), err
}
// Template get content raw template
func (c *Content) Template() *template.Template {
return c.template
}
// Text get plain text
func (c *Content) Text() (string, error) {
return c.Parse(nil)
}
// C create a content, content is empty plain text by default
func C(language Language, text string) *Content {
tmpl, err := template.New("translation").Parse(text)
if err != nil {
panic(fmt.Sprintf("failed parse template: %s", err.Error()))
}
c := Content{
language: language,
template: tmpl,
}
return &c
}