g国际化形式领域

时间:2018-07-12 04:47:43

标签: wagtail

如何为字段标签,帮助文本等实施国际化支持。 字段的标准乘法似乎不起作用 例如 文档中提供的用于创建联系表格的示例。我试图为其他语言添加额外的字段(文档中介绍的第一种方法)

  from django.db import models

  from wagtail.core.models import Page
  from modelcluster.fields import ParentalKey
  from wagtail.admin.edit_handlers import (
    FieldPanel, FieldRowPanel,
    InlinePanel, MultiFieldPanel
  )

  from wagtail.core.fields import RichTextField
  from wagtail.contrib.forms.models import (
    AbstractEmailForm, AbstractForm, AbstractFormField
  )
  from wagtail.contrib.forms.edit_handlers import FormSubmissionsPanel
  from dimaplus.languages import TranslatedField
  # Create your models here.

  class FormField(AbstractFormField):
    page = ParentalKey(
        'FormPage',
        on_delete=models.CASCADE,
        related_name='form_fields',
    )
    label_fa = models.CharField(max_length=255)
    label_ru = models.CharField(max_length=255)
    label_ar = models.CharField(max_length=255)

    tr_label_tag = TranslatedField(
      'label',
      'label_fa',
      'label_ar',
      'label_ru',
    )
    panels = AbstractFormField.panels + [
        FieldPanel('label_fa'),
        FieldPanel('label_ru'),
        FieldPanel('label_ar'),
    ]
  class FormPage(AbstractEmailForm, Page):

    intro = RichTextField(blank=True)
    intro_fa = RichTextField(blank=True)
    intro_ar = RichTextField(blank=True)
    intro_ru = RichTextField(blank=True)

    thank_you_text = RichTextField(blank=True)
    thank_you_text_fa = RichTextField(blank=True)
    thank_you_text_ar = RichTextField(blank=True)
    thank_you_text_ru = RichTextField(blank=True)

    # 'body',
    # 'body_fa',
    # 'body_ar',
    # 'body_ru',
    tr_intro = TranslatedField(
        'intro',
        'intro_fa',
        'intro_ar',
        'intro_ru'
    )
    tr_thank_you_text = TranslatedField(
        'thank_you_text',
        'thank_you_text_fa',
        'thank_you_text_ar',
        'thank_you_text_ru'
    )

    content_panels = AbstractEmailForm.content_panels + [
        FormSubmissionsPanel(),
        FieldPanel('intro_fa', classname='full'),
        InlinePanel('form_fields', label="Form fields"),
        FieldPanel('thank_you_text_fa', classname="full"),
        MultiFieldPanel([
            FieldPanel('intro'),
            FieldPanel('thank_you_text'),
            FieldPanel('intro_ar'),
            FieldPanel('thank_you_text_ar'),
            FieldPanel('intro_ru'),
            FieldPanel('thank_you_text_ru'),
        ]),
        MultiFieldPanel([
            FieldRowPanel([
                FieldPanel('from_address', classname="col6"),
                FieldPanel('to_address', classname="col6"),
            ]),
            FieldPanel('subject'),
        ], "Email"),
    ]

在文档中简单地给出了'TranslatedField'类:

from django.utils import translation
from django.utils.translation import gettext_lazy as _

#translation class
class TranslatedField:
    def __init__(self,
                 en_field,
                 fa_field,
                 ar_field,
                 ru_field):
        self.en_field = en_field
        self.fa_field = fa_field
        self.ar_field = ar_field
        self.ru_field = ru_field

    def __get__(self, instance, owner):
        lang = translation.get_language()
        if lang == 'fa':
            return getattr(instance, self.fa_field)
        elif lang == 'ar':
            return  getattr(instance, self.ar_field)
        elif lang == 'ru':
            return  getattr(instance, self.ru_field)
        else:
            return getattr(instance, self.en_field)

2 个答案:

答案 0 :(得分:0)

from django.utils.translation import ugettext_lazy as _

class FormPage(AbstractEmailForm, Page):
    intro = models.CharField(
        max_length=255, 
        verbose_name=_('Introduction')
        help_text=_('The first paragraph of the page.')
    )

https://docs.djangoproject.com/en/2.0/topics/i18n/translation/ https://docs.djangoproject.com/en/2.0/ref/django-admin/#django-admin-makemessages

答案 1 :(得分:0)

以防有人偶然发现此问题。可以使用文档中的示例执行此操作。您必须将label字段本身用于翻译,因为这是wagtail用来构建表单的字段。只需使用翻译字段覆盖它即可。

class FormField(AbstractFormField):
    label_en = models.CharField(max_length=250)
    label_de = models.CharField(max_length=250)
    label_fr = models.CharField(max_length=250)
    label_ru = models.CharField(max_length=250)

    label = TranslatedField(
        'label_en',
        'label_de',
        'label_fr',
        'label_ru'
    )

    content_panels = AbstractEmailForm.content_panels + [                 
        FieldPanel('label_en'),
        FieldPanel('label_de'),
        FieldPanel('label_fr'),
        FieldPanel('label_ru'),
    ]

与帮助文本字段相同。这些是课程使用的字段:

panels = [
    FieldPanel('label'),
    FieldPanel('help_text'),
    FieldPanel('required'),
    FieldPanel('field_type', classname="formbuilder-type"),
    FieldPanel('choices', classname="formbuilder-choices"),
    FieldPanel('default_value', classname="formbuilder-default"),
]