Skip to content

Commit f9e798e

Browse files
committed
langs/i18n: Fix i18n .Count regression
Fixes #7787
1 parent ee56eff commit f9e798e

File tree

2 files changed

+44
-7
lines changed

2 files changed

+44
-7
lines changed

langs/i18n/i18n.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,19 +74,24 @@ func (t Translator) initFuncs(bndl *i18n.Bundle) {
7474

7575
t.translateFuncs[currentLangKey] = func(translationID string, templateData interface{}) string {
7676

77+
var pluralCount interface{}
78+
7779
if templateData != nil {
7880
tp := reflect.TypeOf(templateData)
7981
if hreflect.IsNumber(tp.Kind()) {
82+
pluralCount = templateData
8083
// This was how go-i18n worked in v1.
8184
templateData = map[string]interface{}{
8285
"Count": templateData,
8386
}
87+
8488
}
8589
}
8690

8791
translated, translatedLang, err := localizer.LocalizeWithTag(&i18n.LocalizeConfig{
8892
MessageID: translationID,
8993
TemplateData: templateData,
94+
PluralCount: pluralCount,
9095
})
9196

9297
if err == nil && currentLang == translatedLang {

langs/i18n/i18n_test.go

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package i18n
1515

1616
import (
17+
"fmt"
1718
"path/filepath"
1819
"testing"
1920

@@ -125,6 +126,35 @@ var i18nTests = []i18nTest{
125126
expected: "¡Hola, 50 gente!",
126127
expectedFlag: "¡Hola, 50 gente!",
127128
},
129+
// https://github.com/gohugoio/hugo/issues/7787
130+
{
131+
name: "readingTime-one",
132+
data: map[string][]byte{
133+
"en.toml": []byte(`[readingTime]
134+
one = "One minute to read"
135+
other = "{{ .Count }} minutes to read"
136+
`),
137+
},
138+
args: 1,
139+
lang: "en",
140+
id: "readingTime",
141+
expected: "One minute to read",
142+
expectedFlag: "One minute to read",
143+
},
144+
{
145+
name: "readingTime-many",
146+
data: map[string][]byte{
147+
"en.toml": []byte(`[readingTime]
148+
one = "One minute to read"
149+
other = "{{ .Count }} minutes to read"
150+
`),
151+
},
152+
args: 21,
153+
lang: "en",
154+
id: "readingTime",
155+
expected: "21 minutes to read",
156+
expectedFlag: "21 minutes to read",
157+
},
128158
// Same id and translation in current language
129159
// https://github.com/gohugoio/hugo/issues/2607
130160
{
@@ -242,13 +272,15 @@ func TestI18nTranslate(t *testing.T) {
242272
v.Set("enableMissingTranslationPlaceholders", enablePlaceholders)
243273

244274
for _, test := range i18nTests {
245-
if enablePlaceholders {
246-
expected = test.expectedFlag
247-
} else {
248-
expected = test.expected
249-
}
250-
actual = doTestI18nTranslate(t, test, v)
251-
c.Assert(actual, qt.Equals, expected)
275+
c.Run(fmt.Sprintf("%s-%t", test.name, enablePlaceholders), func(c *qt.C) {
276+
if enablePlaceholders {
277+
expected = test.expectedFlag
278+
} else {
279+
expected = test.expected
280+
}
281+
actual = doTestI18nTranslate(t, test, v)
282+
c.Assert(actual, qt.Equals, expected)
283+
})
252284
}
253285
}
254286
}

0 commit comments

Comments
 (0)