mirror of
https://github.com/osmarks/mycorrhiza.git
synced 2025-01-20 23:36:51 +00:00
Categories, views: Implement viewutil.Chain
This commit is contained in:
parent
ca3da43e53
commit
cbf416d7bb
@ -28,21 +28,12 @@ var (
|
|||||||
//go:embed *.html
|
//go:embed *.html
|
||||||
fs embed.FS
|
fs embed.FS
|
||||||
m = template.Must
|
m = template.Must
|
||||||
baseEn, baseRu, listEn, listRu, pageEn, pageRu *template.Template
|
baseEn, baseRu *template.Template
|
||||||
|
viewListChain, viewPageChain viewutil.Chain
|
||||||
categoryTemplatesEn *template.Template
|
categoryTemplatesEn *template.Template
|
||||||
categoryTemplatesRu *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() {
|
func prepareViews() {
|
||||||
categoryTemplatesEn = template.Must(template.
|
categoryTemplatesEn = template.Must(template.
|
||||||
New("category").
|
New("category").
|
||||||
@ -55,10 +46,17 @@ func prepareViews() {
|
|||||||
|
|
||||||
baseEn = m(viewutil.BaseEn.Clone())
|
baseEn = m(viewutil.BaseEn.Clone())
|
||||||
baseRu = m(viewutil.BaseRu.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))
|
viewListChain = viewutil.
|
||||||
pageEn = m(m(baseEn.Clone()).ParseFS(fs, "view_page.html"))
|
En(
|
||||||
pageRu = m(m(m(baseRu.Clone()).ParseFS(fs, "view_page.html")).Parse(categoriesRu))
|
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 {
|
func localizedCatTemplates(meta viewutil.Meta) *template.Template {
|
||||||
@ -108,7 +106,7 @@ type pageData struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func categoryPage(meta viewutil.Meta, catName string) {
|
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{
|
BaseData: viewutil.BaseData{
|
||||||
Meta: meta,
|
Meta: meta,
|
||||||
Title: localizedCatTemplateAsString(meta, "category x", catName),
|
Title: localizedCatTemplateAsString(meta, "category x", catName),
|
||||||
@ -129,7 +127,7 @@ type listData struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func categoryList(meta viewutil.Meta) {
|
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{
|
BaseData: viewutil.BaseData{
|
||||||
Meta: meta,
|
Meta: meta,
|
||||||
Title: localizedCatTemplateAsString(meta, "category list heading"),
|
Title: localizedCatTemplateAsString(meta, "category list heading"),
|
||||||
|
33
viewutil/chain.go
Normal file
33
viewutil/chain.go
Normal 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())
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user