Skip to content

How to access the translated version of a page that has a translated slug #308

Answered by kaedroho
carlosnasillo asked this question in Q&A
Discussion options

You must be logged in to vote

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:

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

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@carlosnasillo
Comment options

Answer selected by carlosnasillo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants