Browse Source

update error type text

Dean Karn 9 years ago
parent
commit
fc73d0c4bd
3 changed files with 57 additions and 6 deletions
  1. 1 1
      README.md
  2. 13 3
      errors.go
  3. 43 2
      translator_test.go

+ 1 - 1
README.md

@@ -1,6 +1,6 @@
 ## universal-translator
 <img align="right" src="https://raw.githubusercontent.com/go-playground/universal-translator/master/logo.png">
-![Project status](https://img.shields.io/badge/version-0.13.0-green.svg)
+![Project status](https://img.shields.io/badge/version-0.14.0-green.svg)
 [![Build Status](https://semaphoreci.com/api/v1/joeybloggs/universal-translator/branches/master/badge.svg)](https://semaphoreci.com/joeybloggs/universal-translator)
 [![Coverage Status](https://coveralls.io/repos/github/go-playground/universal-translator/badge.svg)](https://coveralls.io/github/go-playground/universal-translator)
 [![Go Report Card](https://goreportcard.com/badge/github.com/go-playground/universal-translator)](https://goreportcard.com/report/github.com/go-playground/universal-translator)

+ 13 - 3
errors.go

@@ -26,7 +26,7 @@ type ErrExistingTranslator struct {
 
 // Error returns ErrExistingTranslator's internal error text
 func (e *ErrExistingTranslator) Error() string {
-	return fmt.Sprintf("error: conflicting translator key '%s'", e.locale)
+	return fmt.Sprintf("error: conflicting translator for locale '%s'", e.locale)
 }
 
 // ErrConflictingTranslation is the error representing a conflicting translation
@@ -38,7 +38,12 @@ type ErrConflictingTranslation struct {
 
 // Error returns ErrConflictingTranslation's internal error text
 func (e *ErrConflictingTranslation) Error() string {
-	return fmt.Sprintf("error: conflicting key '%#v' rule '%d' with text '%s', value being ignored", e.key, e.rule, e.text)
+
+	if _, ok := e.key.(string); !ok {
+		return fmt.Sprintf("error: conflicting key '%#v' rule '%s' with text '%s', value being ignored", e.key, e.rule, e.text)
+	}
+
+	return fmt.Sprintf("error: conflicting key '%s' rule '%s' with text '%s', value being ignored", e.key, e.rule, e.text)
 }
 
 // ErrRangeTranslation is the error representing a range translation error
@@ -81,5 +86,10 @@ type ErrMissingPluralTranslation struct {
 
 // Error returns ErrMissingPluralTranslation's internal error text
 func (e *ErrMissingPluralTranslation) Error() string {
-	return fmt.Sprintf("error: missing %s plural rule '%s' for translation with key '%#v", e.translationType, e.rule, e.key)
+
+	if _, ok := e.key.(string); !ok {
+		return fmt.Sprintf("error: missing '%s' plural rule '%s' for translation with key '%#v'", e.translationType, e.rule, e.key)
+	}
+
+	return fmt.Sprintf("error: missing '%s' plural rule '%s' for translation with key '%s'", e.translationType, e.rule, e.key)
 }

+ 43 - 2
translator_test.go

@@ -42,6 +42,11 @@ func TestBasicTranslation(t *testing.T) {
 			trans:    "Welcome {0}",
 			expected: nil,
 		},
+		{
+			key:      -1,
+			trans:    "Welcome {0}",
+			expected: nil,
+		},
 		{
 			key:      "test_trans2",
 			trans:    "{0} to the {1}.",
@@ -60,7 +65,13 @@ func TestBasicTranslation(t *testing.T) {
 		{
 			key:           "test_trans",
 			trans:         "{0}{1}",
-			expected:      &ErrConflictingTranslation{key: "bad_trans", text: "{0}{1}"},
+			expected:      &ErrConflictingTranslation{key: "test_trans", text: "{0}{1}"},
+			expectedError: true,
+		},
+		{
+			key:           -1,
+			trans:         "{0}{1}",
+			expected:      &ErrConflictingTranslation{key: -1, text: "{0}{1}"},
 			expectedError: true,
 		},
 		{
@@ -75,8 +86,12 @@ func TestBasicTranslation(t *testing.T) {
 
 		err := en.Add(tt.key, tt.trans, tt.override)
 		if err != tt.expected {
-			if !tt.expectedError && err.Error() != tt.expected.Error() {
+			if !tt.expectedError {
 				t.Errorf("Expected '%s' Got '%s'", tt.expected, err)
+			} else {
+				if err.Error() != tt.expected.Error() {
+					t.Errorf("Expected '%s' Got '%s'", tt.expected.Error(), err.Error())
+				}
 			}
 		}
 	}
@@ -704,6 +719,32 @@ func TestVerifyTranslations(t *testing.T) {
 	}
 }
 
+func TestVerifyTranslationsWithNonStringKeys(t *testing.T) {
+
+	n := nl.New()
+	// dutch
+	uni := New(n, n)
+
+	loc, _ := uni.GetTranslator("nl")
+	if loc.Locale() != "nl" {
+		t.Errorf("Expected '%s' Got '%s'", "nl", loc.Locale())
+	}
+
+	// cardinal checks
+
+	err := loc.AddCardinal(-1, "je {0} dag hebben verlaten", locales.PluralRuleOne, false)
+	if err != nil {
+		t.Fatalf("Expected '<nil>' Got '%s'", err)
+	}
+
+	// fail cardinal rules
+	expected := &ErrMissingPluralTranslation{translationType: "plural", rule: locales.PluralRuleOther, key: -1}
+	err = loc.VerifyTranslations()
+	if err == nil || err.Error() != expected.Error() {
+		t.Errorf("Expected '%s' Got '%s'", expected, err)
+	}
+}
+
 func TestGetFallback(t *testing.T) {
 
 	// dutch