1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2025-01-20 15:32:52 +00:00

Categories, views: Implement viewutil.Chain

This commit is contained in:
Timur Ismagilov 2022-04-02 00:18:37 +03:00
parent ca3da43e53
commit cbf416d7bb
2 changed files with 52 additions and 21 deletions

View File

@ -26,23 +26,14 @@ const categoriesRu = `
var (
//go:embed *.html
fs embed.FS
m = template.Must
baseEn, baseRu, listEn, listRu, pageEn, pageRu *template.Template
categoryTemplatesEn *template.Template
categoryTemplatesRu *template.Template
fs embed.FS
m = template.Must
baseEn, baseRu *template.Template
viewListChain, viewPageChain viewutil.Chain
categoryTemplatesEn *template.Template
categoryTemplatesRu *template.Template
)
func loctmp(meta viewutil.Meta, en *template.Template, ru *template.Template) *template.Template {
switch meta.Locale() {
case "en":
return en
case "ru":
return ru
}
panic("aaa")
}
func prepareViews() {
categoryTemplatesEn = template.Must(template.
New("category").
@ -55,10 +46,17 @@ func prepareViews() {
baseEn = m(viewutil.BaseEn.Clone())
baseRu = m(viewutil.BaseRu.Clone())
listEn = m(m(baseEn.Clone()).ParseFS(fs, "view_list.html"))
listRu = m(m(m(baseRu.Clone()).ParseFS(fs, "view_list.html")).Parse(categoriesRu))
pageEn = m(m(baseEn.Clone()).ParseFS(fs, "view_page.html"))
pageRu = m(m(m(baseRu.Clone()).ParseFS(fs, "view_page.html")).Parse(categoriesRu))
viewListChain = viewutil.
En(
m(m(baseEn.Clone()).ParseFS(fs, "view_list.html"))).
Ru(
m(m(m(baseRu.Clone()).ParseFS(fs, "view_list.html")).Parse(categoriesRu)))
viewPageChain = viewutil.
En(
m(m(baseEn.Clone()).ParseFS(fs, "view_page.html"))).
Ru(
m(m(m(baseRu.Clone()).ParseFS(fs, "view_page.html")).Parse(categoriesRu)))
}
func localizedCatTemplates(meta viewutil.Meta) *template.Template {
@ -108,7 +106,7 @@ type pageData struct {
}
func categoryPage(meta viewutil.Meta, catName string) {
if err := loctmp(meta, pageEn, pageRu).ExecuteTemplate(meta.W, "page", pageData{
if err := viewPageChain.Get(meta).ExecuteTemplate(meta.W, "page", pageData{
BaseData: viewutil.BaseData{
Meta: meta,
Title: localizedCatTemplateAsString(meta, "category x", catName),
@ -129,7 +127,7 @@ type listData struct {
}
func categoryList(meta viewutil.Meta) {
if err := loctmp(meta, listEn, listRu).ExecuteTemplate(meta.W, "page", listData{
if err := viewListChain.Get(meta).ExecuteTemplate(meta.W, "page", listData{
BaseData: viewutil.BaseData{
Meta: meta,
Title: localizedCatTemplateAsString(meta, "category list heading"),

33
viewutil/chain.go Normal file
View File

@ -0,0 +1,33 @@
package viewutil
import "text/template"
// Chain represents a chain of different language versions of the same template.
type Chain struct {
en *template.Template
ru *template.Template
}
// En returns a new Chain. This is the only constructor of the type, so every view is forced to have an English representation.
func En(en *template.Template) Chain {
return Chain{
en: en,
}
}
// Ru adds a Russian translation to the Chain.
func (c Chain) Ru(ru *template.Template) Chain {
c.ru = ru
return c
}
// Get returns an appropriate language representation for the given locale in meta.
func (c Chain) Get(meta Meta) *template.Template {
switch meta.Locale() {
case "en":
return c.en
case "ru":
return c.ru
}
panic("unknown language " + meta.Locale())
}