Skip to content

Commit

Permalink
Fix: Translate from Nil error throws error
Browse files Browse the repository at this point in the history
A Nil error cannot be translated, of course, but it should not cause a panic in the library.
  • Loading branch information
vorlif committed Jun 3, 2022
1 parent 147b3d7 commit 883dbfb
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions localizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,10 @@ func (l *Localizer) DNPGetf(domain localize.Domain, context localize.Context, si
// If a suitable translation is found, it will be returned.
// If no matching translation is found, the original string with the matching plural form and an error are returned.
func (l *Localizer) LocalizeWithError(t localize.Localizable) (string, error) {
if t == nil {
return "<nil>", nil
}

var vars []interface{}
if len(t.GetVars()) > 0 {
vars = append(vars, t.GetVars()...)
Expand Down Expand Up @@ -262,6 +266,10 @@ func (l *Localizer) Localize(t localize.Localizable) string {
// By default, this is the context "errors".
// Using WithErrorContext("other") during bundle creation to change the error context for a bundle.
func (l *Localizer) LocalizeError(err error) error {
if err == nil {
return nil
}

switch v := err.(type) {
case localize.Localizable:
translation, errT := l.LocalizeWithError(v)
Expand Down

0 comments on commit 883dbfb

Please sign in to comment.