What you could do which is a very common way in dealing with translation in other languages is to only use some kind of symbol in all your datastructures and then you want to print it in the current language you look it up and prints that. So for you could you in the title store for example the english version. https://goplay.space/#I7C-sNO0BsG
package main
import (
"html/template"
"log"
"os"
)
type translationItem map[string]string
var translations = map[string]translationItem{
"activities": translationItem{
"DEde": "Tätigkeiten",
"ENen": "Activities",
"FRfr": "Activités",
},
"today": translationItem{
"DEde": "Heute",
"ENen": "Today",
"FRfr": "Ajourdui",
},
}
var currentLang = "DEde"
var funcMap = template.FuncMap{
"translate": translate,
}
func translate(s string) string {
return translations[s][currentLang]
}
var html = `
<h1>{{translate .When }}</h1>
{{ .Count }} {{translate "activities"}}
`
func main() {
data := struct {
When string
Count int
}{
When: "today",
Count: 33,
}
t, err := template.New("foo").Funcs(funcMap).Parse(html)
if err != nil {
log.Fatalln(err)
}
err = t.ExecuteTemplate(os.Stdout, "foo", data)
if err != nil {
log.Fatalln(err)
}
}
Your screenshot doesn’t look like if it were from a struct but from a JSON object. If you make it an map[string]interface{}, you should be able to use index as described on SO:
index works for maps as well, and as I said, if you have your data as a map you can use index, but as you are serialising into a strict with strict fields yo can not access the fields dynamically besides using reflection.