How to access the translated version of a page that has a translated slug #308
-
Typically with common paths on Django the following works fine:
But what if we translated the slug? for example of a blog post Desired output
If I try the above again with a translated slug URL, I get Thanks again for all the help, new to wagtail-localize, wagtail and i18n - so all a bit overwhelming ATM. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hey @carlosnasillo You can't use Django's Instead, you need to get the translated page instance and access its For example: from wagtail.core.models import Locale
spanish = Locale.objects.get(language_code='es')
spanish_blog_page = english_blog_page.get_translation(spanish)
if spanish_blog_page.live:
return spanish_blog_page.url |
Beta Was this translation helpful? Give feedback.
Hey @carlosnasillo
You can't use Django's
translate_url
for this because it's unaware of Wagtail's own routing.Instead, you need to get the translated page instance and access its
.url
attribute. Wagtail will make sure the URL has the correct language prefix.For example: