layout: expose locale functions

pull/341/head
Demian 4 years ago
parent 11ccdd09f5
commit c4f58d405b

@ -98,7 +98,7 @@ func (lt *Layout) Duration(k string) time.Duration {
} }
func (lt *Layout) Text(c tele.Context, k string, args ...interface{}) string { func (lt *Layout) Text(c tele.Context, k string, args ...interface{}) string {
locale, ok := lt.locale(c) locale, ok := lt.Locale(c)
if !ok { if !ok {
return "" return ""
} }
@ -137,7 +137,7 @@ func (lt *Layout) Markup(c tele.Context, k string, args ...interface{}) *tele.Re
} }
var buf bytes.Buffer var buf bytes.Buffer
locale, _ := lt.locale(c) locale, _ := lt.Locale(c)
if err := lt.template(markup.keyboard, locale).Execute(&buf, arg); err != nil { if err := lt.template(markup.keyboard, locale).Execute(&buf, arg); err != nil {
// TODO: Log. // TODO: Log.
} }
@ -163,13 +163,6 @@ func (lt *Layout) Markup(c tele.Context, k string, args ...interface{}) *tele.Re
return &r return &r
} }
func (lt *Layout) locale(c tele.Context) (string, bool) {
lt.mu.RLock()
defer lt.mu.RUnlock()
locale, ok := lt.ctxs[c]
return locale, ok
}
func (lt *Layout) template(tmpl *template.Template, locale string) *template.Template { func (lt *Layout) template(tmpl *template.Template, locale string) *template.Template {
funcs := make(template.FuncMap) funcs := make(template.FuncMap)
@ -179,3 +172,16 @@ func (lt *Layout) template(tmpl *template.Template, locale string) *template.Tem
return tmpl.Funcs(funcs) return tmpl.Funcs(funcs)
} }
func (lt *Layout) SetLocale(c tele.Context, locale string) {
lt.mu.Lock()
lt.ctxs[c] = locale
lt.mu.Unlock()
}
func (lt *Layout) Locale(c tele.Context) (string, bool) {
lt.mu.RLock()
defer lt.mu.RUnlock()
locale, ok := lt.ctxs[c]
return locale, ok
}

@ -19,9 +19,7 @@ func (lt *Layout) Middleware(defaultLocale string, localeFunc ...LocaleFunc) tel
} }
} }
lt.mu.Lock() lt.SetLocale(c, locale)
lt.ctxs[c] = locale
lt.mu.Unlock()
return next(c) return next(c)
} }
} }

Loading…
Cancel
Save