2016-08-27 5 views
0

フォームクラスのいくつかの要素を「オンザフライ」で翻訳する際に問題があります。これは非常に奇妙です。なぜなら、トランスレータは各フォームのラベル(buildForm()メソッド内)では完全に動作しますが、privateフォームのラベルでは動作しないからです。Symfony 3.0 FormType:翻訳者がbuildForm()以外のメソッドで動作しない

プロファイラによれば、トランスレータはこれらのメソッドでも呼び出されません。ここでは、トランスレータが呼び出さと動作します

public function buildForm(FormBuilderInterface $builder, array $options) { 

    $this->options = $options; 
    $builder 
     ->add('title', TextType::class, array(
       'label' => $this->getTranslator()->trans('Title') 
      ) 
     ) 

しかし、ここでは、同じクラスで、それはしていません:

private function getMaterialList() { 
    try { 
     $arrResult = $this->em->getRepository('AppBundle:Material')->findAllOrderedByNameWp(); 
     foreach($arrResult as $material) { 
      $material->setName($this->getTranslator()->trans($material->getName())); 
     } 
    } catch (NoResultException $e) { 
     unset($e); 
    } catch (Exception $ex) { 
     $this->logger->info('Unable to list materials from booktype class ' . $ex->getMessage()); 
     throw new \Exception(
      $this->getTranslator()->trans('errorListingMaterials') 
     ); 
    } 

    return $arrResult; 
} 

(余分なメソッドwhithout)クラス:

クラスBookType extends AbstractType {

private $translator; 
private $em; 
private $request; 
private $logger; 

private function getTranslator() { 

    if(!isset($this->translator)) { 
     $this->translator = new Translator($this->options['locale']); 
    } 

    return $this->translator; 
} 

/** 
* List the Materials 
* 
* @return an array of materials 
* @throws \Exception 
*/ 
private function getMaterialList() { 
    try { 
     $arrResult = $this->em->getRepository('AppBundle:Material')->findAllOrderedByNameWp(); 
     foreach($arrResult as $material) { 
      $material->setName($this->getTranslator()->trans($material->getName())); 
     } 
    } catch (NoResultException $e) { 
     unset($e); 
    } catch (Exception $ex) { 
     $this->logger->info('Unable to list materials from booktype class ' . $ex->getMessage()); 
     throw new \Exception(
      $this->getTranslator()->trans('errorListingMaterials') 
     ); 
    } 

    return $arrResult; 
} 

/** 
* Constructor 
* 
* @param Doctrine $doctrine 
* @param RequestStack $requestStack is the request stacl 
*/ 
public function __construct(Doctrine $doctrine, RequestStack $requestStack, $logger) { 
    $this->em = $doctrine->getManager(); 
    $this->request = $requestStack; 
    $this->logger = $logger;   
} 

public function buildForm(FormBuilderInterface $builder, array $options) { 

    $this->options = $options; 
    $builder 
     ->add('title', TextType::class, array(
       'label' => $this->getTranslator()->trans('Title') 
      ) 
     ) 
     ->add('description', TextareaType::class, array(
       'label' => $this->getTranslator()->trans('Description') 
      ) 
     ) 
     ->add('year', TextType::class, array(
       'label' => $this->getTranslator()->trans('Year') 
      ) 
     ) 
     ->add('editor', EntityType::class, array(
       'class' => 'AppBundle:Editor', 
       'label' => $this->getTranslator()->trans('Editor.single'), 
       'choices' => $this->getEditorList() 
      ) 
     ) 
     ->add('format', EntityType::class, array(
       'class' => 'AppBundle:Format', 
       'label' => $this->getTranslator()->trans('Format.single'), 
       'choices' => $this->getFormatList() 
      ) 
     ) 
     ->add('format', EntityType::class, array(
       'class' => 'AppBundle:Material', 
       'label' => $this->getTranslator()->trans('Material'), 
       'choices' => $this->getMaterialList() 
      ) 
     ) 
     ->add('language', EntityType::class, array(
       'class' => 'AppBundle:Language', 
       'label' => $this->getTranslator()->trans('Language.single'), 
       'choices' => $this->getLanguageList() 
      ) 
     ) 
     ->add('location', EntityType::class, array(
       'class' => 'AppBundle:Location', 
       'label' => $this->getTranslator()->trans('Location.plural'), 
       'choices' => $this->getLocationList(), 
       'multiple' => true 
      ) 
     ) 
     ->add('author', EntityType::class, array(
       'class' => 'AppBundle:Author', 
       'label' => $this->getTranslator()->trans('Author.plural'), 
       'choices' => $this->getAuthorList(), 
       'multiple' => true 
      ) 
     ) 
     ->add('kind', EntityType::class, array(
       'class' => 'AppBundle:Kind', 
       'label' => $this->getTranslator()->trans('Kind.plural'), 
       'choices' => $this->getKindList(), 
       'multiple' => true 
      ) 
     ) 
     ->add('isbn', TextType::class, array(
       'label' => $this->getTranslator()->trans('ISBN'), 
       'required' => false 
      ) 
     ) 
     ->add('keywords', TextType::class, array(
       'label' => $this->getTranslator()->trans('Keywords') 
      ) 
     ) 
     ->add('slug', TextType::class, array(
       'label' => $this->getTranslator()->trans('Slug') 
      ) 
     ) 
     ->add('submit', SubmitType::class, array(
      'label' => $this->getTranslator()->trans('Save') 
     )); 
} 

public function configureOptions(OptionsResolver $resolver) { 
    $resolver->setDefaults(array(
      'data_class' => 'AppBundle\Entity\Book', 
      'locale' => 'en', // the default locale 
     ) 
    ); 
} 

}

答えて

0

翻訳者コンポーネントのコードを歩いた後、私はなぜこれが機能していないのか理解できませんでした。

最後に、これは私がやったことです:私のフォームクラス

  • に部品のトランスレータのインスタンス化を削除3.0.2から3.1.3
    1. のアップグレードは、私の中のトランスレータコンポーネントを注入(サービスとして宣言されている)フォームクラス

    site.type.book: クラス:AppBundle \フォーム\ BookTyp E タグ: - {名:form.type} 引数:[ '@doctrine'、 '@request_stack'、 '@logger'、 '@translator']

  • 関連する問題