Skip to content

Commit

Permalink
Fix Localize regression with empty default message
Browse files Browse the repository at this point in the history
Behavior has changed with #189. Before that, an empty default message returned nil and an error, now it returns "" without error.

This has been detected by mattermost test suit in mattermost/mattermost#21327 which tries to switch all code base to go-i18n v2, see https://github.com/mattermost/mattermost/pull/21327/files#r1365634307
  • Loading branch information
KuSh committed Oct 21, 2023
1 parent 5ee16aa commit d88e295
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
6 changes: 5 additions & 1 deletion v2/i18n/localizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,11 @@ func (l *Localizer) getMessageTemplate(id string, defaultMessage *Message) (lang
if defaultMessage == nil {
return language.Und, nil, &MessageNotFoundErr{tag: tag, messageID: id}
}
return tag, NewMessageTemplate(defaultMessage), nil
mt := NewMessageTemplate(defaultMessage)
if mt == nil {
return language.Und, nil, &MessageNotFoundErr{tag: tag, messageID: id}
}
return tag, mt, nil
}

// Fallback to default language in bundle.
Expand Down
9 changes: 9 additions & 0 deletions v2/i18n/localizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,15 @@ func localizerTests() []localizerTest {
},
expectedErr: &MessageNotFoundErr{tag: language.AmericanEnglish, messageID: "Hello"},
},
{
name: "empty default message",
defaultLanguage: language.English,
acceptLangs: []string{"en"},
conf: &LocalizeConfig{
DefaultMessage: &Message{},
},
expectedErr: &MessageNotFoundErr{tag: language.English, messageID: ""},
},
}
}

Expand Down

0 comments on commit d88e295

Please sign in to comment.