From ad28c687ed69163afd658c4203abcfeca8136db0 Mon Sep 17 00:00:00 2001 From: Nick Snyder Date: Sun, 18 Nov 2018 11:30:33 -0800 Subject: [PATCH] Add LocalizeWithTag (#139) --- .codecov.yml | 3 +++ v2/i18n/localizer.go | 20 +++++++++++++++----- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/.codecov.yml b/.codecov.yml index 2b606e7b..b90d6daf 100644 --- a/.codecov.yml +++ b/.codecov.yml @@ -1,5 +1,8 @@ coverage: status: + patch: + default: + threshold: 50% project: default: false v1: diff --git a/v2/i18n/localizer.go b/v2/i18n/localizer.go index fb3005c0..38020fd8 100644 --- a/v2/i18n/localizer.go +++ b/v2/i18n/localizer.go @@ -103,10 +103,16 @@ func (e *messageIDMismatchErr) Error() string { // Localize returns a localized message. func (l *Localizer) Localize(lc *LocalizeConfig) (string, error) { + msg, _, err := l.LocalizeWithTag(lc) + return msg, err +} + +// LocalizeWithTag returns a localized message and the language tag. +func (l *Localizer) LocalizeWithTag(lc *LocalizeConfig) (string, language.Tag, error) { messageID := lc.MessageID if lc.DefaultMessage != nil { if messageID != "" && messageID != lc.DefaultMessage.ID { - return "", &messageIDMismatchErr{messageID: messageID, defaultMessageID: lc.DefaultMessage.ID} + return "", language.Und, &messageIDMismatchErr{messageID: messageID, defaultMessageID: lc.DefaultMessage.ID} } messageID = lc.DefaultMessage.ID } @@ -117,7 +123,7 @@ func (l *Localizer) Localize(lc *LocalizeConfig) (string, error) { var err error operands, err = plural.NewOperands(lc.PluralCount) if err != nil { - return "", &invalidPluralCountErr{messageID: messageID, pluralCount: lc.PluralCount, err: err} + return "", language.Und, &invalidPluralCountErr{messageID: messageID, pluralCount: lc.PluralCount, err: err} } if templateData == nil { templateData = map[string]interface{}{ @@ -127,13 +133,17 @@ func (l *Localizer) Localize(lc *LocalizeConfig) (string, error) { } tag, template := l.getTemplate(messageID, lc.DefaultMessage) if template == nil { - return "", &MessageNotFoundErr{messageID: messageID} + return "", language.Und, &MessageNotFoundErr{messageID: messageID} } pluralForm := l.pluralForm(tag, operands) if pluralForm == plural.Invalid { - return "", &pluralizeErr{messageID: messageID, tag: tag} + return "", language.Und, &pluralizeErr{messageID: messageID, tag: tag} + } + msg, err := template.Execute(pluralForm, templateData, lc.Funcs) + if err != nil { + return "", language.Und, err } - return template.Execute(pluralForm, templateData, lc.Funcs) + return msg, tag, nil } func (l *Localizer) getTemplate(id string, defaultMessage *Message) (language.Tag, *internal.MessageTemplate) {