Skip to content

Commit

Permalink
Fix Localize regression with empty default message (#305)
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 [related discussion](https://github.com/mattermost/mattermost/pull/21327/files#r1365634307)
  • Loading branch information
KuSh authored Oct 23, 2023
1 parent 5ee16aa commit 54617ef
Show file tree
Hide file tree
Showing 2 changed files with 25 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
20 changes: 20 additions & 0 deletions v2/i18n/localizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,26 @@ 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: ""},
},
{
name: "empty default message with id",
defaultLanguage: language.English,
acceptLangs: []string{"en"},
conf: &LocalizeConfig{
DefaultMessage: &Message{
ID: "Hello",
},
},
expectedErr: &MessageNotFoundErr{tag: language.English, messageID: "Hello"},
},
}
}

Expand Down

0 comments on commit 54617ef

Please sign in to comment.