langs/i18n: Fix i18n .Count regression

Fixes #7787
This commit is contained in:
Bjørn Erik Pedersen 2020-10-06 20:32:52 +02:00
parent ee56efffcb
commit f9e798e8c4
2 changed files with 44 additions and 7 deletions

View file

@ -74,19 +74,24 @@ func (t Translator) initFuncs(bndl *i18n.Bundle) {
t.translateFuncs[currentLangKey] = func(translationID string, templateData interface{}) string { t.translateFuncs[currentLangKey] = func(translationID string, templateData interface{}) string {
var pluralCount interface{}
if templateData != nil { if templateData != nil {
tp := reflect.TypeOf(templateData) tp := reflect.TypeOf(templateData)
if hreflect.IsNumber(tp.Kind()) { if hreflect.IsNumber(tp.Kind()) {
pluralCount = templateData
// This was how go-i18n worked in v1. // This was how go-i18n worked in v1.
templateData = map[string]interface{}{ templateData = map[string]interface{}{
"Count": templateData, "Count": templateData,
} }
} }
} }
translated, translatedLang, err := localizer.LocalizeWithTag(&i18n.LocalizeConfig{ translated, translatedLang, err := localizer.LocalizeWithTag(&i18n.LocalizeConfig{
MessageID: translationID, MessageID: translationID,
TemplateData: templateData, TemplateData: templateData,
PluralCount: pluralCount,
}) })
if err == nil && currentLang == translatedLang { if err == nil && currentLang == translatedLang {

View file

@ -14,6 +14,7 @@
package i18n package i18n
import ( import (
"fmt"
"path/filepath" "path/filepath"
"testing" "testing"
@ -125,6 +126,35 @@ var i18nTests = []i18nTest{
expected: "¡Hola, 50 gente!", expected: "¡Hola, 50 gente!",
expectedFlag: "¡Hola, 50 gente!", expectedFlag: "¡Hola, 50 gente!",
}, },
// https://github.com/gohugoio/hugo/issues/7787
{
name: "readingTime-one",
data: map[string][]byte{
"en.toml": []byte(`[readingTime]
one = "One minute to read"
other = "{{ .Count }} minutes to read"
`),
},
args: 1,
lang: "en",
id: "readingTime",
expected: "One minute to read",
expectedFlag: "One minute to read",
},
{
name: "readingTime-many",
data: map[string][]byte{
"en.toml": []byte(`[readingTime]
one = "One minute to read"
other = "{{ .Count }} minutes to read"
`),
},
args: 21,
lang: "en",
id: "readingTime",
expected: "21 minutes to read",
expectedFlag: "21 minutes to read",
},
// Same id and translation in current language // Same id and translation in current language
// https://github.com/gohugoio/hugo/issues/2607 // https://github.com/gohugoio/hugo/issues/2607
{ {
@ -242,6 +272,7 @@ func TestI18nTranslate(t *testing.T) {
v.Set("enableMissingTranslationPlaceholders", enablePlaceholders) v.Set("enableMissingTranslationPlaceholders", enablePlaceholders)
for _, test := range i18nTests { for _, test := range i18nTests {
c.Run(fmt.Sprintf("%s-%t", test.name, enablePlaceholders), func(c *qt.C) {
if enablePlaceholders { if enablePlaceholders {
expected = test.expectedFlag expected = test.expectedFlag
} else { } else {
@ -249,6 +280,7 @@ func TestI18nTranslate(t *testing.T) {
} }
actual = doTestI18nTranslate(t, test, v) actual = doTestI18nTranslate(t, test, v)
c.Assert(actual, qt.Equals, expected) c.Assert(actual, qt.Equals, expected)
})
} }
} }
} }