layout: implement default layout

pull/425/head
Demian 3 years ago
parent 9be6c9e313
commit 963aaea7bf

@ -0,0 +1,26 @@
package layout
import (
tele "gopkg.in/tucnak/telebot.v3"
)
// DefaultLayout is a simplified layout instance with pre-defined locale by default.
type DefaultLayout struct {
lt *Layout
locale string
}
// Text wraps localized layout function Text using your default locale.
func (dlt *DefaultLayout) Text(k string, args ...interface{}) string {
return dlt.lt.TextLocale(dlt.locale, k, args...)
}
// Button wraps localized layout function Button using your default locale.
func (dlt *DefaultLayout) Button(k string, args ...interface{}) *tele.Btn {
return dlt.lt.ButtonLocale(dlt.locale, k, args...)
}
// Markup wraps localized layout function Markup using your default locale.
func (dlt *DefaultLayout) Markup(k string, args ...interface{}) *tele.ReplyMarkup {
return dlt.lt.MarkupLocale(dlt.locale, k, args...)
}

@ -106,6 +106,13 @@ func (lt *Layout) Settings() tele.Settings {
return *lt.pref
}
// Default returns a simplified layout instance with pre-defined locale.
// It's useful when you have no need in localization and don't want to pass
// context each time you use layout functions.
func (lt *Layout) Default(locale string) *DefaultLayout {
return &DefaultLayout{lt: lt, locale: locale}
}
// Locales returns all presented locales.
func (lt *Layout) Locales() []string {
var keys []string
@ -137,7 +144,7 @@ func (lt *Layout) SetLocale(c tele.Context, locale string) {
// start: Hi, {{.FirstName}}!
//
// Usage:
// func OnStart(c tele.Context) error {
// func onStart(c tele.Context) error {
// return c.Send(lt.Text(c, "start", c.Sender()))
// }
//
@ -150,7 +157,7 @@ func (lt *Layout) Text(c tele.Context, k string, args ...interface{}) string {
return lt.TextLocale(locale, k, args...)
}
// TextLocale returns a localised text processed with standard template engine.
// TextLocale returns a localized text processed with standard template engine.
// See Text for more details.
func (lt *Layout) TextLocale(locale, k string, args ...interface{}) string {
tmpl, ok := lt.locales[locale]
@ -175,9 +182,8 @@ func (lt *Layout) TextLocale(locale, k string, args ...interface{}) string {
// useful for handlers registering.
//
// Example:
//
// // Handling settings button
// b.Handle(lt.Callback("settings"), OnSettings)
// b.Handle(lt.Callback("settings"), onSettings)
//
func (lt *Layout) Callback(k string) tele.CallbackEndpoint {
btn, ok := lt.buttons[k]
@ -221,7 +227,7 @@ func (lt *Layout) Button(c tele.Context, k string, args ...interface{}) *tele.Bt
return lt.ButtonLocale(locale, k, args...)
}
// ButtonLocale returns a localised button processed with standard template engine.
// ButtonLocale returns a localized button processed with standard template engine.
// See Button for more details.
func (lt *Layout) ButtonLocale(locale, k string, args ...interface{}) *tele.Btn {
btn, ok := lt.buttons[k]
@ -270,7 +276,7 @@ func (lt *Layout) ButtonLocale(locale, k string, args ...interface{}) *tele.Btn
// - [settings]
//
// Usage:
// func OnStart(c tele.Context) error {
// func onStart(c tele.Context) error {
// return c.Send(
// lt.Text(c, "start"),
// lt.Markup(c, "menu"))
@ -285,7 +291,7 @@ func (lt *Layout) Markup(c tele.Context, k string, args ...interface{}) *tele.Re
return lt.MarkupLocale(locale, k, args...)
}
// MarkupLocale returns a localised markup processed with standard template engine.
// MarkupLocale returns a localized markup processed with standard template engine.
// See Markup for more details.
func (lt *Layout) MarkupLocale(locale, k string, args ...interface{}) *tele.ReplyMarkup {
markup, ok := lt.markups[k]

@ -8,10 +8,9 @@ import (
// Returned locale will be remembered and linked to the corresponding context.
type LocaleFunc func(tele.Recipient) string
// Middleware builds a global middleware to make internationalisation work.
// Middleware builds a telebot middleware to make localization work.
//
// Usage:
//
// b.Use(lt.Middleware("en", func(r tele.Recipient) string {
// loc, _ := db.UserLocale(r.Recipient())
// return loc
@ -44,3 +43,8 @@ func (lt *Layout) Middleware(defaultLocale string, localeFunc ...LocaleFunc) tel
}
}
}
// Middleware wraps ordinary layout middleware with your default locale.
func (dlt *DefaultLayout) Middleware() tele.MiddlewareFunc {
return dlt.lt.Middleware(dlt.locale)
}

Loading…
Cancel
Save