Skip to content

Commit 9274dcd

Browse files
antograssiotnicolas-grekas
authored andcommitted
[FrameworkBundle] Allow to specify a domain when updating translations
1 parent 22205e5 commit 9274dcd

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

Command/TranslationUpdateCommand.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ protected function configure()
4444
new InputOption('force', null, InputOption::VALUE_NONE, 'Should the update be done'),
4545
new InputOption('no-backup', null, InputOption::VALUE_NONE, 'Should backup be disabled'),
4646
new InputOption('clean', null, InputOption::VALUE_NONE, 'Should clean not found messages'),
47+
new InputOption('domain', null, InputOption::VALUE_OPTIONAL, 'Specify the domain to update'),
4748
))
4849
->setDescription('Updates the translation file')
4950
->setHelp(<<<'EOF'
@@ -139,6 +140,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
139140
}
140141
}
141142

143+
if (null !== $domain = $input->getOption('domain')) {
144+
$currentCatalogue = $this->filterCatalogue($currentCatalogue, $domain);
145+
$extractedCatalogue = $this->filterCatalogue($extractedCatalogue, $domain);
146+
}
147+
142148
// process catalogues
143149
$operation = $input->getOption('clean')
144150
? new TargetOperation($currentCatalogue, $extractedCatalogue)
@@ -213,4 +219,23 @@ protected function execute(InputInterface $input, OutputInterface $output)
213219

214220
$io->success($resultMessage.'.');
215221
}
222+
223+
private function filterCatalogue(MessageCatalogue $catalogue, $domain)
224+
{
225+
$filteredCatalogue = new MessageCatalogue($catalogue->getLocale());
226+
227+
if ($messages = $catalogue->all($domain)) {
228+
$filteredCatalogue->add($messages, $domain);
229+
}
230+
foreach ($catalogue->getResources() as $resource) {
231+
$filteredCatalogue->addResource($resource);
232+
}
233+
if ($metadata = $catalogue->getMetadata('', $domain)) {
234+
foreach ($metadata as $k => $v) {
235+
$filteredCatalogue->setMetadata($k, $v, $domain);
236+
}
237+
}
238+
239+
return $filteredCatalogue;
240+
}
216241
}

Tests/Command/TranslationUpdateCommandTest.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,34 @@ class TranslationUpdateCommandTest extends \PHPUnit_Framework_TestCase
2525

2626
public function testDumpMessagesAndClean()
2727
{
28-
$tester = $this->createCommandTester($this->getContainer(array('foo' => 'foo')));
28+
$tester = $this->createCommandTester($this->getContainer(array('messages' => array('foo' => 'foo'))));
2929
$tester->execute(array('command' => 'translation:update', 'locale' => 'en', 'bundle' => 'foo', '--dump-messages' => true, '--clean' => true));
3030
$this->assertRegExp('/foo/', $tester->getDisplay());
3131
$this->assertRegExp('/2 messages were successfully extracted/', $tester->getDisplay());
3232
}
3333

34+
public function testDumpMessagesForSpecificDomain()
35+
{
36+
$tester = $this->createCommandTester($this->getContainer(array('messages' => array('foo' => 'foo'), 'mydomain' => array('bar' => 'bar'))));
37+
$tester->execute(array('command' => 'translation:update', 'locale' => 'en', 'bundle' => 'foo', '--dump-messages' => true, '--clean' => true, '--domain' => 'mydomain'));
38+
$this->assertRegExp('/bar/', $tester->getDisplay());
39+
$this->assertRegExp('/2 messages were successfully extracted/', $tester->getDisplay());
40+
}
41+
3442
public function testWriteMessages()
3543
{
36-
$tester = $this->createCommandTester($this->getContainer(array('foo' => 'foo')));
44+
$tester = $this->createCommandTester($this->getContainer(array('messages' => array('foo' => 'foo'))));
3745
$tester->execute(array('command' => 'translation:update', 'locale' => 'en', 'bundle' => 'foo', '--force' => true));
3846
$this->assertRegExp('/Translation files were successfully updated./', $tester->getDisplay());
3947
}
4048

49+
public function testWriteMessagesForSpecificDomain()
50+
{
51+
$tester = $this->createCommandTester($this->getContainer(array('messages' => array('foo' => 'foo'), 'mydomain' => array('bar' => 'bar'))));
52+
$tester->execute(array('command' => 'translation:update', 'locale' => 'en', 'bundle' => 'foo', '--force' => true, '--domain' => 'mydomain'));
53+
$this->assertRegExp('/Translation files were successfully updated./', $tester->getDisplay());
54+
}
55+
4156
protected function setUp()
4257
{
4358
$this->fs = new Filesystem();
@@ -82,7 +97,9 @@ private function getContainer($extractedMessages = array(), $loadedMessages = ar
8297
->method('extract')
8398
->will(
8499
$this->returnCallback(function ($path, $catalogue) use ($extractedMessages) {
85-
$catalogue->add($extractedMessages);
100+
foreach ($extractedMessages as $domain => $messages) {
101+
$catalogue->add($messages, $domain);
102+
}
86103
})
87104
);
88105

0 commit comments

Comments
 (0)