Skip to content

Commit

Permalink
layout: allow to specify locale manually
Browse files Browse the repository at this point in the history
  • Loading branch information
demget committed Oct 22, 2020
1 parent 9f08ef9 commit 67283cd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
16 changes: 12 additions & 4 deletions layout/layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ func (lt *Layout) Text(c tele.Context, k string, args ...interface{}) string {
return ""
}

return lt.text(locale, k, args...)
return lt.TextLocale(locale, k, args...)
}

func (lt *Layout) text(locale, k string, args ...interface{}) string {
func (lt *Layout) TextLocale(locale, k string, args ...interface{}) string {
tmpl, ok := lt.locales[locale]
if !ok {
return ""
Expand Down Expand Up @@ -117,6 +117,15 @@ func (lt *Layout) Button(k string) tele.CallbackEndpoint {
}

func (lt *Layout) Markup(c tele.Context, k string, args ...interface{}) *tele.ReplyMarkup {
locale, ok := lt.Locale(c)
if !ok {
return nil
}

return lt.MarkupLocale(locale, k, args...)
}

func (lt *Layout) MarkupLocale(locale, k string, args ...interface{}) *tele.ReplyMarkup {
markup, ok := lt.markups[k]
if !ok {
return nil
Expand All @@ -128,7 +137,6 @@ func (lt *Layout) Markup(c tele.Context, k string, args ...interface{}) *tele.Re
}

var buf bytes.Buffer
locale, _ := lt.Locale(c)
if err := lt.template(markup.keyboard, locale).Execute(&buf, arg); err != nil {
log.Println("telebot/layout:", err)
}
Expand Down Expand Up @@ -157,7 +165,7 @@ func (lt *Layout) template(tmpl *template.Template, locale string) *template.Tem
funcs := make(template.FuncMap)

// Redefining built-in blank functions
funcs["text"] = func(k string) string { return lt.text(locale, k) }
funcs["text"] = func(k string) string { return lt.TextLocale(locale, k) }
funcs["locale"] = func() string { return locale }

return tmpl.Funcs(funcs)
Expand Down
6 changes: 3 additions & 3 deletions layout/layout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ func TestLayout(t *testing.T) {
{{Text: "Settings"}},
},
ResizeKeyboard: true,
}, lt.Markup(nil, "reply_shortened"))
}, lt.MarkupLocale("en", "reply_shortened"))

assert.Equal(t, &tele.ReplyMarkup{
ReplyKeyboard: [][]tele.ReplyButton{{{Text: "Send a contact", Contact: true}}},
ResizeKeyboard: true,
OneTimeKeyboard: true,
}, lt.Markup(nil, "reply_extended"))
}, lt.MarkupLocale("en", "reply_extended"))

assert.Equal(t, &tele.ReplyMarkup{
InlineKeyboard: [][]tele.InlineButton{{{Unique: "stop", Text: "Stop", Data: "1"}}},
}, lt.Markup(nil, "inline", 1))
}, lt.MarkupLocale("en", "inline", 1))
}

0 comments on commit 67283cd

Please sign in to comment.