Skip to content

Commit

Permalink
Add LocalizeWithTag (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicksnyder authored Nov 18, 2018
1 parent 849c441 commit ad28c68
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .codecov.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
coverage:
status:
patch:
default:
threshold: 50%
project:
default: false
v1:
Expand Down
20 changes: 15 additions & 5 deletions v2/i18n/localizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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{}{
Expand All @@ -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) {
Expand Down

0 comments on commit ad28c68

Please sign in to comment.