mirror of
https://github.com/osmarks/mycorrhiza.git
synced 2025-12-09 09:58:06 +00:00
Viewutil: Introduce CopyEnRuWith
Reducing boilerplate as usual
This commit is contained in:
@@ -4,30 +4,30 @@ 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 *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 {
|
||||
// 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,
|
||||
_en: en,
|
||||
}
|
||||
}
|
||||
|
||||
// Ru adds a Russian translation to the Chain.
|
||||
func (c Chain) Ru(ru *template.Template) Chain {
|
||||
c.ru = ru
|
||||
// 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
|
||||
case "_en":
|
||||
return c._en
|
||||
case "_ru":
|
||||
return c._ru
|
||||
}
|
||||
panic("unknown language " + meta.Locale())
|
||||
}
|
||||
|
||||
@@ -75,7 +75,7 @@ func Init() {
|
||||
// TODO: get rid of this
|
||||
func localizedBaseWithWeirdBody(meta Meta) *template.Template {
|
||||
t := func() *template.Template {
|
||||
if meta.Locale() == "ru" {
|
||||
if meta.Locale() == "_ru" {
|
||||
return BaseRu
|
||||
}
|
||||
return BaseEn
|
||||
@@ -121,11 +121,16 @@ func Base(meta Meta, title, body string, headElements ...string) string {
|
||||
return w.String()
|
||||
}
|
||||
|
||||
func CopyEnWith(fsys fs.FS, f string) *template.Template {
|
||||
func CopyEnRuWith(fsys fs.FS, filename, ruTranslation string) Chain {
|
||||
return en(copyEnWith(fsys, filename)).
|
||||
ru(template.Must(copyRuWith(fsys, filename).Parse(ruTranslation)))
|
||||
}
|
||||
|
||||
func copyEnWith(fsys fs.FS, f string) *template.Template {
|
||||
return m(m(BaseEn.Clone()).ParseFS(fsys, f))
|
||||
}
|
||||
|
||||
func CopyRuWith(fsys fs.FS, f string) *template.Template {
|
||||
func copyRuWith(fsys fs.FS, f string) *template.Template {
|
||||
return m(m(BaseRu.Clone()).ParseFS(fsys, f))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user