Skip to content

Commit

Permalink
add i18n support for the messaging module
Browse files Browse the repository at this point in the history
  • Loading branch information
bugfloyd committed May 2, 2024
1 parent 39c9956 commit d881e94
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 13 deletions.
8 changes: 8 additions & 0 deletions bot/common/i18n/en.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,12 @@ var enLocale = LocaleTexts{
NoPreferredLanguageSetText: "You don't have a preferred language yet.",
NeverMindButtonText: "Never mind!",
LanguageUpdatedSuccessfullyText: "Language updated successfully to English.",
YouHaveBlockedThisUserText: "You have blocked this user.",
ThisUserHasBlockedYouText: "This user has blocked you.",
YouHaveANewMessageText: "You have a new message.",
NewReplyToYourMessageText: "New reply to your message",
OpenMessageButtonText: "Open Message",
MessageOpenedText: "Message opened!",
ReplyingToMessageText: "Replying to message...",
ReplyToThisMessageText: "Reply to this message:",
}
8 changes: 8 additions & 0 deletions bot/common/i18n/fa.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,12 @@ var faLocale = LocaleTexts{
NoPreferredLanguageSetText: "شما زبان ترجیح داده شده‌ای ندارید",
NeverMindButtonText: "بیخیال!",
LanguageUpdatedSuccessfullyText: "زبان با موفقیت به فارسی تغییر پیدا کرد.",
YouHaveBlockedThisUserText: "شما این کاربر را بلاک کرده‌اید",
ThisUserHasBlockedYouText: "این کاربر شما را بلاک کرده است",
YouHaveANewMessageText: "شما یک پیغام جدید دارید.",
NewReplyToYourMessageText: "پاسخ جدید به پیغام شما",
OpenMessageButtonText: "پیغام را باز کن",
MessageOpenedText: "پیغام باز شد!",
ReplyingToMessageText: "در حال پاسخ دادن به پیغام...",
ReplyToThisMessageText: "به این پیغام پاسخ بده:",
}
8 changes: 8 additions & 0 deletions bot/common/i18n/translation.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ const (
NoPreferredLanguageSetText TextID = "NoPreferredLanguageSetText"
NeverMindButtonText TextID = "NeverMindButtonText"
LanguageUpdatedSuccessfullyText TextID = "LanguageUpdatedSuccessfullyText"
YouHaveBlockedThisUserText TextID = "YouHaveBlockedThisUserText"
ThisUserHasBlockedYouText TextID = "ThisUserHasBlockedYouText"
YouHaveANewMessageText TextID = "YouHaveANewMessageText"
NewReplyToYourMessageText TextID = "NewReplyToYourMessageText"
OpenMessageButtonText TextID = "OpenMessageButtonText"
MessageOpenedText TextID = "MessageOpenedText"
ReplyingToMessageText TextID = "ReplyingToMessageText"
ReplyToThisMessageText TextID = "ReplyToThisMessageText"
)

