Skip to content

Commit

Permalink
Add more translations (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
bugfloyd authored May 1, 2024
2 parents 1c0f649 + b6ae115 commit 39c9956
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 25 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ jobs:
- name: Checkout code
uses: actions/checkout@v4

- name: Run tests
run: |
cd bot
go test ./... -v
- name: Build
run: |
cd bot
Expand Down
15 changes: 8 additions & 7 deletions bot/common/block.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"
"slices"
"strings"
)
Expand All @@ -24,7 +25,7 @@ func (r *RootHandler) blockCallback(b *gotgbot.Bot, ctx *ext.Context) error {
}

_, err = cb.Answer(b, &gotgbot.AnswerCallbackQueryOpts{
Text: "User blocked!",
Text: i18n.T(i18n.UserBlockedText),
})
if err != nil {
return fmt.Errorf("failed to answer callback: %w", err)
Expand All @@ -35,7 +36,7 @@ func (r *RootHandler) blockCallback(b *gotgbot.Bot, ctx *ext.Context) error {
InlineKeyboard: [][]gotgbot.InlineKeyboardButton{
{
{
Text: "Unblock",
Text: i18n.T(i18n.UnblockButtonText),
CallbackData: fmt.Sprintf("ub|%s|%s", receiverUUID, replyMessageID),
},
},
Expand Down Expand Up @@ -66,7 +67,7 @@ func (r *RootHandler) unBlockCallback(b *gotgbot.Bot, ctx *ext.Context) error {
}

_, err = cb.Answer(b, &gotgbot.AnswerCallbackQueryOpts{
Text: "User unblocked!",
Text: i18n.T(i18n.UserUnblockedText),
})
if err != nil {
return fmt.Errorf("failed to answer callback: %w", err)
Expand All @@ -75,12 +76,12 @@ func (r *RootHandler) unBlockCallback(b *gotgbot.Bot, ctx *ext.Context) error {
var replyMessageKey gotgbot.InlineKeyboardButton
if replyMessageID == "0" {
replyMessageKey = gotgbot.InlineKeyboardButton{
Text: "Send Message",
Text: i18n.T(i18n.SendMessageButtonText),
CallbackData: fmt.Sprintf("r|%s|%s", receiverUUID, replyMessageID),
}
} else {
replyMessageKey = gotgbot.InlineKeyboardButton{
Text: "Reply",
Text: i18n.T(i18n.ReplyButtonText),
CallbackData: fmt.Sprintf("r|%s|%s", receiverUUID, replyMessageID),
}
}
Expand All @@ -91,7 +92,7 @@ func (r *RootHandler) unBlockCallback(b *gotgbot.Bot, ctx *ext.Context) error {
{
replyMessageKey,
{
Text: "Block",
Text: i18n.T(i18n.BlockButtonText),
CallbackData: fmt.Sprintf("b|%s|%s", receiverUUID, replyMessageID),
},
},
Expand All @@ -112,7 +113,7 @@ func (r *RootHandler) unBlockAll(b *gotgbot.Bot, ctx *ext.Context) error {
return fmt.Errorf("failed to unblock all users: %w", err)
}

_, err = ctx.EffectiveMessage.Reply(b, "All users unblocked!", nil)
_, err = ctx.EffectiveMessage.Reply(b, i18n.T(i18n.UnblockAllUsersResultText), nil)
if err != nil {
return fmt.Errorf("failed to send bot info: %w", err)
}
Expand Down
16 changes: 14 additions & 2 deletions bot/common/i18n/en.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
package i18n

var enLocale = LocaleTexts{
StartMessage: "Welcome! Use /link command to get you link!",
InitialSendMessagePrompt: "You are sending message to:\n%s\n\nEnter your message:",
StartMessageText: "Welcome! Use /link command to get you link!",
InitialSendMessagePromptText: "You are sending message to:\n%s\n\nEnter your message:",
UnblockButtonText: "Unblock",
SendMessageButtonText: "Send message",
ReplyButtonText: "Reply",
BlockButtonText: "Block",
UnblockAllUsersResultText: "All users unblocked!",
UserBlockedText: "User blocked!",
UserUnblockedText: "User unblocked!",
CancelButtonText: "Cancel",
YourLanguageText: "Your language is: %s",
NoPreferredLanguageSetText: "You don't have a preferred language yet.",
NeverMindButtonText: "Never mind!",
LanguageUpdatedSuccessfullyText: "Language updated successfully to English.",
}
16 changes: 14 additions & 2 deletions bot/common/i18n/fa.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
package i18n

var faLocale = LocaleTexts{
StartMessage: "خوش اومدی! از دستور /link برای دریافت لینک ناشناس خودت استفاده بکن",
InitialSendMessagePrompt: "در حال ارسال پیام ناشناس به %s هستید.\n\n پیغام خود را بنویسید:",
StartMessageText: "خوش اومدی! از دستور /link برای دریافت لینک ناشناس خودت استفاده بکن",
InitialSendMessagePromptText: "در حال ارسال پیام ناشناس به %s هستید.\n\n پیغام خود را بنویسید:",
UnblockButtonText: "آنبلاک",
SendMessageButtonText: "ارسال پیغام",
ReplyButtonText: "پاسخ بده",
BlockButtonText: "بلاک کن",
UnblockAllUsersResultText: "تمامی کاربران آنبلاک شدند!",
UserBlockedText: "کاربر بلاک شد!",
UserUnblockedText: "کاربر آنبلاک شد!",
CancelButtonText: "لغو",
YourLanguageText: "زبان انتخابی شما: %s",
NoPreferredLanguageSetText: "شما زبان ترجیح داده شده‌ای ندارید",
NeverMindButtonText: "بیخیال!",
LanguageUpdatedSuccessfullyText: "زبان با موفقیت به فارسی تغییر پیدا کرد.",
}
47 changes: 46 additions & 1 deletion bot/common/i18n/i18n_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package i18n

import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"strings"
"testing"
)

// TestCompleteTranslations ensures that each language has all required text IDs translated.
func TestCompleteTranslations(t *testing.T) {
requiredTextIDs := []TextID{StartMessage, InitialSendMessagePrompt}
requiredTextIDs, err := getTextsList()
if err != nil {
t.Errorf("Error reading the text identifers list: %s", err)
}
languages := []Language{EnUS, FaIR}

for _, lang := range languages {
Expand All @@ -25,3 +33,40 @@ func TestCompleteTranslations(t *testing.T) {
}
}
}

func getTextsList() ([]TextID, error) {
filePath := "./translation.go"

// Create a new token file set
fset := token.NewFileSet()

// Parse the file
node, err := parser.ParseFile(fset, filePath, nil, parser.AllErrors)
if err != nil {
return nil, fmt.Errorf("error parsing file: %s", err)
}

var ids []TextID

// Use ast.Inspect to traverse the AST and find constants
ast.Inspect(node, func(n ast.Node) bool {
// Check for general declarations (var, const, type)
genDecl, ok := n.(*ast.GenDecl)
if ok && genDecl.Tok == token.CONST {
for _, spec := range genDecl.Specs {
// Assertion for ValueSpec since constants belong to this category
valSpec, ok := spec.(*ast.ValueSpec)
if ok {
for _, name := range valSpec.Names {
if strings.HasSuffix(name.Name, "Text") {
ids = append(ids, TextID(name.Name))
}
}
}
}
}
return true // Continue to traverse the AST
})

return ids, nil
}
16 changes: 14 additions & 2 deletions bot/common/i18n/translation.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,20 @@ import (
type TextID string

const (
StartMessage TextID = "StartMessage"
InitialSendMessagePrompt TextID = "welcome"
StartMessageText TextID = "StartMessageText"
InitialSendMessagePromptText TextID = "InitialSendMessagePromptText"
UnblockButtonText TextID = "UnblockButtonText"
SendMessageButtonText TextID = "SendMessageButtonText"
ReplyButtonText TextID = "ReplyButtonText"
BlockButtonText TextID = "BlockButtonText"
UnblockAllUsersResultText TextID = "UnblockAllUsersResultText"
UserBlockedText TextID = "UserBlockedText"
UserUnblockedText TextID = "UserUnblockedText"
CancelButtonText TextID = "CancelButtonText"
YourLanguageText TextID = "YourLanguageText"
NoPreferredLanguageSetText TextID = "NoPreferredLanguageSetText"
NeverMindButtonText TextID = "NeverMindButtonText"
LanguageUpdatedSuccessfullyText TextID = "LanguageUpdatedSuccessfullyText"
)

type Language string
Expand Down
22 changes: 13 additions & 9 deletions bot/common/language.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
func (r *RootHandler) manageLanguage(b *gotgbot.Bot, ctx *ext.Context) error {
var text string
if r.user.Language != "" {
text = fmt.Sprintf("Your language is: %s", r.user.Language)
text = fmt.Sprintf(i18n.T(i18n.YourLanguageText), r.user.Language)
} else {
text = "You don't have a preferred language yet."
text = i18n.T(i18n.NoPreferredLanguageSetText)
}
_, err := b.SendMessage(ctx.EffectiveChat.Id, text, &gotgbot.SendMessageOpts{
ReplyMarkup: gotgbot.InlineKeyboardMarkup{
Expand All @@ -24,19 +24,19 @@ func (r *RootHandler) manageLanguage(b *gotgbot.Bot, ctx *ext.Context) error {
CallbackData: fmt.Sprintf("l|%s", i18n.EnUS),
},
{
Text: "Farsi",
Text: "فارسی",
CallbackData: fmt.Sprintf("l|%s", i18n.FaIR),
},
{
Text: "Cancel",
Text: i18n.T(i18n.CancelButtonText),
CallbackData: "lc",
},
},
},
},
})
if err != nil {
return fmt.Errorf("failed to send lkanguage info: %w", err)
return fmt.Errorf("failed to send language info: %w", err)
}

return nil
Expand All @@ -54,7 +54,7 @@ func (r *RootHandler) languageCallback(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 @@ -66,9 +66,11 @@ func (r *RootHandler) languageCallback(b *gotgbot.Bot, ctx *ext.Context, action
}

var isLanguageValid = false
var language i18n.Language
for _, lang := range []i18n.Language{i18n.EnUS, i18n.FaIR} {
if split[1] == string(lang) {
isLanguageValid = true
language = lang
break
}
}
Expand All @@ -81,21 +83,23 @@ func (r *RootHandler) languageCallback(b *gotgbot.Bot, ctx *ext.Context, action
"State": Idle,
"ContactUUID": nil,
"ReplyMessageID": nil,
"Language": split[1],
"Language": language,
})
if err != nil {
return fmt.Errorf("failed to update user language: %w", err)
}
i18n.SetLocale(language)

// Send update status
_, err = ctx.EffectiveMessage.Reply(b, fmt.Sprintf("Language updated successfully to %s", split[1]), nil)
_, err = ctx.EffectiveMessage.Reply(b, i18n.T(i18n.LanguageUpdatedSuccessfullyText), nil)
if err != nil {
return fmt.Errorf("failed to send language update message: %w", err)
}

// Send callback answer to telegram
_, err = cb.Answer(b, &gotgbot.AnswerCallbackQueryOpts{
Text: "Language updated!",
Text: i18n.T(i18n.LanguageUpdatedSuccessfullyText),
ShowAlert: false,
})
if err != nil {
return fmt.Errorf("failed to answer callback: %w", err)
Expand Down
4 changes: 2 additions & 2 deletions bot/common/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (r *RootHandler) start(b *gotgbot.Bot, ctx *ext.Context) error {
return err
}

_, err = b.SendMessage(ctx.EffectiveChat.Id, i18n.T(i18n.StartMessage), &gotgbot.SendMessageOpts{})
_, err = b.SendMessage(ctx.EffectiveChat.Id, i18n.T(i18n.StartMessageText), &gotgbot.SendMessageOpts{})
if err != nil {
return fmt.Errorf("failed to send bot info: %w", err)
}
Expand Down Expand Up @@ -114,7 +114,7 @@ func (r *RootHandler) start(b *gotgbot.Bot, ctx *ext.Context) error {
identity = receiverUser.UUID
}

_, err = b.SendMessage(ctx.EffectiveChat.Id, fmt.Sprintf(i18n.T(i18n.InitialSendMessagePrompt), identity), &gotgbot.SendMessageOpts{})
_, err = b.SendMessage(ctx.EffectiveChat.Id, fmt.Sprintf(i18n.T(i18n.InitialSendMessagePromptText), identity), &gotgbot.SendMessageOpts{})
if err != nil {
return fmt.Errorf("failed to send bot info: %w", err)
}
Expand Down
2 changes: 2 additions & 0 deletions bot/webserver.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build manual

package main

import (
Expand Down

0 comments on commit 39c9956

Please sign in to comment.