Skip to content

Commit fc73d0c

Browse files
Dean KarnDean Karn
authored andcommitted
update error type text
1 parent f15f9a6 commit fc73d0c

File tree

3 files changed

+57
-6
lines changed

3 files changed

+57
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## universal-translator
22
<img align="right" src="https://raw.githubusercontent.com/go-playground/universal-translator/master/logo.png">
3-
![Project status](https://img.shields.io/badge/version-0.13.0-green.svg)
3+
![Project status](https://img.shields.io/badge/version-0.14.0-green.svg)
44
[![Build Status](https://semaphoreci.com/api/v1/joeybloggs/universal-translator/branches/master/badge.svg)](https://semaphoreci.com/joeybloggs/universal-translator)
55
[![Coverage Status](https://coveralls.io/repos/github/go-playground/universal-translator/badge.svg)](https://coveralls.io/github/go-playground/universal-translator)
66
[![Go Report Card](https://goreportcard.com/badge/github.com/go-playground/universal-translator)](https://goreportcard.com/report/github.com/go-playground/universal-translator)

errors.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type ErrExistingTranslator struct {
2626

2727
// Error returns ErrExistingTranslator's internal error text
2828
func (e *ErrExistingTranslator) Error() string {
29-
return fmt.Sprintf("error: conflicting translator key '%s'", e.locale)
29+
return fmt.Sprintf("error: conflicting translator for locale '%s'", e.locale)
3030
}
3131

3232
// ErrConflictingTranslation is the error representing a conflicting translation
@@ -38,7 +38,12 @@ type ErrConflictingTranslation struct {
3838

3939
// Error returns ErrConflictingTranslation's internal error text
4040
func (e *ErrConflictingTranslation) Error() string {
41-
return fmt.Sprintf("error: conflicting key '%#v' rule '%d' with text '%s', value being ignored", e.key, e.rule, e.text)
41+
42+
if _, ok := e.key.(string); !ok {
43+
return fmt.Sprintf("error: conflicting key '%#v' rule '%s' with text '%s', value being ignored", e.key, e.rule, e.text)
44+
}
45+
46+
return fmt.Sprintf("error: conflicting key '%s' rule '%s' with text '%s', value being ignored", e.key, e.rule, e.text)
4247
}
4348

4449
// ErrRangeTranslation is the error representing a range translation error
@@ -81,5 +86,10 @@ type ErrMissingPluralTranslation struct {
8186

8287
// Error returns ErrMissingPluralTranslation's internal error text
8388
func (e *ErrMissingPluralTranslation) Error() string {
84-
return fmt.Sprintf("error: missing %s plural rule '%s' for translation with key '%#v", e.translationType, e.rule, e.key)
89+
90+
if _, ok := e.key.(string); !ok {
91+
return fmt.Sprintf("error: missing '%s' plural rule '%s' for translation with key '%#v'", e.translationType, e.rule, e.key)
92+
}
93+
94+
return fmt.Sprintf("error: missing '%s' plural rule '%s' for translation with key '%s'", e.translationType, e.rule, e.key)
8595
}

translator_test.go

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ func TestBasicTranslation(t *testing.T) {
4242
trans: "Welcome {0}",
4343
expected: nil,
4444
},
45+
{
46+
key: -1,
47+
trans: "Welcome {0}",
48+
expected: nil,
49+
},
4550
{
4651
key: "test_trans2",
4752
trans: "{0} to the {1}.",
@@ -60,7 +65,13 @@ func TestBasicTranslation(t *testing.T) {
6065
{
6166
key: "test_trans",
6267
trans: "{0}{1}",
63-
expected: &ErrConflictingTranslation{key: "bad_trans", text: "{0}{1}"},
68+
expected: &ErrConflictingTranslation{key: "test_trans", text: "{0}{1}"},
69+
expectedError: true,
70+
},
71+
{
72+
key: -1,
73+
trans: "{0}{1}",
74+
expected: &ErrConflictingTranslation{key: -1, text: "{0}{1}"},
6475
expectedError: true,
6576
},
6677
{
@@ -75,8 +86,12 @@ func TestBasicTranslation(t *testing.T) {
7586

7687
err := en.Add(tt.key, tt.trans, tt.override)
7788
if err != tt.expected {
78-
if !tt.expectedError && err.Error() != tt.expected.Error() {
89+
if !tt.expectedError {
7990
t.Errorf("Expected '%s' Got '%s'", tt.expected, err)
91+
} else {
92+
if err.Error() != tt.expected.Error() {
93+
t.Errorf("Expected '%s' Got '%s'", tt.expected.Error(), err.Error())
94+
}
8095
}
8196
}
8297
}
@@ -704,6 +719,32 @@ func TestVerifyTranslations(t *testing.T) {
704719
}
705720
}
706721

722+
func TestVerifyTranslationsWithNonStringKeys(t *testing.T) {
723+
724+
n := nl.New()
725+
// dutch
726+
uni := New(n, n)
727+
728+
loc, _ := uni.GetTranslator("nl")
729+
if loc.Locale() != "nl" {
730+
t.Errorf("Expected '%s' Got '%s'", "nl", loc.Locale())
731+
}
732+
733+
// cardinal checks
734+
735+
err := loc.AddCardinal(-1, "je {0} dag hebben verlaten", locales.PluralRuleOne, false)
736+
if err != nil {
737+
t.Fatalf("Expected '<nil>' Got '%s'", err)
738+
}
739+
740+
// fail cardinal rules
741+
expected := &ErrMissingPluralTranslation{translationType: "plural", rule: locales.PluralRuleOther, key: -1}
742+
err = loc.VerifyTranslations()
743+
if err == nil || err.Error() != expected.Error() {
744+
t.Errorf("Expected '%s' Got '%s'", expected, err)
745+
}
746+
}
747+
707748
func TestGetFallback(t *testing.T) {
708749

709750
// dutch

0 commit comments

Comments
 (0)