type Language string
Expand Down
27 changes: 14 additions & 13 deletions bot/common/messaging.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/PaulSonOfLars/gotgbot/v2"
"github.com/PaulSonOfLars/gotgbot/v2/ext"
"github.com/bugfloyd/anonymous-telegram-bot/common/i18n"
"strconv"
"strings"
)
Expand All @@ -19,9 +20,9 @@ func (r *RootHandler) sendAnonymousMessage(b *gotgbot.Bot, ctx *ext.Context) err
if blockedBy != None {
var reason string
if blockedBy == Sender {
reason = "You have blocked this user."
reason = i18n.T(i18n.YouHaveBlockedThisUserText)
} else if blockedBy == Receiver {
reason = "This user has blocked you."
reason = i18n.T(i18n.ThisUserHasBlockedYouText)
}
_, err = ctx.EffectiveMessage.Reply(b, reason, nil)
if err != nil {
Expand All @@ -38,14 +39,14 @@ func (r *RootHandler) sendAnonymousMessage(b *gotgbot.Bot, ctx *ext.Context) err
}

var replyParameters *gotgbot.ReplyParameters
msgText := "You have a new message."
msgText := i18n.T(i18n.YouHaveANewMessageText)
if r.user.ReplyMessageID != 0 {
replyParameters = &gotgbot.ReplyParameters{
MessageId: r.user.ReplyMessageID,
AllowSendingWithoutReply: true,
}

msgText = "New reply to your message."
msgText = i18n.T(i18n.NewReplyToYourMessageText)
}

// React with sent emoji to senderMessageID
Expand All @@ -67,7 +68,7 @@ func (r *RootHandler) sendAnonymousMessage(b *gotgbot.Bot, ctx *ext.Context) err
InlineKeyboard: [][]gotgbot.InlineKeyboardButton{
{
{
Text: "Open Message",
Text: i18n.T(i18n.OpenMessageButtonText),
CallbackData: fmt.Sprintf("o|%s|%d", r.user.UUID, ctx.EffectiveMessage.MessageId),
},
},
Expand Down Expand Up @@ -102,7 +103,7 @@ func (r *RootHandler) openCallback(b *gotgbot.Bot, ctx *ext.Context) error {

// Send callback answer to telegram
_, err = cb.Answer(b, &gotgbot.AnswerCallbackQueryOpts{
Text: "Message opened!",
Text: i18n.T(i18n.MessageOpenedText),
})
if err != nil {
return fmt.Errorf("failed to answer callback: %w", err)
Expand All @@ -123,11 +124,11 @@ func (r *RootHandler) openCallback(b *gotgbot.Bot, ctx *ext.Context) error {
InlineKeyboard: [][]gotgbot.InlineKeyboardButton{
{
{
Text: "Reply",
Text: i18n.T(i18n.ReplyButtonText),
CallbackData: fmt.Sprintf("r|%s|%d", sender.UUID, senderMessageID),
},
{
Text: "Block",
Text: i18n.T(i18n.BlockButtonText),
CallbackData: fmt.Sprintf("b|%s|%d", sender.UUID, senderMessageID),
},
},
Expand Down Expand Up @@ -187,13 +188,13 @@ func (r *RootHandler) replyCallback(b *gotgbot.Bot, ctx *ext.Context) error {
if blockedBy != None {
var reason string
if blockedBy == Sender {
reason = "You have blocked this user."
reason = i18n.T(i18n.YouHaveBlockedThisUserText)
_, _, err = cb.Message.EditReplyMarkup(b, &gotgbot.EditMessageReplyMarkupOpts{
ReplyMarkup: gotgbot.InlineKeyboardMarkup{
InlineKeyboard: [][]gotgbot.InlineKeyboardButton{
{
{
Text: "Unblock",
Text: i18n.T(i18n.UnblockButtonText),
CallbackData: fmt.Sprintf("ub|%s|%d", receiverUUID, messageID),
},
},
Expand All @@ -206,7 +207,7 @@ func (r *RootHandler) replyCallback(b *gotgbot.Bot, ctx *ext.Context) error {
}

} else if blockedBy == Receiver {
reason = "This user has blocked you."
reason = i18n.T(i18n.ThisUserHasBlockedYouText)
}

_, err = cb.Answer(b, &gotgbot.AnswerCallbackQueryOpts{
Expand All @@ -232,14 +233,14 @@ func (r *RootHandler) replyCallback(b *gotgbot.Bot, ctx *ext.Context) error {

// Send callback answer to telegram
_, err = cb.Answer(b, &gotgbot.AnswerCallbackQueryOpts{
Text: "Replying to message...",
Text: i18n.T(i18n.ReplyingToMessageText),
})
if err != nil {
return fmt.Errorf("failed to answer callback: %w", err)
}

// Send reply instruction
_, err = ctx.EffectiveMessage.Reply(b, "Reply to this message:", nil)
_, err = ctx.EffectiveMessage.Reply(b, i18n.T(i18n.ReplyToThisMessageText), nil)
if err != nil {
return fmt.Errorf("failed to send reply message: %w", err)
}
Expand Down

0 comments on commit d881e94

Please sign in to comment.