Skip to content

[Translation] Add completion feature on translation pull and push commands #43672

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
namespace Symfony\Component\Translation\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Completion\CompletionInput;
use Symfony\Component\Console\Completion\CompletionSuggestions;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand Down Expand Up @@ -52,6 +54,36 @@ public function __construct(TranslationProviderCollection $providerCollection, T
parent::__construct();
}

public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
{
if ($input->mustSuggestArgumentValuesFor('provider')) {
$suggestions->suggestValues($this->providerCollection->keys());

return;
}

if ($input->mustSuggestOptionValuesFor('domains')) {
$provider = $this->providerCollection->get($input->getArgument('provider'));

if ($provider && method_exists($provider, 'getDomains')) {
$domains = $provider->getDomains();
$suggestions->suggestValues($domains);
}

return;
}

if ($input->mustSuggestOptionValuesFor('locales')) {
$suggestions->suggestValues($this->enabledLocales);

return;
}

if ($input->mustSuggestOptionValuesFor('format')) {
$suggestions->suggestValues(['php', 'xlf', 'xlf12', 'xlf20', 'po', 'mo', 'yml', 'yaml', 'ts', 'csv', 'json', 'ini', 'res']);
}
}

/**
* {@inheritdoc}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
namespace Symfony\Component\Translation\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Completion\CompletionInput;
use Symfony\Component\Console\Completion\CompletionSuggestions;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -47,6 +49,30 @@ public function __construct(TranslationProviderCollection $providers, Translatio
parent::__construct();
}

public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
{
if ($input->mustSuggestArgumentValuesFor('provider')) {
$suggestions->suggestValues($this->providers->keys());

return;
}

if ($input->mustSuggestOptionValuesFor('domains')) {
$provider = $this->providers->get($input->getArgument('provider'));

if ($provider && method_exists($provider, 'getDomains')) {
$domains = $provider->getDomains();
$suggestions->suggestValues($domains);
}

return;
}

if ($input->mustSuggestOptionValuesFor('locales')) {
$suggestions->suggestValues($this->enabledLocales);
}
}

/**
* {@inheritdoc}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,15 @@ protected function tearDown(): void
parent::tearDown();
}

protected function getProviderCollection(ProviderInterface $provider, array $locales = ['en'], array $domains = ['messages']): TranslationProviderCollection
protected function getProviderCollection(ProviderInterface $provider, array $providerNames = ['loco'], array $locales = ['en'], array $domains = ['messages']): TranslationProviderCollection
{
return new TranslationProviderCollection([
'loco' => new FilteringProvider($provider, $locales, $domains),
]);
$collection = [];

foreach ($providerNames as $providerName) {
$collection[$providerName] = new FilteringProvider($provider, $locales, $domains);
}

return new TranslationProviderCollection($collection);
}

protected function createFile(array $messages = ['note' => 'NOTE'], $targetLanguage = 'en', $fileNamePattern = 'messages.%locale%.xlf', string $xlfVersion = 'xlf12'): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Translation\Tests\Command;

use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandCompletionTester;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\Translation\Command\TranslationPullCommand;
use Symfony\Component\Translation\Dumper\XliffFileDumper;
Expand Down Expand Up @@ -424,24 +425,65 @@ public function testPullMessagesWithDefaultLocale()
, file_get_contents($filenameFr));
}

/**
* @dataProvider provideCompletionSuggestions
*/
public function testComplete(array $input, array $expectedSuggestions)
{
if (!class_exists(CommandCompletionTester::class)) {
$this->markTestSkipped('Test command completion requires symfony/console 5.4+.');
}

$application = new Application();
$application->add($this->createCommand($this->createMock(ProviderInterface::class), ['en', 'fr', 'it'], ['messages', 'validators'], 'en', ['loco', 'crowdin', 'lokalise']));

$tester = new CommandCompletionTester($application->get('translation:pull'));
$suggestions = $tester->complete($input);
$this->assertSame($expectedSuggestions, $suggestions);
}

