diff --git a/i18n/localizer.go b/i18n/localizer.go index b13160f..a3a2359 100644 --- a/i18n/localizer.go +++ b/i18n/localizer.go @@ -124,7 +124,7 @@ func (l *Localizer) Localize(lc *LocalizeConfig) (string, error) { return msg, err } -// Localize returns a localized message. +// LocalizeMessage returns a localized message. func (l *Localizer) LocalizeMessage(msg *Message) (string, error) { return l.Localize(&LocalizeConfig{ DefaultMessage: msg, @@ -236,3 +236,12 @@ func (l *Localizer) MustLocalize(lc *LocalizeConfig) string { } return localized } + +// MustLocalizeMessage is similar to LocalizeMessage, except it panics if an error happens. +func (l *Localizer) MustLocalizeMessage(msg *Message) string { + localized, err := l.LocalizeMessage(msg) + if err != nil { + panic(err) + } + return localized +} diff --git a/i18n/localizer_test.go b/i18n/localizer_test.go index 5856ec6..7710479 100644 --- a/i18n/localizer_test.go +++ b/i18n/localizer_test.go @@ -768,3 +768,14 @@ func TestMustLocalize(t *testing.T) { MessageID: "hello", }) } + +func TestMustLocalizeMessage(t *testing.T) { + defer func() { + if recover() == nil { + t.Fatalf("MustLocalizeMessage did not panic") + } + }() + bundle := NewBundle(language.English) + localizer := NewLocalizer(bundle) + localizer.MustLocalizeMessage(&Message{}) +}