summaryrefslogtreecommitdiffstats
path: root/Godeps/_workspace/src/github.com/nicksnyder/go-i18n/i18n/translation/template.go
diff options
context:
space:
mode:
author=Corey Hulen <corey@hulen.com>2016-01-19 10:54:19 -0600
committer=Corey Hulen <corey@hulen.com>2016-01-19 10:54:19 -0600
commit4389571dedb9a68d801427c37ad971c8c488991f (patch)
treed66189c2ba3c76b76488a3d0dd6ae210ddfc0096 /Godeps/_workspace/src/github.com/nicksnyder/go-i18n/i18n/translation/template.go
parent80bfdf71f2ab7271dd199c61229fa2b8a7e0213c (diff)
downloadchat-4389571dedb9a68d801427c37ad971c8c488991f.tar.gz
chat-4389571dedb9a68d801427c37ad971c8c488991f.tar.bz2
chat-4389571dedb9a68d801427c37ad971c8c488991f.zip
PLT-7 adding server side libraries
Diffstat (limited to 'Godeps/_workspace/src/github.com/nicksnyder/go-i18n/i18n/translation/template.go')
-rw-r--r--Godeps/_workspace/src/github.com/nicksnyder/go-i18n/i18n/translation/template.go61
1 files changed, 61 insertions, 0 deletions
diff --git a/Godeps/_workspace/src/github.com/nicksnyder/go-i18n/i18n/translation/template.go b/Godeps/_workspace/src/github.com/nicksnyder/go-i18n/i18n/translation/template.go
new file mode 100644
index 000000000..c8756fa4e
--- /dev/null
+++ b/Godeps/_workspace/src/github.com/nicksnyder/go-i18n/i18n/translation/template.go
@@ -0,0 +1,61 @@
+package translation
+
+import (
+ "bytes"
+ "encoding"
+ "strings"
+ gotemplate "text/template"
+)
+
+type template struct {
+ tmpl *gotemplate.Template
+ src string
+}
+
+func newTemplate(src string) (*template, error) {
+ var tmpl template
+ err := tmpl.parseTemplate(src)
+ return &tmpl, err
+}
+
+func mustNewTemplate(src string) *template {
+ t, err := newTemplate(src)
+ if err != nil {
+ panic(err)
+ }
+ return t
+}
+
+func (t *template) String() string {
+ return t.src
+}
+
+func (t *template) Execute(args interface{}) string {
+ if t.tmpl == nil {
+ return t.src
+ }
+ var buf bytes.Buffer
+ if err := t.tmpl.Execute(&buf, args); err != nil {
+ return err.Error()
+ }
+ return buf.String()
+}
+
+func (t *template) MarshalText() ([]byte, error) {
+ return []byte(t.src), nil
+}
+
+func (t *template) UnmarshalText(src []byte) error {
+ return t.parseTemplate(string(src))
+}
+
+func (t *template) parseTemplate(src string) (err error) {
+ t.src = src
+ if strings.Contains(src, "{{") {
+ t.tmpl, err = gotemplate.New(src).Parse(src)
+ }
+ return
+}
+
+var _ = encoding.TextMarshaler(&template{})
+var _ = encoding.TextUnmarshaler(&template{})