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\Translation
;
14 use Symfony\Component\Finder\Finder
;
15 use Symfony\Component\Translation\Extractor\ExtractorInterface
;
16 use Symfony\Component\Translation\MessageCatalogue
;
19 * TwigExtractor extracts translation messages from a twig template.
21 * @author Michel Salib <michelsalib@hotmail.com>
22 * @author Fabien Potencier <fabien@symfony.com>
24 class TwigExtractor
implements ExtractorInterface
27 * Default domain for found messages.
31 private $defaultDomain = 'messages';
34 * Prefix for found message.
41 * The twig environment.
43 * @var \Twig_Environment
47 public function __construct(\Twig_Environment
$twig)
55 public function extract($directory, MessageCatalogue
$catalogue)
57 // load any existing translation files
58 $finder = new Finder();
59 $files = $finder->files()->name('*.twig')->in($directory);
60 foreach ($files as $file) {
61 $this->extractTemplate(file_get_contents($file->getPathname()), $catalogue);
68 public function setPrefix($prefix)
70 $this->prefix
= $prefix;
73 protected function extractTemplate($template, MessageCatalogue
$catalogue)
75 $visitor = $this->twig
->getExtension('translator')->getTranslationNodeVisitor();
78 $this->twig
->parse($this->twig
->tokenize($template));
80 foreach ($visitor->getMessages() as $message) {
81 $catalogue->set(trim($message[0]), $this->prefix
.trim($message[0]), $message[1] ? $message[1] : $this->defaultDomain
);