diff options
author | Bjørn Erik Pedersen <[email protected]> | 2020-10-06 20:32:52 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <[email protected]> | 2020-10-07 00:32:21 +0200 |
commit | f9e798e8c4234bd60277e3cb10663ba254d4ecb7 (patch) | |
tree | 681eadfa8008206e9251716cf3141a3bfc4ce24f | |
parent | ee56efffcb3f81120b0d3e0297b4fb5966124354 (diff) | |
download | hugo-f9e798e8c4234bd60277e3cb10663ba254d4ecb7.tar.gz hugo-f9e798e8c4234bd60277e3cb10663ba254d4ecb7.zip |
langs/i18n: Fix i18n .Count regression
Fixes #7787
-rw-r--r-- | langs/i18n/i18n.go | 5 | ||||
-rw-r--r-- | langs/i18n/i18n_test.go | 46 |
2 files changed, 44 insertions, 7 deletions
diff --git a/langs/i18n/i18n.go b/langs/i18n/i18n.go index 922b06367..83144b89c 100644 --- a/langs/i18n/i18n.go +++ b/langs/i18n/i18n.go @@ -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 { diff --git a/langs/i18n/i18n_test.go b/langs/i18n/i18n_test.go index d9215952a..51134a7d0 100644 --- 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 @@ 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) + }) } } } |