From 83dd959e7536573b685a93c1fde1b8b7e2c14a01 Mon Sep 17 00:00:00 2001 From: googlom Date: Tue, 30 Aug 2022 16:06:52 +0500 Subject: [PATCH] implement CommandsLocale --- layout/layout.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/layout/layout.go b/layout/layout.go index 5459c31..666b35e 100644 --- a/layout/layout.go +++ b/layout/layout.go @@ -174,6 +174,49 @@ func (lt *Layout) Commands() (cmds []tele.Command) { return } +// CommandsLocale returns a list of telebot commands and localized description, which can be +// used in b.SetCommands later. +// +// Example of bot.yml: +// commands: +// /start: '{{ text "cmdStart" }}' +// +// en.yml: +// cmdStart: Start the bot +// +// ru.yml: +// cmdStart: Запуск бота +// +// Usage: +// b.SetCommands(lt.CommandsLocale("en"), "en") +// b.SetCommands(lt.CommandsLocale("ru"), "ru") +func (lt *Layout) CommandsLocale(locale string, args ...interface{}) (cmds []tele.Command) { + var arg interface{} + if len(args) > 0 { + arg = args[0] + } + + for k, v := range lt.commands { + tmpl, err := lt.template(template.New(k).Funcs(lt.funcs), locale).Parse(v) + if err != nil { + log.Println("telebot/layout:", err) + return nil + } + + var buf bytes.Buffer + if err := tmpl.Execute(&buf, arg); err != nil { + log.Println("telebot/layout:", err) + return nil + } + + cmds = append(cmds, tele.Command{ + Text: strings.TrimLeft(k, "/"), + Description: buf.String(), + }) + } + return +} + // Text returns a text, which locale is dependent on the context. // The given optional argument will be passed to the template engine. //