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

时间:2018-01-31 12:54:55

标签: php symfony bundle symfony-3.3 a2lix-translation

我需要更改A2LiX翻译标签的语言。如果我点击" FR"然后需要得到Fr并点击" NL"在表单构建器中需要Nl语言('需要语言在这里')。

对于一个字段中的querybuilder。 当标签更改时,是否可以在表单类型中获取语言?

enter image description here

My Form类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';
    }
}

1 个答案:

答案 0 :(得分:2)

您可以创建自定义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中的数据。

希望它会对你有所帮助。