Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds ability to define custom funcs in LocalizeConfig #99 #100

Merged
merged 4 commits into from
Apr 27, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion v2/i18n/localizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package i18n
import (
"fmt"

"text/template"

"github.com/nicksnyder/go-i18n/v2/internal"
"github.com/nicksnyder/go-i18n/v2/internal/plural"
"golang.org/x/text/language"
Expand Down Expand Up @@ -56,6 +58,9 @@ type LocalizeConfig struct {

// DefaultMessage is used if the message is not found in any message files.
DefaultMessage *Message

// Funcs is used to extend the Go template engines built in functions
Funcs template.FuncMap
}

type invalidPluralCountErr struct {
Expand Down Expand Up @@ -114,7 +119,7 @@ func (l *Localizer) Localize(lc *LocalizeConfig) (string, error) {
if pluralForm == plural.Invalid {
return "", &pluralizeErr{messageID: messageID, tag: tag}
}
return template.Execute(pluralForm, templateData)
return template.Execute(pluralForm, templateData, lc.Funcs)
}

func (l *Localizer) getTemplate(id string, defaultMessage *Message) (language.Tag, *internal.MessageTemplate) {
Expand Down
6 changes: 4 additions & 2 deletions v2/internal/message_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package internal
import (
"bytes"

"text/template"

"github.com/nicksnyder/go-i18n/v2/internal/plural"
)

Expand Down Expand Up @@ -37,9 +39,9 @@ func setPluralTemplate(pluralTemplates map[plural.Form]*Template, pluralForm plu
}

// Execute executes the template for the plural form and template data.
func (mt *MessageTemplate) Execute(pluralForm plural.Form, data interface{}) (string, error) {
func (mt *MessageTemplate) Execute(pluralForm plural.Form, data interface{}, funcs template.FuncMap) (string, error) {
t := mt.PluralTemplates[pluralForm]
if err := t.parse(mt.LeftDelim, mt.RightDelim); err != nil {
if err := t.parse(mt.LeftDelim, mt.RightDelim, funcs); err != nil {
return "", err
}
if t.Template == nil {
Expand Down
4 changes: 2 additions & 2 deletions v2/internal/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ type Template struct {
ParseErr *error
}

func (t *Template) parse(leftDelim, rightDelim string) error {
func (t *Template) parse(leftDelim, rightDelim string, funcs gotemplate.FuncMap) error {
if t.ParseErr == nil {
if strings.Contains(t.Src, leftDelim) {
gt, err := gotemplate.New("").Delims(leftDelim, rightDelim).Parse(t.Src)
gt, err := gotemplate.New("").Funcs(funcs).Delims(leftDelim, rightDelim).Parse(t.Src)
t.Template = gt
t.ParseErr = &err
} else {
Expand Down
23 changes: 21 additions & 2 deletions v2/internal/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package internal
import (
"fmt"
"testing"
"text/template"
)

func TestParse(t *testing.T) {
tmpl := &Template{Src: "hello"}
if err := tmpl.parse("", ""); err != nil {
if err := tmpl.parse("", "", nil); err != nil {
t.Fatal(err)
}
if tmpl.ParseErr == nil {
Expand All @@ -21,7 +22,25 @@ func TestParse(t *testing.T) {
func TestParseError(t *testing.T) {
expectedErr := fmt.Errorf("expected error")
tmpl := &Template{ParseErr: &expectedErr}
if err := tmpl.parse("", ""); err != expectedErr {
if err := tmpl.parse("", "", nil); err != expectedErr {
t.Fatalf("expected %#v; got %#v", expectedErr, err)
}
}

func TestParseWithFunc(t *testing.T) {
tmpl := &Template{Src: "{{foo}}"}
funcs := template.FuncMap{
"foo": func() string {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test doesn't seem to actually use the "foo" func. Can you add a test that does that?

return "bar"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also check that the value produced by the template is actually “bar”

},
}
if err := tmpl.parse("", "", funcs); err != nil {
t.Fatal(err)
}
if tmpl.ParseErr == nil {
t.Fatal("expected non-nil parse error")
}
if tmpl.Template == nil {
t.Fatal("expected non-nil template")
}
}