How to support multiligual links to tags and categories in Hugo ?
I faced this issue in my custom Hugo theme. I use kmt theme on this website you are reading on. It is bilingual. You find blog posts in English and their translations in Arabic. But when I click on a tag in the Arabic part of the website, it is 404 NOT FOUND ! what?!
The problem is that the tag link is /tags/the-tag-itself
but to work it needs to be /ar/tags/the-tag-itself
. So, I knew it is a translation support problem.
I was using this code to show tags and categories.
|
|
Non-scalable, Manual Solution 🔗
I thought of a way to construct the final URL like this.
|
|
But I thing this fix will come to bite me later. So, I read more on Hugo documentation website, and found another way to list the tags and categories in idiomatic way.
Hugo idiomatic way 🔗
|
|
I got this code snippet from Hugo docs . This code is elegant, idiomatic and succinct.
But I wanna show the section of tags if there is at least one tag. And the same for categories.
I rewrote this code using with
as you can see below.
|
|
And the code for categories is.
|
|
You can see this code as the kmt theme project is open source . Feel free to edit or suggest edit and improvements.
I hope this post helps you. If you know a person who can benefit from this information, send them a link of this post. If you want to get notified about new posts, follow me on YouTube , Twitter (x) , LinkedIn , and GitHub .