Skip to content

Commit

Permalink
Merge pull request #334 from alpixel/master
Browse files Browse the repository at this point in the history
Solving translation of choices label using SF > 2.7
  • Loading branch information
Nyholm committed Mar 31, 2016
2 parents 2f17050 + ffcca0b commit c7555f1
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
7 changes: 7 additions & 0 deletions Tests/Translation/Extractor/File/Fixture/MyFormType.php
Expand Up @@ -63,6 +63,13 @@ public function buildForm(FormBuilder $builder, array $options)
'label' => false,
'attr' => array('placeholder' => /** @Desc("Field with a placeholder but no label") */ 'form.placeholder.text.but.no.label')
))
->add('field_with_choice_as_values', 'choice', array(
'choices' => array(
'form.choice.choice_as_values.label.foo' => 'form.choice.choice_as_values.value.foo',
'form.choice.choice_as_values.label.bar' => 'form.choice.choice_as_values.value.bar'
),
'choices_as_values' => true,
))
;
$child = $builder->create('created', 'text', array(
'label' => 'form.label.created'
Expand Down
33 changes: 27 additions & 6 deletions Tests/Translation/Extractor/File/FormExtractorTest.php
Expand Up @@ -27,6 +27,7 @@
use JMS\TranslationBundle\Model\MessageCatalogue;
use PhpParser\Lexer;
use PhpParser\ParserFactory;
use Symfony\Component\HttpKernel\Kernel;

class FormExtractorTest extends \PHPUnit_Framework_TestCase
{
Expand All @@ -40,7 +41,13 @@ public function testExtract()
$expected = new MessageCatalogue();
$path = __DIR__.'/Fixture/MyFormType.php';

$message = new Message('bar');
// Symfony >= 3.0 switch the default behavior of the choice field following a BC break introduced in 2.7
// @see https://github.com/symfony/symfony/blob/master/UPGRADE-3.0.md#choices_as_values
if (Kernel::VERSION_ID >= 30000) {
$message = new Message('foo');
} else {
$message = new Message('bar');
}
$message->addSource(new FileSource($path, 36));
$expected->add($message);

Expand Down Expand Up @@ -83,7 +90,7 @@ public function testExtract()
$expected->add($message);

$message = new Message('form.label.created');
$message->addSource(new FileSource($path, 68));
$message->addSource(new FileSource($path, 75));
$expected->add($message);

$message = new Message('field.with.placeholder');
Expand All @@ -101,15 +108,23 @@ public function testExtract()
$expected->add($message);

$message = new Message('form.dueDate.empty.year');
$message->addSource(new FileSource($path, 72));
$message->addSource(new FileSource($path, 79));
$expected->add($message);

$message = new Message('form.dueDate.empty.month');
$message->addSource(new FileSource($path, 72));
$message->addSource(new FileSource($path, 79));
$expected->add($message);

$message = new Message('form.dueDate.empty.day');
$message->addSource(new FileSource($path, 72));
$message->addSource(new FileSource($path, 79));
$expected->add($message);

$message = new Message('form.choice.choice_as_values.label.foo');
$message->addSource(new FileSource($path, 68));
$expected->add($message);

$message = new Message('form.choice.choice_as_values.label.bar');
$message->addSource(new FileSource($path, 69));
$expected->add($message);

$this->assertEquals($expected, $this->extract('MyFormType.php'));
Expand All @@ -124,7 +139,13 @@ public function testExtractWithInterface()
$expected = new MessageCatalogue();
$path = __DIR__.'/Fixture/MyFormTypeWithInterface.php';

$message = new Message('bar');
// Symfony >= 3.0 switch the default behavior of the choice field following a BC break introduced in 2.7
// @see https://github.com/symfony/symfony/blob/master/UPGRADE-3.0.md#choices_as_values
if (Kernel::VERSION_ID >= 30000) {
$message = new Message('foo');
} else {
$message = new Message('bar');
}
$message->addSource(new FileSource($path, 36));
$expected->add($message);

Expand Down
18 changes: 18 additions & 0 deletions Translation/Extractor/File/FormExtractor.php
Expand Up @@ -33,6 +33,7 @@
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpKernel\Kernel;

class FormExtractor implements FileVisitorInterface, NodeVisitor
{
Expand Down Expand Up @@ -119,7 +120,24 @@ public function enterNode(Node $node)
}

if ('choices' === $item->key->value) {

//Checking for the choice_as_values in the same form item
$choicesAsValues = false;
foreach ($node->items as $choiceItem) {
if ($choiceItem->key !== null && 'choices_as_values' === $choiceItem->key->value) {
$choicesAsValues = ($choiceItem->value->name->parts[0] === 'true');
}
}

foreach ($item->value->items as $sitem) {
// If we have a choice as value that differ from the Symfony default strategy
// we should invert the key and the value
if (Kernel::VERSION_ID < 30000 && $choicesAsValues === true || Kernel::VERSION_ID >= 30000) {
$newItem = clone $sitem;
$newItem->key = $sitem->value;
$newItem->value = $sitem->key;
$sitem = $newItem;
}
$this->parseItem($sitem, $domain);
}
} elseif ('attr' === $item->key->value && is_array($item->value->items) ) {
Expand Down

0 comments on commit c7555f1

Please sign in to comment.