|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the PHP Translation package. |
| 5 | + * |
| 6 | + * (c) PHP Translation team <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Translation\Extractor\Visitor\Twig; |
| 13 | + |
| 14 | +use Translation\Extractor\Model\SourceCollection; |
| 15 | +use Translation\Extractor\Model\SourceLocation; |
| 16 | + |
| 17 | +/** |
| 18 | + * @author Tobias Nyholm <[email protected]> |
| 19 | + */ |
| 20 | +final class WorkerTranslationFilter |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @param \Twig_Node|\Twig_NodeInterface $node |
| 24 | + * @param SourceCollection $collection |
| 25 | + * @param callable $getAbsoluteFilePath |
| 26 | + * |
| 27 | + * @return \Twig_Node|\Twig_NodeInterface |
| 28 | + */ |
| 29 | + public function work($node, SourceCollection $collection, callable $getAbsoluteFilePath) |
| 30 | + { |
| 31 | + if (!$node instanceof \Twig_Node_Expression_Filter) { |
| 32 | + return $node; |
| 33 | + } |
| 34 | + |
| 35 | + $name = $node->getNode('filter')->getAttribute('value'); |
| 36 | + if ('trans' !== $name && 'transchoice' !== $name) { |
| 37 | + return $node; |
| 38 | + } |
| 39 | + |
| 40 | + $idNode = $node->getNode('node'); |
| 41 | + if (!$idNode instanceof \Twig_Node_Expression_Constant) { |
| 42 | + // We can only extract constants |
| 43 | + return $node; |
| 44 | + } |
| 45 | + |
| 46 | + $id = $idNode->getAttribute('value'); |
| 47 | + $index = 'trans' === $name ? 1 : 2; |
| 48 | + $domain = 'messages'; |
| 49 | + $arguments = $node->getNode('arguments'); |
| 50 | + if ($arguments->hasNode($index)) { |
| 51 | + $argument = $arguments->getNode($index); |
| 52 | + if (!$argument instanceof \Twig_Node_Expression_Constant) { |
| 53 | + return $node; |
| 54 | + } |
| 55 | + |
| 56 | + $domain = $argument->getAttribute('value'); |
| 57 | + } |
| 58 | + |
| 59 | + $source = new SourceLocation($id, $getAbsoluteFilePath(), $node->getTemplateLine(), ['domain' => $domain]); |
| 60 | + $collection->addLocation($source); |
| 61 | + |
| 62 | + return $node; |
| 63 | + } |
| 64 | +} |
0 commit comments