Skip to content

Commit

Permalink
Merge pull request #453 from justimmo/bugfix/translation-domain-set-d…
Browse files Browse the repository at this point in the history
…efault

Fixes translation_domain detection with setDefault
  • Loading branch information
gnat42 authored Aug 17, 2017
2 parents 593e788 + bafc346 commit a192120
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/*
* Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace JMS\TranslationBundle\Tests\Translation\Extractor\File\Fixture;

use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class MyFormType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('firstname', 'text', array(
'label' => 'form.label.firstname',
))
->add('lastname', 'text', array(
'label' => /** @Desc("Lastname") */ 'form.label.lastname',
))
->add('street', 'text', array(
'label' => /** @Desc("Street") */ 'form.label.street',
'translation_domain' => 'address'
))
;
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefault('translation_domain', 'person');
$resolver->setDefault('other_option', 'should_not_override_domain');
}
}
28 changes: 21 additions & 7 deletions Tests/Translation/Extractor/File/FormExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,11 @@ public function testExtractWithInterface()
$this->assertEquals($expected, $this->extract('MyFormTypeWithInterface.php'));
}

/**
* This test is used to check if the default 'translation_domain' option
* set for the entire form is extracted correctly
*/
public function testExtractWithDefaultDomain()
protected function getDefaultDomainFixture($fixtureFile)
{
$expected = new MessageCatalogue();
$fileSourceFactory = $this->getFileSourceFactory();
$fixtureSplInfo = new \SplFileInfo(__DIR__.'/Fixture/MyFormTypeWithDefaultDomain.php');
$fixtureSplInfo = new \SplFileInfo($fixtureFile);

$message = new Message('form.label.lastname', 'person');
$message->setDesc('Lastname');
Expand All @@ -220,7 +216,25 @@ public function testExtractWithDefaultDomain()
$message->addSource($fileSourceFactory->create($fixtureSplInfo, 37));
$expected->add($message);

$this->assertEquals($expected, $this->extract('MyFormTypeWithDefaultDomain.php'));
return $expected;
}

/**
* This test is used to check if the default 'translation_domain' option
* set for the entire form is extracted correctly
*/
public function testExtractWithDefaultDomain()
{
$this->assertEquals($this->getDefaultDomainFixture(__DIR__.'/Fixture/MyFormTypeWithDefaultDomain.php'), $this->extract('MyFormTypeWithDefaultDomain.php'));
}

/**
* This test is used to check if the default 'translation_domain' option
* set for the entire form is extracted correctly when set via setDefault
*/
public function testExtractWithDefaultDomainSetDefault()
{
$this->assertEquals($this->getDefaultDomainFixture(__DIR__.'/Fixture/MyFormTypeWithDefaultDomainSetDefault.php'), $this->extract('MyFormTypeWithDefaultDomainSetDefault.php'));
}

/**
Expand Down
11 changes: 10 additions & 1 deletion Translation/Extractor/File/FormExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function enterNode(Node $node)
}

$name = strtolower($node->name);
if ('setdefaults' === $name || 'replacedefaults' === $name) {
if ('setdefaults' === $name || 'replacedefaults' === $name || 'setdefault' === $name) {
$this->parseDefaultsCall($node);
return;
}
Expand Down Expand Up @@ -310,6 +310,15 @@ private function parseDefaultsCall(Node $node)
return;
}

if (isset($node->args[1])
&& $node->args[0]->value instanceof Node\Scalar\String_
&& $node->args[1]->value instanceof Node\Scalar\String_
&& 'translation_domain' === $node->args[0]->value->value
) {
$this->defaultDomain = $node->args[1]->value->value;
return;
}

// ignore everything except an array
if (!$node->args[0]->value instanceof Node\Expr\Array_) {
return;
Expand Down

0 comments on commit a192120

Please sign in to comment.