フォームクラスのいくつかの要素を「オンザフライ」で翻訳する際に問題があります。これは非常に奇妙です。なぜなら、トランスレータは各フォームのラベル(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
)
);
}
}