Skip to content

Commit

Permalink
langs/i18n: Fix i18n .Count regression
Browse files Browse the repository at this point in the history
Fixes #7787
  • Loading branch information
bep committed Oct 6, 2020
1 parent ee56eff commit f9e798e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
5 changes: 5 additions & 0 deletions langs/i18n/i18n.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
46 changes: 39 additions & 7 deletions langs/i18n/i18n_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package i18n

import (
"fmt"
"path/filepath"
"testing"

Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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)
})
}
}
}
Expand Down

0 comments on commit f9e798e

Please sign in to comment.