Skip to content

Commit 8e398e5

Browse files
Base setup for i18n for server with go-i18n library
1 parent e958064 commit 8e398e5

File tree

5 files changed

+28
-2
lines changed

5 files changed

+28
-2
lines changed

assets/i18n/active.en.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"github.command.help.title": "###### Mattermost GitHub Plugin - Slash Command Help\n"
3+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
github.com/gorilla/mux v1.8.1
99
github.com/mattermost/mattermost/server/public v0.0.15
1010
github.com/microcosm-cc/bluemonday v1.0.19
11+
github.com/nicksnyder/go-i18n/v2 v2.4.0
1112
github.com/pkg/errors v0.9.1
1213
github.com/stretchr/testify v1.8.4
1314
golang.org/x/oauth2 v0.17.0

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ dmitri.shuralyov.com/service/change v0.0.0-20181023043359-a85b471d5412/go.mod h1
88
dmitri.shuralyov.com/state v0.0.0-20180228185332-28bcc343414c/go.mod h1:0PRwlb0D6DFvNNtx+9ybjezNCa8XF0xaYcETyp6rHWU=
99
git.apache.org/thrift.git v0.0.0-20180902110319-2566ecd5d999/go.mod h1:fPE2ZNJGynbRyZ4dJvy6G277gSllfV2HJqblrnkyeyg=
1010
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
11+
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
12+
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
1113
github.com/Masterminds/goutils v1.1.1 h1:5nUrii3FMTL5diU80unEVvNevw1nH4+ZV4DSLVJLSYI=
1214
github.com/Masterminds/goutils v1.1.1/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
1315
github.com/Masterminds/semver/v3 v3.1.1 h1:hLg3sBzpNErnxhQtUy/mmLR2I9foDujNK030IGemrRc=
@@ -154,6 +156,8 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJ
154156
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
155157
github.com/neelance/astrewrite v0.0.0-20160511093645-99348263ae86/go.mod h1:kHJEU3ofeGjhHklVoIGuVj85JJwZ6kWPaJwCIxgnFmo=
156158
github.com/neelance/sourcemap v0.0.0-20151028013722-8c68805598ab/go.mod h1:Qr6/a/Q4r9LP1IltGz7tA7iOK1WonHEYhu1HRBA7ZiM=
159+
github.com/nicksnyder/go-i18n/v2 v2.4.0 h1:3IcvPOAvnCKwNm0TB0dLDTuawWEj+ax/RERNC+diLMM=
160+
github.com/nicksnyder/go-i18n/v2 v2.4.0/go.mod h1:nxYSZE9M0bf3Y70gPQjN9ha7XNHX7gMc814+6wVyEI4=
157161
github.com/oklog/run v1.1.0 h1:GEenZ1cK0+q0+wsJew9qUg/DyD8k3JzYsZAi5gYi2mA=
158162
github.com/oklog/run v1.1.0/go.mod h1:sVPdnTZT1zYwAJeCMu2Th4T21pA3FPOQRfWjQlk7DVU=
159163
github.com/openzipkin/zipkin-go v0.1.1/go.mod h1:NtoC/o8u3JlF1lSlyPNswIbeQH9bJTmOf0Erfk+hxe8=

server/plugin/command.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/mattermost/mattermost/server/public/model"
1313
"github.com/mattermost/mattermost/server/public/plugin"
1414
"github.com/mattermost/mattermost/server/public/pluginapi/experimental/command"
15+
"github.com/nicksnyder/go-i18n/v2/i18n"
1516
)
1617

1718
const (
@@ -609,14 +610,22 @@ func (p *Plugin) handleMe(_ *plugin.Context, _ *model.CommandArgs, _ []string, u
609610
return text
610611
}
611612

612-
func (p *Plugin) handleHelp(_ *plugin.Context, _ *model.CommandArgs, _ []string, _ *GitHubUserInfo) string {
613+
func (p *Plugin) handleHelp(_ *plugin.Context, args *model.CommandArgs, _ []string, _ *GitHubUserInfo) string {
614+
l := p.b.GetUserLocalizer(args.UserId)
613615
message, err := renderTemplate("helpText", p.getConfiguration())
614616
if err != nil {
615617
p.client.Log.Warn("Failed to render help template", "error", err.Error())
616618
return "Encountered an error posting help text."
617619
}
618620

619-
return "###### Mattermost GitHub Plugin - Slash Command Help\n" + message
621+
helpTitle := p.b.LocalizeWithConfig(l, &i18n.LocalizeConfig{
622+
DefaultMessage: &i18n.Message{
623+
ID: "github.command.help.title",
624+
Other: "###### Mattermost GitHub Plugin - Slash Command help\n",
625+
},
626+
})
627+
628+
return helpTitle + message
620629
}
621630

622631
func (p *Plugin) handleSettings(_ *plugin.Context, _ *model.CommandArgs, parameters []string, userInfo *GitHubUserInfo) string {

server/plugin/plugin.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/mattermost/mattermost/server/public/pluginapi/experimental/bot/logger"
2424
"github.com/mattermost/mattermost/server/public/pluginapi/experimental/bot/poster"
2525
"github.com/mattermost/mattermost/server/public/pluginapi/experimental/telemetry"
26+
"github.com/mattermost/mattermost/server/public/pluginapi/i18n"
2627

2728
"github.com/mattermost/mattermost-plugin-github/server/plugin/graphql"
2829
)
@@ -95,6 +96,8 @@ type Plugin struct {
9596
poster poster.Poster
9697
flowManager *FlowManager
9798

99+
b *i18n.Bundle
100+
98101
CommandHandlers map[string]CommandHandleFunc
99102

100103
// githubPermalinkRegex is used to parse github permalinks in post messages.
@@ -267,6 +270,12 @@ func (p *Plugin) OnActivate() error {
267270
p.webhookBroker = NewWebhookBroker(p.sendGitHubPingEvent)
268271
p.oauthBroker = NewOAuthBroker(p.sendOAuthCompleteEvent)
269272

273+
i18nBundle, err := i18n.InitBundle(p.API, filepath.Join("assets", "i18n"))
274+
if err != nil {
275+
return err
276+
}
277+
p.b = i18nBundle
278+
270279
botID, err := p.client.Bot.EnsureBot(&model.Bot{
271280
OwnerId: Manifest.Id, // Workaround to support older server version affected by https://github.com/mattermost/mattermost-server/pull/21560
272281
Username: "github",

0 commit comments

Comments
 (0)