Skip to content

Commit d72ff94

Browse files
Dean KarnDean Karn
authored andcommitted
up GetTranslator() and FindTranslator() functions to return if the translation was found
- this change will allow for redirecting to locale selection page when not found but fallback, if desired.
1 parent 7085643 commit d72ff94

File tree

4 files changed

+37
-21
lines changed

4 files changed

+37
-21
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.12.1-green.svg)
3+
![Project status](https://img.shields.io/badge/version-0.12.2-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: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ func BenchmarkBasicTranslation(b *testing.B) {
1010

1111
en := en.New()
1212
ut := New(en, en)
13-
loc := ut.FindTranslator("en")
13+
loc, found := ut.FindTranslator("en")
14+
if !found {
15+
b.Fatalf("Expected '%t' Got '%t'", true, found)
16+
}
1417

1518
translations := []struct {
1619
key interface{}

translator_test.go

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ func TestBasicTranslation(t *testing.T) {
2525

2626
e := en.New()
2727
uni := New(e, e)
28-
en := uni.GetTranslator("en") // or fallback if fails to find 'en'
28+
en, found := uni.GetTranslator("en") // or fallback if fails to find 'en'
29+
if !found {
30+
t.Fatalf("Expected '%t' Got '%t'", true, found)
31+
}
2932

3033
translations := []struct {
3134
key interface{}
@@ -127,7 +130,10 @@ func TestCardinalTranslation(t *testing.T) {
127130

128131
e := en.New()
129132
uni := New(e, e)
130-
en := uni.GetTranslator("en")
133+
en, found := uni.GetTranslator("en")
134+
if !found {
135+
t.Fatalf("Expected '%t' Got '%t'", true, found)
136+
}
131137

132138
translations := []struct {
133139
key interface{}
@@ -228,7 +234,10 @@ func TestOrdinalTranslation(t *testing.T) {
228234

229235
e := en.New()
230236
uni := New(e, e)
231-
en := uni.GetTranslator("en")
237+
en, found := uni.GetTranslator("en")
238+
if !found {
239+
t.Fatalf("Expected '%t' Got '%t'", true, found)
240+
}
232241

233242
translations := []struct {
234243
key interface{}
@@ -372,7 +381,10 @@ func TestRangeTranslation(t *testing.T) {
372381
uni := New(n, n)
373382

374383
// dutch
375-
nl := uni.GetTranslator("nl")
384+
nl, found := uni.GetTranslator("nl")
385+
if !found {
386+
t.Fatalf("Expected '%t' Got '%t'", true, found)
387+
}
376388

377389
translations := []struct {
378390
key interface{}
@@ -511,23 +523,26 @@ func TestFallbackTranslator(t *testing.T) {
511523

512524
e := en.New()
513525
uni := New(e, e)
514-
en := uni.GetTranslator("en")
526+
en, found := uni.GetTranslator("en")
527+
if !found {
528+
t.Fatalf("Expected '%t' Got '%t'", true, found)
529+
}
515530

516531
if en.Locale() != "en" {
517532
t.Errorf("Expected '%s' Got '%s'", "en", en.Locale())
518533
}
519534

520-
fallback := uni.GetTranslator("nl")
535+
fallback, _ := uni.GetTranslator("nl")
521536
if fallback.Locale() != "en" {
522537
t.Errorf("Expected '%s' Got '%s'", "en", fallback.Locale())
523538
}
524539

525-
en = uni.FindTranslator("nl", "en")
540+
en, _ = uni.FindTranslator("nl", "en")
526541
if en.Locale() != "en" {
527542
t.Errorf("Expected '%s' Got '%s'", "en", en.Locale())
528543
}
529544

530-
fallback = uni.FindTranslator("nl")
545+
fallback, _ = uni.FindTranslator("nl")
531546
if fallback.Locale() != "en" {
532547
t.Errorf("Expected '%s' Got '%s'", "en", fallback.Locale())
533548
}
@@ -586,7 +601,7 @@ func TestVerifyTranslations(t *testing.T) {
586601
// dutch
587602
uni := New(n, n)
588603

589-
loc := uni.GetTranslator("nl")
604+
loc, _ := uni.GetTranslator("nl")
590605
if loc.Locale() != "nl" {
591606
t.Errorf("Expected '%s' Got '%s'", "nl", loc.Locale())
592607
}
@@ -647,7 +662,7 @@ func TestVerifyTranslations(t *testing.T) {
647662
t.Fatalf("Expected '<nil>' Got '%s'", err)
648663
}
649664

650-
loc = uni.GetTranslator("en")
665+
loc, _ = uni.GetTranslator("en")
651666
if loc.Locale() != "en" {
652667
t.Errorf("Expected '%s' Got '%s'", "en", loc.Locale())
653668
}

universal-translator.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,29 +40,27 @@ func New(fallback locales.Translator, supportedLocales ...locales.Translator) *U
4040
// FindTranslator trys to find a Translator based on an array of locales
4141
// and returns the first one it can find, otherwise returns the
4242
// fallback translator.
43-
func (t *UniversalTranslator) FindTranslator(locales ...string) (trans Translator) {
44-
45-
var ok bool
43+
func (t *UniversalTranslator) FindTranslator(locales ...string) (trans Translator, found bool) {
4644

4745
for _, locale := range locales {
4846

49-
if trans, ok = t.translators[strings.ToLower(locale)]; ok {
47+
if trans, found = t.translators[strings.ToLower(locale)]; found {
5048
return
5149
}
5250
}
5351

54-
return t.fallback
52+
return t.fallback, false
5553
}
5654

5755
// GetTranslator returns the specified translator for the given locale,
5856
// or fallback if not found
59-
func (t *UniversalTranslator) GetTranslator(locale string) Translator {
57+
func (t *UniversalTranslator) GetTranslator(locale string) (trans Translator, found bool) {
6058

61-
if t, ok := t.translators[strings.ToLower(locale)]; ok {
62-
return t
59+
if trans, found = t.translators[strings.ToLower(locale)]; found {
60+
return
6361
}
6462

65-
return t.fallback
63+
return t.fallback, false
6664
}
6765

6866
// GetFallback returns the fallback locale

0 commit comments

Comments
 (0)