Skip to content

Setting @Ignore to fully ignore the string #194

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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions Resources/doc/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,30 @@ translations in PHP code, the ``@Desc`` annotation:
You can place the doc comment anywhere in the method call chain or directly
before the key.

Ignoring messages
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

You might have some messages that you would like to ignore in your Forms or Controllers.
You can use the @Ignore annotation for this:

.. code-block :: php

<?php

// Controller.php
/** @Ignore */
$this->translator->trans('text.ignore_me')

.. code-block :: php

<?php

// FormType.php
$builder->add('field_with_ignored_label', 'text', array(
'label' => /** @Ignore */ 'form.ignored'
));


Extracting Translation Messages
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This bundle automatically supports extracting messages from the following
Expand Down
2 changes: 1 addition & 1 deletion Tests/Translation/Dumper/PhpDumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ protected function getOutput($key)
throw new InvalidArgumentException(sprintf('There is no output for key "%s".', $key));
}

return file_get_contents($file);
return str_replace("\r\n", "\n", file_get_contents($file));
}
}
2 changes: 1 addition & 1 deletion Tests/Translation/Dumper/XliffDumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,6 @@ protected function getOutput($key)
// $doc = \DOMDocument::load($file);
// $this->assertTrue($doc->schemaValidate(__DIR__.'/../../../Resources/schema/xliff-core-1.2-strict.xsd'));

return file_get_contents($file);
return str_replace("\r\n", "\n", file_get_contents($file));
}
}
2 changes: 1 addition & 1 deletion Tests/Translation/Dumper/YamlDumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ protected function getOutput($key)
throw new InvalidArgumentException(sprintf('There is no output for key "%s".', $key));
}

return file_get_contents($file);
return str_replace("\r\n", "\n", file_get_contents($file));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,19 @@ public function testExtract()

$message = new Message('security.authentication_error.foo', 'authentication');
$message->setDesc('%foo% is invalid.');
$message->addSource(new FileSource(__DIR__.'/Fixture/MyAuthException.php', 31));
$message->addSource(new FileSource(__DIR__.'/Fixture/MyAuthException.php', 35));
$expected->add($message);

$message = new Message('security.authentication_error.bar', 'authentication');
$message->setDesc('An authentication error occurred.');
$message->addSource(new FileSource(__DIR__.'/Fixture/MyAuthException.php', 35));
$message->addSource(new FileSource(__DIR__.'/Fixture/MyAuthException.php', 39));
$expected->add($message);

$this->assertEquals($expected, $this->extract('MyAuthException.php'));
$extracted = $this->extract('MyAuthException.php');

$this->assertFalse($extracted->getDomains()['authentication']->has('security.authentication_error.ignored'));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not supported in PHP5.3


$this->assertEquals($expected, $extracted);
}

protected function getDefaultExtractor()
Expand Down
6 changes: 6 additions & 0 deletions Tests/Translation/Extractor/File/Fixture/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,10 @@ public function assignToVar()
/** @Desc("The var %foo% should be assigned.") */
return $this->translator->trans('text.var.assign', array('%foo%' => 'fooVar'));
}

public function ignoredAction()
{
$this->translator->trans(/** @Ignore */ 'text.ignored');
}
}

4 changes: 4 additions & 0 deletions Tests/Translation/Extractor/File/Fixture/MyAuthException.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ class MyAuthException extends AuthenticationException

public function getMessageKey()
{

/** @Ignore */
$ignored = 'security.authentication_error.ignored';

if (!empty($this->foo)) {
/** @Desc("%foo% is invalid.") */
return 'security.authentication_error.foo';
Expand Down
3 changes: 3 additions & 0 deletions Tests/Translation/Extractor/File/Fixture/MyFormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,8 @@ public function buildForm(FormBuilder $builder, array $options)
$builder->add('dueDate', 'date', array(
'empty_value' => array('year' => 'form.dueDate.empty.year', 'month' => 'form.dueDate.empty.month', 'day'=>'form.dueDate.empty.day')
));
$builder->add('field_with_ignored_label', 'text', array(
'label' => /** @Ignore */ 'form.ignored'
));
}
}
57 changes: 32 additions & 25 deletions Tests/Translation/Extractor/File/FormExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ public function testExtract()
$expected = new MessageCatalogue();
$path = __DIR__.'/Fixture/MyFormType.php';

