Browse Source

correct small bug double wrapping locales.Translator + added GetFallback() method.

Dean Karn 9 years ago
parent
commit
432f72c633
3 changed files with 24 additions and 2 deletions
  1. 1 1
      README.md
  2. 17 0
      translator_test.go
  3. 6 1
      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.11.0-green.svg)
+![Project status](https://img.shields.io/badge/version-0.12.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)

+ 17 - 0
translator_test.go

@@ -688,3 +688,20 @@ func TestVerifyTranslations(t *testing.T) {
 		t.Fatalf("Expected '<nil>' Got '%s'", err)
 	}
 }
+
+func TestGetFallback(t *testing.T) {
+
+	// dutch
+	n := nl.New()
+	e := en.New()
+
+	uni := New(e, n)
+
+	trans := uni.GetFallback()
+
+	expected := "en"
+
+	if trans.Locale() != expected {
+		t.Errorf("Expected '%s' Got '%s'", expected, trans.Locale())
+	}
+}

+ 6 - 1
universal-translator.go

@@ -23,7 +23,7 @@ func New(fallback locales.Translator, supportedLocales ...locales.Translator) *U
 	for _, v := range supportedLocales {
 
 		trans := newTranslator(v)
-		t.translators[strings.ToLower(trans.Locale())] = newTranslator(trans)
+		t.translators[strings.ToLower(trans.Locale())] = trans
 
 		if fallback.Locale() == v.Locale() {
 			t.fallback = trans
@@ -65,6 +65,11 @@ func (t *UniversalTranslator) GetTranslator(locale string) Translator {
 	return t.fallback
 }
 
+// GetFallback returns the fallback locale
+func (t *UniversalTranslator) GetFallback() Translator {
+	return t.fallback
+}
+
 // AddTranslator adds the supplied translator, if it already exists the override param
 // will be checked and if false an error will be returned, otherwise the translator will be
 // overridden; if the fallback matches the supplied translator it will be overridden as well