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 {
var pluralCount interface{}
if templateData != nil {
tp := reflect.TypeOf(templateData)
if hreflect.IsNumber(tp.Kind()) {
pluralCount = templateData
// This was how go-i18n worked in v1.
templateData = map[string]interface{}{
"Count": templateData,
}
}
}
translated, translatedLang, err := localizer.LocalizeWithTag(&i18n.LocalizeConfig{
MessageID: translationID,
TemplateData: templateData,
PluralCount: pluralCount,
})
if err == nil && currentLang == translatedLang {

View file

@ -14,6 +14,7 @@
package i18n
import (
"fmt"
"path/filepath"
"testing"
@ -125,6 +126,35 @@ var i18nTests = []i18nTest{
expected: "¡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
// https://github.com/gohugoio/hugo/issues/2607
{
@ -242,13 +272,15 @@ func TestI18nTranslate(t *testing.T) {
v.Set("enableMissingTranslationPlaceholders", enablePlaceholders)
for _, test := range i18nTests {
if enablePlaceholders {
expected = test.expectedFlag
} else {
expected = test.expected
}
actual = doTestI18nTranslate(t, test, v)
c.Assert(actual, qt.Equals, expected)
c.Run(fmt.Sprintf("%s-%t", test.name, enablePlaceholders), func(c *qt.C) {
if enablePlaceholders {
expected = test.expectedFlag
} else {
expected = test.expected
}
actual = doTestI18nTranslate(t, test, v)
c.Assert(actual, qt.Equals, expected)
})
}
}
}