Skip to content

Commit 7c1c675

Browse files
committed
Add choice_translation_domain handling
+ Fix test
1 parent e2edc20 commit 7c1c675

File tree

3 files changed

+32
-8
lines changed

3 files changed

+32
-8
lines changed

Tests/Translation/Extractor/File/Fixture/MyFormType.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ public function buildForm(FormBuilder $builder, array $options)
7676
'choices' => array('foo' => 'bar'),
7777
'choice_translation_domain' => 'choice-domain'
7878
))
79+
->add('choices_without_translation', 'choice', array(
80+
'choices' => array('foo' => 'bar'),
81+
'choice_translation_domain' => false,
82+
))
83+
->add('untranslatable_label', 'text', array(
84+
'label' => 'bar',
85+
'translation_domain' => false,
86+
))
7987
;
8088
}
8189
}

Tests/Translation/Extractor/FileExtractorTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,9 @@
2323
use JMS\TranslationBundle\Translation\Extractor\File\FormExtractor;
2424
use Doctrine\Common\Annotations\AnnotationReader;
2525
use Symfony\Component\Validator\Mapping\Loader\AnnotationLoader;
26-
use Symfony\Component\Validator\Mapping\ClassMetadataFactory;
2726
use JMS\TranslationBundle\Translation\Extractor\File\ValidationExtractor;
2827
use JMS\TranslationBundle\Model\FileSource;
2928
use JMS\TranslationBundle\Model\Message;
30-
use JMS\TranslationBundle\Model\MessageCatalogue;
3129
use JMS\TranslationBundle\Translation\Extractor\File\TwigFileExtractor;
3230
use JMS\TranslationBundle\Translation\Extractor\File\TranslationContainerExtractor;
3331
use JMS\TranslationBundle\Translation\Extractor\File\DefaultPhpFileExtractor;
@@ -77,7 +75,7 @@ public function testExtractWithSimpleTestFixtures()
7775
$expected[$engine.'.foo_bar'] = $message;
7876
}
7977

80-
$actual = $this->extract(__DIR__.'/Fixture/SimpleTest/')->getDomain('messages')->all();
78+
$actual = $this->extract(__DIR__.'/Fixture/SimpleTest')->getDomain('messages')->all();
8179

8280
asort($expected);
8381
asort($actual);

Translation/Extractor/File/FormExtractor.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,30 @@ public function enterNode(\PHPParser_Node $node)
7070
if ($node instanceof \PHPParser_Node_Expr_Array) {
7171
// first check if a translation_domain is set for this field
7272
$domain = null;
73+
$choiceDomain = null;
7374
foreach ($node->items as $item) {
7475
if (!$item->key instanceof \PHPParser_Node_Scalar_String) {
7576
continue;
7677
}
7778

7879
if ('translation_domain' === $item->key->value) {
79-
if (!$item->value instanceof \PHPParser_Node_Scalar_String) {
80-
continue;
80+
if ($item->value instanceof \PHPParser_Node_Scalar_String) {
81+
$domain = $item->value->value;
82+
} elseif ($item->value instanceof \PHPParser_Node_Expr_ConstFetch) {
83+
$constant = (string) $item->value->name;
84+
if ($constant === 'false') {
85+
$domain = false;
86+
}
87+
}
88+
} elseif ('choice_translation_domain' === $item->key->value) {
89+
if ($item->value instanceof \PHPParser_Node_Scalar_String) {
90+
$choiceDomain = $item->value->value;
91+
} elseif ($item->value instanceof \PHPParser_Node_Expr_ConstFetch) {
92+
$constant = (string) $item->value->name;
93+
if ($constant === 'false') {
94+
$choiceDomain = false;
95+
}
8196
}
82-
83-
$domain = $item->value->value;
8497
}
8598
}
8699

@@ -118,7 +131,7 @@ public function enterNode(\PHPParser_Node $node)
118131

119132
if ('choices' === $item->key->value) {
120133
foreach ($item->value->items as $sitem) {
121-
$this->parseItem($sitem, $domain);
134+
$this->parseItem($sitem, $choiceDomain !== null ? $choiceDomain : $domain);
122135
}
123136
} elseif ('attr' === $item->key->value && is_array($item->value->items)) {
124137
foreach ($item->value->items as $sitem) {
@@ -255,6 +268,11 @@ private function parseItem($item, $domain = null)
255268
throw new RuntimeException($message);
256269
}
257270

271+
if ($domain === false) {
272+
// Don't translate when domain is `false`
273+
return;
274+
}
275+
258276
$source = new FileSource((string) $this->file, $item->value->getLine());
259277
$id = $item->value->value;
260278

0 commit comments

Comments
 (0)