Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/gohugoio/go-i18n.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Snyder <nickdsnyder@gmail.com>2019-05-10 23:46:52 +0300
committerGitHub <noreply@github.com>2019-05-10 23:46:52 +0300
commit6d605d058e31a71473af9699e17434e85568f659 (patch)
treefe9b36d881609a24af579457305508ff50ad1c3f
parent4cc2a0fd6ae3fe13cc87cf3d4512681c9d8bcdac (diff)
attempt fallback to "other" plural form (#174)v2.0.0-beta.7
-rw-r--r--v2/i18n/localizer.go11
-rw-r--r--v2/i18n/localizer_test.go29
2 files changed, 40 insertions, 0 deletions
diff --git a/v2/i18n/localizer.go b/v2/i18n/localizer.go
index f44b362..5d3b0df 100644
--- a/v2/i18n/localizer.go
+++ b/v2/i18n/localizer.go
@@ -121,6 +121,7 @@ func (l *Localizer) LocalizeMessage(msg *Message) (string, error) {
// }
// LocalizeWithTag returns a localized message and the language tag.
+// It may return a best effort localized message even if an error happens.
func (l *Localizer) LocalizeWithTag(lc *LocalizeConfig) (string, language.Tag, error) {
messageID := lc.MessageID
if lc.DefaultMessage != nil {
@@ -144,16 +145,26 @@ func (l *Localizer) LocalizeWithTag(lc *LocalizeConfig) (string, language.Tag, e
}
}
}
+
tag, template := l.getTemplate(messageID, lc.DefaultMessage)
if template == nil {
return "", language.Und, &MessageNotFoundErr{messageID: messageID}
}
+
pluralForm := l.pluralForm(tag, operands)
if pluralForm == plural.Invalid {
return "", language.Und, &pluralizeErr{messageID: messageID, tag: tag}
}
+
msg, err := template.Execute(pluralForm, templateData, lc.Funcs)
if err != nil {
+ // Attempt to fallback to "Other" pluralization in case translations are incomplete.
+ if pluralForm != plural.Other {
+ msg2, err2 := template.Execute(plural.Other, templateData, lc.Funcs)
+ if err2 == nil {
+ return msg2, tag, err
+ }
+ }
return "", language.Und, err
}
return msg, tag, nil
diff --git a/v2/i18n/localizer_test.go b/v2/i18n/localizer_test.go
index 546293d..d08e970 100644
--- a/v2/i18n/localizer_test.go
+++ b/v2/i18n/localizer_test.go
@@ -4,6 +4,7 @@ import (
"reflect"
"testing"
+ "github.com/nicksnyder/go-i18n/v2/internal/plural"
"golang.org/x/text/language"
)
@@ -267,6 +268,34 @@ func TestLocalizer_Localize(t *testing.T) {
expectedLocalized: "I have 1 cat",
},
{
+ name: "plural count missing one, default message",
+ defaultLanguage: language.English,
+ acceptLangs: []string{"en"},
+ conf: &LocalizeConfig{
+ PluralCount: 1,
+ DefaultMessage: &Message{
+ ID: "Cats",
+ Other: "I have {{.PluralCount}} cats",
+ },
+ },
+ expectedLocalized: "I have 1 cats",
+ expectedErr: pluralFormNotFoundError{messageID: "Cats", pluralForm: plural.One},
+ },
+ {
+ name: "plural count missing other, default message",
+ defaultLanguage: language.English,
+ acceptLangs: []string{"en"},
+ conf: &LocalizeConfig{
+ PluralCount: 2,
+ DefaultMessage: &Message{
+ ID: "Cats",
+ One: "I have {{.PluralCount}} cat",
+ },
+ },
+ expectedLocalized: "",
+ expectedErr: pluralFormNotFoundError{messageID: "Cats", pluralForm: plural.Other},
+ },
+ {
name: "plural count other, default message",
defaultLanguage: language.English,
acceptLangs: []string{"en"},