diff options
Diffstat (limited to 'vendor/github.com/nicksnyder/go-i18n/i18n')
4 files changed, 67 insertions, 2 deletions
diff --git a/vendor/github.com/nicksnyder/go-i18n/i18n/bundle/bundle.go b/vendor/github.com/nicksnyder/go-i18n/i18n/bundle/bundle.go index e93db95d7..8e46fa296 100644 --- a/vendor/github.com/nicksnyder/go-i18n/i18n/bundle/bundle.go +++ b/vendor/github.com/nicksnyder/go-i18n/i18n/bundle/bundle.go @@ -260,6 +260,11 @@ func (b *Bundle) translate(lang *language.Language, translationID string, args . dataMap["Count"] = count data = dataMap } + } else { + dataMap := toMap(data) + if c, ok := dataMap["Count"]; ok { + count = c + } } p, _ := lang.Plural(count) diff --git a/vendor/github.com/nicksnyder/go-i18n/i18n/bundle/bundle_test.go b/vendor/github.com/nicksnyder/go-i18n/i18n/bundle/bundle_test.go index b9c0a0593..b241ad1d4 100644 --- a/vendor/github.com/nicksnyder/go-i18n/i18n/bundle/bundle_test.go +++ b/vendor/github.com/nicksnyder/go-i18n/i18n/bundle/bundle_test.go @@ -270,6 +270,26 @@ func BenchmarkTranslatePluralWithMap(b *testing.B) { } } +func BenchmarkTranslatePluralWithMapAndCountField(b *testing.B) { + data := map[string]interface{}{ + "Person": "Bob", + "Count": 26, + } + + translationTemplate := map[string]interface{}{ + "one": "{{.Person}} is {{.Count}} year old.", + "other": "{{.Person}} is {{.Count}} years old.", + } + expected := "Bob is 26 years old." + + tf := createBenchmarkTranslateFunc(b, translationTemplate, nil, expected) + + b.ResetTimer() + for i := 0; i < b.N; i++ { + tf(data) + } +} + func BenchmarkTranslatePluralWithStruct(b *testing.B) { data := struct{ Person string }{Person: "Bob"} tf := createBenchmarkPluralTranslateFunc(b) diff --git a/vendor/github.com/nicksnyder/go-i18n/i18n/example_test.go b/vendor/github.com/nicksnyder/go-i18n/i18n/example_test.go index d2d9706a7..305c5b3df 100644 --- a/vendor/github.com/nicksnyder/go-i18n/i18n/example_test.go +++ b/vendor/github.com/nicksnyder/go-i18n/i18n/example_test.go @@ -30,6 +30,15 @@ func Example() { fmt.Println(T("person_unread_email_count", 1, bobStruct)) fmt.Println(T("person_unread_email_count", 2, bobStruct)) + type Count struct{ Count int } + fmt.Println(T("your_unread_email_count", Count{0})) + fmt.Println(T("your_unread_email_count", Count{1})) + fmt.Println(T("your_unread_email_count", Count{2})) + + fmt.Println(T("your_unread_email_count", map[string]interface{}{"Count": 0})) + fmt.Println(T("your_unread_email_count", map[string]interface{}{"Count": "1"})) + fmt.Println(T("your_unread_email_count", map[string]interface{}{"Count": "3.14"})) + fmt.Println(T("person_unread_email_count_timeframe", 3, map[string]interface{}{ "Person": "Bob", "Timeframe": T("d_days", 0), @@ -43,6 +52,22 @@ func Example() { "Timeframe": T("d_days", 2), })) + fmt.Println(T("person_unread_email_count_timeframe", 1, map[string]interface{}{ + "Count": 30, + "Person": "Bob", + "Timeframe": T("d_days", 0), + })) + fmt.Println(T("person_unread_email_count_timeframe", 2, map[string]interface{}{ + "Count": 20, + "Person": "Bob", + "Timeframe": T("d_days", 1), + })) + fmt.Println(T("person_unread_email_count_timeframe", 3, map[string]interface{}{ + "Count": 10, + "Person": "Bob", + "Timeframe": T("d_days", 2), + })) + // Output: // Hello world // Hello Bob @@ -57,7 +82,16 @@ func Example() { // Bob has 0 unread emails. // Bob has 1 unread email. // Bob has 2 unread emails. + // You have 0 unread emails. + // You have 1 unread email. + // You have 2 unread emails. + // You have 0 unread emails. + // You have 1 unread email. + // You have 3.14 unread emails. // Bob has 3 unread emails in the past 0 days. // Bob has 3 unread emails in the past 1 day. // Bob has 3 unread emails in the past 2 days. + // Bob has 1 unread email in the past 0 days. + // Bob has 2 unread emails in the past 1 day. + // Bob has 3 unread emails in the past 2 days. } diff --git a/vendor/github.com/nicksnyder/go-i18n/i18n/i18n.go b/vendor/github.com/nicksnyder/go-i18n/i18n/i18n.go index f96842966..c478ff6ea 100644 --- a/vendor/github.com/nicksnyder/go-i18n/i18n/i18n.go +++ b/vendor/github.com/nicksnyder/go-i18n/i18n/i18n.go @@ -69,9 +69,15 @@ import ( // If translationID is a non-plural form, then the first variadic argument may be a map[string]interface{} // or struct that contains template data. // -// If translationID is a plural form, then the first variadic argument must be an integer type +// If translationID is a plural form, the function accepts two parameter signatures +// 1. T(count int, data struct{}) +// The first variadic argument must be an integer type // (int, int8, int16, int32, int64) or a float formatted as a string (e.g. "123.45"). -// The second variadic argument may be a map[string]interface{} or struct that contains template data. +// The second variadic argument may be a map[string]interface{} or struct{} that contains template data. +// 2. T(data struct{}) +// data must be a struct{} or map[string]interface{} that contains a Count field and the template data, +// Count field must be an integer type (int, int8, int16, int32, int64) +// or a float formatted as a string (e.g. "123.45"). type TranslateFunc func(translationID string, args ...interface{}) string // IdentityTfunc returns a TranslateFunc that always returns the translationID passed to it. |