Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add convenience function MustLocalizeMessage #352

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion i18n/localizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
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,
Expand Down Expand Up @@ -236,3 +236,12 @@
}
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

Check warning on line 246 in i18n/localizer.go

View check run for this annotation

Codecov / codecov/patch

i18n/localizer.go#L246

Added line #L246 was not covered by tests
}
11 changes: 11 additions & 0 deletions i18n/localizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
}
Loading