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

Add option to skip template execution #316

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
15 changes: 13 additions & 2 deletions v2/i18n/localizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ type LocalizeConfig struct {

// Funcs is used to extend the Go template engine's built in functions
Funcs template.FuncMap

// NoTemplateExec is used to disable template execution.
// The localized string will be returned as is.
NoTemplateExec bool
}

type invalidPluralCountErr struct {
Expand Down Expand Up @@ -152,15 +156,15 @@ func (l *Localizer) LocalizeWithTag(lc *LocalizeConfig) (string, language.Tag, e
}

pluralForm := l.pluralForm(tag, operands)
msg, err2 := template.Execute(pluralForm, templateData, lc.Funcs)
msg, err2 := getOrExecute(template, pluralForm, templateData, lc)
if err2 != nil {
if err == nil {
err = err2
}

// Attempt to fallback to "Other" pluralization in case translations are incomplete.
if pluralForm != plural.Other {
msg2, err3 := template.Execute(plural.Other, templateData, lc.Funcs)
msg2, err3 := getOrExecute(template, plural.Other, templateData, lc)
if err3 == nil {
msg = msg2
}
Expand All @@ -169,6 +173,13 @@ func (l *Localizer) LocalizeWithTag(lc *LocalizeConfig) (string, language.Tag, e
return msg, tag, err
}

func getOrExecute(template *MessageTemplate, pluralForm plural.Form, data interface{}, lc *LocalizeConfig) (string, error) {
if lc.NoTemplateExec {
return template.Get(pluralForm)
}
return template.Execute(pluralForm, data, lc.Funcs)
}

func (l *Localizer) getMessageTemplate(id string, defaultMessage *Message) (language.Tag, *MessageTemplate, error) {
_, i, _ := l.bundle.matcher.Match(l.tags...)
tag := l.bundle.tags[i]
Expand Down
16 changes: 16 additions & 0 deletions v2/i18n/localizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,22 @@ func localizerTests() []localizerTest {
},
expectedLocalized: "Hello Nick",
},
{
name: "ignore template, bundle message",
defaultLanguage: language.English,
messages: map[language.Tag][]*Message{
language.English: {{
ID: "HelloPerson",
Other: "Hello {{.Person}}",
}},
},
acceptLangs: []string{"en"},
conf: &LocalizeConfig{
MessageID: "HelloPerson",
NoTemplateExec: true,
},
expectedLocalized: "Hello {{.Person}}",
},
{
name: "template data, default message",
defaultLanguage: language.English,
Expand Down
12 changes: 12 additions & 0 deletions v2/i18n/message_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,15 @@ func (mt *MessageTemplate) Execute(pluralForm plural.Form, data interface{}, fun
}
return t.Execute(funcs, data)
}

// Get returns unprocessed template string for the plural form.
func (mt *MessageTemplate) Get(pluralForm plural.Form) (string, error) {
t := mt.PluralTemplates[pluralForm]
if t == nil {
return "", pluralFormNotFoundError{
pluralForm: pluralForm,
messageID: mt.Message.ID,
}
}
return t.Src, nil
}
12 changes: 12 additions & 0 deletions v2/i18n/message_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,15 @@ func TestMessageTemplatePluralFormMissing(t *testing.T) {
t.Errorf("expected error %#v; got %#v", expectedErr, err)
}
}

func TestMessageIgnoreTemplatePluralFormMissing(t *testing.T) {
mt := NewMessageTemplate(&Message{ID: "HelloWorld", Other: "Hello World"})
s, err := mt.Get(plural.Few)
if s != "" {
t.Errorf("expected %q; got %q", "", s)
}
expectedErr := pluralFormNotFoundError{pluralForm: plural.Few, messageID: "HelloWorld"}
if !reflect.DeepEqual(err, expectedErr) {
t.Errorf("expected error %#v; got %#v", expectedErr, err)
}
}
Loading