我正在使用JMS Translation Bundle来提取我的翻译。一切正常,但当我尝试从表单构建器中提取翻译消息时,我收到以下消息:
[JMS\TranslationBundle\Exception\RuntimeException]
Unable to extract translation id for form label from non-string values, but got "PHPParser_Node_Expr_MethodCall" in /srv/local.project.com/app/../src/Project/MyBundleBundle/Form/Type/EmailType
.php on line 30. Please refactor your code to pass a string, or add "/** @Ignore */".
我使用的代码如下:
public function __construct($translator)
{
//translation service passed from controller
$this->trans = $translator;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('email', 'repeated', array(
'type' => 'email',
'first_name' => 'email',
'second_name' => 'email-repeat',
//TRANSLATION GIVES ERROR ON NEXT LINE
'invalid_message' => $this->trans->trans('online.form.email.errors.equal', array(), 'messages_roadrunner'),
'error_bubbling' => true,
'first_options' => array('label' => $this->trans->trans("online.form.email.fields.one", array(), 'messages_roadrunner')),
'second_options' => array('label' => $this->trans->trans("online.form.email.fields.second", array(), 'messages_roadrunner')),
)
);
$builder->add('optin', 'checkbox', array(
'required' => false,
'label' => $this->trans->trans('online.form.email.fields.optin', array(), 'messages_roadrunner')
));
}
奇怪的是,在我的控制器中我使用相同的东西,但一切都正常:
$content['text'] = $this->get('translator')->trans('online.tariff.text.no_advice', array(), 'messages_roadrunner');
有没有人有这个问题的经验?
提前致谢!
答案 0 :(得分:3)
不要忘记,默认情况下,提取器配置为自动从FormBuilder::add()
调用参数中提取值(请参阅doc)。在您的情况下,它期望'label'
是一个字符串,但它包含一个方法调用,并且出现错误。
这实际上意味着您不必将翻译器传递到FormType
。