Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/gohugoio/go-i18n.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Dyakov <denis.dyakov@gmail.com>2018-12-09 03:03:30 +0300
committerNick Snyder <nickdsnyder@gmail.com>2018-12-09 03:03:30 +0300
commitd698e2fd90b98c03d2aa7c5e7872e2b570284291 (patch)
tree070b8d86f29140cef3d2a1f7dd6997a25276275f
parentcc0ca3bb8ee4a7c17dc0e0e3dab8728eea371401 (diff)
Provide nice output of plural form not found error (#147)
-rw-r--r--v2/internal/message_template.go16
-rw-r--r--v2/internal/message_template_test.go13
2 files changed, 29 insertions, 0 deletions
diff --git a/v2/internal/message_template.go b/v2/internal/message_template.go
index 03aed6f..134dae7 100644
--- a/v2/internal/message_template.go
+++ b/v2/internal/message_template.go
@@ -2,6 +2,7 @@ package internal
import (
"bytes"
+ "fmt"
"text/template"
@@ -38,9 +39,24 @@ func setPluralTemplate(pluralTemplates map[plural.Form]*Template, pluralForm plu
}
}
+type pluralFormNotFoundError struct {
+ pluralForm plural.Form
+ messageID string
+}
+
+func (e pluralFormNotFoundError) Error() string {
+ return fmt.Sprintf("message %q has no plural form %q", e.messageID, e.pluralForm)
+}
+
// Execute executes the template for the plural form and template data.
func (mt *MessageTemplate) Execute(pluralForm plural.Form, data interface{}, funcs template.FuncMap) (string, error) {
t := mt.PluralTemplates[pluralForm]
+ if t == nil {
+ return "", pluralFormNotFoundError{
+ pluralForm: pluralForm,
+ messageID: mt.Message.ID,
+ }
+ }
if err := t.parse(mt.LeftDelim, mt.RightDelim, funcs); err != nil {
return "", err
}
diff --git a/v2/internal/message_template_test.go b/v2/internal/message_template_test.go
index 4f631fd..a32708c 100644
--- a/v2/internal/message_template_test.go
+++ b/v2/internal/message_template_test.go
@@ -1,6 +1,7 @@
package internal
import (
+ "reflect"
"testing"
"github.com/nicksnyder/go-i18n/v2/internal/plural"
@@ -18,3 +19,15 @@ func TestNilMessageTemplate(t *testing.T) {
panic(mt)
}
}
+
+func TestMessageTemplatePluralFormMissing(t *testing.T) {
+ mt := NewMessageTemplate(&Message{ID: "HelloWorld", Other: "Hello World"})
+ s, err := mt.Execute(plural.Few, nil, nil)
+ 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)
+ }
+}