8000 [Form] merge translation parameters with value configured for parent form by xabbuh · Pull Request #40124 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] merge translation parameters with value configured for parent form #40124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
merge translation parameters with value configured for parent form
  • Loading branch information
xabbuh committed Feb 7, 2021
commit 3de453c368d1925dc1c1c4ef64fb0fe510407190
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,16 @@ public function mapViolation(ConstraintViolation $violation, FormInterface $form

if (null !== $this->translator) {
$form = $scope;
$translationParameters = $form->getConfig()->getOption('label_translation_parameters', []);

do {
$translationDomain = $form->getConfig()->getOption('translation_domain');
$translationParameters = array_merge($form->getConfig()->getOption('label_translation_parameters', []), $translationParameters);
} while (null === $translationDomain && null !== $form = $form->getParent());

$label = $this->translator->trans(
$label,
$scope->getConfig()->getOption('label_translation_parameters', []),
$translationParameters,
$translationDomain
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1786,6 +1786,44 @@ public function testLabelPlaceholderTranslatedWithTranslationDomainDefinedByPare
$this->assertSame('Message "translated foo label"', $errors[0]->getMessage());
}

public function testLabelPlaceholderTranslatedWithTranslationParametersMergedFromParentForm()
{
$translator = $this->createMock(TranslatorInterface::class);
$translator->expects($this->any())
->method('trans')
->with('foo', [
'{{ param_defined_in_parent }}' => 'param defined in parent value',
'{{ param_defined_in_child }}' => 'param defined in child value',
'{{ param_defined_in_parent_overridden_in_child }}' => 'param defined in parent overridden in child child value',
])
->willReturn('translated foo label')
;
$this->mapper = new ViolationMapper(null, $translator);

$form = $this->getForm('', null, null, [], false, true, [
'label_translation_parameters' => [
'{{ param_defined_in_parent }}' => 'param defined in parent value',
'{{ param_defined_in_parent_overridden_in_child }}' => 'param defined in parent overridden in child parent value',
],
]);
$child = $this->getForm('foo', 'foo', null, [], false, true, [
'label' => 'foo',
'label_translation_parameters' => [
'{{ param_defined_in_child }}' => 'param defined in child value',
'{{ param_defined_in_parent_overridden_in_child }}' => 'param defined in parent overridden in child child value',
],
]);
$form->add($child);

$violation = new ConstraintViolation('Message "{{ label }}"', null, [], null, 'data.foo', null);
$this->mapper->mapViolation($violation, $form);

$errors = iterator_to_array($child->getErrors());

$this->assertCount(1, $errors, $child->getName().' should have an error, but has none');
$this->assertSame('Message "translated foo label"', $errors[0]->getMessage());
}

public function testTranslatorNotCalledWithoutLabel()
{
$renderer = $this->getMockBuilder(FormRenderer::class)
Expand Down
0