Skip to content

Commit 73ca466

Browse files
committed
Extract translations from form 'help' option
1 parent d14af48 commit 73ca466

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
namespace JMS\TranslationBundle\Tests\Translation\Extractor\File\Fixture;
22+
23+
use Symfony\Component\Form\AbstractType;
24+
use Symfony\Component\Form\FormBuilderInterface;
25+
26+
class MyHelpFormType extends AbstractType
27+
{
28+
public function buildForm(FormBuilderInterface $builder, array $options)
29+
{
30+
$builder
31+
->add('field_with_attr_help', 'text', [
32+
'label' => 'field.with.help',
33+
'help' => /** @Desc("Field with a help value") */ 'form.help.text',
34+
])
35+
->add('field_without_label_with_attr_help', 'text', [
36+
'label' => false,
37+
'help' => /** @Desc("Field with a help but no label") */ 'form.help.text.but.no.label',
38+
])
39+
->add('field_help', 'choice', [
40+
'help' => /** @Desc("Choice field with a help") */ 'form.choice_help'
41+
]);
42+
}
43+
}

Tests/Translation/Extractor/File/FormExtractorTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,37 @@
2626

2727
class FormExtractorTest extends BasePhpFileExtractorTest
2828
{
29+
/**
30+
* @group help
31+
*/
32+
public function testHelpExtract()
33+
{
34+
$expected = new MessageCatalogue();
35+
$fileSourceFactory = $this->getFileSourceFactory();
36+
$fixtureSplInfo = new \SplFileInfo(__DIR__ . '/Fixture/MyHelpFormType.php');
37+
38+
$message = new Message('field.with.help');
39+
$message->addSource($fileSourceFactory->create($fixtureSplInfo, 32));
40+
$expected->add($message);
41+
42+
$message = new Message('form.help.text');
43+
$message->setDesc('Field with a help value');
44+
$message->addSource($fileSourceFactory->create($fixtureSplInfo, 33));
45+
$expected->add($message);
46+
47+
$message = new Message('form.help.text.but.no.label');
48+
$message->setDesc('Field with a help but no label');
49+
$message->addSource($fileSourceFactory->create($fixtureSplInfo, 37));
50+
$expected->add($message);
51+
52+
$message = new Message('form.choice_help');
53+
$message->setDesc('Choice field with a help');
54+
$message->addSource($fileSourceFactory->create($fixtureSplInfo, 40));
55+
$expected->add($message);
56+
57+
$this->assertEquals($expected, $this->extract('MyHelpFormType.php'));
58+
}
59+
2960
/**
3061
* @group placeholder
3162
*/

Translation/Extractor/File/FormExtractor.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ public function enterNode(Node $node)
126126

127127
switch ($item->key->value) {
128128
case 'label':
129+
case 'help':
129130
$this->parseItem($item, $domain);
130131
break;
131132
case 'invalid_message':

0 commit comments

Comments
 (0)