TwigExtractor.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bridge\Twig\Translation;
  11. use Symfony\Component\Finder\Finder;
  12. use Symfony\Component\Translation\Extractor\AbstractFileExtractor;
  13. use Symfony\Component\Translation\Extractor\ExtractorInterface;
  14. use Symfony\Component\Translation\MessageCatalogue;
  15. use Twig\Environment;
  16. use Twig\Error\Error;
  17. use Twig\Source;
  18. /**
  19. * TwigExtractor extracts translation messages from a twig template.
  20. *
  21. * @author Michel Salib <michelsalib@hotmail.com>
  22. * @author Fabien Potencier <fabien@symfony.com>
  23. */
  24. class TwigExtractor extends AbstractFileExtractor implements ExtractorInterface
  25. {
  26. /**
  27. * Default domain for found messages.
  28. *
  29. * @var string
  30. */
  31. private $defaultDomain = 'messages';
  32. /**
  33. * Prefix for found message.
  34. *
  35. * @var string
  36. */
  37. private $prefix = '';
  38. private $twig;
  39. public function __construct(Environment $twig)
  40. {
  41. $this->twig = $twig;
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function extract($resource, MessageCatalogue $catalogue)
  47. {
  48. foreach ($this->extractFiles($resource) as $file) {
  49. try {
  50. $this->extractTemplate(file_get_contents($file->getPathname()), $catalogue);
  51. } catch (Error $e) {
  52. // ignore errors, these should be fixed by using the linter
  53. }
  54. }
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function setPrefix(string $prefix)
  60. {
  61. $this->prefix = $prefix;
  62. }
  63. protected function extractTemplate(string $template, MessageCatalogue $catalogue)
  64. {
  65. $visitor = $this->twig->getExtension('Symfony\Bridge\Twig\Extension\TranslationExtension')->getTranslationNodeVisitor();
  66. $visitor->enable();
  67. $this->twig->parse($this->twig->tokenize(new Source($template, '')));
  68. foreach ($visitor->getMessages() as $message) {
  69. $catalogue->set(trim($message[0]), $this->prefix.trim($message[0]), $message[1] ?: $this->defaultDomain);
  70. }
  71. $visitor->disable();
  72. }
  73. /**
  74. * @return bool
  75. */
  76. protected function canBeExtracted(string $file)
  77. {
  78. return $this->isFile($file) && 'twig' === pathinfo($file, \PATHINFO_EXTENSION);
  79. }
  80. /**
  81. * {@inheritdoc}
  82. */
  83. protected function extractFromDirectory($directory)
  84. {
  85. $finder = new Finder();
  86. return $finder->files()->name('*.twig')->in($directory);
  87. }
  88. }