Phase 7: languages
- Complete Spanish (neutral Latin American, Paraguay friendly, no inverted marks) and Dutch catalogs, including the Mini App and owner texts - Command menus per language in three scopes (private, group members, group admins); per-chat menus follow a user's /language and a group's /grouplang, from chat and from the Mini App - Bot name, description and short description per language, sent only when the catalog text changes (TELEGRAM_SET_PROFILE) - Dates from the catalog, numbers, sizes and durations per locale - Pseudo-locale en-XA generated from English (I18N_PSEUDO), owners only - Catalog checks: completeness, placeholders, tags, commands, plurals, Telegram length limits, untranslated text, unused and missing IDs - CI workflow; make i18n-check; docs/TRANSLATING.md - Test fix: the queued-download helper could match another chat's status Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
4f11f939a0
commit
f1f9c6156c
+14
-18
@@ -263,22 +263,25 @@ func (b *Bot) cmdGroupLang(ctx context.Context, m *telego.Message, act actor) {
|
||||
return
|
||||
}
|
||||
arg := strings.ToLower(strings.TrimSpace(commandArgs(m)))
|
||||
usage := i18n.Args{"Langs": strings.Join(b.visibleLangs(act.user.ID), ", ")}
|
||||
changed, newLang := false, ""
|
||||
b.changeSettings(ctx, m, act, func(st *db.GroupSettings) (string, i18n.Args) {
|
||||
if arg == "" {
|
||||
return "!grouplang.usage", i18n.Args{"Langs": strings.Join(b.tr.Langs(), ", ")}
|
||||
return "!grouplang.usage", usage
|
||||
}
|
||||
if arg == "auto" {
|
||||
st.Lang = ""
|
||||
st.Lang, changed = "", true
|
||||
return "grouplang.auto", nil
|
||||
}
|
||||
for _, l := range b.tr.Langs() {
|
||||
if l == arg {
|
||||
st.Lang = l
|
||||
return "grouplang.set", i18n.Args{"Lang": b.tr.T(l, "language.name", nil)}
|
||||
}
|
||||
if l := b.findLang(arg, act.user.ID); l != "" {
|
||||
st.Lang, changed, newLang = l, true, l
|
||||
return "grouplang.set", i18n.Args{"Lang": b.tr.T(l, "language.name", nil)}
|
||||
}
|
||||
return "!grouplang.usage", i18n.Args{"Langs": strings.Join(b.tr.Langs(), ", ")}
|
||||
return "!grouplang.usage", usage
|
||||
})
|
||||
if changed {
|
||||
b.SyncGroupMenu(ctx, m.Chat.ID, newLang)
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Bot) cmdAutoDetect(ctx context.Context, m *telego.Message, act actor) {
|
||||
@@ -566,10 +569,7 @@ func (b *Bot) cbSetQuality(ctx context.Context, q *telego.CallbackQuery, act act
|
||||
func (b *Bot) cmdLanguage(ctx context.Context, m *telego.Message, act actor) {
|
||||
var row []telego.InlineKeyboardButton
|
||||
var rows [][]telego.InlineKeyboardButton
|
||||
for _, l := range b.tr.Langs() {
|
||||
if b.tr.Hidden(l) {
|
||||
continue
|
||||
}
|
||||
for _, l := range b.visibleLangs(act.user.ID) {
|
||||
// Each language is listed under its own name.
|
||||
row = append(row, button(b.tr.T(l, "language.name", nil), cbData(cbLanguage, l)))
|
||||
if len(row) == 3 {
|
||||
@@ -584,12 +584,7 @@ func (b *Bot) cmdLanguage(ctx context.Context, m *telego.Message, act actor) {
|
||||
func (b *Bot) cbSetLanguage(ctx context.Context, q *telego.CallbackQuery, act actor, value string) {
|
||||
lang := ""
|
||||
if value != "auto" {
|
||||
for _, l := range b.tr.Langs() {
|
||||
if l == value {
|
||||
lang = l
|
||||
}
|
||||
}
|
||||
if lang == "" {
|
||||
if lang = b.findLang(value, act.user.ID); lang == "" {
|
||||
b.answer(ctx, q, b.tr.T(act.lang, "notice.expired", nil), false)
|
||||
return
|
||||
}
|
||||
@@ -602,6 +597,7 @@ func (b *Bot) cbSetLanguage(ctx context.Context, q *telego.CallbackQuery, act ac
|
||||
if shown == "" {
|
||||
shown = b.tr.Resolve(act.user.Lang)
|
||||
}
|
||||
b.SyncUserMenu(ctx, act.user.ID, lang)
|
||||
b.answer(ctx, q, b.tr.T(shown, "notice.saved", nil), false)
|
||||
if q.Message != nil {
|
||||
_ = b.edit(ctx, q.Message.GetChat().ID, int64(q.Message.GetMessageID()), false,
|
||||
|
||||
+173
-17
@@ -2,31 +2,187 @@ package bot
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/mymmrac/telego"
|
||||
)
|
||||
|
||||
// menuCommands are the commands shown in Telegram's command menu, in
|
||||
// order. Names are lowercase ASCII in every language; only descriptions
|
||||
// are translated, under "menu.<name>".
|
||||
var menuCommands = []string{"dl", "audio", "thumb", "info", "queue", "cancel", "history", "account", "buy", "help"}
|
||||
// The command menus. Names are lowercase ASCII in every language; only the
|
||||
// descriptions are translated, under "menu.<name>". Each list is published
|
||||
// for every language, in three scopes: private chats, group members and
|
||||
// group admins. Owner commands are never listed.
|
||||
var (
|
||||
privateMenu = []string{
|
||||
"dl", "audio", "thumb", "subs", "info", "queue", "cancel", "history",
|
||||
"account", "buy", "credits", "limits", "redeem", "referral", "donate",
|
||||
"quality", "language", "app", "support", "privacy", "terms", "help",
|
||||
}
|
||||
groupMenu = []string{"dl", "audio", "thumb", "info", "queue", "cancel", "groupwallet", "grouppass", "app", "help"}
|
||||
adminMenu = append(append([]string{}, groupMenu...),
|
||||
"settings", "grouplang", "autodetect", "allow", "block", "spending", "groupstats")
|
||||
)
|
||||
|
||||
// RegisterCommands publishes the command menu. A failure is logged and
|
||||
// otherwise ignored: the bot works without a menu.
|
||||
func (b *Bot) menu(lang string, names []string) []telego.BotCommand {
|
||||
cmds := make([]telego.BotCommand, 0, len(names))
|
||||
for _, name := range names {
|
||||
if name == "app" && !b.cfg.MiniAppEnabled() {
|
||||
continue
|
||||
}
|
||||
cmds = append(cmds, telego.BotCommand{Command: name, Description: b.tr.T(lang, "menu."+name, nil)})
|
||||
}
|
||||
return cmds
|
||||
}
|
||||
|
||||
// menuLangs are the languages menus and profile texts are published for.
|
||||
// Telegram takes a two-letter code only, so a regional catalog (es-PY)
|
||||
// shares its base language's slot, and the pseudo-locale is left out.
|
||||
func (b *Bot) menuLangs() []string {
|
||||
var out []string
|
||||
seen := map[string]bool{}
|
||||
for _, l := range b.tr.Langs() {
|
||||
if b.tr.Hidden(l) || len(l) != 2 || seen[l] {
|
||||
continue
|
||||
}
|
||||
seen[l] = true
|
||||
out = append(out, l)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// RegisterCommands publishes the command menus and the bot's profile texts
|
||||
// in every language. The source language is also the default for clients
|
||||
// in languages without a catalog. A failure is logged and otherwise
|
||||
// ignored: the bot works without a menu.
|
||||
func (b *Bot) RegisterCommands(ctx context.Context) {
|
||||
for _, lang := range b.tr.Langs() {
|
||||
cmds := make([]telego.BotCommand, 0, len(menuCommands))
|
||||
for _, name := range menuCommands {
|
||||
cmds = append(cmds, telego.BotCommand{Command: name, Description: b.tr.T(lang, "menu."+name, nil)})
|
||||
scopes := []struct {
|
||||
scope telego.BotCommandScope
|
||||
names []string
|
||||
}{
|
||||
{&telego.BotCommandScopeAllPrivateChats{Type: telego.ScopeTypeAllPrivateChats}, privateMenu},
|
||||
{&telego.BotCommandScopeAllGroupChats{Type: telego.ScopeTypeAllGroupChats}, groupMenu},
|
||||
{&telego.BotCommandScopeAllChatAdministrators{Type: telego.ScopeTypeAllChatAdministrators}, adminMenu},
|
||||
}
|
||||
for i, lang := range b.menuLangs() {
|
||||
code := lang
|
||||
if i == 0 {
|
||||
code = "" // the default menu
|
||||
}
|
||||
params := &telego.SetMyCommandsParams{Commands: cmds}
|
||||
if lang != b.tr.Langs()[0] {
|
||||
// The source language is the default menu; the others are
|
||||
// registered for their own language code.
|
||||
params.LanguageCode = lang
|
||||
for _, s := range scopes {
|
||||
err := b.api.SetMyCommands(ctx, &telego.SetMyCommandsParams{
|
||||
Commands: b.menu(lang, s.names), Scope: s.scope, LanguageCode: code,
|
||||
})
|
||||
if err != nil {
|
||||
b.log.Warn("setMyCommands failed", "lang", lang, "scope", s.scope.ScopeType(), "err", err)
|
||||
}
|
||||
}
|
||||
if err := b.api.SetMyCommands(ctx, params); err != nil {
|
||||
b.log.Warn("setMyCommands failed", "lang", lang, "err", err)
|
||||
if b.cfg.Telegram.SetProfile {
|
||||
b.setProfile(ctx, lang, code)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// setProfile sets the bot's name and descriptions for one language. These
|
||||
// calls are rate limited hard by Telegram, so they are made only when the
|
||||
// catalog text changed since the last time they went through.
|
||||
func (b *Bot) setProfile(ctx context.Context, lang, code string) {
|
||||
name := b.tr.T(lang, "profile.name", nil)
|
||||
about := b.tr.T(lang, "profile.about", nil)
|
||||
short := b.tr.T(lang, "profile.short", nil)
|
||||
sum := sha256.Sum256([]byte(name + "\x00" + about + "\x00" + short))
|
||||
hash := hex.EncodeToString(sum[:])
|
||||
key := "profile:" + lang
|
||||
if old, _ := b.store.GetKV(ctx, key); old == hash {
|
||||
return
|
||||
}
|
||||
steps := []func() error{
|
||||
func() error { return b.api.SetMyName(ctx, &telego.SetMyNameParams{Name: name, LanguageCode: code}) },
|
||||
func() error {
|
||||
return b.api.SetMyDescription(ctx, &telego.SetMyDescriptionParams{Description: about, LanguageCode: code})
|
||||
},
|
||||
func() error {
|
||||
return b.api.SetMyShortDescription(ctx, &telego.SetMyShortDescriptionParams{ShortDescription: short, LanguageCode: code})
|
||||
},
|
||||
}
|
||||
for _, step := range steps {
|
||||
if err := step(); err != nil {
|
||||
// Not recorded, so the next start tries again.
|
||||
b.log.Warn("setting the bot profile failed", "lang", lang, "err", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
if err := b.store.SetKV(ctx, key, hash); err != nil {
|
||||
b.log.Warn("remember bot profile", "lang", lang, "err", err)
|
||||
}
|
||||
}
|
||||
|
||||
// SyncUserMenu makes the command menu of a private chat follow the user's
|
||||
// language setting. Telegram picks a menu by the language of the app, so a
|
||||
// user who chose another language in the bot gets a menu for that chat
|
||||
// alone. An empty lang removes it, and the app's language applies again.
|
||||
func (b *Bot) SyncUserMenu(ctx context.Context, userID int64, lang string) {
|
||||
if !b.rate.allow("menu:"+itoa(userID), 6, time.Minute) {
|
||||
return
|
||||
}
|
||||
scope := &telego.BotCommandScopeChat{Type: telego.ScopeTypeChat, ChatID: telego.ChatID{ID: userID}}
|
||||
var err error
|
||||
if lang == "" {
|
||||
err = b.api.DeleteMyCommands(ctx, &telego.DeleteMyCommandsParams{Scope: scope})
|
||||
} else {
|
||||
err = b.api.SetMyCommands(ctx, &telego.SetMyCommandsParams{Commands: b.menu(lang, privateMenu), Scope: scope})
|
||||
}
|
||||
if err != nil {
|
||||
b.log.Warn("set the menu of a chat", "chat_id", userID, "err", err)
|
||||
}
|
||||
}
|
||||
|
||||
// SyncGroupMenu does the same for a group with its own language: one menu
|
||||
// for its members and one for its admins.
|
||||
func (b *Bot) SyncGroupMenu(ctx context.Context, chatID int64, lang string) {
|
||||
if !b.rate.allow("menu:"+itoa(chatID), 6, time.Minute) {
|
||||
return
|
||||
}
|
||||
id := telego.ChatID{ID: chatID}
|
||||
scopes := []struct {
|
||||
scope telego.BotCommandScope
|
||||
names []string
|
||||
}{
|
||||
{&telego.BotCommandScopeChat{Type: telego.ScopeTypeChat, ChatID: id}, groupMenu},
|
||||
{&telego.BotCommandScopeChatAdministrators{Type: telego.ScopeTypeChatAdministrators, ChatID: id}, adminMenu},
|
||||
}
|
||||
for _, s := range scopes {
|
||||
var err error
|
||||
if lang == "" {
|
||||
err = b.api.DeleteMyCommands(ctx, &telego.DeleteMyCommandsParams{Scope: s.scope})
|
||||
} else {
|
||||
err = b.api.SetMyCommands(ctx, &telego.SetMyCommandsParams{Commands: b.menu(lang, s.names), Scope: s.scope})
|
||||
}
|
||||
if err != nil {
|
||||
b.log.Warn("set the menu of a group", "chat_id", chatID, "err", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// visibleLangs lists the languages a user may pick. The pseudo-locale is
|
||||
// for owners only.
|
||||
func (b *Bot) visibleLangs(userID int64) []string {
|
||||
var out []string
|
||||
for _, l := range b.tr.Langs() {
|
||||
if !b.tr.Hidden(l) || b.cfg.IsOwner(userID) {
|
||||
out = append(out, l)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// findLang matches a language code typed by a user against the catalogs.
|
||||
func (b *Bot) findLang(code string, userID int64) string {
|
||||
for _, l := range b.visibleLangs(userID) {
|
||||
if strings.EqualFold(l, code) {
|
||||
return l
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user