Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to preview a Wagtail page with translated fields in different languages?

I have a multi-language Wagtail website with two languages (English and German) using duplicated fields in my page models, e.g. a text block has two fields, text_de and text_en, and I define text as a translated field following the example in the translating content documentation. (I.e. I am NOT duplicating the whole page tree.) Here's an example how that looks in my code:

[models.py]

class MyPage(Page):
    ...
    text_de = models.CharField(max_length=1024)
    text_en = models.CharField(max_length=1024)
    text = TranslatedField('text_en', 'text_de')

Everything works perfectly fine, in templates I can just use {{ text }} and depending on the active language (using i18n patterns and LocaleMiddleware) the correct version is displayed.

BUT: I have issues with getting a page preview in both languages.

When an editor creates a draft page in Wagtail admin and clicks on 'preview', then a page preview is shown in the language used within the Wagtail admin, i.e. in the language defined by the current editor's language preferences in his or her account settings.

How could the editor also preview the page in another language (without switching the language in his or her account settings back and forth)?

Is there maybe a way to construct a view that sets a different language before creating the page preview? Or is there another way to solve this?

I've tried to find out when Wagtail/Django decides which language it should serve and found the method get_url_parts of wagtail/admin/core/models, Page class. The page_path returned from this function is suffixed with '/de' or '/en', depending on the editor's account settings. I can permanently change the language in which a page is displayed using translation.activate(). E.g. if I add translation.activate('en') to the get_context method of the MyPage class, then a MyPage-page and its previews are always shown in English. That's not really helpful.

I tried to construct a view, that first sets the language and then redirects to the preview like this:

[views.py]

from django.http import HttpResponseRedirect
from django.urls import reverse
from django.utils import translation

def preview_language(request, pk=None, language='en'):
    if language == 'de':
        translation.activate(language)
    else:
        translation.activate('en')
    return HttpResponseRedirect(reverse('wagtailadmin_pages:preview_on_edit', args=(pk,)))

But the language is set back to the editor's language from the account settings when the redirect is performed. And I am not sure that this is actually the way to go ...

Does anyone have a (different?) idea how to enable editors to easily preview a page with translated fields in different languages?

I am currently using Wagtail 2.4, Django 2.1, Python 3.5.

Any help is greatly appreciated! :-)

like image 423
Kristin Avatar asked Mar 07 '19 10:03

Kristin


1 Answers

Wagtail's "preview modes" feature would probably help here: https://docs.wagtail.io/en/stable/reference/pages/model_reference.html#wagtail.core.models.Page.preview_modes

You could define English and German as two preview modes on your page model, and override the serve_preview method to activate the appropriate translation based on the received mode_name argument.

like image 113
gasman Avatar answered Nov 09 '22 09:11

gasman