Skip to content

Commit 94bc451

Browse files
Dean KarnDean Karn
authored andcommitted
finalizing function names and params
- updated to accept translator interface instead of string, this allows for easier overriding - decreases compile time as not referencing ever locale is references anymore, but provided. - remove Overwrite functions and add param to override or not. - add AddTranslator function.
1 parent e61ecf1 commit 94bc451

File tree

6 files changed

+190
-221
lines changed

6 files changed

+190
-221
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.10.0-green.svg)
3+
![Project status](https://img.shields.io/badge/version-0.11.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)

benchmarks_test.go

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
package ut
22

3-
import "testing"
3+
import (
4+
"testing"
5+
6+
"github.com/go-playground/locales/en"
7+
)
48

59
func BenchmarkBasicTranslation(b *testing.B) {
610

7-
ut, _ := New("en", "en")
11+
en := en.New()
12+
ut := New(en, en)
813
loc := ut.FindTranslator("en")
914

1015
translations := []struct {
1116
key interface{}
1217
trans string
1318
expected error
19+
override bool
1420
}{
1521
{
1622
key: "welcome",
@@ -30,16 +36,20 @@ func BenchmarkBasicTranslation(b *testing.B) {
3036
}
3137

3238
for _, tt := range translations {
33-
if err := loc.Add(tt.key, tt.trans); err != nil {
39+
if err := loc.Add(tt.key, tt.trans, tt.override); err != nil {
3440
b.Fatalf("adding translation '%s' failed with key '%s'", tt.trans, tt.key)
3541
}
3642
}
3743

44+
var err error
45+
3846
b.ResetTimer()
3947

4048
b.Run("", func(b *testing.B) {
4149
for i := 0; i < b.N; i++ {
42-
loc.T("welcome")
50+
if _, err = loc.T("welcome"); err != nil {
51+
b.Error(err)
52+
}
4353
}
4454
})
4555

@@ -48,14 +58,18 @@ func BenchmarkBasicTranslation(b *testing.B) {
4858
b.RunParallel(func(pb *testing.PB) {
4959

5060
for pb.Next() {
51-
loc.T("welcome")
61+
if _, err = loc.T("welcome"); err != nil {
62+
b.Error(err)
63+
}
5264
}
5365
})
5466
})
5567

5668
b.Run("With1Param", func(b *testing.B) {
5769
for i := 0; i < b.N; i++ {
58-
loc.T("welcome-user", "Joeybloggs")
70+
if _, err = loc.T("welcome-user", "Joeybloggs"); err != nil {
71+
b.Error(err)
72+
}
5973
}
6074
})
6175

@@ -64,14 +78,18 @@ func BenchmarkBasicTranslation(b *testing.B) {
6478
b.RunParallel(func(pb *testing.PB) {
6579

6680
for pb.Next() {
67-
loc.T("welcome-user", "Joeybloggs")
81+
if _, err = loc.T("welcome-user", "Joeybloggs"); err != nil {
82+
b.Error(err)
83+
}
6884
}
6985
})
7086
})
7187

7288
b.Run("With2Param", func(b *testing.B) {
7389
for i := 0; i < b.N; i++ {
74-
loc.T("welcome-user2", "Joeybloggs", "/dev/tty0")
90+
if _, err = loc.T("welcome-user2", "Joeybloggs", "/dev/tty0"); err != nil {
91+
b.Error(err)
92+
}
7593
}
7694
})
7795

@@ -80,7 +98,9 @@ func BenchmarkBasicTranslation(b *testing.B) {
8098
b.RunParallel(func(pb *testing.PB) {
8199

82100
for pb.Next() {
83-
loc.T("welcome-user2", "Joeybloggs", "/dev/tty0")
101+
if _, err = loc.T("welcome-user2", "Joeybloggs", "/dev/tty0"); err != nil {
102+
b.Error(err)
103+
}
84104
}
85105
})
86106
})

errors.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,18 @@ var _ error = new(ErrConflictingTranslation)
1616
var _ error = new(ErrRangeTranslation)
1717
var _ error = new(ErrOrdinalTranslation)
1818
var _ error = new(ErrCardinalTranslation)
19-
var _ error = new(ErrLocaleNotFound)
2019
var _ error = new(ErrMissingPluralTranslation)
20+
var _ error = new(ErrExistingTranslator)
21+
22+
// ErrExistingTranslator is the error representing a conflicting translator
23+
type ErrExistingTranslator struct {
24+
locale string
25+
}
26+
27+
// Error returns ErrExistingTranslator's internal error text
28+
func (e *ErrExistingTranslator) Error() string {
29+
return fmt.Sprintf("error: conflicting translator key '%s'", e.locale)
30+
}
2131

2232
// ErrConflictingTranslation is the error representing a conflicting translation
2333
type ErrConflictingTranslation struct {
@@ -28,7 +38,7 @@ type ErrConflictingTranslation struct {
2838

2939
// Error returns ErrConflictingTranslation's internal error text
3040
func (e *ErrConflictingTranslation) Error() string {
31-
return fmt.Sprintf("warning: conflicting key '%#v' rule '%d' with text '%s', value being ignored", e.key, e.rule, e.text)
41+
return fmt.Sprintf("error: conflicting key '%#v' rule '%d' with text '%s', value being ignored", e.key, e.rule, e.text)
3242
}
3343

3444
// ErrRangeTranslation is the error representing a range translation error
@@ -61,16 +71,6 @@ func (e *ErrCardinalTranslation) Error() string {
6171
return e.text
6272
}
6373

64-
// ErrLocaleNotFound is the error signifying a locale which could not be found
65-
type ErrLocaleNotFound struct {
66-
text string
67-
}
68-
69-
// Error returns ErrLocaleNotFound's internal error text
70-
func (e *ErrLocaleNotFound) Error() string {
71-
return e.text
72-
}
73-
7474
// ErrMissingPluralTranslation is the error signifying a missing translation given
7575
// the locales plural rules.
7676
type ErrMissingPluralTranslation struct {

translator.go

Lines changed: 16 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -24,42 +24,30 @@ type Translator interface {
2424
// adds a normal translation for a particular language/locale
2525
// {#} is the only replacement type accepted and are add infintium
2626
// eg. one: '{0} day left' other: '{0} days left'
27-
Add(key interface{}, text string) error
28-
29-
// is the same as Add only it allows existing translations to be overridden
30-
Overwrite(key interface{}, text string) error
27+
Add(key interface{}, text string, override bool) error
3128

3229
// adds a cardinal plural translation for a particular language/locale
3330
// {0} is the only replacement type accepted and only one variable is accepted as
3431
// multiple cannot be used for a plural rule determination, unless it is a range;
3532
// see AddRange below.
3633
// eg. in locale 'en' one: '{0} day left' other: '{0} days left'
37-
AddCardinal(key interface{}, text string, rule locales.PluralRule) error
38-
39-
// is the same as AddCardinal only it allows existing translations to be overridden
40-
OverwriteCardinal(key interface{}, text string, rule locales.PluralRule) error
34+
AddCardinal(key interface{}, text string, rule locales.PluralRule, override bool) error
4135

4236
// adds an ordinal plural translation for a particular language/locale
4337
// {0} is the only replacement type accepted and only one variable is accepted as
4438
// multiple cannot be used for a plural rule determination, unless it is a range;
4539
// see AddRange below.
4640
// eg. in locale 'en' one: '{0}st day of spring' other: '{0}nd day of spring'
4741
// - 1st, 2nd, 3rd...
48-
AddOrdinal(key interface{}, text string, rule locales.PluralRule) error
49-
50-
// is the same as AddOrdinal only it allows for existing translations to be overridden
51-
OverwriteOrdinal(key interface{}, text string, rule locales.PluralRule) error
42+
AddOrdinal(key interface{}, text string, rule locales.PluralRule, override bool) error
5243

5344
// adds a range plural translation for a particular language/locale
5445
// {0} and {1} are the only replacement types accepted and only these are accepted.
5546
// eg. in locale 'nl' one: '{0}-{1} day left' other: '{0}-{1} days left'
56-
AddRange(key interface{}, text string, rule locales.PluralRule) error
57-
58-
// is the same as AddRange only allows an existing translation to be overridden
59-
OverwriteRange(key interface{}, text string, rule locales.PluralRule) error
47+
AddRange(key interface{}, text string, rule locales.PluralRule, override bool) error
6048

6149
// creates the translation for the locale given the 'key' and params passed in
62-
T(key interface{}, params ...string) string
50+
T(key interface{}, params ...string) (string, error)
6351

6452
// creates the cardinal translation for the locale given the 'key', 'num' and 'digit' arguments
6553
// and param passed in
@@ -107,18 +95,9 @@ func newTranslator(trans locales.Translator) Translator {
10795
// Add adds a normal translation for a particular language/locale
10896
// {#} is the only replacement type accepted and are add infintium
10997
// eg. one: '{0} day left' other: '{0} days left'
110-
func (t *translator) Add(key interface{}, text string) error {
111-
return t.add(key, text, false)
112-
}
98+
func (t *translator) Add(key interface{}, text string, override bool) error {
11399

114-
// Overwrite is the same as Add only it allows existing translations to be overridden
115-
func (t *translator) Overwrite(key interface{}, text string) error {
116-
return t.add(key, text, true)
117-
}
118-
119-
func (t *translator) add(key interface{}, text string, overwrite bool) error {
120-
121-
if _, ok := t.translations[key]; ok && !overwrite {
100+
if _, ok := t.translations[key]; ok && !override {
122101
return &ErrConflictingTranslation{key: key, text: text}
123102
}
124103

@@ -154,21 +133,12 @@ func (t *translator) add(key interface{}, text string, overwrite bool) error {
154133
// multiple cannot be used for a plural rule determination, unless it is a range;
155134
// see AddRange below.
156135
// eg. in locale 'en' one: '{0} day left' other: '{0} days left'
157-
func (t *translator) AddCardinal(key interface{}, text string, rule locales.PluralRule) error {
158-
return t.addCardinal(key, text, rule, false)
159-
}
160-
161-
// OverwriteCardinal is the same as AddCardinal only it allows existing translations to be overridden
162-
func (t *translator) OverwriteCardinal(key interface{}, text string, rule locales.PluralRule) error {
163-
return t.addCardinal(key, text, rule, true)
164-
}
165-
166-
func (t *translator) addCardinal(key interface{}, text string, rule locales.PluralRule, overwrite bool) error {
136+
func (t *translator) AddCardinal(key interface{}, text string, rule locales.PluralRule, override bool) error {
167137

168138
tarr, ok := t.cardinalTanslations[key]
169139
if ok {
170140
// verify not adding a conflicting record
171-
if len(tarr) > 0 && tarr[rule] != nil && !overwrite {
141+
if len(tarr) > 0 && tarr[rule] != nil && !override {
172142
return &ErrConflictingTranslation{key: key, rule: rule, text: text}
173143
}
174144

@@ -201,21 +171,12 @@ func (t *translator) addCardinal(key interface{}, text string, rule locales.Plur
201171
// multiple cannot be used for a plural rule determination, unless it is a range;
202172
// see AddRange below.
203173
// eg. in locale 'en' one: '{0}st day of spring' other: '{0}nd day of spring' - 1st, 2nd, 3rd...
204-
func (t *translator) AddOrdinal(key interface{}, text string, rule locales.PluralRule) error {
205-
return t.addOrdinal(key, text, rule, false)
206-
}
207-
208-
// OverwriteOrdinal is the same as AddOrdinal only it allows for existing translations to be overridden
209-
func (t *translator) OverwriteOrdinal(key interface{}, text string, rule locales.PluralRule) error {
210-
return t.addOrdinal(key, text, rule, true)
211-
}
212-
213-
func (t *translator) addOrdinal(key interface{}, text string, rule locales.PluralRule, overwrite bool) error {
174+
func (t *translator) AddOrdinal(key interface{}, text string, rule locales.PluralRule, override bool) error {
214175

215176
tarr, ok := t.ordinalTanslations[key]
216177
if ok {
217178
// verify not adding a conflicting record
218-
if len(tarr) > 0 && tarr[rule] != nil && !overwrite {
179+
if len(tarr) > 0 && tarr[rule] != nil && !override {
219180
return &ErrConflictingTranslation{key: key, rule: rule, text: text}
220181
}
221182

@@ -246,21 +207,12 @@ func (t *translator) addOrdinal(key interface{}, text string, rule locales.Plura
246207
// AddRange adds a range plural translation for a particular language/locale
247208
// {0} and {1} are the only replacement types accepted and only these are accepted.
248209
// eg. in locale 'nl' one: '{0}-{1} day left' other: '{0}-{1} days left'
249-
func (t *translator) AddRange(key interface{}, text string, rule locales.PluralRule) error {
250-
return t.addRange(key, text, rule, false)
251-
}
252-
253-
// OverwriteRange is the same as AddRange only allows an existing translation to be overridden
254-
func (t *translator) OverwriteRange(key interface{}, text string, rule locales.PluralRule) error {
255-
return t.addRange(key, text, rule, true)
256-
}
257-
258-
func (t *translator) addRange(key interface{}, text string, rule locales.PluralRule, overwrite bool) error {
210+
func (t *translator) AddRange(key interface{}, text string, rule locales.PluralRule, override bool) error {
259211

260212
tarr, ok := t.rangeTanslations[key]
261213
if ok {
262214
// verify not adding a conflicting record
263-
if len(tarr) > 0 && tarr[rule] != nil && !overwrite {
215+
if len(tarr) > 0 && tarr[rule] != nil && !override {
264216
return &ErrConflictingTranslation{key: key, rule: rule, text: text}
265217
}
266218

@@ -298,11 +250,11 @@ func (t *translator) addRange(key interface{}, text string, rule locales.PluralR
298250
}
299251

300252
// T creates the translation for the locale given the 'key' and params passed in
301-
func (t *translator) T(key interface{}, params ...string) string {
253+
func (t *translator) T(key interface{}, params ...string) (string, error) {
302254

303255
trans, ok := t.translations[key]
304256
if !ok {
305-
return unknownTranslation
257+
return unknownTranslation, ErrUnknowTranslation
306258
}
307259

308260
b := make([]byte, 0, 64)
@@ -320,7 +272,7 @@ func (t *translator) T(key interface{}, params ...string) string {
320272

321273
b = append(b, trans.text[start:]...)
322274

323-
return string(b)
275+
return string(b), nil
324276
}
325277

326278
// C creates the cardinal translation for the locale given the 'key', 'num' and 'digit' arguments and param passed in

0 commit comments

Comments
 (0)