Listing mulilingual taxonomies

I’m just trying to make a ‘categories’ menu in a multilingual environment.

{{ $ctx := . }}    
{{ $categories := i18n "categories" }}    
{{ $taxo := print ".Site.Taxonomies.categories" }}                                                                                                                                          
{{ range $key, $value := $taxo }}    
<a class="navbar-item "href="{{ $ctx.Site.BaseURL | absLangURL }}{{ $ctx.Site.Language.Lang }}/{{ 
$categories }}/{{ $key }}">{{ $key | humanize }}</a>    
  {{ end }} 


‘categories’ may in french resolve to catégories, or sujets.

But with the above code (using range $taxo) I will get the error message the hugo cannot range over .Site.Taxonomies.categories (even if it points to english). I’m really actually wanting to use:

{{ $taxo := print ".Site.Taxonomies." $categories }}

to get the language specific category.

And I don’t even know yet if there is some problem with accents. Some googling suggests to me thats possible. Not finding too much information on multilingual taxonomies. Any help appreciated.

1 Like

Try this

  {{- if isset site.Taxonomies "categories" }}
  {{- if not (eq (len site.Taxonomies.categories) 0) }}
    {{- range $name, $items := site.Taxonomies.categories }}
    <li><a href="{{ `categories/` | relLangURL }}{{ $name | urlize | lower }}">{{ $name | humanize }} 
    <small>({{ len $items }})</small></a></li>
    {{- end }}
  {{- end }}
  {{- end }}

<small>({{ len $items }})</small> is for count the category item, you can remove it if you want.

No that doesn’t seem to work either. As I see things the range site.Taxonomies.categories must refer to what is defined in config.yaml for exemple the default category: categories. So if in another language you have sujet: sujets then the range must be site.Taxonomies.sujets (for that language). So I suppose that internally hugo doesn’t know that sujets in one language = categories in another. Maybe this can’t be done? Although since Hugo can spit out publc/en/categories and public/fr/sujets then you could get a menu going that could present that.

As far as I understand, you have created another taxonomy called “sujet”. There is no simple way to link its terms to the categories terms to my knowledge.

What I do in my multilingual site is use the same taxonomy (tags, categories) and translate it with the i18n functions. Every part of your theme that displays the taxonomies should be using the i18n function. The URL slugs for tags will not match the translated text, but that’s too little to worry about.

To give you one example, I would assign the following tags to a post in the frontmatter:

  - tag-architecture
  - tag-artifact

The tag- prefix is optional, but it’s best to keep the keywords to be translated unique.
Then I maintain the translations in the corresponding /i18n/en.yaml file:

  other: Architecture

  other: Artifact

and then /i18n/en.yaml for Greek:

  other: Αρχιτεκτόνημα

  other: Τέχνεργο

Thank you!. But yes if you look at my example this is exactly what I have done with $categories := 18n “categories”. However if I then try to concaenate that variable to the range: {{ $taxo := print “.Site.Taxonomies.” $categories }} it will give me an error messag that hugo cannot range over .Site.Taxonomies.whatever-the-i18n-value-was.

Actually i’m just re-reading your comment, and I see there is a difference here. I want the url to be translated. And I will also translate the categores or tags in each post. My hacky link:

<a class="navbar-item "href="{{ $ctx.Site.BaseURL | absLangURL }}{{ $ctx.Site.Language.Lang }}/{{ 
$categories }}/{{ $key }}">{{ $key | humanize }}</a>  

works just fine… as long as the range is .Site.Taxonomies.categories or .Site.Taxonomies.fɛɛndɔ or whatever the language category is. So you see I have tried to range over

{{ print ".Site.Taxonomies." $categories. }}  And this is what is not working̀

I can see the issue is related to this: Using a Variable in a Range

Edited to add: The reason it fails is because you are iterating over $range , which is a string, and not the data file that you want to iterate over.

But still trying to find the right method/syntax…

ok this is solved. I finally got it! I can now change the langage of my blog and then the categories menu will display the categories of the current language.

{{ $ctx := . }}    
{{ $taxo := i18n "categories" }}    
{{ range $key, $value := ( index .Site.Taxonomies $taxo ) }}    
<a class="navbar-item "href="{{ $ctx.Site.BaseURL | absLangURL }}{{ $ctx.Site.Language.Lang }}/{{ 
 $taxo }}/{{ $key }}">{{ $key | humanize }}</a>    
{{ end }}

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