mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-28 22:19:59 +02:00
langs/i18n: Fix warning regression in i18n
Fix this by 1. Making sure that only numerical values are treated as plural counts 2. Making sure that `i18n.pluralFormNotFoundError` is not logged as a warning if `other` resolved. Note that 2. isn't a new problem, but became visible because of the plural improvements in Hugo `0.83.0`. Fixes #8492
This commit is contained in:
@@ -14,6 +14,7 @@
|
||||
package i18n
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
@@ -89,10 +90,22 @@ func (t Translator) initFuncs(bndl *i18n.Bundle) {
|
||||
PluralCount: pluralCount,
|
||||
})
|
||||
|
||||
if err == nil && currentLang == translatedLang {
|
||||
sameLang := currentLang == translatedLang
|
||||
|
||||
if err == nil && sameLang {
|
||||
return translated
|
||||
}
|
||||
|
||||
if err != nil && sameLang && translated != "" {
|
||||
// See #8492
|
||||
// TODO(bep) this needs to be improved/fixed upstream,
|
||||
// but currently we get an error even if the fallback to
|
||||
// "other" succeeds.
|
||||
if fmt.Sprintf("%T", err) == "i18n.pluralFormNotFoundError" {
|
||||
return translated
|
||||
}
|
||||
}
|
||||
|
||||
if _, ok := err.(*i18n.MessageNotFoundErr); !ok {
|
||||
t.logger.Warnf("Failed to get translated string for language %q and ID %q: %s", currentLangStr, translationID, err)
|
||||
}
|
||||
@@ -120,9 +133,12 @@ func (c intCount) Count() int {
|
||||
const countFieldName = "Count"
|
||||
|
||||
// getPluralCount gets the plural count as a string (floats) or an integer.
|
||||
// If v is nil, nil is returned.
|
||||
func getPluralCount(v interface{}) interface{} {
|
||||
if v == nil {
|
||||
return 0
|
||||
// i18n called without any argument, make sure it does not
|
||||
// get any plural count.
|
||||
return nil
|
||||
}
|
||||
|
||||
switch v := v.(type) {
|
||||
@@ -171,11 +187,11 @@ func toPluralCountValue(in interface{}) interface{} {
|
||||
return in
|
||||
}
|
||||
// A non-numeric value.
|
||||
return 0
|
||||
return nil
|
||||
default:
|
||||
if i, err := cast.ToIntE(in); err == nil {
|
||||
return i
|
||||
}
|
||||
return 0
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user