diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/TranslationUpdateCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/TranslationUpdateCommand.php index a29e3081f36e6..63d04856082b8 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/TranslationUpdateCommand.php +++ b/src/Symfony/Bundle/FrameworkBundle/Command/TranslationUpdateCommand.php @@ -24,6 +24,7 @@ use Symfony\Component\Translation\MessageCatalogue; use Symfony\Component\Translation\Reader\TranslationReaderInterface; use Symfony\Component\Translation\Writer\TranslationWriterInterface; +use Symfony\Component\Routing\RouterInterface; /** * A command that parses templates to extract translation messages and adds them @@ -43,8 +44,10 @@ class TranslationUpdateCommand extends Command private $defaultLocale; private $defaultTransPath; private $defaultViewsPath; + private $router; + private $defaultControllersPath; - public function __construct(TranslationWriterInterface $writer, TranslationReaderInterface $reader, ExtractorInterface $extractor, string $defaultLocale, string $defaultTransPath = null, string $defaultViewsPath = null) + public function __construct(TranslationWriterInterface $writer, TranslationReaderInterface $reader, ExtractorInterface $extractor, string $defaultLocale, string $defaultTransPath = null, string $defaultViewsPath = null, RouterInterface $router = null, array $defaultControllersPath = array()) { parent::__construct(); @@ -54,6 +57,8 @@ public function __construct(TranslationWriterInterface $writer, TranslationReade $this->defaultLocale = $defaultLocale; $this->defaultTransPath = $defaultTransPath; $this->defaultViewsPath = $defaultViewsPath; + $this->router = $router; + $this->defaultControllersPath = $defaultControllersPath; } /** @@ -127,6 +132,20 @@ protected function execute(InputInterface $input, OutputInterface $output) if ($this->defaultViewsPath) { $viewsPaths[] = $this->defaultViewsPath; } + $controllersPaths = array($kernel->getRootDir().'/Controller'); + if ($this->defaultControllersPath) { + $controllersPaths = array_merge($controllersPaths, $this->defaultControllersPath); + } + if ($this->router instanceof RouterInterface) { + foreach ($this->router->getRouteCollection() as $route) { + if (!$route->hasDefault('_controller')) { + continue; + } + $controllersPaths[] = dirname($route->getPath()); + } + $controllersPaths = array_unique($controllersPaths); + } + $currentName = 'app folder'; // Override with provided Bundle info @@ -169,6 +188,14 @@ protected function execute(InputInterface $input, OutputInterface $output) } } + // load any messages from controllers + $errorIo->comment('Parsing controllers...'); + foreach ($controllersPaths as $path) { + if (is_dir($path)) { + $this->extractor->extract($path, $extractedCatalogue); + } + } + // load any existing messages from the translation files $currentCatalogue = new MessageCatalogue($input->getArgument('locale')); $errorIo->comment('Loading translation files...'); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Command/TranslationUpdateCommandTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Command/TranslationUpdateCommandTest.php index 9232b7b027e0f..0f86c6420ac43 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Command/TranslationUpdateCommandTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Command/TranslationUpdateCommandTest.php @@ -17,6 +17,8 @@ use Symfony\Bundle\FrameworkBundle\Command\TranslationUpdateCommand; use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\HttpKernel; +use Symfony\Component\Routing\RouteCollection; +use Symfony\Component\Routing\RouterInterface; class TranslationUpdateCommandTest extends TestCase { @@ -95,6 +97,7 @@ protected function setUp() $this->fs->mkdir($this->translationDir.'/Resources/views'); $this->fs->mkdir($this->translationDir.'/translations'); $this->fs->mkdir($this->translationDir.'/templates'); + $this->fs->mkdir($this->translationDir.'/controllers'); } protected function tearDown() @@ -179,7 +182,13 @@ private function createCommandTester($extractedMessages = array(), $loadedMessag ->method('getContainer') ->will($this->returnValue($this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock())); - $command = new TranslationUpdateCommand($writer, $loader, $extractor, 'en', $this->translationDir.'/translations', $this->translationDir.'/templates'); + $router = $this->getMockBuilder(RouterInterface::class)->getMock(); + $router + ->expects($this->any()) + ->method('getRouteCollection') + ->will($this->returnValue(new RouteCollection())); + + $command = new TranslationUpdateCommand($writer, $loader, $extractor, 'en', $this->translationDir.'/translations', $this->translationDir.'/templates', $router, array($this->translationDir.'/controllers')); $application = new Application($kernel); $application->add($command);