Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow @Desc to appear earlier in the file #233

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
4 changes: 2 additions & 2 deletions Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ class ApiController
* @Route("/configs/{config}/domains/{domain}/locales/{locale}/messages",
* name="jms_translation_update_message",
* defaults = {"id" = null},
* options = {"i18n" = false})
* @Method("PUT")
* options = {"i18n" = false},
* methods={"PUT"})
* @param Request $request
* @param string $config
* @param string $domain
Expand Down
10 changes: 10 additions & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
<parameter key="jms_translation.extractor.file.translation_container_extractor">JMS\TranslationBundle\Translation\Extractor\File\TranslationContainerExtractor</parameter>
<parameter key="jms_translation.extractor.file.twig_extractor">JMS\TranslationBundle\Translation\Extractor\File\TwigFileExtractor</parameter>
<parameter key="jms_translation.extractor.file.form_extractor.class">JMS\TranslationBundle\Translation\Extractor\File\FormExtractor</parameter>
<parameter key="jms_translation.extractor.file.table_extractor.class">JMS\TranslationBundle\Translation\Extractor\File\TableExtractor</parameter>
<parameter key="jms_translation.extractor.file.toolbar_extractor.class">JMS\TranslationBundle\Translation\Extractor\File\ToolbarExtractor</parameter>
<parameter key="jms_translation.extractor.file.validation_extractor.class">JMS\TranslationBundle\Translation\Extractor\File\ValidationExtractor</parameter>
<parameter key="jms_translation.extractor.file.authentication_message_extractor.class">JMS\TranslationBundle\Translation\Extractor\File\AuthenticationMessagesExtractor</parameter>

Expand Down Expand Up @@ -91,6 +93,14 @@
<argument type="service" id="jms_translation.doc_parser" />
<tag name="jms_translation.file_visitor" />
</service>
<service id="jms_translation.extractor.file.table_extractor" class="%jms_translation.extractor.file.table_extractor.class%" public="false">
<argument type="service" id="jms_translation.doc_parser" />
<tag name="jms_translation.file_visitor" />
</service>
<service id="jms_translation.extractor.file.toolbar_extractor" class="%jms_translation.extractor.file.toolbar_extractor.class%" public="false">
<argument type="service" id="jms_translation.doc_parser" />
<tag name="jms_translation.file_visitor" />
</service>
<service id="jms_translation.extractor.file.translation_container_extractor" class="%jms_translation.extractor.file.translation_container_extractor%" public="false">
<tag name="jms_translation.file_visitor" />
</service>
Expand Down
19 changes: 14 additions & 5 deletions Tests/Translation/Extractor/File/DefaultPhpFileExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,37 @@ public function testExtractController()
$message->addSource(new FileSource($path, 45));
$expected->add($message);

$message = new Message('text.baz_qux');
$message->setDesc('Baz Qux');
$message->addSource(new FileSource($path, 51));
$expected->add($message);

$message = new Message('text.qux_baz');
$message->addSource(new FileSource($path, 52));
$expected->add($message);

$message = new Message('text.sign_up_successful');
$message->setDesc('Welcome %name%! Thanks for signing up.');
$message->addSource(new FileSource($path, 52));
$message->addSource(new FileSource($path, 59));
$expected->add($message);

$message = new Message('button.archive');
$message->setDesc('Archive Message');
$message->setMeaning('The verb (to archive), describes an action');
$message->addSource(new FileSource($path, 59));
$message->addSource(new FileSource($path, 66));
$expected->add($message);

$message = new Message('text.irrelevant_doc_comment', 'baz');
$message->addSource(new FileSource($path, 71));
$message->addSource(new FileSource($path, 78));
$expected->add($message);

$message = new Message('text.array_method_call');
$message->addSource(new FileSource($path, 76));
$message->addSource(new FileSource($path, 83));
$expected->add($message);

$message = new Message('text.var.assign');
$message->setDesc('The var %foo% should be assigned.');
$message->addSource(new FileSource($path, 82));
$message->addSource(new FileSource($path, 89));
$expected->add($message);

$this->assertEquals($expected, $catalogue);
Expand Down
7 changes: 7 additions & 0 deletions Tests/Translation/Extractor/File/Fixture/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ public function indexAction()
{
$this->session->setFlash('foo', $this->translator->trans(/** @Desc("Foo bar") */ 'text.foo_bar'));
}

public function anotherAction()
{
/** @Desc("Baz Qux") */
$this->session->setFlash('baz', $this->translator->trans('text.baz_qux'));
$this->session->setFlash('qux', $this->translator->trans('text.qux_baz'));
}

