How to get multilingual content from a JSON file in multilingual mode?

I am refactoring a theme to be multilingual with 2 languages.

Currently, on the homepage it reads out from a json file:

{{if .Site.Data.features}}
  {{ range .Site.Data.features }}
    <div>
      <h2>{{ .title }}</h2>
      <p>{{ .description }}</p>
    </div>
  {{end}}
{{end}}

the /data/features.json looks like this:

[
  {
    "title": "Example-Title 1",
    "description": "topic1, topic2, topic3",
  },
  {
    "title": "Example-Title 2",
    "description": "topic1, topic2, topic3",
  },
  {
    "title": "Example-Title 3",
    "description": "topic1, topic2, topic3",
  }
]

How can I refactor this to add content for the second language?

structure

data/
ā”œā”€ā”€ de/
ā”‚   ā””ā”€ā”€ features.json
ā””ā”€ā”€ en/
    ā””ā”€ā”€ features.json

template

{{ range index site.Data .Language.Lang "features" }}
  <div>
    <h2>{{ .title }}</h2>
    <p>{{ .description }}</p>
  </div>
{{ end }}

Note that you do not need to wrap this with an if condition. See:
https://pkg.go.dev/text/template#hdr-Actions

2 Likes

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.