This tutorial will show you a method of creating multilingual sites in Wagtail by duplicating the page tree.
For example::
/
en/
about/
contact/
fr/
about/
contact/
The root page
=============
The root page (``/``) should detect the browsers language and forward them to the correct language homepage (``/en/``, ``/fr/``). This page should sit at the site root (where the homepage would normally be).
We must set Django's ``LANGUAGES`` setting so we don't redirect non English/French users to pages that don't exist.
..code-block:: python
# settings.py
LANGUAGES = (
('en', _("English")),
('fr', _("French")),
)
# models.py
from django.utils import translation
from django.http import HttpResponseRedirect
from wagtail.wagtailcore.models import Page
class LanguageRedirectionPage(Page):
def serve(self, request):
# This will only return a language that is in the LANGUAGES Django setting
language = translation.get_language_from_request(request)
return HttpResponseRedirect(self.url + language + '/')
Linking pages together
======================
It may be useful to link different versions of the same page together to allow the user to easily switch between languages. But we don't want to increase the burden on the editor too much so ideally, editors should only need to link one of the pages to the other versions and the links between the other versions should be created implicitly.
As this behaviour needs to be added to all page types that would be translated, its best to put this behaviour in a mixin.
Here's an example of how this could be implemented (with English as the main language and French/Spanish as alternative languages):
..code-block:: python
class TranslatablePageMixin(models.Model):
# One link for each alternative language
# These should only be used on the main language page (english)