Translating tags

Is creating tag terms branch bundles the only way to translate them? I want them linked together by the language switcher?

Preferably, if I have tags: [red] or tags: [red, white], I would like to have each linked to an entry in the i18n file of the same name and fall back to the params one. So

i18n/en.html 
red = "Red"

i18n/fr.md
red =  "Rouge"

That works for list pages. I am open to better ideas.

{{- $term := .Data.Term }}
{{- i18n $term | default .Title | humanize }}

(I noticed URLs remain in the default language e.g /fr/tags/red/. It seems creating branch bundles for the tags is the only way to translate tags, but with hundreds of them, I would prefer not to do that)

After spending hours on this, I am dreading that I will have to translate all the tags by hand using branch bundles. Yikes!

./content/tags
.
├── foo
│   ├── _index.md
│   └── _index.fr.md
└── bar
    ├── _index.md
    └── _index.fr.md
<ul>
  {{ range .Params.tags }}
    {{ with  site.Taxonomies.tags.Get . }}
      <li><a href="{{ .Page.RelPermalink }}">{{ .Page.Title }}</a></li>
    {{ end }}
  {{ end }}
</ul>

While your example above works great, the idiomatic way to render a tag list is to use the .GetTerms method on a Page object:

{{ with .GetTerms "tags" }}
  <ul>
    {{ range . }}
      <li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>
    {{ end }}
  </ul>
{{ end }}

In relation to translating taxonomy and term names, creating branch bundles is the right way to handle it. This allows you to translate the names and optionally translate the URLs using the url front matter field.

I actually spent over half a day finding the internet on how to get terms for pages in a list template. I had to copy paste the taxonomy docs page to grok and it came up with this. Somehow, get terms does not work on list pages and params tags returns the default language tags.

I am already using get terms on pages. Perhaps on list pages, there should be an example in the taxonomy docs to get these tags on a list for each page.

The .GetTerms method works fine on list pages:

layouts/section.html
{{ define "main" }}
  <h1>{{ .Title }}</h1>
  {{ .Content }}
  {{ range .Pages }}
    <h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2>

    {{ with .GetTerms "tags" }}
      <ul>
        {{ range . }}
          <li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>
        {{ end }}
      </ul>
    {{ end }}

  {{ end }}
{{ end }}

But you have definitely found a bug with how it works on multilingual sites when one or more of the tags has draft set to true:

https://github.com/gohugoio/hugo/issues/14031

My earlier test of simply looking at the published directory structure was invalid.

I am unsure what I was doing then. I can confirm it works.

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