public function provideCompletionSuggestions(): \Generator
{
yield 'provider' => [
[''],
['loco', 'crowdin', 'lokalise'],
];

yield '--domains' => [
['loco', '--domains'],
['messages', 'validators'],
];

yield '--locales' => [
['loco', '--locales'],
['en', 'fr', 'it'],
];
}

private function createCommandTester(ProviderInterface $provider, array $locales = ['en'], array $domains = ['messages'], $defaultLocale = 'en'): CommandTester
{
$command = $this->createCommand($provider, $locales, $domains, $defaultLocale);
$application = new Application();
$application->add($command);

return new CommandTester($application->find('translation:pull'));
}

private function createCommand(ProviderInterface $provider, array $locales = ['en'], array $domains = ['messages'], $defaultLocale = 'en', array $providerNames = ['loco']): TranslationPullCommand
{
$writer = new TranslationWriter();
$writer->addDumper('xlf', new XliffFileDumper());

$reader = new TranslationReader();
$reader->addLoader('xlf', new XliffFileLoader());

$command = new TranslationPullCommand(
$this->getProviderCollection($provider, $locales, $domains),
return new TranslationPullCommand(
$this->getProviderCollection($provider, $providerNames, $locales, $domains),
$writer,
$reader,
$defaultLocale,
[$this->translationAppDir.'/translations']
[$this->translationAppDir.'/translations'],
$locales
);
$application = new Application();
$application->add($command);

return new CommandTester($application->find('translation:pull'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\FrameworkBundle\Tests\Command;
namespace Symfony\Component\Translation\Tests\Command;

use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandCompletionTester;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\Translation\Command\TranslationPushCommand;
use Symfony\Component\Translation\Loader\ArrayLoader;
use Symfony\Component\Translation\Loader\XliffFileLoader;
use Symfony\Component\Translation\Provider\ProviderInterface;
use Symfony\Component\Translation\Reader\TranslationReader;
use Symfony\Component\Translation\Tests\Command\TranslationProviderTestCase;
use Symfony\Component\Translation\TranslatorBag;

/**
Expand Down Expand Up @@ -249,20 +249,60 @@ public function testPushForceAndDeleteMissingMessages()
$this->assertStringContainsString('[OK] All local translations has been sent to "null" (for "en, fr" locale(s), and "messages" domain(s)).', trim($tester->getDisplay()));
}

/**
* @dataProvider provideCompletionSuggestions
*/
public function testComplete(array $input, array $expectedSuggestions)
{
if (!class_exists(CommandCompletionTester::class)) {
$this->markTestSkipped('Test command completion requires symfony/console 5.4+.');
}

$application = new Application();
$application->add($this->createCommand($this->createMock(ProviderInterface::class), ['en', 'fr', 'it'], ['messages', 'validators'], ['loco', 'crowdin', 'lokalise']));

$tester = new CommandCompletionTester($application->get('translation:push'));
$suggestions = $tester->complete($input);
$this->assertSame($expectedSuggestions, $suggestions);
}

public function provideCompletionSuggestions(): \Generator
{
yield 'provider' => [
[''],
['loco', 'crowdin', 'lokalise'],
];

yield '--domains' => [
['loco', '--domains'],
['messages', 'validators'],
];

yield '--locales' => [
['loco', '--locales'],
['en', 'fr', 'it'],
];
}

private function createCommandTester(ProviderInterface $provider, array $locales = ['en'], array $domains = ['messages']): CommandTester
{
$command = $this->createCommand($provider, $locales, $domains);
$application = new Application();
$application->add($command);

return new CommandTester($application->find('translation:push'));
}

private function createCommand(ProviderInterface $provider, array $locales = ['en'], array $domains = ['messages'], array $providerNames = ['loco']): TranslationPushCommand
{
$reader = new TranslationReader();
$reader->addLoader('xlf', new XliffFileLoader());

$command = new TranslationPushCommand(
$this->getProviderCollection($provider, $locales, $domains),
return new TranslationPushCommand(
$this->getProviderCollection($provider, $providerNames, $locales, $domains),
$reader,
[$this->translationAppDir.'/translations'],
$locales
);
$application = new Application();
$application->add($command);

return new CommandTester($application->find('translation:push'));
}
}