diff --git a/Resources/doc/usage.rst b/Resources/doc/usage.rst index 681e51a9..50d5a735 100644 --- a/Resources/doc/usage.rst +++ b/Resources/doc/usage.rst @@ -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 + + translator->trans('text.ignore_me') + +.. code-block :: php + + add('field_with_ignored_label', 'text', array( + 'label' => /** @Ignore */ 'form.ignored' + )); + + Extracting Translation Messages ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ This bundle automatically supports extracting messages from the following diff --git a/Tests/Translation/Dumper/PhpDumperTest.php b/Tests/Translation/Dumper/PhpDumperTest.php index 7d8d76b1..d3b46a51 100644 --- a/Tests/Translation/Dumper/PhpDumperTest.php +++ b/Tests/Translation/Dumper/PhpDumperTest.php @@ -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)); } } diff --git a/Tests/Translation/Dumper/XliffDumperTest.php b/Tests/Translation/Dumper/XliffDumperTest.php index 655a9869..1ed5c1fb 100644 --- a/Tests/Translation/Dumper/XliffDumperTest.php +++ b/Tests/Translation/Dumper/XliffDumperTest.php @@ -157,6 +157,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)); } } diff --git a/Tests/Translation/Dumper/YamlDumperTest.php b/Tests/Translation/Dumper/YamlDumperTest.php index aa9d19f7..ebbeb914 100644 --- a/Tests/Translation/Dumper/YamlDumperTest.php +++ b/Tests/Translation/Dumper/YamlDumperTest.php @@ -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)); } } diff --git a/Tests/Translation/Extractor/File/AuthenticationMessagesExtractorTest.php b/Tests/Translation/Extractor/File/AuthenticationMessagesExtractorTest.php index e42906c1..a452fb60 100644 --- a/Tests/Translation/Extractor/File/AuthenticationMessagesExtractorTest.php +++ b/Tests/Translation/Extractor/File/AuthenticationMessagesExtractorTest.php @@ -33,15 +33,22 @@ public function testExtract() $message = new Message('security.authentication_error.foo', 'authentication'); $message->setDesc('%foo% is invalid.'); - $message->addSource($fileSourceFactory->create($fixtureSplInfo, 31)); + $message->addSource($fileSourceFactory->create($fixtureSplInfo, 35)); $expected->add($message); $message = new Message('security.authentication_error.bar', 'authentication'); $message->setDesc('An authentication error occurred.'); - $message->addSource($fileSourceFactory->create($fixtureSplInfo, 35)); + $message->addSource($fileSourceFactory->create($fixtureSplInfo, 39)); + $expected->add($message); - $this->assertEquals($expected, $this->extract('MyAuthException.php')); + $extracted = $this->extract('MyAuthException.php'); + + $domains = $extracted->getDomains(); + + $this->assertFalse($domains['authentication']->has('security.authentication_error.ignored')); + + $this->assertEquals($expected, $extracted); } protected function getDefaultExtractor() diff --git a/Tests/Translation/Extractor/File/Fixture/Controller.php b/Tests/Translation/Extractor/File/Fixture/Controller.php index 7f691546..56623cdc 100644 --- a/Tests/Translation/Extractor/File/Fixture/Controller.php +++ b/Tests/Translation/Extractor/File/Fixture/Controller.php @@ -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'); + } } + diff --git a/Tests/Translation/Extractor/File/Fixture/MyAuthException.php b/Tests/Translation/Extractor/File/Fixture/MyAuthException.php index ca3fb3af..e6fedab9 100644 --- a/Tests/Translation/Extractor/File/Fixture/MyAuthException.php +++ b/Tests/Translation/Extractor/File/Fixture/MyAuthException.php @@ -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'; diff --git a/Tests/Translation/Extractor/File/Fixture/MyFormType.php b/Tests/Translation/Extractor/File/Fixture/MyFormType.php index d29be365..cd678c2e 100644 --- a/Tests/Translation/Extractor/File/Fixture/MyFormType.php +++ b/Tests/Translation/Extractor/File/Fixture/MyFormType.php @@ -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' + )); } } diff --git a/Tests/Translation/Extractor/File/FormExtractorTest.php b/Tests/Translation/Extractor/File/FormExtractorTest.php index 6d3f70a1..4ec1ef8e 100644 --- a/Tests/Translation/Extractor/File/FormExtractorTest.php +++ b/Tests/Translation/Extractor/File/FormExtractorTest.php @@ -70,6 +70,16 @@ public function testExtract() $fileSourceFactory = $this->getFileSourceFactory(); $fixtureSplInfo = new \SplFileInfo(__DIR__.'/Fixture/MyFormType.php'); + $message = new Message('form.label.firstname'); + $message->setDesc(null); + $message->addSource($fileSourceFactory->create($fixtureSplInfo, 30)); + $expected->add($message); + + $message = new Message('form.label.lastname'); + $message->setDesc('Lastname'); + $message->addSource($fileSourceFactory->create($fixtureSplInfo, 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) { @@ -85,14 +95,6 @@ public function testExtract() $message->addSource($fileSourceFactory->create($fixtureSplInfo, 37)); $expected->add($message); - $message = new Message('form.label.lastname'); - $message->setDesc('Lastname'); - $message->addSource($fileSourceFactory->create($fixtureSplInfo, 33)); - $expected->add($message); - - $message = new Message('form.label.firstname'); - $message->addSource($fileSourceFactory->create($fixtureSplInfo, 30)); - $expected->add($message); $message = new Message('form.label.password'); $message->addSource($fileSourceFactory->create($fixtureSplInfo, 42)); @@ -103,6 +105,11 @@ public function testExtract() $message->addSource($fileSourceFactory->create($fixtureSplInfo, 45)); $expected->add($message); + $message = new Message('form.error.password_mismatch', 'validators'); + $message->setDesc('The entered passwords do not match'); + $message->addSource($fileSourceFactory->create($fixtureSplInfo, 47)); + $expected->add($message); + $message = new Message('form.label.street', 'address'); $message->setDesc('Street'); $message->addSource($fileSourceFactory->create($fixtureSplInfo, 50)); @@ -113,15 +120,6 @@ public function testExtract() $message->addSource($fileSourceFactory->create($fixtureSplInfo, 55)); $expected->add($message); - $message = new Message('form.error.password_mismatch', 'validators'); - $message->setDesc('The entered passwords do not match'); - $message->addSource($fileSourceFactory->create($fixtureSplInfo, 47)); - $expected->add($message); - - $message = new Message('form.label.created'); - $message->addSource($fileSourceFactory->create($fixtureSplInfo, 75)); - $expected->add($message); - $message = new Message('field.with.placeholder'); $message->addSource($fileSourceFactory->create($fixtureSplInfo, 59)); $expected->add($message); @@ -136,6 +134,18 @@ public function testExtract() $message->addSource($fileSourceFactory->create($fixtureSplInfo, 64)); $expected->add($message); + $message = new Message('form.choice.choice_as_values.label.foo'); + $message->addSource($fileSourceFactory->create($fixtureSplInfo, 68)); + $expected->add($message); + + $message = new Message('form.choice.choice_as_values.label.bar'); + $message->addSource($fileSourceFactory->create($fixtureSplInfo, 69)); + $expected->add($message); + + $message = new Message('form.label.created'); + $message->addSource($fileSourceFactory->create($fixtureSplInfo, 75)); + $expected->add($message); + $message = new Message('form.dueDate.empty.year'); $message->addSource($fileSourceFactory->create($fixtureSplInfo, 79)); $expected->add($message); @@ -156,7 +166,14 @@ public function testExtract() $message->addSource($fileSourceFactory->create($fixtureSplInfo, 69)); $expected->add($message); - $this->assertEquals($expected, $this->extract('MyFormType.php')); + $extracted = $this->extract('MyFormType.php'); + + $domains = $extracted->getDomains(); + // Test ignore + $this->assertFalse($domains['messages']->has('form.ignored')); + + $this->assertEquals($expected, $extracted); + } /** diff --git a/Tests/Translation/Extractor/FileExtractorTest.php b/Tests/Translation/Extractor/FileExtractorTest.php index d0f2109b..974f0b30 100644 --- a/Tests/Translation/Extractor/FileExtractorTest.php +++ b/Tests/Translation/Extractor/FileExtractorTest.php @@ -90,7 +90,7 @@ public function testExtractWithSimpleTestFixtures() asort($expected); asort($actual); - $this->assertEquals($expected, $actual); + $this->assertEquals($expected, str_replace('/', '\\', $actual)); } private function extract($directory) diff --git a/Translation/Extractor/File/DefaultPhpFileExtractor.php b/Translation/Extractor/File/DefaultPhpFileExtractor.php index e12b1562..77decdcc 100644 --- a/Translation/Extractor/File/DefaultPhpFileExtractor.php +++ b/Translation/Extractor/File/DefaultPhpFileExtractor.php @@ -140,10 +140,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()); @@ -159,10 +161,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()); diff --git a/Translation/Extractor/File/FormExtractor.php b/Translation/Extractor/File/FormExtractor.php index d8a7df83..02bb4034 100644 --- a/Translation/Extractor/File/FormExtractor.php +++ b/Translation/Extractor/File/FormExtractor.php @@ -370,10 +370,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) {