Skip to content

Commit

Permalink
add i18n support for the username module
Browse files Browse the repository at this point in the history
  • Loading branch information
bugfloyd committed May 2, 2024
1 parent 1314d32 commit 178e510
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 16 deletions.
13 changes: 13 additions & 0 deletions bot/common/i18n/en.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,17 @@ var enLocale = LocaleTexts{
OrText: "or:",
InvalidCommandText: "Invalid command!",
ErrorText: "Error: %s",
YourCurrentUsernameText: "Your current username is: %s",
ChangeUsernameButtonText: "Change",
RemoveUsernameButtonText: "Remove",
YouDontHaveAUsernameText: "You don't have a username!",
SetUsernameButtonText: "Set one",
UsernameExplanationText: "Choose a 3 to 20 characters long username which may contain letters, numbers, or underscores (_) and starts with a letter. Usernames are automatically converted to lowercase.",
EnterANewUsernameText: "Enter a new username:",
SettingUsernameText: "Setting username...",
UsernameHasBeenRemovedText: "Username has been removed!",
InvalidUsernameText: "The entered username is not valid. Enter another one:",
UsernameHasBeenSetText: "Username has been set: %s",
UsernameExistsText: "The entered username exists. Enter another one:",
SameUsernameText: "You already own this username silly! If you want to change it, run the username command once more!",
}
13 changes: 13 additions & 0 deletions bot/common/i18n/fa.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,17 @@ var faLocale = LocaleTexts{
OrText: "یا:",
InvalidCommandText: "دستور نامعتبر!",
ErrorText: "خطا: %s",
YourCurrentUsernameText: "نام کاربری فعلی شما: %s",
ChangeUsernameButtonText: "تغییر بده",
RemoveUsernameButtonText: "حذف کن",
YouDontHaveAUsernameText: "شما نام کاربری ندارید!",
SetUsernameButtonText: "یکی انتخاب کن",
UsernameExplanationText: "یک نام کاربری انتخاب کنید که ۳ الی ۲۰ کارکتر داشته باشد. فقط شامل حروف و اعداد انگلیسی و یا آندرلاین (_) باشد و با یک حرف شروع شود. نام‌های کاربری بصورت اتوماتیک به حروف کوچک تبدیل می‌شوند.",
EnterANewUsernameText: "یک نام کاربری جدید وارد کنید:",
SettingUsernameText: "در حال تنظیم نام کاربری...",
UsernameHasBeenRemovedText: "نام کاربری پاک شد!",
InvalidUsernameText: "نام کاربری وارد شده نامعتبر است. یکی دیگر انتخاب کنید:",
UsernameHasBeenSetText: "نام کاربری اعمال شد: %s",
UsernameExistsText: "نام کاربری وارد شده موجود نمی‌باشد. یکی دیگر وارد کنید:",
SameUsernameText: "تو همین الان این نام کاربری رو داری خنگول! اگه می خواهی تغییرش بدی، دستور نام کاربری رو یک بار دیگه اجرا کن!",
}
13 changes: 13 additions & 0 deletions bot/common/i18n/translation.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@ const (
OrText TextID = "OrText"
InvalidCommandText TextID = "InvalidCommandText"
ErrorText TextID = "ErrorText"
YourCurrentUsernameText TextID = "YourCurrentUsernameText"
ChangeUsernameButtonText TextID = "ChangeUsernameButtonText"
RemoveUsernameButtonText TextID = "RemoveUsernameButtonText"
YouDontHaveAUsernameText TextID = "YouDontHaveAUsernameText"
SetUsernameButtonText TextID = "SetUsernameButtonText"
UsernameExplanationText TextID = "UsernameExplanationText"
EnterANewUsernameText TextID = "EnterANewUsernameText"
SettingUsernameText TextID = "SettingUsernameText"
UsernameHasBeenRemovedText TextID = "UsernameHasBeenRemovedText"
InvalidUsernameText TextID = "InvalidUsernameText"
UsernameHasBeenSetText TextID = "UsernameHasBeenSetText"
UsernameExistsText TextID = "UsernameExistsText"
SameUsernameText TextID = "SameUsernameText"
)

