Browse Source

Add language based strings

master
Jan-Lukas Else 3 months ago
parent
commit
43cd803342
9 changed files with 42 additions and 21 deletions
  1. +1
    -1
      render.go
  2. +22
    -9
      templateStrings.go
  3. +2
    -2
      templates/index.gohtml
  4. +2
    -2
      templates/photos.gohtml
  5. +1
    -1
      templates/photosummary.gohtml
  6. +4
    -4
      templates/post.gohtml
  7. +5
    -0
      templates/strings/de.yaml
  8. +4
    -1
      templates/strings/default.yaml
  9. +1
    -1
      templates/summary.gohtml

+ 1
- 1
render.go View File

@ -66,7 +66,7 @@ func initRendering() error {
return d.Format(format)
},
"asset": assetFile,
"string": getDefaultTemplateString,
"string": getTemplateStringVariant,
"include": func(templateName string, blog *configBlog, data interface{}) (template.HTML, error) {
buf := new(bytes.Buffer)
err := templates[templateName].ExecuteTemplate(buf, templateName, &renderData{


+ 22
- 9
templateStrings.go View File

@ -1,23 +1,32 @@
package main
import (
"gopkg.in/yaml.v3"
"io/ioutil"
"os"
"path"
"gopkg.in/yaml.v3"
)
const stringsDir = "templates/strings"
const defaultVariant = "default"
const defaultStrings = "default"
const variantFileExt = ".yaml"
var templateStrings map[string]map[string]string
func initTemplateStrings() error {
templateStrings = map[string]map[string]string{}
for _, variant := range []string{defaultVariant} {
variants := []string{defaultStrings}
for _, blog := range appConfig.Blogs {
variants = append(variants, blog.Lang)
}
for _, variant := range variants {
variantStrings := map[string]string{}
fileContent, err := ioutil.ReadFile(path.Join(stringsDir, variant+variantFileExt))
if err != nil {
if os.IsNotExist(err) {
continue
}
return err
}
err = yaml.Unmarshal(fileContent, variantStrings)
@ -29,10 +38,14 @@ func initTemplateStrings() error {
return nil
}
func getDefaultTemplateString(name string) string {
return getTemplateStringVariant(name, defaultVariant)
}
func getTemplateStringVariant(name, variant string) string {
return templateStrings[variant][name]
func getTemplateStringVariant(lang, name string) (result string) {
m, ok := templateStrings[lang]
if !ok {
m = templateStrings[defaultStrings]
}
result, ok = m[name]
if !ok {
result = templateStrings[defaultStrings][name]
}
return
}

+ 2
- 2
templates/index.gohtml View File

@ -30,10 +30,10 @@
{{ include "summary" $blog $post }}
{{ end }}
{{ if .Data.HasPrev }}
<p><a href="{{ .Data.Prev }}">Prev</a></p>
<p><a href="{{ .Data.Prev }}">{{ string .Blog.Lang "prev" }}</a></p>
{{ end }}
{{ if .Data.HasNext }}
<p><a href="{{ .Data.Next }}">Next</a></p>
<p><a href="{{ .Data.Next }}">{{ string .Blog.Lang "next" }}</a></p>
{{ end }}
</main>
{{ end }}


+ 2
- 2
templates/photos.gohtml View File

@ -18,10 +18,10 @@
{{ include "photosummary" $blog $post }}
{{ end }}
{{ if .Data.HasPrev }}
<p><a href="{{ .Data.Prev }}">Prev</a></p>
<p><a href="{{ .Data.Prev }}">{{ string .Blog.Lang "prev" }}</a></p>
{{ end }}
{{ if .Data.HasNext }}
<p><a href="{{ .Data.Next }}">Next</a></p>
<p><a href="{{ .Data.Next }}">{{ string .Blog.Lang "next" }}</a></p>
{{ end }}
</main>
{{ end }}


+ 1
- 1
templates/photosummary.gohtml View File

@ -6,7 +6,7 @@
{{ md ( printf "![](%s)" $photo ) }}
{{ end }}
<p>{{ summary .Data }}</p>
<a href="{{ .Data.Path }}">View</a>
<a href="{{ .Data.Path }}">{{ string .Blog.Lang "view" }}</a>
</article>
<hr>
{{ end }}

+ 4
- 4
templates/post.gohtml View File

@ -6,10 +6,10 @@
<main class=h-entry>
<article>
{{ with title .Data }}<h1 class=p-name>{{ . }}</h1>{{ end }}
{{ with .Data.Published }}
<p>{{ string "publishedon" }} {{ dateformat . "02. Jan 2006" }}</p>{{ end }}
{{ with .Data.Updated }}
<p>{{ string "updatedon" }} {{ dateformat . "02. Jan 2006" }}</p>{{ end }}
{{ if .Data.Published }}
<p>{{ string .Blog.Lang "publishedon" }} {{ dateformat .Data.Published "02. Jan 2006" }}</p>{{ end }}
{{ if .Data.Updated }}
<p>{{ string .Blog.Lang "updatedon" }} {{ dateformat .Data.Updated "02. Jan 2006" }}</p>{{ end }}
{{ with .Data.Content }}
<div class=e-content>{{ md . }}</div>
{{ end }}


+ 5
- 0
templates/strings/de.yaml View File

@ -0,0 +1,5 @@
publishedon: "Veröffentlicht am"
updatedon: "Aktualisiert am"
next: "Weiter"
prev: "Zurück"
view: "Anschauen"

+ 4
- 1
templates/strings/default.yaml View File

@ -1,2 +1,5 @@
publishedon: "Published on"
updatedon: "Updated on"
updatedon: "Updated on"
next: "Next"
prev: "Previous"
view: "View"

+ 1
- 1
templates/summary.gohtml View File

@ -3,7 +3,7 @@
{{ with p .Data "title" }}<h2>{{ . }}</h2>{{ end }}
{{ with .Data.Published }}<p>{{ dateformat . "02. Jan 2006" }}</p>{{ end }}
<p>{{ summary .Data }}</p>
<a href="{{ .Data.Path }}">View</a>
<a href="{{ .Data.Path }}">{{ string .Blog.Lang "view" }}</a>
</article>
<hr>
{{ end }}

Loading…
Cancel
Save