Skip to content

Commit

Permalink
Add translation for slash command (matterpoll#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
hanzei authored May 28, 2019
1 parent e2f2b80 commit f6c5980
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 44 deletions.
8 changes: 8 additions & 0 deletions assets/i18n/active.de.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
"hash": "sha1-a007d7390f1601bbdc83a397f619335ed591699f",
"other": "Umfragen Bot"
},
"command.autoComplete.desc": {
"hash": "sha1-1cd8e96070c8a1464138423657b6ffef691fab49",
"other": "Erstelle eine Umfrage"
},
"command.autoComplete.hint": {
"hash": "sha1-7355a4eaa6ce5feb9d4b4c0ed10f3af30101bf41",
"other": "\"[Frage]\" \"[Antwort 1]\" \"[Antwort 2]\"..."
},
"command.default.no": {
"hash": "sha1-816c52fd2bdd94a63cd0944823a6c0aa9384c103",
"other": "Nein"
Expand Down
2 changes: 2 additions & 0 deletions assets/i18n/active.en.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"bot.description": "Poll Bot",
"command.autoComplete.desc": "Create a poll",
"command.autoComplete.hint": "\"[Question]\" \"[Answer 1]\" \"[Answer 2]\"...",
"command.default.no": "No",
"command.default.yes": "Yes",
"command.error.generic": "Something went wrong. Please try again later.",
Expand Down
16 changes: 8 additions & 8 deletions server/plugin/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestServeHTTP(t *testing.T) {
api := &plugintest.API{}
api.On("LogDebug", GetMockArgumentsWithType("string", 7)...).Return()
defer api.AssertExpectations(t)
p := setupTestPlugin(t, api, &mockstore.Store{}, testutils.GetSiteURL())
p := setupTestPlugin(t, api, &mockstore.Store{})

w := httptest.NewRecorder()
r := httptest.NewRequest("GET", test.RequestURL, nil)
Expand Down Expand Up @@ -97,7 +97,7 @@ func TestServeFile(t *testing.T) {
api := test.SetupAPI(&plugintest.API{})
api.On("LogDebug", GetMockArgumentsWithType("string", 7)...).Return()
defer api.AssertExpectations(t)
p := setupTestPlugin(t, api, &mockstore.Store{}, testutils.GetSiteURL())
p := setupTestPlugin(t, api, &mockstore.Store{})

w := httptest.NewRecorder()
r := httptest.NewRequest("GET", fmt.Sprintf("/%s", iconFilename), nil)
Expand Down Expand Up @@ -258,7 +258,7 @@ func TestHandleVote(t *testing.T) {
defer api.AssertExpectations(t)
store := test.SetupStore(&mockstore.Store{})
defer store.AssertExpectations(t)
p := setupTestPlugin(t, api, store, testutils.GetSiteURL())
p := setupTestPlugin(t, api, store)

w := httptest.NewRecorder()
r := httptest.NewRequest("POST", fmt.Sprintf("/api/v1/polls/%s/vote/%d", testutils.GetPollID(), test.VoteIndex), bytes.NewReader(test.Request.ToJson()))
Expand Down Expand Up @@ -343,7 +343,7 @@ func TestHandleAddOption(t *testing.T) {
defer api.AssertExpectations(t)
store := test.SetupStore(&mockstore.Store{})
defer store.AssertExpectations(t)
p := setupTestPlugin(t, api, store, testutils.GetSiteURL())
p := setupTestPlugin(t, api, store)

w := httptest.NewRecorder()
r := httptest.NewRequest("POST", fmt.Sprintf("/api/v1/polls/%s/option/add", testutils.GetPollID()), bytes.NewReader(test.Request.ToJson()))
Expand Down Expand Up @@ -439,7 +439,7 @@ func TestHandleAddOptionDialogRequest(t *testing.T) {
defer api.AssertExpectations(t)
store := test.SetupStore(&mockstore.Store{})
defer store.AssertExpectations(t)
p := setupTestPlugin(t, api, store, testutils.GetSiteURL())
p := setupTestPlugin(t, api, store)

w := httptest.NewRecorder()
r := httptest.NewRequest("POST", fmt.Sprintf("/api/v1/polls/%s/option/add/request", testutils.GetPollID()), bytes.NewReader(test.Request.ToJson()))
Expand Down Expand Up @@ -629,7 +629,7 @@ func TestHandleEndPoll(t *testing.T) {
defer api.AssertExpectations(t)
store := test.SetupStore(&mockstore.Store{})
defer store.AssertExpectations(t)
p := setupTestPlugin(t, api, store, testutils.GetSiteURL())
p := setupTestPlugin(t, api, store)

w := httptest.NewRecorder()
r := httptest.NewRequest("POST", fmt.Sprintf("/api/v1/polls/%s/end", testutils.GetPollID()), bytes.NewReader(test.Request.ToJson()))
Expand Down Expand Up @@ -712,7 +712,7 @@ func TestPostEndPollAnnouncement(t *testing.T) {
},
} {
t.Run(name, func(t *testing.T) {
p := setupTestPlugin(t, test.SetupAPI(&plugintest.API{}), &mockstore.Store{}, testutils.GetSiteURL())
p := setupTestPlugin(t, test.SetupAPI(&plugintest.API{}), &mockstore.Store{})
p.postEndPollAnnouncement(test.Request, "Question")
})
}
Expand Down Expand Up @@ -843,7 +843,7 @@ func TestHandleDeletePoll(t *testing.T) {
defer api.AssertExpectations(t)
store := test.SetupStore(&mockstore.Store{})
defer store.AssertExpectations(t)
p := setupTestPlugin(t, api, store, testutils.GetSiteURL())
p := setupTestPlugin(t, api, store)

w := httptest.NewRecorder()
r := httptest.NewRequest("POST", fmt.Sprintf("/api/v1/polls/%s/delete", testutils.GetPollID()), bytes.NewReader(test.Request.ToJson()))
Expand Down
19 changes: 14 additions & 5 deletions server/plugin/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ const (
)

var (
commandAutoCompleteDesc = &i18n.Message{
ID: "command.autoComplete.desc",
Other: "Create a poll",
}
commandAutoCompleteHint = &i18n.Message{
ID: "command.autoComplete.hint",
Other: `"[Question]" "[Answer 1]" "[Answer 2]"...`,
}

commandDefaultYes = &i18n.Message{
ID: "command.default.yes",
Other: "Yes",
Expand Down Expand Up @@ -161,13 +170,13 @@ func (p *MatterpollPlugin) executeCommand(args *model.CommandArgs) (string, *mod
return "", nil
}

func getCommand(trigger string) *model.Command {
func (p *MatterpollPlugin) getCommand(trigger string) *model.Command {
localizer := p.getServerLocalizer()

return &model.Command{
Trigger: trigger,
DisplayName: "Matterpoll",
Description: "Polling feature by https://github.com/matterpoll/matterpoll",
AutoComplete: true,
AutoCompleteDesc: "Create a poll",
AutoCompleteHint: `"[Question]" "[Answer 1]" "[Answer 2]"...`,
AutoCompleteDesc: p.LocalizeDefaultMessage(localizer, commandAutoCompleteDesc),
AutoCompleteHint: p.LocalizeDefaultMessage(localizer, commandAutoCompleteHint),
}
}
2 changes: 1 addition & 1 deletion server/plugin/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func TestPluginExecuteCommand(t *testing.T) {
defer api.AssertExpectations(t)
store := test.SetupStore(&mockstore.Store{})
defer store.AssertExpectations(t)
p := setupTestPlugin(t, api, store, testutils.GetSiteURL())
p := setupTestPlugin(t, api, store)
p.configuration.Trigger = trigger

patch1 := monkey.Patch(model.GetMillis, func() int64 { return 1234567890 })
Expand Down
18 changes: 9 additions & 9 deletions server/plugin/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ func (p *MatterpollPlugin) OnConfigurationChange() error {
return errors.New("Empty trigger not allowed")
}

if oldConfiguration.Trigger != "" {
if err := p.API.UnregisterCommand("", oldConfiguration.Trigger); err != nil {
return errors.Wrap(err, "failed to unregister old command")
}
}
if err := p.API.RegisterCommand(getCommand(configuration.Trigger)); err != nil {
return errors.Wrap(err, "failed to register new command")
}

// This require a loaded i18n bundle
if p.isActivated() {
// Update slash command help text
if oldConfiguration.Trigger != "" {
if err := p.API.UnregisterCommand("", oldConfiguration.Trigger); err != nil {
return errors.Wrap(err, "failed to unregister old command")
}
}
if err := p.API.RegisterCommand(p.getCommand(configuration.Trigger)); err != nil {
return errors.Wrap(err, "failed to register new command")
}
// Update bot description
if err := p.patchBotDescription(); err != nil {
return errors.Wrap(err, "failed to patch bot description")
Expand Down
4 changes: 1 addition & 3 deletions server/plugin/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ func TestOnConfigurationChange(t *testing.T) {
AutoComplete: true,
AutoCompleteDesc: "Create a poll",
AutoCompleteHint: `"[Question]" "[Answer 1]" "[Answer 2]"...`,
DisplayName: "Matterpoll",
Description: "Polling feature by https://github.com/matterpoll/matterpoll",
}

botPatch := &model.BotPatch{
Expand Down Expand Up @@ -137,7 +135,7 @@ func TestOnConfigurationChange(t *testing.T) {

api := test.SetupAPI(&plugintest.API{})
defer api.AssertExpectations(t)
p := setupTestPlugin(t, api, &mockstore.Store{}, testutils.GetSiteURL())
p := setupTestPlugin(t, api, &mockstore.Store{})
p.setConfiguration(test.Configuration)

err := p.OnConfigurationChange()
Expand Down
11 changes: 5 additions & 6 deletions server/plugin/i18n_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ import (

"github.com/mattermost/mattermost-server/plugin/plugintest"
"github.com/matterpoll/matterpoll/server/store/mockstore"
"github.com/matterpoll/matterpoll/server/utils/testutils"
)

func TestLocalizeDefaultMessage(t *testing.T) {
t.Run("fine", func(t *testing.T) {
api := &plugintest.API{}

p := setupTestPlugin(t, api, &mockstore.Store{}, testutils.GetSiteURL())
p := setupTestPlugin(t, api, &mockstore.Store{})
l := p.getServerLocalizer()
m := &i18n.Message{
Other: "test message",
Expand All @@ -28,7 +27,7 @@ func TestLocalizeDefaultMessage(t *testing.T) {
api.On("LogWarn", GetMockArgumentsWithType("string", 3)...).Return()
defer api.AssertExpectations(t)

p := setupTestPlugin(t, api, &mockstore.Store{}, testutils.GetSiteURL())
p := setupTestPlugin(t, api, &mockstore.Store{})
l := p.getServerLocalizer()
m := &i18n.Message{}

Expand All @@ -40,7 +39,7 @@ func TestLocalizeWithConfig(t *testing.T) {
t.Run("fine", func(t *testing.T) {
api := &plugintest.API{}

p := setupTestPlugin(t, api, &mockstore.Store{}, testutils.GetSiteURL())
p := setupTestPlugin(t, api, &mockstore.Store{})
l := p.getServerLocalizer()
lc := &i18n.LocalizeConfig{
DefaultMessage: &i18n.Message{
Expand All @@ -55,7 +54,7 @@ func TestLocalizeWithConfig(t *testing.T) {
api.On("LogWarn", GetMockArgumentsWithType("string", 3)...).Return()
defer api.AssertExpectations(t)

p := setupTestPlugin(t, api, &mockstore.Store{}, testutils.GetSiteURL())
p := setupTestPlugin(t, api, &mockstore.Store{})
l := p.getServerLocalizer()
lc := &i18n.LocalizeConfig{}

Expand All @@ -66,7 +65,7 @@ func TestLocalizeWithConfig(t *testing.T) {
api.On("LogWarn", GetMockArgumentsWithType("string", 3)...).Return()
defer api.AssertExpectations(t)

p := setupTestPlugin(t, api, &mockstore.Store{}, testutils.GetSiteURL())
p := setupTestPlugin(t, api, &mockstore.Store{})
l := p.getServerLocalizer()
lc := &i18n.LocalizeConfig{
DefaultMessage: &i18n.Message{},
Expand Down
16 changes: 4 additions & 12 deletions server/plugin/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,9 @@ import (
"golang.org/x/text/language"
)

func setupTestPlugin(t *testing.T, api *plugintest.API, store *mockstore.Store, siteURL string) *MatterpollPlugin {
defaultServerLocale := "en"
func setupTestPlugin(t *testing.T, api *plugintest.API, store *mockstore.Store) *MatterpollPlugin {
p := &MatterpollPlugin{
ServerConfig: &model.Config{
ServiceSettings: model.ServiceSettings{
SiteURL: &siteURL,
},
LocalizationSettings: model.LocalizationSettings{
DefaultServerLocale: &defaultServerLocale,
},
},
ServerConfig: testutils.GetServerConfig(),
}
p.setConfiguration(&configuration{
Trigger: "poll",
Expand Down Expand Up @@ -281,7 +273,7 @@ func TestPluginOnActivate(t *testing.T) {
func TestPluginOnDeactivate(t *testing.T) {
t.Run("all fine", func(t *testing.T) {
api := &plugintest.API{}
p := setupTestPlugin(t, api, &mockstore.Store{}, testutils.GetSiteURL())
p := setupTestPlugin(t, api, &mockstore.Store{})
api.On("UnregisterCommand", "", p.getConfiguration().Trigger).Return(nil)
defer api.AssertExpectations(t)

Expand All @@ -291,7 +283,7 @@ func TestPluginOnDeactivate(t *testing.T) {

t.Run("UnregisterCommand fails", func(t *testing.T) {
api := &plugintest.API{}
p := setupTestPlugin(t, api, &mockstore.Store{}, testutils.GetSiteURL())
p := setupTestPlugin(t, api, &mockstore.Store{})
api.On("UnregisterCommand", "", p.getConfiguration().Trigger).Return(&model.AppError{})
defer api.AssertExpectations(t)

Expand Down

0 comments on commit f6c5980

Please sign in to comment.