From 8e398e5b5eefea492c776ee5370ed8b49ce6cf26 Mon Sep 17 00:00:00 2001 From: kshitij <kshitij.katiyar@brightscout.com> Date: Tue, 28 May 2024 16:08:38 +0530 Subject: [PATCH] Base setup for i18n for server with go-i18n library --- assets/i18n/active.en.json | 3 +++ go.mod | 1 + go.sum | 4 ++++ server/plugin/command.go | 13 +++++++++++-- server/plugin/plugin.go | 9 +++++++++ 5 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 assets/i18n/active.en.json diff --git a/assets/i18n/active.en.json b/assets/i18n/active.en.json new file mode 100644 index 000000000..4fcc1da20 --- /dev/null +++ b/assets/i18n/active.en.json @@ -0,0 +1,3 @@ +{ + "github.command.help.title": "###### Mattermost GitHub Plugin - Slash Command Help\n" +} diff --git a/go.mod b/go.mod index 1fbf422bd..65cd3c74c 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/gorilla/mux v1.8.1 github.com/mattermost/mattermost/server/public v0.0.15 github.com/microcosm-cc/bluemonday v1.0.19 + github.com/nicksnyder/go-i18n/v2 v2.4.0 github.com/pkg/errors v0.9.1 github.com/stretchr/testify v1.8.4 golang.org/x/oauth2 v0.17.0 diff --git a/go.sum b/go.sum index bdfd13e6d..ff3862542 100644 --- a/go.sum +++ b/go.sum @@ -8,6 +8,8 @@ dmitri.shuralyov.com/service/change v0.0.0-20181023043359-a85b471d5412/go.mod h1 dmitri.shuralyov.com/state v0.0.0-20180228185332-28bcc343414c/go.mod h1:0PRwlb0D6DFvNNtx+9ybjezNCa8XF0xaYcETyp6rHWU= git.apache.org/thrift.git v0.0.0-20180902110319-2566ecd5d999/go.mod h1:fPE2ZNJGynbRyZ4dJvy6G277gSllfV2HJqblrnkyeyg= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8= +github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= github.com/Masterminds/goutils v1.1.1 h1:5nUrii3FMTL5diU80unEVvNevw1nH4+ZV4DSLVJLSYI= github.com/Masterminds/goutils v1.1.1/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU= 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 github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/neelance/astrewrite v0.0.0-20160511093645-99348263ae86/go.mod h1:kHJEU3ofeGjhHklVoIGuVj85JJwZ6kWPaJwCIxgnFmo= github.com/neelance/sourcemap v0.0.0-20151028013722-8c68805598ab/go.mod h1:Qr6/a/Q4r9LP1IltGz7tA7iOK1WonHEYhu1HRBA7ZiM= +github.com/nicksnyder/go-i18n/v2 v2.4.0 h1:3IcvPOAvnCKwNm0TB0dLDTuawWEj+ax/RERNC+diLMM= +github.com/nicksnyder/go-i18n/v2 v2.4.0/go.mod h1:nxYSZE9M0bf3Y70gPQjN9ha7XNHX7gMc814+6wVyEI4= github.com/oklog/run v1.1.0 h1:GEenZ1cK0+q0+wsJew9qUg/DyD8k3JzYsZAi5gYi2mA= github.com/oklog/run v1.1.0/go.mod h1:sVPdnTZT1zYwAJeCMu2Th4T21pA3FPOQRfWjQlk7DVU= github.com/openzipkin/zipkin-go v0.1.1/go.mod h1:NtoC/o8u3JlF1lSlyPNswIbeQH9bJTmOf0Erfk+hxe8= diff --git a/server/plugin/command.go b/server/plugin/command.go index dd20c9769..e6ae489a8 100644 --- a/server/plugin/command.go +++ b/server/plugin/command.go @@ -12,6 +12,7 @@ import ( "github.com/mattermost/mattermost/server/public/model" "github.com/mattermost/mattermost/server/public/plugin" "github.com/mattermost/mattermost/server/public/pluginapi/experimental/command" + "github.com/nicksnyder/go-i18n/v2/i18n" ) const ( @@ -609,14 +610,22 @@ func (p *Plugin) handleMe(_ *plugin.Context, _ *model.CommandArgs, _ []string, u return text } -func (p *Plugin) handleHelp(_ *plugin.Context, _ *model.CommandArgs, _ []string, _ *GitHubUserInfo) string { +func (p *Plugin) handleHelp(_ *plugin.Context, args *model.CommandArgs, _ []string, _ *GitHubUserInfo) string { + l := p.b.GetUserLocalizer(args.UserId) message, err := renderTemplate("helpText", p.getConfiguration()) if err != nil { p.client.Log.Warn("Failed to render help template", "error", err.Error()) return "Encountered an error posting help text." } - return "###### Mattermost GitHub Plugin - Slash Command Help\n" + message + helpTitle := p.b.LocalizeWithConfig(l, &i18n.LocalizeConfig{ + DefaultMessage: &i18n.Message{ + ID: "github.command.help.title", + Other: "###### Mattermost GitHub Plugin - Slash Command help\n", + }, + }) + + return helpTitle + message } func (p *Plugin) handleSettings(_ *plugin.Context, _ *model.CommandArgs, parameters []string, userInfo *GitHubUserInfo) string { diff --git a/server/plugin/plugin.go b/server/plugin/plugin.go index d360be6e4..ab0626106 100644 --- a/server/plugin/plugin.go +++ b/server/plugin/plugin.go @@ -23,6 +23,7 @@ import ( "github.com/mattermost/mattermost/server/public/pluginapi/experimental/bot/logger" "github.com/mattermost/mattermost/server/public/pluginapi/experimental/bot/poster" "github.com/mattermost/mattermost/server/public/pluginapi/experimental/telemetry" + "github.com/mattermost/mattermost/server/public/pluginapi/i18n" "github.com/mattermost/mattermost-plugin-github/server/plugin/graphql" ) @@ -95,6 +96,8 @@ type Plugin struct { poster poster.Poster flowManager *FlowManager + b *i18n.Bundle + CommandHandlers map[string]CommandHandleFunc // githubPermalinkRegex is used to parse github permalinks in post messages. @@ -267,6 +270,12 @@ func (p *Plugin) OnActivate() error { p.webhookBroker = NewWebhookBroker(p.sendGitHubPingEvent) p.oauthBroker = NewOAuthBroker(p.sendOAuthCompleteEvent) + i18nBundle, err := i18n.InitBundle(p.API, filepath.Join("assets", "i18n")) + if err != nil { + return err + } + p.b = i18nBundle + botID, err := p.client.Bot.EnsureBot(&model.Bot{ OwnerId: Manifest.Id, // Workaround to support older server version affected by https://github.com/mattermost/mattermost-server/pull/21560 Username: "github",