Поддержка перевода label'ов формы

This commit is contained in:
Brokhovetsky Dmitriy 2015-04-14 13:32:33 +03:00
parent 3007472fe3
commit 0f9a265a9c

View File

@ -22,6 +22,7 @@ use Symfony\Component\Form\ResolvedFormTypeInterface;
use Symfony\Component\OptionsResolver\Exception\MissingOptionsException; use Symfony\Component\OptionsResolver\Exception\MissingOptionsException;
use Symfony\Component\Form\FormFactoryInterface; use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\Exception\FormException; use Symfony\Component\Form\Exception\FormException;
use Symfony\Component\Translation\TranslatorInterface;
class FormTypeParser implements ParserInterface class FormTypeParser implements ParserInterface
{ {
@ -35,6 +36,11 @@ class FormTypeParser implements ParserInterface
*/ */
protected $formRegistry; protected $formRegistry;
/**
* @var \Symfony\Component\Translation\TranslatorInterface
*/
protected $translator;
/** /**
* @var array * @var array
*/ */
@ -52,9 +58,10 @@ class FormTypeParser implements ParserInterface
'file' => DataTypes::FILE, 'file' => DataTypes::FILE,
); );
public function __construct(FormFactoryInterface $formFactory) public function __construct(FormFactoryInterface $formFactory, TranslatorInterface $translator)
{ {
$this->formFactory = $formFactory; $this->formFactory = $formFactory;
$this->translator = $translator;
} }
/** /**
@ -124,6 +131,7 @@ class FormTypeParser implements ParserInterface
private function parseForm($form) private function parseForm($form)
{ {
$parameters = array(); $parameters = array();
$domain = $form->getConfig()->getOption('translation_domain');
foreach ($form as $name => $child) { foreach ($form as $name => $child) {
$config = $child->getConfig(); $config = $child->getConfig();
$bestType = ''; $bestType = '';
@ -187,7 +195,7 @@ class FormTypeParser implements ParserInterface
'default' => null, 'default' => null,
'subType' => $subType, 'subType' => $subType,
'required' => $config->getRequired(), 'required' => $config->getRequired(),
'description' => ($config->getOption('description')) ? $config->getOption('description'):$config->getOption('label'), 'description' => $this->getFormDescription($config, $domain),
'readonly' => $config->getDisabled(), 'readonly' => $config->getDisabled(),
'children' => $children, 'children' => $children,
); );
@ -205,7 +213,7 @@ class FormTypeParser implements ParserInterface
'actualType' => 'string', 'actualType' => 'string',
'default' => $config->getData(), 'default' => $config->getData(),
'required' => $config->getRequired(), 'required' => $config->getRequired(),
'description' => ($config->getOption('description')) ? $config->getOption('description'):$config->getOption('label'), 'description' => $this->getFormDescription($config, $domain),
'readonly' => $config->getDisabled(), 'readonly' => $config->getDisabled(),
); );
} }
@ -221,7 +229,7 @@ class FormTypeParser implements ParserInterface
'subType' => $subType, 'subType' => $subType,
'default' => $config->getData(), 'default' => $config->getData(),
'required' => $config->getRequired(), 'required' => $config->getRequired(),
'description' => ($config->getOption('description')) ? $config->getOption('description'):$config->getOption('label'), 'description' => $this->getFormDescription($config, $domain),
'readonly' => $config->getDisabled(), 'readonly' => $config->getDisabled(),
); );
@ -330,4 +338,11 @@ class FormTypeParser implements ParserInterface
return $choices; return $choices;
} }
private function getFormDescription($config, $domain = null)
{
$descr = $config->getOption('description') ?:$config->getOption('label');
return $this->translator->trans($descr, [], $domain);
}
} }