From befd12e3e5ff8b7e0c7e31081b7ccaa40097b58d Mon Sep 17 00:00:00 2001 From: Vitaliy Chesnokov Date: Wed, 24 Apr 2019 17:26:10 +0300 Subject: [PATCH] Form labels translation support. --- Parser/FormTypeParser.php | 30 +++++++++++++++++++++++++---- Resources/config/services.form.xml | 1 + Tests/Parser/FormTypeParserTest.php | 7 ++++--- 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/Parser/FormTypeParser.php b/Parser/FormTypeParser.php index 5e225c2..2ea8bc7 100644 --- a/Parser/FormTypeParser.php +++ b/Parser/FormTypeParser.php @@ -22,6 +22,7 @@ use Symfony\Component\Form\FormFactoryInterface; use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\ResolvedFormTypeInterface; use Symfony\Component\OptionsResolver\Exception\MissingOptionsException; +use Symfony\Component\Translation\TranslatorInterface; class FormTypeParser implements ParserInterface { @@ -35,6 +36,11 @@ class FormTypeParser implements ParserInterface */ protected $formRegistry; + /** + * @var \Symfony\Component\Translation\TranslatorInterface + */ + protected $translator; + /** * @var boolean */ @@ -105,9 +111,10 @@ class FormTypeParser implements ParserInterface ), ); - public function __construct(FormFactoryInterface $formFactory, $entityToChoice) + public function __construct(FormFactoryInterface $formFactory, TranslatorInterface $translator, $entityToChoice) { $this->formFactory = $formFactory; + $this->translator = $translator; $this->entityToChoice = (boolean) $entityToChoice; } @@ -202,6 +209,8 @@ class FormTypeParser implements ParserInterface private function parseForm($form) { $parameters = array(); + $domain = $form->getConfig()->getOption('translation_domain'); + foreach ($form as $name => $child) { $config = $child->getConfig(); $options = $config->getOptions(); @@ -293,7 +302,7 @@ class FormTypeParser implements ParserInterface 'default' => null, 'subType' => $subType, 'required' => $config->getRequired(), - 'description' => ($config->getOption('description')) ? $config->getOption('description') : $config->getOption('label'), + 'description' => $this->getFormDescription($config, $domain), 'readonly' => $config->getDisabled(), 'children' => $children, ); @@ -311,7 +320,7 @@ class FormTypeParser implements ParserInterface 'actualType' => 'string', 'default' => $config->getData(), 'required' => $config->getRequired(), - 'description' => ($config->getOption('description')) ? $config->getOption('description') : $config->getOption('label'), + 'description' => $this->getFormDescription($config, $domain), 'readonly' => $config->getDisabled(), ); } @@ -327,7 +336,7 @@ class FormTypeParser implements ParserInterface 'subType' => $subType, 'default' => $config->getData(), 'required' => $config->getRequired(), - 'description' => ($config->getOption('description')) ? $config->getOption('description'):$config->getOption('label'), + 'description' => $this->getFormDescription($config, $domain), 'readonly' => $config->getDisabled(), ); @@ -445,4 +454,17 @@ class FormTypeParser implements ParserInterface return $choices; } + + private function getFormDescription($config, $domain = null) + { + $description = ($config->getOption('description')) + ? $config->getOption('description') + : $config->getOption('label'); + + if ($description != null) { + return $this->translator->trans($description, [], $domain); + } + + return null; + } } diff --git a/Resources/config/services.form.xml b/Resources/config/services.form.xml index 133ddc8..5d51842 100644 --- a/Resources/config/services.form.xml +++ b/Resources/config/services.form.xml @@ -10,6 +10,7 @@ + %nelmio_api_doc.sandbox.entity_to_choice% diff --git a/Tests/Parser/FormTypeParserTest.php b/Tests/Parser/FormTypeParserTest.php index 22bfca5..5222859 100644 --- a/Tests/Parser/FormTypeParserTest.php +++ b/Tests/Parser/FormTypeParserTest.php @@ -22,6 +22,7 @@ use Symfony\Component\Form\Extension\Core\Type\DateTimeType; use Symfony\Component\Form\FormFactory; use Symfony\Component\Form\FormFactoryBuilder; use Symfony\Component\Form\ResolvedFormTypeFactory; +use Symfony\Component\Translation\Translator; class FormTypeParserTest extends \PHPUnit_Framework_TestCase { @@ -37,7 +38,7 @@ class FormTypeParserTest extends \PHPUnit_Framework_TestCase $formFactoryBuilder->addTypeExtension(new DescriptionFormTypeExtension()); $formFactoryBuilder->addType(new DependencyType(array('foo'))); $formFactory = $formFactoryBuilder->getFormFactory(); - $formTypeParser = new FormTypeParser($formFactory, $entityToChoice = true); + $formTypeParser = new FormTypeParser($formFactory, new Translator('en'), $entityToChoice = true); set_error_handler(array('Nelmio\ApiDocBundle\Tests\WebTestCase', 'handleDeprecation')); trigger_error('test', E_USER_DEPRECATED); @@ -64,7 +65,7 @@ class FormTypeParserTest extends \PHPUnit_Framework_TestCase $formFactoryBuilder->addExtension(new CoreExtension()); $formFactoryBuilder->addTypeExtension(new DescriptionFormTypeExtension()); $formFactory = $formFactoryBuilder->getFormFactory(); - $formTypeParser = new FormTypeParser($formFactory, $entityToChoice = true); + $formTypeParser = new FormTypeParser($formFactory, new Translator('en'), $entityToChoice = true); set_error_handler(array('Nelmio\ApiDocBundle\Tests\WebTestCase', 'handleDeprecation')); trigger_error('test', E_USER_DEPRECATED); @@ -87,7 +88,7 @@ class FormTypeParserTest extends \PHPUnit_Framework_TestCase $formFactoryBuilder->addTypeExtension(new DescriptionFormTypeExtension()); $formFactoryBuilder->addType(new DependencyType(array('bar'))); $formFactory = $formFactoryBuilder->getFormFactory(); - $formTypeParser = new FormTypeParser($formFactory, $entityToChoice = false); + $formTypeParser = new FormTypeParser($formFactory, new Translator('en'), $entityToChoice = false); set_error_handler(array('Nelmio\ApiDocBundle\Tests\WebTestCase', 'handleDeprecation')); trigger_error('test', E_USER_DEPRECATED);