Creating a language switch that returns to the home page in the desired language is relatively easy. Making it return to the same page in the desired language requires some more work.
Django has many language related utilities. In this tutorial we will use some of them to create a language switch that we can activate via a link in our navigation menu and that will return the page that we were on in the chosen language. Defining a view in our views.py
that returns the home page in the chosen language is relatively straightforward:
This assumes that we are using language prefixes for our urls. We first check whether the desired language_code
is in the LANGUAGES
of our settings. Then we set the homepage url to go to, activate the language and set a language cookie.
If we would like to return to the page we where on, but in a different language, the first thing we have to do is retrieve the page that we where on:
If we cannot find a previous page, then we default to the homepage. If there is a previous page and the current language is already the requested language, then we return immediately. We retrieve the current language with Django's function get_language
.
Now there are two options: either previous
belongs to a page that is part of the Wagtail tree, or it does not. To find previous
in the Wagtail tree requires a little url-engineering. First split off the path of the url with Python's function urlparse
:
Now we can look this up in the Wagtail tree, using the field url_path
. However, there is one small complication: in multilingual sites wagtailtrans prefixed the translatable root path to url_path
. We can find the current site using the method find_for_request
of Wagtail's Site model. So our lookup becomes (both paths have a beginning and ending /
, we remove one with the slice [1:]
):
Now we can retrieve the translated version of this page using the TranslatablePage
fields canonical_page
and language
, just as we did in our navigation menu.
That finishes the case where the previous page is part of the Wagtail tree. Multilingual pages in our project outside of the tree should be constructed with i18n patterns. Django provides a function to translate their urls:
This function is also used in Django's set_language
implementation. If translate_url
cannot find a translation, then it returns the original url. We already took care of the case where language_code
equals the current language, so if this happens then apparently there is no translation. In that case we redirect to the home page:
Combining these two cases and taking care of exceptions gives us the full code that we put in the views.py
file of our app:
In the urls.py
of our app we create a path to this function:
We can include these url patterns in our project urls.py
by adding:
If we would have more entries in our app urls.py
then we would probably do this differently, perhaps create a separate app, but for now this will do. We can now activate the function by creating links to /language/en/
, /language/fr/
etc. wherever we want the language switch. In a previous tutorial we have created a menu builder; we can create a menu language
, define the language links for every menu item, add icons with flags of the different languages to it, and add this menu as an entry to our main menu. Adding a new language in the future is then just a matter of adding it to the menu. View the video to see this in action.
Read on if you want to allow users to add comments to articles on your site.
Comment on this article (sign in first or confirm by name and email below)