public function welcomeAction()
{
Expand Down
106 changes: 106 additions & 0 deletions Tests/Translation/Extractor/File/Fixture/DummyTableDefinition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

namespace StreamOne\DummyBundle\Table;

/**
* Table definition for the dummy list table
*/
class DummyTableDefinition implements TableDefinitionInterface
{
/**
* {@inheritdoc}
*/
public function buildTable(TableBuilder $builder)
{
$builder
->setTableId('dummy')
->setDefaultSortColumn('title')
->setPaginationEnabled()
->addColumn('', 'xyz', array())
->addColumn('dummy.xyz', 'xyz', array())
->addColumn(
/** @Desc("This is abc") */
'dummy.abc', 'abc', array(
'formatter' => array(
'formatter' => 'link',
'component' => 'dummy',
'page' => 'detail',
'route_params' => array(
'dummy' => '%id%',
),
'name_formatter' => array(
'formatter' => 'date',
'date_format' => \IntlDateFormatter::SHORT,
'time_format' => \IntlDateFormatter::SHORT,
),
),
'sortable' => true,
'sort_field' => 'created',
)
)
->addColumn(
/** @Desc("This is a test") */
'dummy.def', 'def', array(
'formatter' => array(
'formatter' => 'dropdown',
'links' => array(
/** @Desc("This is a drop-down link name") */
'dummy.linka' => array(
'link' => function (TableRowInterface $row)
{
return $row->getField('dummyextlink');
}
),
'dummy.linkb' => array(
'component' => 'dummy',
'page' => 'delete',
'route_params' => array(
'dummy' => '%id%',
)
),
)
),
'sortable' => true,
)
)
->addColumn(
'', 'def', array(
'formatter' => array(
'formatter' => 'icon',
'icons' => function ($value, TableRowInterface $row)
{
$icons = array();

switch ($row->getField('type'))
{
case 'a':
/** @Desc("Dummy icon desc") */
$icons[] = array(
'icon' => 'a',
'title' => 'dummy.icon.a'
);
break;
case 'bcd':
$icons[] = array(
'icon' => 'bcd',
'title' => 'dummy.icon.bcd'
);
break;
}

if ($row->getField('archived'))
{
$icons[] = 'archive';
}

return $icons;
}
),
)
)
->addColumn(
/** @Desc("Ghi") */
'dummy.ghi', 'ghi'
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace StreamOne\DummyBundle\Table;

/**
* Toolbar definition for the dummy list table
*/
class DummyToolbarDefinition implements TableDefinitionInterface
{
/**
* {@inheritdoc}
*/
public function buildToolbar(toolbarBuilder $builder)
{
$builder
->addButton('dummy.xyz', 'xyz', 'component', 'page', array())
->addButton(
/** @Desc("This is abc") */
'dummy.abc', 'abc', 'abc', 'def', array(
'dummy' => '%id%',
)
)
->addButton(
/** @Desc("This is a test") */
'dummy.def', 'def', 'ghi', 'jkl', array(
'dummy' => '%id%',
)
)
->addButton(
/** @Desc("Ghi") */
'dummy.ghi', 'ghi', 'ghi', 'ghi'
);
}
}
1 change: 1 addition & 0 deletions Tests/Translation/Extractor/File/Fixture/MyFormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function buildForm(FormBuilder $builder, array $options)
->add('states', 'choice', array(
'choices' => array('foo' => 'bar'),
'empty_value' => /** @Desc("Please select a state") */ 'form.states.empty_value',
'help' => /** @Desc("This is a help text") */ 'form.states.help',
))
->add('countries', 'choice', array('empty_value' => false))
->add('password', 'repeated', array(
Expand Down
35 changes: 21 additions & 14 deletions Tests/Translation/Extractor/File/FormExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public function testExtract()
$message->addSource(new FileSource($path, 37));
$expected->add($message);

$message = new Message('form.states.help');
$message->setDesc('This is a help text');
$message->addSource(new FileSource($path, 38));
$expected->add($message);

$message = new Message('form.label.lastname');
$message->setDesc('Lastname');
$message->addSource(new FileSource($path, 33));
Expand All @@ -66,65 +71,67 @@ public function testExtract()
$expected->add($message);

$message = new Message('form.label.password');
$message->addSource(new FileSource($path, 42));
$message->addSource(new FileSource($path, 43));
$expected->add($message);

$message = new Message('form.label.password_repeated');
$message->setDesc('Repeat password');
$message->addSource(new FileSource($path, 45));
$message->addSource(new FileSource($path, 46));
$expected->add($message);

$message = new Message('form.label.street', 'address');
$message->setDesc('Street');
$message->addSource(new FileSource($path, 50));
$message->addSource(new FileSource($path, 51));
$expected->add($message);

$message = new Message('form.label.zip', 'address');
$message->setDesc('ZIP');
$message->addSource(new FileSource($path, 55));
$message->addSource(new FileSource($path, 56));
$expected->add($message);

$message = new Message('form.error.password_mismatch', 'validators');
$message->setDesc('The entered passwords do not match');
$message->addSource(new FileSource($path, 47));
$message->addSource(new FileSource($path, 48));
$expected->add($message);

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

$message = new Message('field.with.placeholder');
$message->addSource(new FileSource($path, 59));
$message->addSource(new FileSource($path, 60));
$expected->add($message);

$message = new Message('form.placeholder.text');
$message->setDesc('Field with a placeholder value');
$message->addSource(new FileSource($path, 60));
$message->setLocaleString($message->getDesc());
$message->addSource(new FileSource($path, 61));
$expected->add($message);

$message = new Message('form.placeholder.text.but.no.label');
$message->setDesc('Field with a placeholder but no label');
$message->addSource(new FileSource($path, 64));
$message->setLocaleString($message->getDesc());
$message->addSource(new FileSource($path, 65));
$expected->add($message);

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

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

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

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

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

$this->assertEquals($expected, $this->extract('MyFormType.php'));
Expand Down
Loading