Skip to content

Commit

Permalink
add i18n support for the start & link commands
Browse files Browse the repository at this point in the history
  • Loading branch information
bugfloyd committed May 2, 2024
1 parent d881e94 commit 1314d32
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
6 changes: 6 additions & 0 deletions bot/common/i18n/en.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,10 @@ var enLocale = LocaleTexts{
MessageOpenedText: "Message opened!",
ReplyingToMessageText: "Replying to message...",
ReplyToThisMessageText: "Reply to this message:",
UserNotFoundText: "User not found! Wrong link?",
MessageToYourselfTextText: "Do you really want to talk to yourself? So sad! Share your link with friends or post it on social media to get anonymous messages!",
LinkText: "Other people can use this link to send you anonymous messages:",
OrText: "or:",
InvalidCommandText: "Invalid command!",
ErrorText: "Error: %s",
}
6 changes: 6 additions & 0 deletions bot/common/i18n/fa.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,10 @@ var faLocale = LocaleTexts{
MessageOpenedText: "پیغام باز شد!",
ReplyingToMessageText: "در حال پاسخ دادن به پیغام...",
ReplyToThisMessageText: "به این پیغام پاسخ بده:",
UserNotFoundText: "کاربر پیدا نشد! لینک اشتباهی؟",
MessageToYourselfTextText: "واقعا می‌خواهی با خودت حرف بزنی؟ چه غمگین! لینک خودت رو با دوستات به اشتراک بذار و یا روی شبکه‌های اجتماعی پست کن تا پیغام‌های ناشناس بگیری!",
LinkText: "افراد دیگر با استفاده از این لینک می‌توانند به شما پیغام ناشناس بفرستند:",
OrText: "یا:",
InvalidCommandText: "دستور نامعتبر!",
ErrorText: "خطا: %s",
}
6 changes: 6 additions & 0 deletions bot/common/i18n/translation.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ const (
MessageOpenedText TextID = "MessageOpenedText"
ReplyingToMessageText TextID = "ReplyingToMessageText"
ReplyToThisMessageText TextID = "ReplyToThisMessageText"
UserNotFoundText TextID = "UserNotFoundText"
MessageToYourselfTextText TextID = "MessageToYourselfTextText"
LinkText TextID = "LinkText"
OrText TextID = "OrText"
InvalidCommandText TextID = "InvalidCommandText"
ErrorText TextID = "ErrorText"
)

type Language string
Expand Down
16 changes: 8 additions & 8 deletions bot/common/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ func (r *RootHandler) start(b *gotgbot.Bot, ctx *ext.Context) error {
}

if err != nil || receiverUser == nil {
_, err = b.SendMessage(ctx.EffectiveChat.Id, "User not found! Wrong link?", &gotgbot.SendMessageOpts{})
_, err = b.SendMessage(ctx.EffectiveChat.Id, i18n.T(i18n.UserNotFoundText), &gotgbot.SendMessageOpts{})
if err != nil {
return fmt.Errorf("failed to send bot info: %w", err)
}
return nil
}

if receiverUser.UUID == r.user.UUID {
_, err = b.SendMessage(ctx.EffectiveChat.Id, "Do you really want to talk to yourself? So sad! Share your link with friends or post it on social media to get anonymous messages!", &gotgbot.SendMessageOpts{})
_, err = b.SendMessage(ctx.EffectiveChat.Id, i18n.T(i18n.MessageToYourselfTextText), &gotgbot.SendMessageOpts{})
if err != nil {
return fmt.Errorf("failed to send bot info: %w", err)
}
Expand All @@ -70,20 +70,20 @@ func (r *RootHandler) start(b *gotgbot.Bot, ctx *ext.Context) error {
var reason string
var keyboard gotgbot.InlineKeyboardMarkup
if blockedBy == Sender {
reason = "You have blocked this user."
reason = i18n.T(i18n.YouHaveBlockedThisUserText)
keyboard = gotgbot.InlineKeyboardMarkup{
InlineKeyboard: [][]gotgbot.InlineKeyboardButton{
{
{
Text: "Unblock",
Text: i18n.T(i18n.UnblockButtonText),
CallbackData: fmt.Sprintf("ub|%s|%d", receiverUser.UUID, 0),
},
},
},
}

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

_, err = ctx.EffectiveMessage.Reply(b, reason, &gotgbot.SendMessageOpts{
Expand Down Expand Up @@ -140,7 +140,7 @@ func (r *RootHandler) getLink(b *gotgbot.Bot, ctx *ext.Context) error {
if r.user.Username != "" {
usernameLink := fmt.Sprintf("https://t.me/%s?start=_%s", b.User.Username, r.user.Username)
uuidLink := fmt.Sprintf("https://t.me/%s?start=%s", b.User.Username, r.user.UUID)
link = fmt.Sprintf("%s\n\nor:\n\n%s", usernameLink, uuidLink)
link = fmt.Sprintf("%s\n%s\n\n%s\n\n%s", i18n.T(i18n.LinkText), usernameLink, i18n.T(i18n.OrText), uuidLink)
} else {
link = fmt.Sprintf("https://t.me/%s?start=%s", b.User.Username, r.user.UUID)
}
Expand All @@ -162,12 +162,12 @@ func (r *RootHandler) processText(b *gotgbot.Bot, ctx *ext.Context) error {
case SettingUsername:
return r.setUsername(b, ctx)
default:
return r.sendError(b, ctx, "Unknown Command!")
return r.sendError(b, ctx, i18n.T(i18n.InvalidCommandText))
}
}

func (r *RootHandler) sendError(b *gotgbot.Bot, ctx *ext.Context, message string) error {
errorMessage := fmt.Sprintf("Error: %s", message)
errorMessage := fmt.Sprintf(i18n.T(i18n.ErrorText), message)
_, err := ctx.EffectiveMessage.Reply(b, errorMessage, nil)
if err != nil {
return fmt.Errorf("failed to send error message: %w", err)
Expand Down

0 comments on commit 1314d32

Please sign in to comment.