Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: telebot/layout #541

Closed
Googlom opened this issue Aug 12, 2022 · 2 comments
Closed

docs: telebot/layout #541

Googlom opened this issue Aug 12, 2022 · 2 comments
Labels

Comments

@Googlom
Copy link
Contributor

Googlom commented Aug 12, 2022

Hello. First of all, thanks for amazing library :)

I'm wondering if there are some how-tos about layout package? As far as I understand this package helps to handle multiple locales
I'm building multilangual bot that replies depending on language that user sets at the start/settings.

@Googlom
Copy link
Contributor Author

Googlom commented Aug 15, 2022

Anyway, figured it out myself.

  1. You need to create your layout YAML file (can take template from example.yml)
  2. Put your localized texts YAML file into locales dir (by default its ./locales). File name (without extension) must match the locale you return in LocaleFunc (see below). Example of texts file:

en.yml

greeting: Hello
good_bye: Bye
  1. Implement your own LocaleFunc. In short: this function must return string value of a locale name, for example "en" or "ru" and also your texts file must be named en.yml or ru.yml
  2. Put it all together:
import (
	tele "gopkg.in/telebot.v3"
	"gopkg.in/telebot.v3/layout"
)

var tgbot *tele.Bot
var lt *layout.Layout

func InitBot(settings tele.Settings) {
	var err error
	tgbot, err = tele.NewBot(settings)
	if err != nil {
		panic(err)
	}

	lt, err = layout.New("layout.yml")
	if err != nil {
		panic(err)
	}

	// Middlewares first argument is 'fallback' locale
	tgbot.Use(lt.Middleware("en", getUserLocale))
}

func getUserLocale(r tele.Recipient) string {
	// your get locale logic
	return "en"
}
  1. And in your handlers:
func handler(c tele.Context) error {
	tgbot.Send(c.Sender(), lt.Text(c, "greeting"))
	return err
}

You can find other methods in layout/layout.go

@demget
Copy link
Collaborator

demget commented Aug 16, 2022

  • You can find the basic documentation here
  • And many examples built on the newest telebot here
  • Also you can use this template for your future bots

@demget demget pinned this issue Aug 16, 2022
@demget demget closed this as completed Oct 1, 2022
@demget demget changed the title Any documentation about how to use layout package? Documentation for the layout package Oct 1, 2022
@demget demget changed the title Documentation for the layout package docs: telebot/layout Nov 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants