|
22 | 22 | */ |
23 | 23 | final class TranslationFilter extends BaseVisitor implements \Twig_NodeVisitorInterface |
24 | 24 | { |
25 | | - /** |
26 | | - * @var WorkerTranslationFilter |
27 | | - */ |
28 | | - private $worker; |
29 | | - |
30 | | - public function __construct() |
31 | | - { |
32 | | - $this->worker = new WorkerTranslationFilter(); |
33 | | - } |
34 | | - |
35 | 25 | public function enterNode(Twig_NodeInterface $node, Twig_Environment $env) |
36 | 26 | { |
37 | | - return $this->worker->work($node, $this->collection, function () { |
38 | | - return $this->getAbsoluteFilePath(); |
39 | | - }); |
| 27 | + if (!$node instanceof \Twig_Node_Expression_Filter) { |
| 28 | + return $node; |
| 29 | + } |
| 30 | + |
| 31 | + $name = $node->getNode('filter')->getAttribute('value'); |
| 32 | + if ('trans' !== $name && 'transchoice' !== $name) { |
| 33 | + return $node; |
| 34 | + } |
| 35 | + |
| 36 | + $idNode = $node->getNode('node'); |
| 37 | + if (!$idNode instanceof \Twig_Node_Expression_Constant) { |
| 38 | + // We can only extract constants |
| 39 | + return $node; |
| 40 | + } |
| 41 | + |
| 42 | + $id = $idNode->getAttribute('value'); |
| 43 | + $index = 'trans' === $name ? 1 : 2; |
| 44 | + $domain = 'messages'; |
| 45 | + $arguments = $node->getNode('arguments'); |
| 46 | + if ($arguments->hasNode($index)) { |
| 47 | + $argument = $arguments->getNode($index); |
| 48 | + if (!$argument instanceof \Twig_Node_Expression_Constant) { |
| 49 | + return $node; |
| 50 | + } |
| 51 | + |
| 52 | + $domain = $argument->getAttribute('value'); |
| 53 | + } |
| 54 | + |
| 55 | + $source = new SourceLocation($id, $this->getAbsoluteFilePath(), $node->getLine(), ['domain' => $domain]); |
| 56 | + $this->collection->addLocation($source); |
| 57 | + |
| 58 | + return $node; |
40 | 59 | } |
41 | 60 |
|
42 | 61 | public function leaveNode(Twig_NodeInterface $node, Twig_Environment $env) |
|
0 commit comments