更改语言选项卡上的 A2LiX 翻译以表格形式获取语言

A2LiX Translation on change language tab get language in form

我需要更改 A2LiX 翻译选项卡的语言。如果我点击 "FR" 然后需要获取 Fr 并且点击 "NL" 需要在表单生成器中使用 Nl 语言 ('Need Language Here')。

对于一个字段中的查询生成器。 是否可以在选项卡更改时获取表单类型中的语言?

我的表格 class MixCampaignTranslationType :

<?php

namespace BackEndBundle\Form;

use BackEndBundle\Validator\Constraints\CampaignSlugDuplicate;
use BackEndBundle\Validator\Constraints\MaxMixCampaign;
use Doctrine\ORM\EntityRepository;
use SurveyBundle\Entity\Survey;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Type;

class MixCampaignTranslationType extends AbstractType
{
    public $campaignId = null;

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        if (null !== $options['campaignId']) {
            $this->campaignId = $options['campaignId'];
        }

        $builder
            ->add('surveys', EntityType::class, [
                'class'    => Survey::class,
                'required' => false,
                'multiple' => true,
                'mapped'   => true,
                'attr'     => [
                    'class' => 'select2',
                ],
                'constraints' => new MaxMixCampaign(),
            ])
            ->add('startDate', DateTimeType::class, [
                'widget'      => 'single_text',
                'format'      => 'yyyy-MM-dd',
                'constraints' => new NotBlank(),
                'attr'        => [
                    'class' => 'datepicker',
                ],
            ])
           ->add('endDate', DateTimeType::class, [
                'widget'      => 'single_text',
                'format'      => 'yyyy-MM-dd',
                'constraints' => new NotBlank(),
                'attr'        => [
                    'class' => 'datepicker',
                ],
            ])
           ->add('slug', TextType::class, [
                'constraints' => [new NotBlank(), new Type('string'), new CampaignSlugDuplicate($this->campaignId, $options['locale'], 'mix')],
             ])
            ->add('isClosed');
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => 'SurveyBundle\Entity\MixCampaignTranslation',
            'locale'     => 'fr',
            'campaignId' => null
        ]);
    }

    public function getName()
    {
        return 'back_end_bundle_mix_campaign_translation_type';
    }
}

您可以创建自定义 TranslationFormType,然后将循环语言作为当前语言传递,请检查如下。

/**
 * @param \Symfony\Component\Form\FormBuilderInterface $builder
 * @param array                                        $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->addEventSubscriber($this->translationsListener);
    $formsOptions = $this->translationForm->getFormsOptions($options);
    foreach ($options['locales'] as $locale) {
        if (isset($formsOptions[$locale])) {
            $builder->add($locale, $options['form_type'], [
                    'currentLanguage' => $locale,
                    'campaignId' => $formsOptions[$locale]['campaignId']
                ]
            );
        }
    }
}

并且您可以使用 currentLanguage 在 form_type 中过滤 query_builder 中的数据。

希望对您有所帮助。