Skip to content

Commit 3249a4e

Browse files
k0d3r1sfabpot
authored andcommitted
[FrameworkBundle] Added --sort option for TranslationUpdateCommand
1 parent 6916822 commit 3249a4e

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ CHANGELOG
1515
* Not tagging service route loaders with `routing.route_loader` has been deprecated.
1616
* Overriding the methods `KernelTestCase::tearDown()` and `WebTestCase::tearDown()` without the `void` return-type is deprecated.
1717
* Added new `error_controller` configuration to handle system exceptions
18-
18+
* Added sort option for `translation:update` command.
19+
1920
4.3.0
2021
-----
2122

src/Symfony/Bundle/FrameworkBundle/Command/TranslationUpdateCommand.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636
*/
3737
class TranslationUpdateCommand extends Command
3838
{
39+
private const ASC = 'asc';
40+
private const DESC = 'desc';
41+
private const SORT_ORDERS = [self::ASC, self::DESC];
42+
3943
protected static $defaultName = 'translation:update';
4044

4145
private $writer;
@@ -78,6 +82,7 @@ protected function configure()
7882
new InputOption('clean', null, InputOption::VALUE_NONE, 'Should clean not found messages'),
7983
new InputOption('domain', null, InputOption::VALUE_OPTIONAL, 'Specify the domain to update'),
8084
new InputOption('xliff-version', null, InputOption::VALUE_OPTIONAL, 'Override the default xliff version', '1.2'),
85+
new InputOption('sort', null, InputOption::VALUE_OPTIONAL, 'Return list of messages sorted alphabetically'),
8186
])
8287
->setDescription('Updates the translation file')
8388
->setHelp(<<<'EOF'
@@ -94,6 +99,10 @@ protected function configure()
9499
Example running against default messages directory
95100
<info>php %command.full_name% --dump-messages en</info>
96101
<info>php %command.full_name% --force --prefix="new_" fr</info>
102+
103+
Example running with sorting option
104+
<info>php %command.full_name% --dump-messages --sort=asc en AcmeBundle</info>
105+
<info>php %command.full_name% --dump-messages --sort=desc fr</info>
97106
EOF
98107
)
99108
;
@@ -261,6 +270,21 @@ protected function execute(InputInterface $input, OutputInterface $output): int
261270

262271
$domainMessagesCount = \count($list);
263272

273+
if ($sort = $input->getOption('sort')) {
274+
$sort = strtolower($sort);
275+
if (!\in_array($sort, self::SORT_ORDERS, true)) {
276+
$errorIo->error(['Wrong sort order', 'Supported formats are: '.implode(', ', self::SORT_ORDERS).'.']);
277+
278+
return 1;
279+
}
280+
281+
if (self::DESC === $sort) {
282+
rsort($list);
283+
} else {
284+
sort($list);
285+
}
286+
}
287+
264288
$io->section(sprintf('Messages extracted for domain "<info>%s</info>" (%d message%s)', $domain, $domainMessagesCount, $domainMessagesCount > 1 ? 's' : ''));
265289
$io->listing($list);
266290

src/Symfony/Bundle/FrameworkBundle/Tests/Command/TranslationUpdateCommandTest.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,29 @@ public function testDumpMessagesAndClean()
3232
$this->assertRegExp('/1 message was successfully extracted/', $tester->getDisplay());
3333
}
3434

35+
public function testDumpSortedMessagesAndClean()
36+
{
37+
$tester = $this->createCommandTester(['messages' => ['foo' => 'foo', 'test' => 'test', 'bar' => 'bar']]);
38+
$tester->execute(['command' => 'translation:update', 'locale' => 'en', 'bundle' => 'foo', '--dump-messages' => true, '--clean' => true, '--sort' => 'asc']);
39+
$this->assertRegExp("/\*bar\*foo\*test/", preg_replace('/\s+/', '', $tester->getDisplay()));
40+
$this->assertRegExp('/3 messages were successfully extracted/', $tester->getDisplay());
41+
}
42+
43+
public function testDumpReverseSortedMessagesAndClean()
44+
{
45+
$tester = $this->createCommandTester(['messages' => ['foo' => 'foo', 'test' => 'test', 'bar' => 'bar']]);
46+
$tester->execute(['command' => 'translation:update', 'locale' => 'en', 'bundle' => 'foo', '--dump-messages' => true, '--clean' => true, '--sort' => 'desc']);
47+
$this->assertRegExp("/\*test\*foo\*bar/", preg_replace('/\s+/', '', $tester->getDisplay()));
48+
$this->assertRegExp('/3 messages were successfully extracted/', $tester->getDisplay());
49+
}
50+
51+
public function testDumpWrongSortAndClean()
52+
{
53+
$tester = $this->createCommandTester(['messages' => ['foo' => 'foo', 'test' => 'test', 'bar' => 'bar']]);
54+
$tester->execute(['command' => 'translation:update', 'locale' => 'en', 'bundle' => 'foo', '--dump-messages' => true, '--clean' => true, '--sort' => 'test']);
55+
$this->assertRegExp('/\[ERROR\] Wrong sort order/', $tester->getDisplay());
56+
}
57+
3558
public function testDumpMessagesAndCleanInRootDirectory()
3659
{
3760
$this->fs->remove($this->translationDir);
@@ -118,7 +141,10 @@ protected function tearDown(): void
118141
$this->fs->remove($this->translationDir);
119142
}
120143

121-
private function createCommandTester($extractedMessages = [], $loadedMessages = [], HttpKernel\KernelInterface $kernel = null, array $transPaths = [], array $viewsPaths = []): CommandTester
144+
/**
145+
* @return CommandTester
146+
*/
147+
private function createCommandTester($extractedMessages = [], $loadedMessages = [], HttpKernel\KernelInterface $kernel = null, array $transPaths = [], array $viewsPaths = [])
122148
{
123149
$translator = $this->getMockBuilder('Symfony\Component\Translation\Translator')
124150
->disableOriginalConstructor()

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy