HUGO

Multilingual data files

Hey,

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?

Thanks. Any help appreciated.

1 Like

One idea was to use something like this:

{{ 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?

This seems similar to this question: https://discuss.gohugo.io/t/index-on-sort/4279/2

A sketch of what I would do:

/data/en/team.toml
/data/de/team.toml
{{ $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.

3 Likes

Yeah that was my fallback idea, but seemed imperfect. (I agree it might be the simplest solution)

Will add some magic for the non multilingual case, where team resides at /data/team.toml.

Just to clarify. Sort while assigning index and value is not possible then?

Will post my solution, when it’s finished.

In your example, how would one sort it by “joined” “asc”?

Yes it is.

1 Like

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.

hello @stp-ip, did you succeeded ?

I have the same thing to do and would love to see what you have working.
Regards.

You may want to have a look at my solution, here:


I’m using successfully multilingual data files to create carousels and other neat features. See it in action here: https://moodlebox.net/.

1 Like

Thanks. Looks like it is usefull for me. thanks

Thanks @bep this perfect. So elegant !!! thanks

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.

Thank you for your help!