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

error if message ids mismatch #132

Merged
merged 1 commit into from
Sep 26, 2018
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
12 changes: 12 additions & 0 deletions v2/i18n/localizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,22 @@ func (e *pluralizeErr) Error() string {
return fmt.Sprintf("unable to pluralize %q because there no plural rule for %q", e.messageID, e.tag)
}

type messageIDMismatchErr struct {
messageID string
defaultMessageID string
}

func (e *messageIDMismatchErr) Error() string {
return fmt.Sprintf("message id %q does not match default message id %q", e.messageID, e.defaultMessageID)
}

// Localize returns a localized message.
func (l *Localizer) Localize(lc *LocalizeConfig) (string, error) {
messageID := lc.MessageID
if lc.DefaultMessage != nil {
if messageID != "" && messageID != lc.DefaultMessage.ID {
return "", &messageIDMismatchErr{messageID: messageID, defaultMessageID: lc.DefaultMessage.ID}
}
messageID = lc.DefaultMessage.ID
}

Expand Down
27 changes: 27 additions & 0 deletions v2/i18n/localizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,33 @@ func TestLocalizer_Localize(t *testing.T) {
expectedErr error
expectedLocalized string
}{
{
name: "message id mismatch",
defaultLanguage: language.English,
acceptLangs: []string{"en"},
conf: &LocalizeConfig{
MessageID: "HelloWorld",
DefaultMessage: &Message{
ID: "DefaultHelloWorld",
},
},
expectedErr: &messageIDMismatchErr{messageID: "HelloWorld", defaultMessageID: "DefaultHelloWorld"},
},
{
name: "message id not mismatched",
defaultLanguage: language.English,
messages: map[language.Tag][]*Message{
language.English: {{ID: "HelloWorld", Other: "Hello!"}},
},
acceptLangs: []string{"en"},
conf: &LocalizeConfig{
MessageID: "HelloWorld",
DefaultMessage: &Message{
ID: "HelloWorld",
},
},
expectedLocalized: "Hello!",
},
{
name: "missing translation from default language",
defaultLanguage: language.English,
Expand Down