schmittjoh / JMSTranslationBundle

Puts the Symfony2 Translation Component on steroids
http://jmsyst.com/bundles/JMSTranslationBundle
426 stars 292 forks source link

Not parsing choices on ChoiceType form field when using function / method #415

Open marek1989 opened 7 years ago

marek1989 commented 7 years ago
Q A
Bundle version 1.3.1
Symfony version 3.1.6
PHP version 7.0.13

Expected behavior

When we are using function e.g. array_flip on choices option for Symfony\Component\Form\Extension\Core\Type\ChoiceType, the text of choices should be translated (added to the translation files)

Actual behavior

Text of choices is not added to translation files

Steps to reproduce

Having e.g. ExampleType class as below will not translate choices, because of the condition in JMS\TranslationBundle\Translation\Extractor\File\FormExtractor in line 227 as $item->value is instance of PhpParser\Node\Expr\FuncCall (not possible to use 'choices_as_values' on the form builder, as it is deprecated since Symfony 3.0).

class ExampleType extends AbstractType
{
    public static $testChoices = [
        1 => 'test.option_1',
        2 => 'test.option_2',
        3 => 'test.option_3',
        4 => 'test.option_4',
    ];

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('testField', FormType\ChoiceType::class, [
                    'label' => 'form.test.label',
                    'translation_domain' => 'test_domain',
                    'choice_translation_domain' => 'test_domain',
                    'choices' => array_flip(self::$visibleBudget)
                ]);
    }
....
....
}