2015-05-28 36 views
0

翻译我有一个FormErrors帮手:Symfony的形状误差由助手

class FormErrors implements \JsonSerializable 
{ 

    public function __construct(FormInterface $form) 
    { 
     $this->form = $form; 
    } 

    public function all() 
    { 
     return $this->getErrors($this->form); 
    } 

    private function getErrors(FormInterface $form) 
    { 
     $result = array(); 
     $result[$form->getName()] = array(); 
     foreach ($form->getErrors() as $error) { 
      $result[$form->getName()][] = $error->getMessage(); 
     } 


     foreach ($form->all() as $child) 
     { 
      $errors = $this->getErrors($child); 

      if (count($errors[$child->getName()])) 
       $result[$form->getName()][$child->getName()] = $errors[$child->getName()]; 
     } 
     return $result; 
    } 

    public function jsonSerialize() 
    { 
     return $this->all(); 
    } 

} 

它这样使用: return (new \Bundle\Util\FormErrors($form))->all();

但是这个代码不返回翻译的消息。 该文档明确指出$ error-> getMessage()应该返回一个已翻译的消息。

我到处都是通过区域设置进行调试,它没有设置为英文,但我仍然收到英文消息。

我宁愿不将容器注入帮助器,因为我不需要更改帮助器的构造函数,并且因为我在我的应用程序的许多地方使用此函数,所以可能会破坏代码。

+0

你有翻译文件为您的语言环境? –

+0

@MichaelSivolobov是的 –

回答

0

加入你的错误,在“validators.en.yml”或指定翻译域的形式可能会有所帮助:

$resolver->setDefaults(array(
    'translation_domain' => 'errors' 
));