schmittjoh / JMSTranslationBundle

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

JMS translation doesn't extract dynamic form build? #444

Closed ArmandArthur closed 7 years ago

ArmandArthur commented 7 years ago
Q A
Bundle version dev-master
Symfony version 3.2.8
PHP version ??

I'm not good in english but when i do:

$builder->addEventListener( FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($formModifier, $builder ) { $builder ->add('numero',TextType::class, array( 'label' => 'form.lot.numero' )) } ); it's works

and when i do

    $formModifier = function (FormInterface $form, $lot = null) {

        if($lot->getId() == null)
        {

            $champsOptionnelsLotList = $this->em->getRepository('AppBundle:FiscaliteChampsOptionnels')->findAll();
        }
        else
        {
            $champsOptionnelsLotList = $lot->getProgramme()->getChampsOptionnelsLot()->getValues();
        }

        foreach($champsOptionnelsLotList as $key => $champsOptionnelsLot)
        {
            $form->add($champsOptionnelsLot->getNomChampsLot(),TextType::class, array(
                'label' => $champsOptionnelsLot->getKeyTranslation()
            ));
        }
    };

    $builder->addEventListener(
        FormEvents::PRE_SET_DATA,
        function (FormEvent $event) use ($formModifier) {
                $data = $event->getData();
                $formModifier($event->getForm(), $data);

        }
    );

it nots works

as i do: $array = array('var','pour'); foreach($array as $key => $value) { $builder->add('numero1', TextType::class, array( 'label' => 'form.'.$value ));
}

it's don't work

it's normal ou it is a bug?

gnat42 commented 7 years ago

This is normal as far as I know. Its not possible to extract labels from files where the data is dynamic. You need to implement Translation/TranslationContainerInterface which provides getTranslationMessages that the extractor will look for.

ArmandArthur commented 7 years ago

How i can acces to entity manager form getTranslationMessages, it's a static function..? thanks

gnat42 commented 7 years ago

If your labels are retrieved from a DB, I think you are expected to use other methods to provide them in a translated way. This is to translate the interface and static strings.