4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Bridge\Twig\NodeVisitor
;
14 use Symfony\Bridge\Twig\Node\TransNode
;
17 * TranslationNodeVisitor extracts translation messages.
19 * @author Fabien Potencier <fabien@symfony.com>
21 class TranslationNodeVisitor
implements \Twig_NodeVisitorInterface
23 const UNDEFINED_DOMAIN
= '_undefined';
25 private $enabled = false;
26 private $messages = array();
28 public function enable()
30 $this->enabled
= true;
31 $this->messages
= array();
34 public function disable()
36 $this->enabled
= false;
37 $this->messages
= array();
40 public function getMessages()
42 return $this->messages
;
48 public function enterNode(\Twig_NodeInterface
$node, \Twig_Environment
$env)
50 if (!$this->enabled
) {
55 $node instanceof \Twig_Node_Expression_Filter
&&
56 'trans' === $node->getNode('filter')->getAttribute('value') &&
57 $node->getNode('node') instanceof \Twig_Node_Expression_Constant
59 // extract constant nodes with a trans filter
60 $this->messages
[] = array(
61 $node->getNode('node')->getAttribute('value'),
62 $this->getReadDomainFromArguments($node->getNode('arguments'), 1),
65 $node instanceof \Twig_Node_Expression_Filter
&&
66 'transchoice' === $node->getNode('filter')->getAttribute('value') &&
67 $node->getNode('node') instanceof \Twig_Node_Expression_Constant
69 // extract constant nodes with a trans filter
70 $this->messages
[] = array(
71 $node->getNode('node')->getAttribute('value'),
72 $this->getReadDomainFromArguments($node->getNode('arguments'), 2),
74 } elseif ($node instanceof TransNode
) {
75 // extract trans nodes
76 $this->messages
[] = array(
77 $node->getNode('body')->getAttribute('data'),
78 $this->getReadDomainFromNode($node->getNode('domain')),
88 public function leaveNode(\Twig_NodeInterface
$node, \Twig_Environment
$env)
96 public function getPriority()
102 * @param \Twig_Node $arguments
105 * @return string|null
107 private function getReadDomainFromArguments(\Twig_Node
$arguments, $index)
109 if ($arguments->hasNode('domain')) {
110 $argument = $arguments->getNode('domain');
111 } elseif ($arguments->hasNode($index)) {
112 $argument = $arguments->getNode($index);
117 return $this->getReadDomainFromNode($argument);
121 * @param \Twig_Node $node
123 * @return string|null
125 private function getReadDomainFromNode(\Twig_Node
$node = null)
127 if (null === $node) {
131 if ($node instanceof \Twig_Node_Expression_Constant
) {
132 return $node->getAttribute('value');
135 return self
::UNDEFINED_DOMAIN
;