Forráskód Böngészése

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.
Dean Karn 9 éve
szülő
commit
d72ff9453c
4 módosított fájl, 37 hozzáadás és 21 törlés
  1. 1 1
      README.md
  2. 4 1
      benchmarks_test.go
  3. 25 10
      translator_test.go
  4. 7 9
      universal-translator.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.12.1-green.svg)
+![Project status](https://img.shields.io/badge/version-0.12.2-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)

+ 4 - 1
benchmarks_test.go

@@ -10,7 +10,10 @@ func BenchmarkBasicTranslation(b *testing.B) {
 
 	en := en.New()
 	ut := New(en, en)
-	loc := ut.FindTranslator("en")
+	loc, found := ut.FindTranslator("en")
+	if !found {
+		b.Fatalf("Expected '%t' Got '%t'", true, found)
+	}
 
 	translations := []struct {
 		key      interface{}

+ 25 - 10
translator_test.go

@@ -25,7 +25,10 @@ func TestBasicTranslation(t *testing.T) {
 
 	e := en.New()
 	uni := New(e, e)
-	en := uni.GetTranslator("en") // or fallback if fails to find 'en'
+	en, found := uni.GetTranslator("en") // or fallback if fails to find 'en'
+	if !found {
+		t.Fatalf("Expected '%t' Got '%t'", true, found)
+	}
 
 	translations := []struct {
 		key           interface{}
@@ -127,7 +130,10 @@ func TestCardinalTranslation(t *testing.T) {
 
 	e := en.New()
 	uni := New(e, e)
-	en := uni.GetTranslator("en")
+	en, found := uni.GetTranslator("en")
+	if !found {
+		t.Fatalf("Expected '%t' Got '%t'", true, found)
+	}
 
 	translations := []struct {
 		key           interface{}
@@ -228,7 +234,10 @@ func TestOrdinalTranslation(t *testing.T) {
 
 	e := en.New()
 	uni := New(e, e)
-	en := uni.GetTranslator("en")
+	en, found := uni.GetTranslator("en")
+	if !found {
+		t.Fatalf("Expected '%t' Got '%t'", true, found)
+	}
 
 	translations := []struct {
 		key           interface{}
@@ -372,7 +381,10 @@ func TestRangeTranslation(t *testing.T) {
 	uni := New(n, n)
 
 	// dutch
-	nl := uni.GetTranslator("nl")
+	nl, found := uni.GetTranslator("nl")
+	if !found {
+		t.Fatalf("Expected '%t' Got '%t'", true, found)
+	}
 
 	translations := []struct {
 		key           interface{}
@@ -511,23 +523,26 @@ func TestFallbackTranslator(t *testing.T) {
 
 	e := en.New()
 	uni := New(e, e)
-	en := uni.GetTranslator("en")
+	en, found := uni.GetTranslator("en")
+	if !found {
+		t.Fatalf("Expected '%t' Got '%t'", true, found)
+	}
 
 	if en.Locale() != "en" {
 		t.Errorf("Expected '%s' Got '%s'", "en", en.Locale())
 	}
 
-	fallback := uni.GetTranslator("nl")
+	fallback, _ := uni.GetTranslator("nl")
 	if fallback.Locale() != "en" {
 		t.Errorf("Expected '%s' Got '%s'", "en", fallback.Locale())
 	}
 
-	en = uni.FindTranslator("nl", "en")
+	en, _ = uni.FindTranslator("nl", "en")
 	if en.Locale() != "en" {
 		t.Errorf("Expected '%s' Got '%s'", "en", en.Locale())
 	}
 
-	fallback = uni.FindTranslator("nl")
+	fallback, _ = uni.FindTranslator("nl")
 	if fallback.Locale() != "en" {
 		t.Errorf("Expected '%s' Got '%s'", "en", fallback.Locale())
 	}
@@ -586,7 +601,7 @@ func TestVerifyTranslations(t *testing.T) {
 	// dutch
 	uni := New(n, n)
 
-	loc := uni.GetTranslator("nl")
+	loc, _ := uni.GetTranslator("nl")
 	if loc.Locale() != "nl" {
 		t.Errorf("Expected '%s' Got '%s'", "nl", loc.Locale())
 	}
@@ -647,7 +662,7 @@ func TestVerifyTranslations(t *testing.T) {
 		t.Fatalf("Expected '<nil>' Got '%s'", err)
 	}
 
-	loc = uni.GetTranslator("en")
+	loc, _ = uni.GetTranslator("en")
 	if loc.Locale() != "en" {
 		t.Errorf("Expected '%s' Got '%s'", "en", loc.Locale())
 	}

+ 7 - 9
universal-translator.go

@@ -40,29 +40,27 @@ func New(fallback locales.Translator, supportedLocales ...locales.Translator) *U
 // FindTranslator trys to find a Translator based on an array of locales
 // and returns the first one it can find, otherwise returns the
 // fallback translator.
-func (t *UniversalTranslator) FindTranslator(locales ...string) (trans Translator) {
-
-	var ok bool
+func (t *UniversalTranslator) FindTranslator(locales ...string) (trans Translator, found bool) {
 
 	for _, locale := range locales {
 
-		if trans, ok = t.translators[strings.ToLower(locale)]; ok {
+		if trans, found = t.translators[strings.ToLower(locale)]; found {
 			return
 		}
 	}
 
-	return t.fallback
+	return t.fallback, false
 }
 
 // GetTranslator returns the specified translator for the given locale,
 // or fallback if not found
-func (t *UniversalTranslator) GetTranslator(locale string) Translator {
+func (t *UniversalTranslator) GetTranslator(locale string) (trans Translator, found bool) {
 
-	if t, ok := t.translators[strings.ToLower(locale)]; ok {
-		return t
+	if trans, found = t.translators[strings.ToLower(locale)]; found {
+		return
 	}
 
-	return t.fallback
+	return t.fallback, false
 }
 
 // GetFallback returns the fallback locale