Skip to content

Commit 4fe0e6d

Browse files
Merge branch 'lakiboy-master'
2 parents 18b9e6f + 9a2f2e5 commit 4fe0e6d

File tree

7 files changed

+86
-1
lines changed

7 files changed

+86
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## v2.5.0
4+
5+
- Support for 'translation_domain' option for labels (#27).
6+
37
## v2.2.1
48

59
- Forms now accept pre-filled data (#21).

features/interactive.feature

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,21 @@ Feature: It is possible to interactively fill in a form from the CLI
147147
)
148148
"""
149149

150+
Scenario: Translatable label
151+
When I run the command "form:translatable_label" and I provide as input
152+
"""
153+
empty[enter]empty[enter]
154+
"""
155+
Then the command has finished successfully
156+
And the output should be
157+
"""
158+
Child first field: Parent second field: Array
159+
(
160+
[fieldOne] => empty
161+
[fieldTwo] => empty
162+
)
163+
"""
164+
150165
Scenario: Provide no value with no default value, value should be asked again
151166
When I run the command "form:name_without_default_value" and I provide as input "[enter]Jelmer[enter]"
152167
And the output should contain

src/Bridge/Transformer/AbstractTransformer.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function __construct(TranslatorInterface $translator)
3232
*/
3333
protected function questionFrom(FormInterface $form)
3434
{
35-
$question = $this->translator->trans(FormUtil::label($form));
35+
$question = $this->translator->trans(FormUtil::label($form), [], $this->translationDomainFrom($form));
3636

3737
return $this->formattedQuestion($question, $this->defaultValueFrom($form));
3838
}
@@ -52,6 +52,20 @@ protected function defaultValueFrom(FormInterface $form)
5252
return $defaultValue;
5353
}
5454

55+
/**
56+
* @param FormInterface $form
57+
*
58+
* @return string|null
59+
*/
60+
protected function translationDomainFrom(FormInterface $form)
61+
{
62+
while ((null === $domain = $form->getConfig()->getOption('translation_domain')) && $form->getParent()) {
63+
$form = $form->getParent();
64+
}
65+
66+
return $domain;
67+
}
68+
5569
/**
5670
* @param string $question
5771
* @param string $defaultValue
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Matthias\SymfonyConsoleForm\Tests\Form;
4+
5+
use Symfony\Component\Form\AbstractType;
6+
use Symfony\Component\Form\Extension\Core\Type\TextType;
7+
use Symfony\Component\Form\FormBuilderInterface;
8+
use Symfony\Component\OptionsResolver\OptionsResolver;
9+
10+
class TranslatableLabelType extends AbstractType
11+
{
12+
public function buildForm(FormBuilderInterface $builder, array $options)
13+
{
14+
$builder
15+
->add(
16+
'fieldOne',
17+
TextType::class,
18+
[
19+
'label' => 'field_one',
20+
'translation_domain' => 'child_domain',
21+
]
22+
)
23+
->add(
24+
'fieldTwo',
25+
TextType::class,
26+
[
27+
'label' => 'field_two',
28+
]
29+
)
30+
;
31+
}
32+
33+
public function configureOptions(OptionsResolver $resolver)
34+
{
35+
$resolver->setDefault('translation_domain', 'parent_domain');
36+
}
37+
}

test/config.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,21 @@ services:
7474
tags:
7575
- { name: console.command }
7676

77+
translatable_label_command:
78+
class: Matthias\SymfonyConsoleForm\Tests\Command\PrintFormDataCommand
79+
arguments:
80+
- Matthias\SymfonyConsoleForm\Tests\Form\TranslatableLabelType
81+
- translatable_label
82+
tags:
83+
- { name: console.command }
84+
7785
framework:
7886
form:
7987
csrf_protection: true
8088
secret: "%secret%"
8189
csrf_protection: ~
8290
session:
8391
handler_id: ~
92+
translator:
93+
paths:
94+
- "%kernel.root_dir%/translations"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
field_one: Child first field
2+
field_two: Child second field
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
field_one: Parent first field
2+
field_two: Parent second field

0 commit comments

Comments
 (0)