I am trying to use multilingual data files. They are structured similar to multilingual content files. data.en.toml and data.de.toml for example. Is there any sane way to use them per language?
Currently I have:
{{ range sort $.Site.Data.team }}
{{ . }}
{{ end }}
This prints all items in all data files. Seems like data files are not first class citizens within the multilingual change.
Any workarounds? Or should I move the data files to content files in front-matter only style?
{{ range $.Site.Data }}
{{ range sort .team }}
{{ . }}
{{ end }}
{{ end }}
So multilingual data files would be separated at the root data directory âdata/de/â and âdata/en/â. Doesnât seem consistent with the way content files are handled and therefore I would still prefer a more consistent solution.
This is my current solution to get multilingual features similar to content files:
{{ range $key, $e := $.Site.Data.team }}
{{ if ne $.Site.LanguagePrefix "" }}
{{ $.Scratch.Set "lang" (print "." (replace $.Site.LanguagePrefix "/" "")) }}
{{ if in (printf $key) ($.Scratch.Get "lang") }}
{{ with $e }}
{{ partial "members.html" . }}
{{ end }}
{{ end }}
{{ else }}
{{ if or (not (in $key ".")) (in $key (print "." $.Site.Language.Lang)) }}
{{ with $e }}
{{ partial "members.html" . }}
{{ end }}
{{ end }}
{{ end }}
{{ end }}
data/team/$name.toml -> will be rendered as default for default language
data/team/$name.de.toml -> will be rendered on german pages
data/team/$name.en.toml -> will be rendered on english pages
For a default language of âenâ both $name.toml and $name.en.toml will be rendered and basically merged.
Seems to work quite alright except, that I canât seem to figure out how to sort with having variables set on range at the same time.
Sorting similar to this would be needed: {{ range sort $.Site.Data.team "joined" "asc" }}
Any suggestions on how to do sorting while retaining the variable assignment for keys and values?
{{ $data := index .Site.Data .Site.Language.Lang }}
{{ range $data.team }}
...
That and variations of the above should be pretty flexible. I donât think /data will ever be a first class citizen of the multilingual feature, because most of its use is by nature cross-lingual.
I came here with the same question. In the end i found easier to have just one data file with translation ids instead of labels, and translate everything with i18n files.
Just wanted to see if dirData = âdata/jaâ for Japanese data would work or not. Been trying this out but unfortunately have had no luck.
print .Site.Params i get:
features:map[datadir:data/ja enable:true]
but when print .Site.Params.dataDir is done it shows nil
Update: Followed steps similar to @martignoni 's reference.
Put in a language settings
{{ $information := index .Site.Data .Site.Language.Lang }}
and then called it inside the range
{{ range $index, $element := sort $information.features âweightâ }}
Was wondering if you knew how {{ i18n âreadingTimeâ .ReadingTime }} in https://gohugo.io/content-management/multilingual/ works.
Been trying to use it to get English into Japanese for things like menu and have had no luck.
-> Think i have figured this out. I used
i18n (lower .Name))
to do the translation as itâs case sensitive.