package misc import ( "github.com/bouncepaw/mycorrhiza/cfg" "github.com/bouncepaw/mycorrhiza/l18n" "github.com/bouncepaw/mycorrhiza/user" "github.com/bouncepaw/mycorrhiza/version" "log" "strings" "text/template" // sic! ) type L10nEntry struct { _en string _ru string } func En(v string) L10nEntry { return L10nEntry{_en: v} } func (e L10nEntry) Ru(v string) L10nEntry { e._ru = v return e } func (e L10nEntry) Get(lang string) string { if lang == "ru" && e._ru != "" { return e._ru } return e._en } const aboutTemplateString = `

{{ printf (get .L.Title) .Cfg.WikiName }}

{{ get .L.Version }}
{{ .Version }}
{{ if .Cfg.UseAuth }}
{{ get .L.HomeHypha }}
{{ .Cfg.HomeHypha }}
{{get .L.Auth}}
{{ get .L.AuthOn }}
{{if .Cfg.TelegramEnabled}}
{{get .L.TelegramOn}}
{{end}}
{{ get .L.UserCount }}
{{ .UserCount }}
{{if .Cfg.RegistrationLimit}}
{{get .L.RegistrationLimit}}
{{.RegistrationLimit}}
{{end}}
{{ get .L.Admins }}
{{$cfg := .Cfg}}{{ range $i, $username := .Admins }}
{{ $username }}
{{ end }} {{ else }}
{{get .L.Auth}}
{{ get .L.AuthOff }}
{{ end }}
` var aboutData = struct { L map[string]L10nEntry Version string Cfg map[string]interface{} Admins []string UserCount uint64 RegistrationLimit uint64 }{ L: map[string]L10nEntry{ "Title": En("About %s").Ru("О %s"), "Version": En("Mycorrhiza Wiki version").Ru("Версия Микоризы"), "UserCount": En("User count").Ru("Число пользователей"), "HomeHypha": En("Home hypha").Ru("Домашняя гифа"), "RegistrationLimit": En("RegistrationLimit").Ru("Максимум пользователей"), "Admins": En("Administrators").Ru("Администраторы"), "Auth": En("Authentication").Ru("Аутентификация"), "AuthOn": En("Authentication is on").Ru("Аутентификация включена"), "AuthOff": En("Authentication is off").Ru("Аутентификация не включена"), "TelegramOn": En("Telegram authentication is on").Ru("Вход через Телеграм включён"), }, } func AboutHTML(lc *l18n.Localizer) string { get := func(e L10nEntry) string { return e.Get(lc.Locale) } temp, err := template.New("about wiki").Funcs(template.FuncMap{"get": get}).Parse(aboutTemplateString) if err != nil { log.Fatalln(err) } data := aboutData data.Version = version.Short data.Admins = user.ListUsersWithGroup("admin") data.UserCount = user.Count() data.RegistrationLimit = cfg.RegistrationLimit data.Cfg = map[string]interface{}{ "UseAuth": cfg.UseAuth, "WikiName": cfg.WikiName, "HomeHypha": cfg.HomeHypha, "TelegramEnabled": cfg.TelegramEnabled, "RegistrationLimit": cfg.RegistrationLimit, } var out strings.Builder err = temp.Execute(&out, data) if err != nil { log.Println(err) } return out.String() }