1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-12-12 13:30:26 +00:00

Categories: Perform l10n tricks

This commit is contained in:
Timur Ismagilov 2022-03-23 15:46:08 +03:00
parent 4d56657938
commit a5cb7c0a3d
2 changed files with 38 additions and 33 deletions

View File

@ -15,40 +15,55 @@ const categoriesRu = `
{{define "cat"}}Категория{{end}} {{define "cat"}}Категория{{end}}
{{define "hypha name"}}Имя гифы{{end}} {{define "hypha name"}}Имя гифы{{end}}
{{define "categories"}}Категории{{end}} {{define "categories"}}Категории{{end}}
{{define "placeholder"}}Имя категории{{end}} {{define "placeholder"}}Имя категории...{{end}}
{{define "remove from category title"}}Убрать гифу из этой категории{{end}} {{define "remove from category title"}}Убрать гифу из этой категории{{end}}
{{define "add to category"}}Добавить гифу в эту категорию{{end}} {{define "add to category title"}}Добавить гифу в эту категорию{{end}}
{{define "category list heading"}}Список категорий{{end}}
{{define "no categories"}}В этой вики нет категорий.{{end}}
{{define "category x"}}Категория {{. | beautifulName}}{{end}}
` `
var ( var (
categoryT *template.Template categoryTemplatesEn *template.Template
categoryTemplatesRu *template.Template
) )
func init() { func init() {
categoryT = template.Must(template. categoryTemplatesEn = template.Must(template.
New("category"). New("category").
Funcs( Funcs(
template.FuncMap{ template.FuncMap{
"beautifulName": util.BeautifulName, "beautifulName": util.BeautifulName,
}). }).
ParseFS(fs, "categories.html")) ParseFS(fs, "categories.html"))
categoryTemplatesRu = template.Must(template.Must(categoryTemplatesEn.Clone()).Parse(categoriesRu))
} }
func categoryCard(meta Meta, hyphaName string) string { func localizedTemplates(meta Meta) *template.Template {
if meta.Lc.Locale == "ru" {
return categoryTemplatesRu
}
return categoryTemplatesEn
}
func localizedTemplateAsString(meta Meta, name string, datum ...interface{}) string {
var buf strings.Builder var buf strings.Builder
t, err := categoryT.Clone() var err error
if len(datum) == 1 {
err = localizedTemplates(meta).ExecuteTemplate(&buf, name, datum[0])
} else {
err = localizedTemplates(meta).ExecuteTemplate(&buf, name, nil)
}
if err != nil { if err != nil {
log.Println(err) log.Println(err)
return "" return ""
} }
if meta.Lc.Locale == "ru" { return buf.String()
_, err = t.Parse(categoriesRu) }
if err != nil {
log.Println(err) func categoryCard(meta Meta, hyphaName string) string {
return "" var buf strings.Builder
} err := localizedTemplates(meta).ExecuteTemplate(&buf, "category card", struct {
}
err = t.ExecuteTemplate(&buf, "category card", struct {
HyphaName string HyphaName string
Categories []string Categories []string
}{ }{
@ -63,19 +78,7 @@ func categoryCard(meta Meta, hyphaName string) string {
func CategoryPage(meta Meta, catName string) { func CategoryPage(meta Meta, catName string) {
var buf strings.Builder var buf strings.Builder
var t, err = categoryT.Clone() err := localizedTemplates(meta).ExecuteTemplate(&buf, "category page", struct {
if err != nil {
log.Println(err)
return
}
if meta.Lc.Locale == "ru" {
_, err = t.Parse(categoriesRu)
if err != nil {
log.Println(err)
return
}
}
err = t.ExecuteTemplate(&buf, "category page", struct {
CatName string CatName string
Hyphae []string Hyphae []string
}{ }{
@ -86,7 +89,7 @@ func CategoryPage(meta Meta, catName string) {
log.Println(err) log.Println(err)
} }
_, err = io.WriteString(meta.W, Base( _, err = io.WriteString(meta.W, Base(
"Category "+util.BeautifulName(catName), localizedTemplateAsString(meta, "category x", catName),
buf.String(), buf.String(),
meta.Lc, meta.Lc,
meta.U, meta.U,
@ -98,7 +101,7 @@ func CategoryPage(meta Meta, catName string) {
func CategoryList(meta Meta) { func CategoryList(meta Meta) {
var buf strings.Builder var buf strings.Builder
err := categoryT.ExecuteTemplate(&buf, "category list", struct { err := localizedTemplates(meta).ExecuteTemplate(&buf, "category list", struct {
Categories []string Categories []string
}{ }{
categories.List(), categories.List(),
@ -107,7 +110,7 @@ func CategoryList(meta Meta) {
log.Println(err) log.Println(err)
} }
_, err = io.WriteString(meta.W, Base( _, err = io.WriteString(meta.W, Base(
"Category list", localizedTemplateAsString(meta, "category list heading"),
buf.String(), buf.String(),
meta.Lc, meta.Lc,
meta.U, meta.U,

View File

@ -1,3 +1,5 @@
{{define "category x"}}Category {{. | beautifulName}}{{end}}
{{define "category card"}} {{define "category card"}}
{{$hyphaName := .HyphaName}} {{$hyphaName := .HyphaName}}
<aside class="layout-card categories-card"> <aside class="layout-card categories-card">
@ -22,7 +24,7 @@
<input type="hidden" name="hypha" value="{{$hyphaName}}"> <input type="hidden" name="hypha" value="{{$hyphaName}}">
<input type="hidden" name="redirect-to" value="/hypha/{{$hyphaName}}"> <input type="hidden" name="redirect-to" value="/hypha/{{$hyphaName}}">
<input type="submit" class="btn categories-card__btn" value="+" <input type="submit" class="btn categories-card__btn" value="+"
title="{{block `add to category` .}}Add the hypha to this category{{end}}"> title="{{block `add to category title` .}}Add the hypha to this category{{end}}">
</form> </form>
</li> </li>
</ul> </ul>
@ -57,7 +59,7 @@
{{define "category list"}} {{define "category list"}}
<main class="main-width category-list"> <main class="main-width category-list">
<h1>Category list</h1> <h1>{{block `category list heading` .}}Category list{{end}}</h1>
{{if len .Categories}} {{if len .Categories}}
<ul class="category-list__entries"> <ul class="category-list__entries">
{{range .Categories}} {{range .Categories}}
@ -67,7 +69,7 @@
{{end}} {{end}}
</ul> </ul>
{{else}} {{else}}
<p>This wiki has no categories.</p> <p>{{block `no categories` .}}This wiki has no categories.{{end}}</p>
{{end}} {{end}}
</main> </main>
{{end}} {{end}}