2022-04-01 21:18:37 +00:00
|
|
|
package viewutil
|
|
|
|
|
|
|
|
import "text/template"
|
|
|
|
|
|
|
|
// Chain represents a chain of different language versions of the same template.
|
|
|
|
type Chain struct {
|
2022-05-18 16:58:24 +00:00
|
|
|
_en *template.Template
|
|
|
|
_ru *template.Template
|
2022-04-01 21:18:37 +00:00
|
|
|
}
|
|
|
|
|
2022-05-18 16:58:24 +00:00
|
|
|
// 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 {
|
2022-04-01 21:18:37 +00:00
|
|
|
return Chain{
|
2022-05-18 16:58:24 +00:00
|
|
|
_en: en,
|
2022-04-01 21:18:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-18 16:58:24 +00:00
|
|
|
// ru adds a Russian translation to the Chain.
|
|
|
|
func (c Chain) ru(ru *template.Template) Chain {
|
|
|
|
c._ru = ru
|
2022-04-01 21:18:37 +00:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns an appropriate language representation for the given locale in meta.
|
2022-05-18 17:11:12 +00:00
|
|
|
//
|
|
|
|
// TODO: Get rid of this
|
2022-04-01 21:18:37 +00:00
|
|
|
func (c Chain) Get(meta Meta) *template.Template {
|
|
|
|
switch meta.Locale() {
|
2022-05-18 17:11:12 +00:00
|
|
|
case "en":
|
2022-05-18 16:58:24 +00:00
|
|
|
return c._en
|
2022-05-18 17:11:12 +00:00
|
|
|
case "ru":
|
2022-05-18 16:58:24 +00:00
|
|
|
return c._ru
|
2022-04-01 21:18:37 +00:00
|
|
|
}
|
|
|
|
panic("unknown language " + meta.Locale())
|
|
|
|
}
|