My JSON File look like this:
[
{
"id": "SA345654"
"color_de": "weiß"
"color_en": "white"
"color_fr": "blanc"
},
...
]
How can i access the color if the user switches the language?
I was thinking of something like this
{{ .Scratch.Set "colorLang" ".color_" }}
{{ .Scratch.Add "colorLang" $.Site.Language.Lang }}
{{ $.Scratch.Get "colorLang" }}
But obviously that doesnt work.
You obviously already have a way to get the JSON into a variable. So the following is only a way to retrieve it from that collection:
{{ $index := printf "color_%s" $.Site.Language.Lang }}
{{ $value := index $jsonObject $index }}
1 Like
I use range:
{{ range $.Site.Data.colours }}
<ul>
<li>Colour: {{ .colour_de }}</li>
</ul>
{{ end }}
Is there no “easy” way, so i can use the variable $index from
{{ $index := printf "colour_%s" $.Site.Language.Lang }}
in my code above?
something like
{{ range $.Site.Data.colours }}
<ul>
<li>Colour: {{ .($index) }}</li>
</ul>
{{ end }}
{{ range $.Site.Data.colours }}
{{ $index := printf "color_%s" $.Site.Language.Lang }}
{{ $value := index . $index }}
<ul>
<li>Colour: {{ $value }}</li>
</ul>
{{ end }}
should work. Inside of the range you have a context (the dot .
) which is a single item, so .id
will be SA345654, .color_de
is “weiss” and so on.
1 Like
system
Closed
5
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.