I am trying to make the URLs translatable according to the language selected for the site.
-
1What do you mean with translatable? Do you want to translate the slug or do you want to change the complete URL format?– Robin SchambachJan 29, 2018 at 9:53
-
Which one is the best and translatable?– Kantesh HotchandaniJan 30, 2018 at 10:44
-
I'm not sure what you want to know. You have to insert different values for different languages nothing more and nothing less. It's like every other field in Craft. Maybe you have to explain your issue properly– Robin SchambachJan 30, 2018 at 10:53
-
I have site in 2 different languages english and dutch, for example One single entry for About Us so its english/dutch URL is www.demo.com/about-us but i want dutch url like www.demo.com/over-ons– Kantesh HotchandaniJan 30, 2018 at 11:00
-
Ok, then go to your entry and change the slug field i.imgur.com/jVeyTjq.png I don't see any problem here– Robin SchambachJan 30, 2018 at 11:05
1 Answer
To give this an answer, the URI can be set on a per-site basis, so it is simply a matter of setting the URI for the English site to about-us
and the URI for the Dutch site to over-ons
on the section's settings page.
Then in your template, assuming you have the entry
, you can fetch the entry in any other site and output its URL:
{% set dutchEntry = craft.entries.id(entry.id).site('dutch').one() %}
{% if dutchEntry %}
<a href="{{ dutchEntry.url }}">Dutch</a>
{% endif %}
-
How do you translate this in the twig templates? Lets say <a href="{{ url('about-us') }}" when you want the dutch variant?– KevonSep 23, 2021 at 13:11
-
Assuming you have the
entry
, you can just fetch the dutch entry and output its URL. I updated my answer with the code for this. Sep 28, 2021 at 8:56