From a8f3a9387413590bb7aaf73bac6cc1fb2744eb7e Mon Sep 17 00:00:00 2001 From: antograssiot Date: Sat, 9 Jul 2016 18:21:29 +0200 Subject: [PATCH] [FrameworkBundle] Allow to specify a domain when updating translations --- .../Command/TranslationUpdateCommand.php | 25 +++++++++++++++++++ .../Command/TranslationUpdateCommandTest.php | 23 ++++++++++++++--- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/TranslationUpdateCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/TranslationUpdateCommand.php index a9a004735b..a7318dd39f 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/TranslationUpdateCommand.php +++ b/src/Symfony/Bundle/FrameworkBundle/Command/TranslationUpdateCommand.php @@ -44,6 +44,7 @@ class TranslationUpdateCommand extends ContainerAwareCommand new InputOption('force', null, InputOption::VALUE_NONE, 'Should the update be done'), new InputOption('no-backup', null, InputOption::VALUE_NONE, 'Should backup be disabled'), new InputOption('clean', null, InputOption::VALUE_NONE, 'Should clean not found messages'), + new InputOption('domain', null, InputOption::VALUE_OPTIONAL, 'Specify the domain to update'), )) ->setDescription('Updates the translation file') ->setHelp(<<<'EOF' @@ -139,6 +140,11 @@ EOF } } + if (null !== $domain = $input->getOption('domain')) { + $currentCatalogue = $this->filterCatalogue($currentCatalogue, $domain); + $extractedCatalogue = $this->filterCatalogue($extractedCatalogue, $domain); + } + // process catalogues $operation = $input->getOption('clean') ? new TargetOperation($currentCatalogue, $extractedCatalogue) @@ -213,4 +219,23 @@ EOF $io->success($resultMessage.'.'); } + + private function filterCatalogue(MessageCatalogue $catalogue, $domain) + { + $filteredCatalogue = new MessageCatalogue($catalogue->getLocale()); + + if ($messages = $catalogue->all($domain)) { + $filteredCatalogue->add($messages, $domain); + } + foreach ($catalogue->getResources() as $resource) { + $filteredCatalogue->addResource($resource); + } + if ($metadata = $catalogue->getMetadata('', $domain)) { + foreach ($metadata as $k => $v) { + $filteredCatalogue->setMetadata($k, $v, $domain); + } + } + + return $filteredCatalogue; + } } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Command/TranslationUpdateCommandTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Command/TranslationUpdateCommandTest.php index f7eca9d366..d815c9d610 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Command/TranslationUpdateCommandTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Command/TranslationUpdateCommandTest.php @@ -25,19 +25,34 @@ class TranslationUpdateCommandTest extends \PHPUnit_Framework_TestCase public function testDumpMessagesAndClean() { - $tester = $this->createCommandTester($this->getContainer(array('foo' => 'foo'))); + $tester = $this->createCommandTester($this->getContainer(array('messages' => array('foo' => 'foo')))); $tester->execute(array('command' => 'translation:update', 'locale' => 'en', 'bundle' => 'foo', '--dump-messages' => true, '--clean' => true)); $this->assertRegExp('/foo/', $tester->getDisplay()); $this->assertRegExp('/2 messages were successfully extracted/', $tester->getDisplay()); } + public function testDumpMessagesForSpecificDomain() + { + $tester = $this->createCommandTester($this->getContainer(array('messages' => array('foo' => 'foo'), 'mydomain' => array('bar' => 'bar')))); + $tester->execute(array('command' => 'translation:update', 'locale' => 'en', 'bundle' => 'foo', '--dump-messages' => true, '--clean' => true, '--domain' => 'mydomain')); + $this->assertRegExp('/bar/', $tester->getDisplay()); + $this->assertRegExp('/2 messages were successfully extracted/', $tester->getDisplay()); + } + public function testWriteMessages() { - $tester = $this->createCommandTester($this->getContainer(array('foo' => 'foo'))); + $tester = $this->createCommandTester($this->getContainer(array('messages' => array('foo' => 'foo')))); $tester->execute(array('command' => 'translation:update', 'locale' => 'en', 'bundle' => 'foo', '--force' => true)); $this->assertRegExp('/Translation files were successfully updated./', $tester->getDisplay()); } + public function testWriteMessagesForSpecificDomain() + { + $tester = $this->createCommandTester($this->getContainer(array('messages' => array('foo' => 'foo'), 'mydomain' => array('bar' => 'bar')))); + $tester->execute(array('command' => 'translation:update', 'locale' => 'en', 'bundle' => 'foo', '--force' => true, '--domain' => 'mydomain')); + $this->assertRegExp('/Translation files were successfully updated./', $tester->getDisplay()); + } + protected function setUp() { $this->fs = new Filesystem(); @@ -82,7 +97,9 @@ class TranslationUpdateCommandTest extends \PHPUnit_Framework_TestCase ->method('extract') ->will( $this->returnCallback(function ($path, $catalogue) use ($extractedMessages) { - $catalogue->add($extractedMessages); + foreach ($extractedMessages as $domain => $messages) { + $catalogue->add($messages, $domain); + } }) );