Skip to content

Commit

Permalink
feature #19325 [FrameworkBundle] Allow to specify a domain when updat…
Browse files Browse the repository at this point in the history
…ing translations (antograssiot)

This PR was merged into the 3.2-dev branch.

Discussion
----------

[FrameworkBundle] Allow to specify a domain when updating translations

| Q             | A
| ------------- | ---
| Branch?       | "master"
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes/
| Fixed tickets |
| License       | MIT
| Doc PR        | no

The MR add the `--domain` option to the `translation:update` console command
When working with large number of domains, this helps to reduce the noise in the diff when updating only a translation file.

Commits
-------

a8f3a93 [FrameworkBundle] Allow to specify a domain when updating translations
  • Loading branch information
fabpot committed Jul 18, 2016
2 parents cbd1915 + a8f3a93 commit 56c7206
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
Expand Up @@ -44,6 +44,7 @@ protected function configure()
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'
Expand Down Expand Up @@ -139,6 +140,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
}
}

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)
Expand Down Expand Up @@ -213,4 +219,23 @@ protected function execute(InputInterface $input, OutputInterface $output)

$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;
}
}
Expand Up @@ -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();
Expand Down Expand Up @@ -82,7 +97,9 @@ private function getContainer($extractedMessages = array(), $loadedMessages = ar
->method('extract')
->will(
$this->returnCallback(function ($path, $catalogue) use ($extractedMessages) {
$catalogue->add($extractedMessages);
foreach ($extractedMessages as $domain => $messages) {
$catalogue->add($messages, $domain);
}
})
);

Expand Down

0 comments on commit 56c7206

Please sign in to comment.