From 94364da345a2632f76735f676843b3cc81271cf1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Simon?= Date: Wed, 27 Feb 2013 10:07:28 +0100 Subject: [PATCH 1/8] [TwigBridge] added trans_default_domain + trans test --- src/Symfony/Bridge/Twig/Tests/Translation/TwigExtractorTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Symfony/Bridge/Twig/Tests/Translation/TwigExtractorTest.php b/src/Symfony/Bridge/Twig/Tests/Translation/TwigExtractorTest.php index a5859db1e1c75..5bbbb1576c363 100644 --- a/src/Symfony/Bridge/Twig/Tests/Translation/TwigExtractorTest.php +++ b/src/Symfony/Bridge/Twig/Tests/Translation/TwigExtractorTest.php @@ -67,6 +67,7 @@ public function getExtractData() array('{% trans from "domain" %}new key{% endtrans %}', array('new key' => 'domain')), array('{% set foo = "new key" | trans %}', array('new key' => 'messages')), array('{{ 1 ? "new key" | trans : "another key" | trans }}', array('new key' => 'messages', 'another key' => 'messages')), + array('{% trans_default_domain "domain" %}{{ "new key"|trans }}', array('new key' => 'domain')), ); } } From 11b7dbce523ccf9df4fd14010ffcb9d6b5a8bd2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Simon?= Date: Wed, 27 Feb 2013 15:27:20 +0100 Subject: [PATCH 2/8] TwigBridge] added translation extractor's node visitors tests --- ...ranslationDefaultDomainNodeVisitorTest.php | 49 +++++++++++++++++ .../TranslationNodeVisitorTest.php | 35 ++++++++++++ .../Tests/NodeVisitor/TwigNodeProvider.php | 55 +++++++++++++++++++ 3 files changed, 139 insertions(+) create mode 100644 src/Symfony/Bridge/Twig/Tests/NodeVisitor/TranslationDefaultDomainNodeVisitorTest.php create mode 100644 src/Symfony/Bridge/Twig/Tests/NodeVisitor/TranslationNodeVisitorTest.php create mode 100644 src/Symfony/Bridge/Twig/Tests/NodeVisitor/TwigNodeProvider.php diff --git a/src/Symfony/Bridge/Twig/Tests/NodeVisitor/TranslationDefaultDomainNodeVisitorTest.php b/src/Symfony/Bridge/Twig/Tests/NodeVisitor/TranslationDefaultDomainNodeVisitorTest.php new file mode 100644 index 0000000000000..9fd367c6c94e4 --- /dev/null +++ b/src/Symfony/Bridge/Twig/Tests/NodeVisitor/TranslationDefaultDomainNodeVisitorTest.php @@ -0,0 +1,49 @@ + false, 'autoescape' => false, 'optimizations' => 0)); + + $visitor = new TranslationDefaultDomainNodeVisitor(); + + // visit trans_default_domain tag + $defaultDomain = TwigNodeProvider::getTransDefaultDomainTag('domain'); + $visitor->enterNode($defaultDomain, $env); + $visitor->leaveNode($defaultDomain, $env); + + // visit tested node + $enteredNode = $visitor->enterNode($node, $env); + $leavedNode = $visitor->leaveNode($node, $env); + $this->assertSame($node, $enteredNode); + $this->assertSame($node, $leavedNode); + + // extracting tested node messages + $visitor = new TranslationNodeVisitor(); + $visitor->enable(); + $visitor->enterNode($node, $env); + $visitor->leaveNode($node, $env); + + $this->assertEquals(array(array(self::$message, self::$domain)), $visitor->getMessages()); + } + + public function getDefaultDomainAssignmentTestData() + { + return array( + array(TwigNodeProvider::getTransFilter(self::$message, self::$domain)), + array(TwigNodeProvider::getTransChoiceFilter(self::$message, self::$domain)), + array(TwigNodeProvider::getTransTag(self::$message, self::$domain)), + ); + } +} diff --git a/src/Symfony/Bridge/Twig/Tests/NodeVisitor/TranslationNodeVisitorTest.php b/src/Symfony/Bridge/Twig/Tests/NodeVisitor/TranslationNodeVisitorTest.php new file mode 100644 index 0000000000000..35acc933a8ec8 --- /dev/null +++ b/src/Symfony/Bridge/Twig/Tests/NodeVisitor/TranslationNodeVisitorTest.php @@ -0,0 +1,35 @@ + false, 'autoescape' => false, 'optimizations' => 0)); + $visitor = new TranslationNodeVisitor(); + $visitor->enable(); + $visitor->enterNode($node, $env); + $visitor->leaveNode($node, $env); + $this->assertEquals($expectedMessages, $visitor->getMessages()); + } + + public function getMessagesExtractionTestData() + { + $message = 'new key'; + $domain = 'domain'; + + return array( + array(TwigNodeProvider::getTransFilter($message), array(array($message, null))), + array(TwigNodeProvider::getTransChoiceFilter($message), array(array($message, null))), + array(TwigNodeProvider::getTransTag($message), array(array($message, 'messages'))), + array(TwigNodeProvider::getTransFilter($message, $domain), array(array($message, $domain))), + array(TwigNodeProvider::getTransChoiceFilter($message, $domain), array(array($message, $domain))), + array(TwigNodeProvider::getTransTag($message, $domain), array(array($message, $domain))), + ); + } +} diff --git a/src/Symfony/Bridge/Twig/Tests/NodeVisitor/TwigNodeProvider.php b/src/Symfony/Bridge/Twig/Tests/NodeVisitor/TwigNodeProvider.php new file mode 100644 index 0000000000000..2b753ce7afc79 --- /dev/null +++ b/src/Symfony/Bridge/Twig/Tests/NodeVisitor/TwigNodeProvider.php @@ -0,0 +1,55 @@ + $message)), + $domain ? new \Twig_Node_Expression_Constant($domain, 0) : null + ); + } + + public static function getTransDefaultDomainTag($domain) + { + return new TransDefaultDomainNode( + new \Twig_Node_Expression_Constant($domain, 0) + ); + } +} From 2f3d0e006191032551f9feb33ad3302e2dd292db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Simon?= Date: Wed, 27 Feb 2013 17:36:18 +0100 Subject: [PATCH 3/8] [TwigBridge] fixed node visitors priority & added extractor tests --- .../Twig/NodeVisitor/TranslationDefaultDomainNodeVisitor.php | 2 +- src/Symfony/Bridge/Twig/NodeVisitor/TranslationNodeVisitor.php | 2 +- src/Symfony/Bridge/Twig/Tests/Translation/TwigExtractorTest.php | 2 ++ 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Bridge/Twig/NodeVisitor/TranslationDefaultDomainNodeVisitor.php b/src/Symfony/Bridge/Twig/NodeVisitor/TranslationDefaultDomainNodeVisitor.php index c7bebdd12a573..73427a5654e79 100644 --- a/src/Symfony/Bridge/Twig/NodeVisitor/TranslationDefaultDomainNodeVisitor.php +++ b/src/Symfony/Bridge/Twig/NodeVisitor/TranslationDefaultDomainNodeVisitor.php @@ -76,6 +76,6 @@ public function leaveNode(\Twig_NodeInterface $node, \Twig_Environment $env) */ public function getPriority() { - return 0; + return -10; } } diff --git a/src/Symfony/Bridge/Twig/NodeVisitor/TranslationNodeVisitor.php b/src/Symfony/Bridge/Twig/NodeVisitor/TranslationNodeVisitor.php index 3a943f7106cac..6c219cea144cc 100644 --- a/src/Symfony/Bridge/Twig/NodeVisitor/TranslationNodeVisitor.php +++ b/src/Symfony/Bridge/Twig/NodeVisitor/TranslationNodeVisitor.php @@ -93,6 +93,6 @@ public function leaveNode(\Twig_NodeInterface $node, \Twig_Environment $env) */ public function getPriority() { - return -10; + return 0; } } diff --git a/src/Symfony/Bridge/Twig/Tests/Translation/TwigExtractorTest.php b/src/Symfony/Bridge/Twig/Tests/Translation/TwigExtractorTest.php index 5bbbb1576c363..12c29586cf916 100644 --- a/src/Symfony/Bridge/Twig/Tests/Translation/TwigExtractorTest.php +++ b/src/Symfony/Bridge/Twig/Tests/Translation/TwigExtractorTest.php @@ -68,6 +68,8 @@ public function getExtractData() array('{% set foo = "new key" | trans %}', array('new key' => 'messages')), array('{{ 1 ? "new key" | trans : "another key" | trans }}', array('new key' => 'messages', 'another key' => 'messages')), array('{% trans_default_domain "domain" %}{{ "new key"|trans }}', array('new key' => 'domain')), + array('{% trans_default_domain "domain" %}{{ "new key"|transchoice }}', array('new key' => 'domain')), + array('{% trans_default_domain "domain" %}{% trans %}new key{% endtrans %}', array('new key' => 'domain')), ); } } From 5a2c1c0774587c7ce82e376cde645ea15c8e7c61 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Wed, 27 Feb 2013 17:10:57 +0100 Subject: [PATCH 4/8] removed temp variable when compiling trans_default_domain to allow other visitor to get the value when it is a constant --- .../TranslationDefaultDomainNodeVisitor.php | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Bridge/Twig/NodeVisitor/TranslationDefaultDomainNodeVisitor.php b/src/Symfony/Bridge/Twig/NodeVisitor/TranslationDefaultDomainNodeVisitor.php index 73427a5654e79..f6869dee7761e 100644 --- a/src/Symfony/Bridge/Twig/NodeVisitor/TranslationDefaultDomainNodeVisitor.php +++ b/src/Symfony/Bridge/Twig/NodeVisitor/TranslationDefaultDomainNodeVisitor.php @@ -33,11 +33,17 @@ public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env) } if ($node instanceof TransDefaultDomainNode) { - $var = $env->getParser()->getVarName(); - $name = new \Twig_Node_Expression_AssignName($var, $node->getLine()); - $this->domain = new \Twig_Node_Expression_Name($var, $node->getLine()); + if ($node->getNode('expr') instanceof \Twig_Node_Expression_Constant) { + $this->domain = $node->getNode('expr'); - return new \Twig_Node_Set(false, new \Twig_Node(array($name)), new \Twig_Node(array($node->getNode('expr'))), $node->getLine()); + return $node; + } else { + $var = $env->getParser()->getVarName(); + $name = new \Twig_Node_Expression_AssignName($var, $node->getLine()); + $this->domain = new \Twig_Node_Expression_Name($var, $node->getLine()); + + return new \Twig_Node_Set(false, new \Twig_Node(array($name)), new \Twig_Node(array($node->getNode('expr'))), $node->getLine()); + } } if (null === $this->domain) { @@ -68,6 +74,10 @@ public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env) */ public function leaveNode(\Twig_NodeInterface $node, \Twig_Environment $env) { + if ($node instanceof TransDefaultDomainNode) { + return false; + } + return $node; } From f5dd9c78f399fed763d63e4ae58472c4d527de67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Simon?= Date: Wed, 27 Feb 2013 17:56:28 +0100 Subject: [PATCH 5/8] [TwigBridge] applied previous fix on node visitors --- .../NodeVisitor/TranslationNodeVisitor.php | 21 ++++++++++++++++--- .../TranslationNodeVisitorTest.php | 2 +- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Bridge/Twig/NodeVisitor/TranslationNodeVisitor.php b/src/Symfony/Bridge/Twig/NodeVisitor/TranslationNodeVisitor.php index 6c219cea144cc..91aba47dc67d3 100644 --- a/src/Symfony/Bridge/Twig/NodeVisitor/TranslationNodeVisitor.php +++ b/src/Symfony/Bridge/Twig/NodeVisitor/TranslationNodeVisitor.php @@ -20,6 +20,8 @@ */ class TranslationNodeVisitor implements \Twig_NodeVisitorInterface { + const UNDEFINED_DOMAIN = '_undefined'; + private $enabled = false; private $messages = array(); @@ -57,7 +59,7 @@ public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env) // extract constant nodes with a trans filter $this->messages[] = array( $node->getNode('node')->getAttribute('value'), - $node->getNode('arguments')->hasNode(1) ? $node->getNode('arguments')->getNode(1)->getAttribute('value') : null, + $this->readDomain($node->getNode('arguments')->hasNode(1) ? $node->getNode('arguments')->getNode(1) : null), ); } elseif ( $node instanceof \Twig_Node_Expression_Filter && @@ -67,13 +69,13 @@ public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env) // extract constant nodes with a trans filter $this->messages[] = array( $node->getNode('node')->getAttribute('value'), - $node->getNode('arguments')->hasNode(2) ? $node->getNode('arguments')->getNode(2)->getAttribute('value') : null, + $this->readDomain($node->getNode('arguments')->hasNode(2) ? $node->getNode('arguments')->getNode(2) : null), ); } elseif ($node instanceof TransNode) { // extract trans nodes $this->messages[] = array( $node->getNode('body')->getAttribute('data'), - null === $node->getNode('domain') ? 'messages' : $node->getNode('domain')->getAttribute('value'), + $this->readDomain($node->getNode('domain')), ); } @@ -95,4 +97,17 @@ public function getPriority() { return 0; } + + private function readDomain(\Twig_Node $node = null) + { + if (null === $node) { + return null; + } + + if ($node instanceof \Twig_Node_Expression_Constant) { + return $node->getAttribute('value'); + } + + return self::UNDEFINED_DOMAIN; + } } diff --git a/src/Symfony/Bridge/Twig/Tests/NodeVisitor/TranslationNodeVisitorTest.php b/src/Symfony/Bridge/Twig/Tests/NodeVisitor/TranslationNodeVisitorTest.php index 35acc933a8ec8..55c8281ffed4b 100644 --- a/src/Symfony/Bridge/Twig/Tests/NodeVisitor/TranslationNodeVisitorTest.php +++ b/src/Symfony/Bridge/Twig/Tests/NodeVisitor/TranslationNodeVisitorTest.php @@ -26,7 +26,7 @@ public function getMessagesExtractionTestData() return array( array(TwigNodeProvider::getTransFilter($message), array(array($message, null))), array(TwigNodeProvider::getTransChoiceFilter($message), array(array($message, null))), - array(TwigNodeProvider::getTransTag($message), array(array($message, 'messages'))), + array(TwigNodeProvider::getTransTag($message), array(array($message, null))), array(TwigNodeProvider::getTransFilter($message, $domain), array(array($message, $domain))), array(TwigNodeProvider::getTransChoiceFilter($message, $domain), array(array($message, $domain))), array(TwigNodeProvider::getTransTag($message, $domain), array(array($message, $domain))), From 0923e1d895fa1e25293386e901b9f7ff891c0cf9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Simon?= Date: Wed, 27 Feb 2013 18:20:18 +0100 Subject: [PATCH 6/8] [TwigBridge] added message extraction with invalid domain node test --- .../NodeVisitor/TranslationNodeVisitorTest.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/Symfony/Bridge/Twig/Tests/NodeVisitor/TranslationNodeVisitorTest.php b/src/Symfony/Bridge/Twig/Tests/NodeVisitor/TranslationNodeVisitorTest.php index 55c8281ffed4b..175a7c8ac48cc 100644 --- a/src/Symfony/Bridge/Twig/Tests/NodeVisitor/TranslationNodeVisitorTest.php +++ b/src/Symfony/Bridge/Twig/Tests/NodeVisitor/TranslationNodeVisitorTest.php @@ -18,6 +18,23 @@ public function testMessagesExtraction(\Twig_Node $node, array $expectedMessages $this->assertEquals($expectedMessages, $visitor->getMessages()); } + public function testMessageExtractionWithInvalidDomainNode() + { + $message = 'new key'; + + $node = new \Twig_Node_Expression_Filter( + new \Twig_Node_Expression_Constant($message, 0), + new \Twig_Node_Expression_Constant('trans', 0), + new \Twig_Node(array( + new \Twig_Node_Expression_Array(array(), 0), + new \Twig_Node_Expression_Name('variable', 0), + )), + 0 + ); + + $this->testMessagesExtraction($node, array(array($message, TranslationNodeVisitor::UNDEFINED_DOMAIN))); + } + public function getMessagesExtractionTestData() { $message = 'new key'; From c032595c21066f3bb9c36c744ab8f2a6c9a509f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Simon?= Date: Thu, 28 Feb 2013 10:04:40 +0100 Subject: [PATCH 7/8] [TwigBridge] added support for named arguments to translation visitor --- .../NodeVisitor/TranslationNodeVisitor.php | 32 ++++++++++++++++--- .../Tests/Translation/TwigExtractorTest.php | 6 ++++ 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Bridge/Twig/NodeVisitor/TranslationNodeVisitor.php b/src/Symfony/Bridge/Twig/NodeVisitor/TranslationNodeVisitor.php index 91aba47dc67d3..14cddb7a157ad 100644 --- a/src/Symfony/Bridge/Twig/NodeVisitor/TranslationNodeVisitor.php +++ b/src/Symfony/Bridge/Twig/NodeVisitor/TranslationNodeVisitor.php @@ -59,7 +59,7 @@ public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env) // extract constant nodes with a trans filter $this->messages[] = array( $node->getNode('node')->getAttribute('value'), - $this->readDomain($node->getNode('arguments')->hasNode(1) ? $node->getNode('arguments')->getNode(1) : null), + $this->readDomainFromArguments($node->getNode('arguments'), 1), ); } elseif ( $node instanceof \Twig_Node_Expression_Filter && @@ -69,13 +69,13 @@ public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env) // extract constant nodes with a trans filter $this->messages[] = array( $node->getNode('node')->getAttribute('value'), - $this->readDomain($node->getNode('arguments')->hasNode(2) ? $node->getNode('arguments')->getNode(2) : null), + $this->readDomainFromArguments($node->getNode('arguments'), 2), ); } elseif ($node instanceof TransNode) { // extract trans nodes $this->messages[] = array( $node->getNode('body')->getAttribute('data'), - $this->readDomain($node->getNode('domain')), + $this->readDomainFromNode($node->getNode('domain')), ); } @@ -98,7 +98,31 @@ public function getPriority() return 0; } - private function readDomain(\Twig_Node $node = null) + /** + * @param \Twig_Node $arguments + * @param int $index + * + * @return string|null + */ + private function readDomainFromArguments(\Twig_Node $arguments, $index) + { + if ($arguments->hasNode('domain')) { + $argument = $arguments->getNode('domain'); + } elseif ($arguments->hasNode($index)) { + $argument = $arguments->getNode($index); + } else { + return null; + } + + return $this->readDomainFromNode($argument); + } + + /** + * @param \Twig_Node $node + * + * @return string|null + */ + private function readDomainFromNode(\Twig_Node $node = null) { if (null === $node) { return null; diff --git a/src/Symfony/Bridge/Twig/Tests/Translation/TwigExtractorTest.php b/src/Symfony/Bridge/Twig/Tests/Translation/TwigExtractorTest.php index 12c29586cf916..a2c5cd3d030b7 100644 --- a/src/Symfony/Bridge/Twig/Tests/Translation/TwigExtractorTest.php +++ b/src/Symfony/Bridge/Twig/Tests/Translation/TwigExtractorTest.php @@ -67,9 +67,15 @@ public function getExtractData() array('{% trans from "domain" %}new key{% endtrans %}', array('new key' => 'domain')), array('{% set foo = "new key" | trans %}', array('new key' => 'messages')), array('{{ 1 ? "new key" | trans : "another key" | trans }}', array('new key' => 'messages', 'another key' => 'messages')), + + // make sure 'trans_default_domain' tag is supported array('{% trans_default_domain "domain" %}{{ "new key"|trans }}', array('new key' => 'domain')), array('{% trans_default_domain "domain" %}{{ "new key"|transchoice }}', array('new key' => 'domain')), array('{% trans_default_domain "domain" %}{% trans %}new key{% endtrans %}', array('new key' => 'domain')), + + // make sure this works with twig's named arguments + array('{{ "new key" | trans(domain="domain") }}', array('new key' => 'domain')), + array('{{ "new key" | transchoice(domain="domain", count=1) }}', array('new key' => 'domain')), ); } } From e4be8e76156dde4b4a014ff9f405047dcb3dfa1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Simon?= Date: Thu, 28 Feb 2013 13:31:53 +0100 Subject: [PATCH 8/8] [TwigBridge] renamed private methods --- .../Twig/NodeVisitor/TranslationNodeVisitor.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Bridge/Twig/NodeVisitor/TranslationNodeVisitor.php b/src/Symfony/Bridge/Twig/NodeVisitor/TranslationNodeVisitor.php index 14cddb7a157ad..592c2506258d4 100644 --- a/src/Symfony/Bridge/Twig/NodeVisitor/TranslationNodeVisitor.php +++ b/src/Symfony/Bridge/Twig/NodeVisitor/TranslationNodeVisitor.php @@ -59,7 +59,7 @@ public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env) // extract constant nodes with a trans filter $this->messages[] = array( $node->getNode('node')->getAttribute('value'), - $this->readDomainFromArguments($node->getNode('arguments'), 1), + $this->getReadDomainFromArguments($node->getNode('arguments'), 1), ); } elseif ( $node instanceof \Twig_Node_Expression_Filter && @@ -69,13 +69,13 @@ public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env) // extract constant nodes with a trans filter $this->messages[] = array( $node->getNode('node')->getAttribute('value'), - $this->readDomainFromArguments($node->getNode('arguments'), 2), + $this->getReadDomainFromArguments($node->getNode('arguments'), 2), ); } elseif ($node instanceof TransNode) { // extract trans nodes $this->messages[] = array( $node->getNode('body')->getAttribute('data'), - $this->readDomainFromNode($node->getNode('domain')), + $this->getReadDomainFromNode($node->getNode('domain')), ); } @@ -104,7 +104,7 @@ public function getPriority() * * @return string|null */ - private function readDomainFromArguments(\Twig_Node $arguments, $index) + private function getReadDomainFromArguments(\Twig_Node $arguments, $index) { if ($arguments->hasNode('domain')) { $argument = $arguments->getNode('domain'); @@ -114,7 +114,7 @@ private function readDomainFromArguments(\Twig_Node $arguments, $index) return null; } - return $this->readDomainFromNode($argument); + return $this->getReadDomainFromNode($argument); } /** @@ -122,7 +122,7 @@ private function readDomainFromArguments(\Twig_Node $arguments, $index) * * @return string|null */ - private function readDomainFromNode(\Twig_Node $node = null) + private function getReadDomainFromNode(\Twig_Node $node = null) { if (null === $node) { return null;