Skip to content

Commit 585504c

Browse files
committed
Fix git
1 parent 01b1e61 commit 585504c

File tree

3 files changed

+97
-18
lines changed

3 files changed

+97
-18
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
7.2
55
---
66

7+
* Add support for `--sort` option when extracting translations with `translation:extract` command and `--force` option
78
* Add support for setting `headers` with `Symfony\Bundle\FrameworkBundle\Controller\TemplateController`
89
* Add `--resolve-env-vars` option to `lint:container` command
910
* Derivate `kernel.secret` from the decryption secret when its env var is not defined

Command/TranslationUpdateCommand.php

Lines changed: 69 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ protected function configure(): void
7676
new InputOption('force', null, InputOption::VALUE_NONE, 'Should the extract be done'),
7777
new InputOption('clean', null, InputOption::VALUE_NONE, 'Should clean not found messages'),
7878
new InputOption('domain', null, InputOption::VALUE_OPTIONAL, 'Specify the domain to extract'),
79-
new InputOption('sort', null, InputOption::VALUE_OPTIONAL, 'Return list of messages sorted alphabetically (only works with --dump-messages)', 'asc'),
79+
new InputOption('sort', null, InputOption::VALUE_OPTIONAL, 'Return list of messages sorted alphabetically'),
8080
new InputOption('as-tree', null, InputOption::VALUE_OPTIONAL, 'Dump the messages as a tree-like structure: The given value defines the level where to switch to inline YAML'),
8181
])
8282
->setHelp(<<<'EOF'
@@ -100,7 +100,7 @@ protected function configure(): void
100100
You can sort the output with the <comment>--sort</> flag:
101101
102102
<info>php %command.full_name% --dump-messages --sort=asc en AcmeBundle</info>
103-
<info>php %command.full_name% --dump-messages --sort=desc fr</info>
103+
<info>php %command.full_name% --force --sort=desc fr</info>
104104
105105
You can dump a tree-like structure using the yaml format with <comment>--as-tree</> flag:
106106
@@ -207,6 +207,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int
207207

208208
$operation->moveMessagesToIntlDomainsIfPossible('new');
209209

210+
if ($sort = $input->getOption('sort')) {
211+
$sort = strtolower($sort);
212+
if (!\in_array($sort, self::SORT_ORDERS, true)) {
213+
$errorIo->error(['Wrong sort order', 'Supported formats are: '.implode(', ', self::SORT_ORDERS).'.']);
214+
215+
return 1;
216+
}
217+
}
218+
210219
// show compiled list of messages
211220
if (true === $input->getOption('dump-messages')) {
212221
$extractedMessagesCount = 0;
@@ -223,19 +232,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
223232

224233
$domainMessagesCount = \count($list);
225234

226-
if ($sort = $input->getOption('sort')) {
227-
$sort = strtolower($sort);
228-
if (!\in_array($sort, self::SORT_ORDERS, true)) {
229-
$errorIo->error(['Wrong sort order', 'Supported formats are: '.implode(', ', self::SORT_ORDERS).'.']);
230-
231-
return 1;
232-
}
233-
234-
if (self::DESC === $sort) {
235-
rsort($list);
236-
} else {
237-
sort($list);
238-
}
235+
if (self::DESC === $sort) {
236+
rsort($list);
237+
} else {
238+
sort($list);
239239
}
240240

241241
$io->section(\sprintf('Messages extracted for domain "<info>%s</info>" (%d message%s)', $domain, $domainMessagesCount, $domainMessagesCount > 1 ? 's' : ''));
@@ -266,7 +266,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
266266
$bundleTransPath = end($transPaths);
267267
}
268268

269-
$this->writer->write($operation->getResult(), $format, ['path' => $bundleTransPath, 'default_locale' => $this->defaultLocale, 'xliff_version' => $xliffVersion, 'as_tree' => $input->getOption('as-tree'), 'inline' => $input->getOption('as-tree') ?? 0]);
269+
$operationResult = $operation->getResult();
270+
if ($sort) {
271+
$operationResult = $this->sortCatalogue($operationResult, $sort);
272+
}
273+
274+
$this->writer->write($operationResult, $format, ['path' => $bundleTransPath, 'default_locale' => $this->defaultLocale, 'xliff_version' => $xliffVersion, 'as_tree' => $input->getOption('as-tree'), 'inline' => $input->getOption('as-tree') ?? 0]);
270275

271276
if (true === $input->getOption('dump-messages')) {
272277
$resultMessage .= ' and translation files were updated';
@@ -365,6 +370,54 @@ private function filterCatalogue(MessageCatalogue $catalogue, string $domain): M
365370
return $filteredCatalogue;
366371
}
367372

373+
private function sortCatalogue(MessageCatalogue $catalogue, string $sort): MessageCatalogue
374+
{
375+
$sortedCatalogue = new MessageCatalogue($catalogue->getLocale());
376+
377+
foreach ($catalogue->getDomains() as $domain) {
378+
// extract intl-icu messages only
379+
$intlDomain = $domain.MessageCatalogueInterface::INTL_DOMAIN_SUFFIX;
380+
if ($intlMessages = $catalogue->all($intlDomain)) {
381+
if (self::DESC === $sort) {
382+
krsort($intlMessages);
383+
} elseif (self::ASC === $sort) {
384+
ksort($intlMessages);
385+
}
386+
387+
$sortedCatalogue->add($intlMessages, $intlDomain);
388+
}
389+
390+
// extract all messages and subtract intl-icu messages
391+
if ($messages = array_diff($catalogue->all($domain), $intlMessages)) {
392+
if (self::DESC === $sort) {
393+
krsort($messages);
394+
} elseif (self::ASC === $sort) {
395+
ksort($messages);
396+
}
397+
398+
$sortedCatalogue->add($messages, $domain);
399+
}
400+
401+
if ($metadata = $catalogue->getMetadata('', $intlDomain)) {
402+
foreach ($metadata as $k => $v) {
403+
$sortedCatalogue->setMetadata($k, $v, $intlDomain);
404+
}
405+
}
406+
407+
if ($metadata = $catalogue->getMetadata('', $domain)) {
408+
foreach ($metadata as $k => $v) {
409+
$sortedCatalogue->setMetadata($k, $v, $domain);
410+
}
411+
}
412+
}
413+
414+
foreach ($catalogue->getResources() as $resource) {
415+
$sortedCatalogue->addResource($resource);
416+
}
417+
418+
return $sortedCatalogue;
419+
}
420+
368421
private function extractMessages(string $locale, array $transPaths, string $prefix): MessageCatalogue
369422
{
370423
$extractedCatalogue = new MessageCatalogue($locale);

Tests/Command/TranslationUpdateCommandTest.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
2121
use Symfony\Component\HttpKernel\KernelInterface;
2222
use Symfony\Component\Translation\Extractor\ExtractorInterface;
23+
use Symfony\Component\Translation\MessageCatalogue;
2324
use Symfony\Component\Translation\Reader\TranslationReader;
2425
use Symfony\Component\Translation\Translator;
2526
use Symfony\Component\Translation\Writer\TranslationWriter;
@@ -103,11 +104,25 @@ public function testDumpMessagesForSpecificDomain()
103104

104105
public function testWriteMessages()
105106
{
106-
$tester = $this->createCommandTester(['messages' => ['foo' => 'foo']]);
107+
$tester = $this->createCommandTester(['messages' => ['foo' => 'foo', 'test' => 'test', 'bar' => 'bar']], writerMessages: ['foo', 'test', 'bar']);
107108
$tester->execute(['command' => 'translation:extract', 'locale' => 'en', 'bundle' => 'foo', '--force' => true]);
108109
$this->assertMatchesRegularExpression('/Translation files were successfully updated./', $tester->getDisplay());
109110
}
110111

112+
public function testWriteSortMessages()
113+
{
114+
$tester = $this->createCommandTester(['messages' => ['foo' => 'foo', 'test' => 'test', 'bar' => 'bar']], writerMessages: ['bar', 'foo', 'test']);
115+
$tester->execute(['command' => 'translation:extract', 'locale' => 'en', 'bundle' => 'foo', '--force' => true, '--sort' => 'asc']);
116+
$this->assertMatchesRegularExpression('/Translation files were successfully updated./', $tester->getDisplay());
117+
}
118+
119+
public function testWriteReverseSortedMessages()
120+
{
121+
$tester = $this->createCommandTester(['messages' => ['foo' => 'foo', 'test' => 'test', 'bar' => 'bar']], writerMessages: ['test', 'foo', 'bar']);
122+
$tester->execute(['command' => 'translation:extract', 'locale' => 'en', 'bundle' => 'foo', '--force' => true, '--sort' => 'desc']);
123+
$this->assertMatchesRegularExpression('/Translation files were successfully updated./', $tester->getDisplay());
124+
}
125+
111126
public function testWriteMessagesInRootDirectory()
112127
{
113128
$tester = $this->createCommandTester(['messages' => ['foo' => 'foo']]);
@@ -175,7 +190,7 @@ protected function tearDown(): void
175190
$this->fs->remove($this->translationDir);
176191
}
177192

178-
private function createCommandTester($extractedMessages = [], $loadedMessages = [], ?KernelInterface $kernel = null, array $transPaths = [], array $codePaths = []): CommandTester
193+
private function createCommandTester($extractedMessages = [], $loadedMessages = [], ?KernelInterface $kernel = null, array $transPaths = [], array $codePaths = [], ?array $writerMessages = null): CommandTester
179194
{
180195
$translator = $this->createMock(Translator::class);
181196
$translator
@@ -212,6 +227,16 @@ function ($path, $catalogue) use ($loadedMessages) {
212227
->willReturn(
213228
['xlf', 'yml', 'yaml']
214229
);
230+
if (null !== $writerMessages) {
231+
$writer
232+
->expects($this->any())
233+
->method('write')
234+
->willReturnCallback(
235+
function (MessageCatalogue $catalogue) use ($writerMessages) {
236+
$this->assertSame($writerMessages, array_keys($catalogue->all()['messages']));
237+
}
238+
);
239+
}
215240

216241
if (null === $kernel) {
217242
$returnValues = [

0 commit comments

Comments
 (0)