From 54617eff8617774f6c2deea248cf2f131781f43e Mon Sep 17 00:00:00 2001 From: Nicolas Le Cam Date: Mon, 23 Oct 2023 16:51:58 +0200 Subject: [PATCH] Fix Localize regression with empty default message (#305) 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 https://github.com/mattermost/mattermost/pull/21327 which tries to switch all code base to go-i18n v2, see [related discussion](https://github.com/mattermost/mattermost/pull/21327/files#r1365634307) --- v2/i18n/localizer.go | 6 +++++- v2/i18n/localizer_test.go | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/v2/i18n/localizer.go b/v2/i18n/localizer.go index 256cd494..9895fcbd 100644 --- a/v2/i18n/localizer.go +++ b/v2/i18n/localizer.go @@ -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. diff --git a/v2/i18n/localizer_test.go b/v2/i18n/localizer_test.go index 3dedfb67..dba7a141 100644 --- a/v2/i18n/localizer_test.go +++ b/v2/i18n/localizer_test.go @@ -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"}, + }, } }