gpt4 book ai didi

表单字段的 Wagtail 国际化

转载 作者:行者123 更新时间:2023-12-04 15:57:24 25 4
gpt4 key购买 nike

如何实现字段标签、帮助文本等的国际化支持。字段的标准乘法似乎不起作用例如文档中给出的用于创建联系表单的示例。我尝试为其他语言添加额外的字段(文档中描述的第一种方法)

  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)

最佳答案

以防有人偶然发现这个。可以使用文档中的示例来执行此操作。您必须使用标签字段本身进行翻译,因为那是 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'
)

# this is to remove the original label from the field panels
# you could also just copy the panels from the AbstractFormFieldClass
# wagtail will throw an error otherwise
AbstractFormField.panels.remove(next((x for x in AbstractFormField.panels if x.field_name == 'label'), None))

content_panels = AbstractFormField._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"),
]

关于表单字段的 Wagtail 国际化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51297460/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com