user7346216
user7346216

Reputation: 61

wagtail StreamField block name translation issue

What I found in docs is the following:

The parameter to StreamField is a list of (name, block_type) tuples. ‘name’ is used to identify the block type within templates and the internal JSON representation (and should follow standard Python conventions for variable names: lower-case and underscores, no spaces) and ‘block_type’ should be a block definition object as described below. (Alternatively, StreamField can be passed a single StreamBlock instance - see Structural block types.)

'name' will be displayed in wagtail admin interface as the name of block.

Now I want to translate the name of blocks in StreamField.

I tried to use gettext and ugettext_lazy to wrap the name.

If gettext is used, then the display text on admin interface will be language of "LANGUAGE_CODE" in 'settings/base.py'. User specified language preference in admin interface will not make a difference.

If ugettext_lazy is used and user specified language differs from LANGUAGE_CODE in 'settings/base.py', there will be keyerrors where key is the translated content.

Is there a way to translate the name properly?

Upvotes: 2

Views: 826

Answers (1)

gasman
gasman

Reputation: 25227

All block types accept a label argument for the name to display in the admin; you should apply the translation function there.

from django.utils.translation import ugettext_lazy as _

class MyPage(Page):
    body = StreamField([
        ('heading', blocks.CharBlock(label=_("Heading"))),
    ])

Upvotes: 4

Related Questions