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

Export fields in MessageNotFoundErr (#306) #307

Merged
merged 1 commit into from
Nov 18, 2023
Merged
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
16 changes: 8 additions & 8 deletions v2/i18n/localizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ func (e *invalidPluralCountErr) Error() string {

// MessageNotFoundErr is returned from Localize when a message could not be found.
type MessageNotFoundErr struct {
tag language.Tag
messageID string
Tag language.Tag
MessageID string
}

func (e *MessageNotFoundErr) Error() string {
return fmt.Sprintf("message %q not found in language %q", e.messageID, e.tag)
return fmt.Sprintf("message %q not found in language %q", e.MessageID, e.Tag)
}

type pluralizeErr struct {
Expand Down Expand Up @@ -188,26 +188,26 @@ func (l *Localizer) getMessageTemplate(id string, defaultMessage *Message) (lang

if tag == l.bundle.defaultLanguage {
if defaultMessage == nil {
return language.Und, nil, &MessageNotFoundErr{tag: tag, messageID: id}
return language.Und, nil, &MessageNotFoundErr{Tag: tag, MessageID: id}
}
mt := NewMessageTemplate(defaultMessage)
if mt == nil {
return language.Und, nil, &MessageNotFoundErr{tag: tag, messageID: id}
return language.Und, nil, &MessageNotFoundErr{Tag: tag, MessageID: id}
}
return tag, mt, nil
}

// Fallback to default language in bundle.
mt = l.bundle.getMessageTemplate(l.bundle.defaultLanguage, id)
if mt != nil {
return l.bundle.defaultLanguage, mt, &MessageNotFoundErr{tag: tag, messageID: id}
return l.bundle.defaultLanguage, mt, &MessageNotFoundErr{Tag: tag, MessageID: id}
}

// Fallback to default message.
if defaultMessage == nil {
return language.Und, nil, &MessageNotFoundErr{tag: tag, messageID: id}
return language.Und, nil, &MessageNotFoundErr{Tag: tag, MessageID: id}
}
return l.bundle.defaultLanguage, NewMessageTemplate(defaultMessage), &MessageNotFoundErr{tag: tag, messageID: id}
return l.bundle.defaultLanguage, NewMessageTemplate(defaultMessage), &MessageNotFoundErr{Tag: tag, MessageID: id}
}

func (l *Localizer) pluralForm(tag language.Tag, operands *plural.Operands) plural.Form {
Expand Down
24 changes: 12 additions & 12 deletions v2/i18n/localizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func localizerTests() []localizerTest {
defaultLanguage: language.English,
acceptLangs: []string{"en"},
conf: &LocalizeConfig{MessageID: "HelloWorld"},
expectedErr: &MessageNotFoundErr{tag: language.English, messageID: "HelloWorld"},
expectedErr: &MessageNotFoundErr{Tag: language.English, MessageID: "HelloWorld"},
expectedLocalized: "",
},
{
Expand All @@ -65,7 +65,7 @@ func localizerTests() []localizerTest {
},
acceptLangs: []string{"es"},
conf: &LocalizeConfig{MessageID: "HelloWorld"},
expectedErr: &MessageNotFoundErr{tag: language.Spanish, messageID: "HelloWorld"},
expectedErr: &MessageNotFoundErr{Tag: language.Spanish, MessageID: "HelloWorld"},
expectedLocalized: "Hello World!",
},
{
Expand All @@ -76,15 +76,15 @@ func localizerTests() []localizerTest {
},
acceptLangs: []string{"en"},
conf: &LocalizeConfig{MessageID: "HelloWorld"},
expectedErr: &MessageNotFoundErr{tag: language.English, messageID: "HelloWorld"},
expectedErr: &MessageNotFoundErr{Tag: language.English, MessageID: "HelloWorld"},
expectedLocalized: "",
},
{
name: "missing translations from not default language",
defaultLanguage: language.English,
acceptLangs: []string{"es"},
conf: &LocalizeConfig{MessageID: "HelloWorld"},
expectedErr: &MessageNotFoundErr{tag: language.English, messageID: "HelloWorld"},
expectedErr: &MessageNotFoundErr{Tag: language.English, MessageID: "HelloWorld"},
expectedLocalized: "",
},
{
Expand All @@ -95,7 +95,7 @@ func localizerTests() []localizerTest {
},
acceptLangs: []string{"es"},
conf: &LocalizeConfig{MessageID: "HelloWorld"},
expectedErr: &MessageNotFoundErr{tag: language.Spanish, messageID: "HelloWorld"},
expectedErr: &MessageNotFoundErr{Tag: language.Spanish, MessageID: "HelloWorld"},
expectedLocalized: "",
},
{
Expand All @@ -107,7 +107,7 @@ func localizerTests() []localizerTest {
},
acceptLangs: []string{"es"},
conf: &LocalizeConfig{MessageID: "HelloWorld"},
expectedErr: &MessageNotFoundErr{tag: language.Spanish, messageID: "HelloWorld"},
expectedErr: &MessageNotFoundErr{Tag: language.Spanish, MessageID: "HelloWorld"},
expectedLocalized: "",
},
{
Expand Down Expand Up @@ -533,7 +533,7 @@ func localizerTests() []localizerTest {
conf: &LocalizeConfig{
MessageID: "Hello",
},
expectedErr: &MessageNotFoundErr{tag: language.AmericanEnglish, messageID: "Hello"},
expectedErr: &MessageNotFoundErr{Tag: language.AmericanEnglish, MessageID: "Hello"},
},
{
name: "fallback default message",
Expand All @@ -556,7 +556,7 @@ func localizerTests() []localizerTest {
},
},
expectedLocalized: "Hola!",
expectedErr: &MessageNotFoundErr{tag: language.AmericanEnglish, messageID: "Hello"},
expectedErr: &MessageNotFoundErr{Tag: language.AmericanEnglish, MessageID: "Hello"},
},
{
name: "no fallback default message",
Expand All @@ -575,7 +575,7 @@ func localizerTests() []localizerTest {
conf: &LocalizeConfig{
MessageID: "Hello",
},
expectedErr: &MessageNotFoundErr{tag: language.AmericanEnglish, messageID: "Hello"},
expectedErr: &MessageNotFoundErr{Tag: language.AmericanEnglish, MessageID: "Hello"},
},
{
name: "empty default message",
Expand All @@ -584,7 +584,7 @@ func localizerTests() []localizerTest {
conf: &LocalizeConfig{
DefaultMessage: &Message{},
},
expectedErr: &MessageNotFoundErr{tag: language.English, messageID: ""},
expectedErr: &MessageNotFoundErr{Tag: language.English, MessageID: ""},
},
{
name: "empty default message with id",
Expand All @@ -595,7 +595,7 @@ func localizerTests() []localizerTest {
ID: "Hello",
},
},
expectedErr: &MessageNotFoundErr{tag: language.English, messageID: "Hello"},
expectedErr: &MessageNotFoundErr{Tag: language.English, MessageID: "Hello"},
},
}
}
Expand Down Expand Up @@ -652,7 +652,7 @@ func BenchmarkLocalizer_Localize(b *testing.B) {
}

func TestMessageNotFoundError(t *testing.T) {
actual := (&MessageNotFoundErr{tag: language.AmericanEnglish, messageID: "hello"}).Error()
actual := (&MessageNotFoundErr{Tag: language.AmericanEnglish, MessageID: "hello"}).Error()
expected := `message "hello" not found in language "en-US"`
if actual != expected {
t.Fatalf("expected %q; got %q", expected, actual)
Expand Down
Loading