0
votes

I have set up a multiligual Wagtail website which can switch between Japanese and English. Each page in the cms has two fields for content that needs to be translated. I am trying to find out if a translatable field is left blank, whether the content from a fallback language field can be displayed.

This relates to this question but my set up seems different: [Django, how to create a fallback language in a multilingual website?

class TranslatedField(object):

def __init__(self, en_field, ja_field):
    self.en_field = en_field
    self.ja_field = ja_field

def __get__(self, instance, owner):
    if translation.get_language() == 'ja':
        return getattr(instance, self.ja_field)
    else:
        return getattr(instance, self.en_field)

class HomePage(Page):

body_en = RichTextField(blank=True)
body_ja = RichTextField(blank=True)

body = TranslatedField(
    'body_en',
    'body_ja',
)

content_panels = Page.content_panels + [
    ImageChooserPanel('background_image'),
    FieldPanel('body_en', classname="full"),
    FieldPanel('body_ja', classname="full")
]

So then in the template the content is just called with {{page.body|richtext}}. Maybe I would need to add something to my def get if/else statement.

Thanks for any help.

1

1 Answers

0
votes

You can just check if the variable in the template is set like so:

<div>
  <span>
     {% if page.body_jp %}
        私の日本語テキスト
     {% else %}
        This is my fallback text in English!
     {% endif %}
  </span>
</div>