我有一个问题需要翻译"在飞行中"表单类中的一些元素。这很奇怪,因为翻译器完美地适用于每个表单的标签(在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;
}
班级(没有额外的方法):
类BookType扩展了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 :(得分:0)
在浏览翻译器组件的代码之后,我不明白为什么这不起作用。
最后,这就是我所做的:
site.type.book: class:AppBundle \ Form \ BookType 标签: - {name:form.type} 参数:['@ doctrine','@ request_stack','@ logger','@ transnslator']