type Language string
Expand Down
33 changes: 17 additions & 16 deletions bot/common/username.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"
"regexp"
"strings"
)
Expand All @@ -13,33 +14,33 @@ func (r *RootHandler) manageUsername(b *gotgbot.Bot, ctx *ext.Context) error {
var buttons [][]gotgbot.InlineKeyboardButton

if r.user.Username != "" {
text = fmt.Sprintf("Your current username is: %s", r.user.Username)
text = fmt.Sprintf(i18n.T(i18n.YourCurrentUsernameText), r.user.Username)
buttons = [][]gotgbot.InlineKeyboardButton{
{
{
Text: "Change",
Text: i18n.T(i18n.ChangeUsernameButtonText),
CallbackData: "u",
},
{
Text: "Remove",
Text: i18n.T(i18n.RemoveUsernameButtonText),
CallbackData: "ru",
},
{
Text: "Cancel",
Text: i18n.T(i18n.CancelButtonText),
CallbackData: "cu",
},
},
}
} else {
text = "You don't have a username!"
text = i18n.T(i18n.YouDontHaveAUsernameText)
buttons = [][]gotgbot.InlineKeyboardButton{
{
{
Text: "Set one",
Text: i18n.T(i18n.SetUsernameButtonText),
CallbackData: "u",
},
{
Text: "Cancel",
Text: i18n.T(i18n.CancelButtonText),
CallbackData: "cu",
},
},
Expand Down Expand Up @@ -70,7 +71,7 @@ func (r *RootHandler) usernameCallback(b *gotgbot.Bot, ctx *ext.Context, action
if action == "CANCEL" {
// Send callback answer to telegram
_, err = cb.Answer(b, &gotgbot.AnswerCallbackQueryOpts{
Text: "Never mind!",
Text: i18n.T(i18n.NeverMindButtonText),
})
if err != nil {
return fmt.Errorf("failed to answer callback: %w", err)
Expand All @@ -86,14 +87,14 @@ func (r *RootHandler) usernameCallback(b *gotgbot.Bot, ctx *ext.Context, action
}

// Send reply instruction
_, err = ctx.EffectiveMessage.Reply(b, "Create a username that starts with a letter, includes 3-20 characters, and may contain letters, numbers, or underscores (_). Usernames are automatically converted to lowercase. \n\nEnter new username:", nil)
_, err = ctx.EffectiveMessage.Reply(b, fmt.Sprintf("%s\n\n%s", i18n.T(i18n.UsernameExplanationText), i18n.T(i18n.EnterANewUsernameText)), nil)
if err != nil {
return fmt.Errorf("failed to send reply message: %w", err)
}

// Send callback answer to telegram
_, err = cb.Answer(b, &gotgbot.AnswerCallbackQueryOpts{
Text: "Setting username...",
Text: i18n.T(i18n.SettingUsernameText),
})
if err != nil {
return fmt.Errorf("failed to answer callback: %w", err)
Expand All @@ -109,14 +110,14 @@ func (r *RootHandler) usernameCallback(b *gotgbot.Bot, ctx *ext.Context, action
return fmt.Errorf("failed to remove username: %w", err)
}

_, _, err = cb.Message.EditText(b, "Username has been removed!", &gotgbot.EditMessageTextOpts{})
_, _, err = cb.Message.EditText(b, i18n.T(i18n.UsernameHasBeenRemovedText), &gotgbot.EditMessageTextOpts{})
if err != nil {
return fmt.Errorf("failed to update username message text: %w", err)
}

// Send callback answer to telegram
_, err = cb.Answer(b, &gotgbot.AnswerCallbackQueryOpts{
Text: "Username removed!",
Text: i18n.T(i18n.UsernameHasBeenRemovedText),
})
if err != nil {
return fmt.Errorf("failed to answer callback: %w", err)
Expand All @@ -131,7 +132,7 @@ func (r *RootHandler) setUsername(b *gotgbot.Bot, ctx *ext.Context) error {

if isValidUsername(username) == false {
// Send username instruction
_, err := ctx.EffectiveMessage.Reply(b, "The entered username is not valid. Enter another one:", nil)
_, err := ctx.EffectiveMessage.Reply(b, i18n.T(i18n.InvalidUsernameText), nil)
if err != nil {
return fmt.Errorf("failed to send reply message: %w", err)
}
Expand All @@ -154,16 +155,16 @@ func (r *RootHandler) setUsername(b *gotgbot.Bot, ctx *ext.Context) error {
}

// Send username instruction
_, err = ctx.EffectiveMessage.Reply(b, fmt.Sprintf("Username has been set: %s", username), nil)
_, err = ctx.EffectiveMessage.Reply(b, fmt.Sprintf(i18n.T(i18n.UsernameHasBeenSetText), username), nil)
if err != nil {
return fmt.Errorf("failed to send reply message: %w", err)
}
} else {
var text string
if existingUser.UUID != r.user.UUID {
text = "The entered username exists. Enter another one:"
text = i18n.T(i18n.UsernameExistsText)
} else {
text = "You already own this username silly! If you want to change it, run the username command once more!"
text = i18n.T(i18n.SameUsernameText)

// Reset sender user
err = r.userRepo.resetUserState(r.user.UUID)
Expand Down

0 comments on commit 178e510

Please sign in to comment.