$message = new Message('form.label.firstname');
$message->setDesc(null);
$message->addSource(new FileSource($path, 30));
$expected->add($message);

$message = new Message('form.label.lastname');
$message->setDesc('Lastname');
$message->addSource(new FileSource($path, 33));
$expected->add($message);

// 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) {
Expand All @@ -94,15 +104,6 @@ public function testExtract()
$message->addSource(new FileSource($path, 37));
$expected->add($message);

$message = new Message('form.label.lastname');
$message->setDesc('Lastname');
$message->addSource(new FileSource($path, 33));
$expected->add($message);

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

$message = new Message('form.label.password');
$message->addSource(new FileSource($path, 42));
$expected->add($message);
Expand All @@ -112,6 +113,11 @@ public function testExtract()
$message->addSource(new FileSource($path, 45));
$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));
$expected->add($message);

$message = new Message('form.label.street', 'address');
$message->setDesc('Street');
$message->addSource(new FileSource($path, 50));
Expand All @@ -122,15 +128,6 @@ public function testExtract()
$message->addSource(new FileSource($path, 55));
$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));
$expected->add($message);

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

$message = new Message('field.with.placeholder');
$message->addSource(new FileSource($path, 59));
$expected->add($message);
Expand All @@ -145,6 +142,18 @@ public function testExtract()
$message->addSource(new FileSource($path, 64));
$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);

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

$message = new Message('form.dueDate.empty.year');
$message->addSource(new FileSource($path, 79));
$expected->add($message);
Expand All @@ -157,15 +166,13 @@ public function testExtract()
$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);
$extracted = $this->extract('MyFormType.php');

$message = new Message('form.choice.choice_as_values.label.bar');
$message->addSource(new FileSource($path, 69));
$expected->add($message);
// Test ignore
$this->assertFalse($extracted->getDomains()['messages']->has('form.ignored'));

$this->assertEquals($expected, $extracted);

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

/**
Expand Down
2 changes: 1 addition & 1 deletion Tests/Translation/Extractor/FileExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function testExtractWithSimpleTestFixtures()
asort($expected);
asort($actual);

$this->assertEquals($expected, $actual);
$this->assertEquals($expected, str_replace('/', '\\', $actual));
}

private function extract($directory)
Expand Down
12 changes: 6 additions & 6 deletions Translation/Extractor/File/DefaultPhpFileExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,12 @@ public function enterNode(Node $node)
}
}

if ($ignore) {
return;
}

if (!$node->args[0]->value instanceof String_) {
if ($ignore) {
return;
}


$message = sprintf('Can only extract the translation id from a scalar string, but got "%s". Please refactor your code to make it extractable, or add the doc comment /** @Ignore */ to this code element (in %s on line %d).', get_class($node->args[0]->value), $this->file, $node->args[0]->value->getLine());

Expand All @@ -152,10 +154,8 @@ public function enterNode(Node $node)

$index = $this->methodsToExtractFrom[strtolower($node->name)];
if (isset($node->args[$index])) {

if (!$node->args[$index]->value instanceof String_) {
if ($ignore) {
return;
}

$message = sprintf('Can only extract the translation domain from a scalar string, but got "%s". Please refactor your code to make it extractable, or add the doc comment /** @Ignore */ to this code element (in %s on line %d).', get_class($node->args[0]->value), $this->file, $node->args[0]->value->getLine());

Expand Down
7 changes: 4 additions & 3 deletions Translation/Extractor/File/FormExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,10 +364,11 @@ private function parseItem($item, $domain = null)
// check if the value is explicitly set to false => e.g. for FormField that should be rendered without label
$ignore = $ignore || !$item->value instanceof Node\Scalar\String_ || $item->value->value == false;

if ($ignore) {
return;
}

if (!$item->value instanceof Node\Scalar\String_ && !$item->value instanceof Node\Scalar\LNumber) {
if ($ignore) {
return;
}

$message = sprintf('Unable to extract translation id for form label/title/placeholder from non-string values, but got "%s" in %s on line %d. Please refactor your code to pass a string, or add "/** @Ignore */".', get_class($item->value), $this->file, $item->value->getLine());
if ($this->logger) {
Expand Down