Form labels translation support.

This commit is contained in:
Vitaliy Chesnokov 2019-04-24 17:26:10 +03:00
parent 81274509da
commit befd12e3e5
No known key found for this signature in database
GPG Key ID: FD23DF1B48ECC3EB
3 changed files with 31 additions and 7 deletions

View File

@ -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;
}
}

View File

@ -10,6 +10,7 @@
<services>
<service id="nelmio_api_doc.parser.form_type_parser" class="%nelmio_api_doc.parser.form_type_parser.class%">
<argument type="service" id="form.factory" />
<argument type="service" id="translator" />
<argument>%nelmio_api_doc.sandbox.entity_to_choice%</argument>
<tag name="nelmio_api_doc.extractor.parser" />
</service>

View File

@ -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);