From 850c94bf0e7818580c919dc8493ce515ab2f95df Mon Sep 17 00:00:00 2001 From: LemonNeko Date: Wed, 20 Mar 2024 20:05:25 +0800 Subject: [PATCH] get language in event --- internal/bots/discord/listeners/listeners.go | 18 +++++++++++------- internal/bots/slack/handlers/handlers.go | 3 +-- internal/services/smr/processor.go | 1 - 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/internal/bots/discord/listeners/listeners.go b/internal/bots/discord/listeners/listeners.go index 9e9b62e..6e079c3 100644 --- a/internal/bots/discord/listeners/listeners.go +++ b/internal/bots/discord/listeners/listeners.go @@ -2,6 +2,7 @@ package listeners import ( "fmt" + "github.com/nekomeowww/insights-bot/pkg/i18n" "strings" "github.com/disgoorg/disgo/discord" @@ -26,11 +27,13 @@ type NewListenersParam struct { Logger *logger.Logger SmrQueue *smrqueue.Queue + I18n *i18n.I18n } type Listeners struct { logger *logger.Logger smrQueue *smrqueue.Queue + i18n *i18n.I18n } func NewListeners() func(param NewListenersParam) *Listeners { @@ -38,6 +41,7 @@ func NewListeners() func(param NewListenersParam) *Listeners { return &Listeners{ logger: param.Logger, smrQueue: param.SmrQueue, + i18n: param.I18n, } } } @@ -52,14 +56,15 @@ func (b *Listeners) smrCmd(event *events.ApplicationCommandInteractionCreate, da b.logger.Info(fmt.Sprintf("discord: command received: /smr %s", urlString)) + lang := event.Locale().String() + // url check err, originErr := smr.CheckUrl(urlString) if err != nil { if smr.IsUrlCheckError(err) { err = event.CreateMessage( discord.NewMessageCreateBuilder(). - // TODO: i18n support for discord - SetContent(smr.FormatUrlCheckError(err, bot.FromPlatformDiscord, "", nil)). + SetContent(smr.FormatUrlCheckError(err, bot.FromPlatformDiscord, lang, nil)). Build(), ) if err != nil { @@ -69,7 +74,7 @@ func (b *Listeners) smrCmd(event *events.ApplicationCommandInteractionCreate, da return } - err = event.CreateMessage(discord.NewMessageCreateBuilder().SetContent("出现了一些问题,可以再试试?").Build()) + err = event.CreateMessage(discord.NewMessageCreateBuilder().SetContent(b.i18n.TWithLanguage(lang, "commands.groups.summarization.commands.smr.failedToRead")).Build()) if err != nil { b.logger.Warn("discord: failed to send error message", zap.Error(err), zap.NamedError("original_error", originErr)) } @@ -79,7 +84,7 @@ func (b *Listeners) smrCmd(event *events.ApplicationCommandInteractionCreate, da // must reply the interaction as soon as possible err = event.CreateMessage(discord.NewMessageCreateBuilder(). - SetContent("请稍等,量子速读中..."). + SetContent(b.i18n.TWithLanguage(lang, "commands.groups.summarization.commands.smr.reading")). Build()) if err != nil { b.logger.Warn("discord: failed to send response message", zap.Error(err)) @@ -90,13 +95,12 @@ func (b *Listeners) smrCmd(event *events.ApplicationCommandInteractionCreate, da Platform: bot.FromPlatformDiscord, URL: urlString, ChannelID: event.Channel().ID().String(), - // TODO: support i18n for discord - Language: "zh-CN", + Language: lang, }) if err != nil { b.logger.Warn("discord: failed to add task", zap.Error(err)) - err = event.CreateMessage(discord.NewMessageCreateBuilder().SetContent("出现了一些问题,可以再试试?").Build()) + err = event.CreateMessage(discord.NewMessageCreateBuilder().SetContent("commands.groups.summarization.commands.smr.failedToRead").Build()) if err != nil { b.logger.Warn("discord: failed to send error message", zap.Error(err)) } diff --git a/internal/bots/slack/handlers/handlers.go b/internal/bots/slack/handlers/handlers.go index 72b5728..f0cafb7 100644 --- a/internal/bots/slack/handlers/handlers.go +++ b/internal/bots/slack/handlers/handlers.go @@ -153,8 +153,7 @@ func (h *Handlers) PostCommandInfo(ctx *gin.Context) { URL: urlString, ChannelID: body.ChannelID, TeamID: body.TeamID, - // TODO: support i18n for discord - Language: "zh-CN", + Language: user.Locale, }) if err != nil { h.logger.Warn("slack: failed to add task", zap.Error(err)) diff --git a/internal/services/smr/processor.go b/internal/services/smr/processor.go index a832d61..5777695 100644 --- a/internal/services/smr/processor.go +++ b/internal/services/smr/processor.go @@ -174,7 +174,6 @@ func (s *Service) processor(info types.TaskInfo) { smrResult, err := s.model.SummarizeInputURL(ctx, info.URL, info.Platform) if err != nil { s.logger.Warn("smr service: summarization failed", zap.Error(err)) - // TODO: support i18n for discord errStr := s.processError(err, lo.Ternary(info.Language == "", "en", info.Language)) s.sendResult(nil, info, errStr)