ref: f9e798e8c4234bd60277e3cb10663ba254d4ecb7
parent: ee56efffcb3f81120b0d3e0297b4fb5966124354
author: Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
date: Tue Oct 6 16:32:52 EDT 2020
langs/i18n: Fix i18n .Count regression Fixes #7787
--- a/langs/i18n/i18n.go
+++ b/langs/i18n/i18n.go
@@ -74,13 +74,17 @@
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,
}
+
}
}
@@ -87,6 +91,7 @@
translated, translatedLang, err := localizer.LocalizeWithTag(&i18n.LocalizeConfig{
MessageID: translationID,
TemplateData: templateData,
+ PluralCount: pluralCount,
})
if err == nil && currentLang == translatedLang {
--- a/langs/i18n/i18n_test.go
+++ b/langs/i18n/i18n_test.go
@@ -14,6 +14,7 @@
package i18n
import (
+ "fmt"
"path/filepath"
"testing"
@@ -125,6 +126,35 @@
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 @@
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)
+ })
}
}
}