Skip to content

[FrameworkBundle] Applied new styles to translation commands #14595

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
May 20, 2015
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 @@ -11,8 +11,8 @@

namespace Symfony\Bundle\FrameworkBundle\Command;

use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Translation\Catalogue\MergeOperation;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
Expand Down Expand Up @@ -85,8 +85,9 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$output = new SymfonyStyle($input, $output);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imho we should choose another name here and not override one of the method arguments.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was already mentioned here #14132 (comment) but I'm not sure about it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I don't know. We started this debate over here: #14132 (comment), but no result for now.

I'm not convinced, as SymfonyStyle acts as a pseudo-decorator, and fully replace the output for the whole command (IMO it's even better, by avoiding using the non-decorated output by error and "mix styles").


Edit: aitboudad was faster :)

if (false !== strpos($input->getFirstArgument(), ':d')) {
$output->writeln('<comment>The use of "translation:debug" command is deprecated since version 2.7 and will be removed in 3.0. Use the "debug:translation" instead.</comment>');
$output->caution('The use of "translation:debug" command is deprecated since version 2.7 and will be removed in 3.0. Use the "debug:translation" instead.');
}

$locale = $input->getArgument('locale');
Expand All @@ -106,7 +107,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$rootPath = $input->getArgument('bundle');

if (!is_dir($rootPath)) {
throw new \InvalidArgumentException(sprintf('<error>"%s" is neither an enabled bundle nor a directory.</error>', $rootPath));
throw new \InvalidArgumentException(sprintf('"%s" is neither an enabled bundle nor a directory.', $rootPath));
}
}
}
Expand Down Expand Up @@ -135,13 +136,13 @@ protected function execute(InputInterface $input, OutputInterface $output)

// No defined or extracted messages
if (empty($allMessages) || null !== $domain && empty($allMessages[$domain])) {
$outputMessage = sprintf('<info>No defined or extracted messages for locale "%s"</info>', $locale);
$outputMessage = sprintf('No defined or extracted messages for locale "%s"', $locale);

if (null !== $domain) {
$outputMessage .= sprintf(' <info>and domain "%s"</info>', $domain);
$outputMessage .= sprintf(' and domain "%s"', $domain);
}

$output->writeln($outputMessage);
$output->warning($outputMessage);

return;
}
Expand All @@ -161,19 +162,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
}
}

if (class_exists('Symfony\Component\Console\Helper\Table')) {
$table = new Table($output);
} else {
$table = $this->getHelperSet()->get('table');
}

// Display header line
$headers = array('State', 'Domain', 'Id', sprintf('Message Preview (%s)', $locale));
foreach ($fallbackCatalogues as $fallbackCatalogue) {
$headers[] = sprintf('Fallback Message Preview (%s)', $fallbackCatalogue->getLocale());
}
$table->setHeaders($headers);

$rows = array();
// Iterate all message ids and determine their state
foreach ($allMessages as $domain => $messages) {
foreach (array_keys($messages) as $messageId) {
Expand Down Expand Up @@ -206,15 +200,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
$row[] = $this->sanitizeString($fallbackCatalogue->get($messageId, $domain));
}

$table->addRow($row);
$rows[] = $row;
}
}

if (class_exists('Symfony\Component\Console\Helper\Table')) {
$table->render();
} else {
$table->render($output);
}
$output->table($headers, $rows);
}

private function formatState($state)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Bundle\FrameworkBundle\Command;

use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Translation\Catalogue\DiffOperation;
use Symfony\Component\Translation\Catalogue\MergeOperation;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -67,9 +68,10 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$output = new SymfonyStyle($input, $output);
// check presence of force or dump-message
if ($input->getOption('force') !== true && $input->getOption('dump-messages') !== true) {
$output->writeln('<info>You must choose one of --force or --dump-messages</info>');
$output->error('You must choose one of --force or --dump-messages');

return 1;
}
Expand All @@ -78,16 +80,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
$writer = $this->getContainer()->get('translation.writer');
$supportedFormats = $writer->getFormats();
if (!in_array($input->getOption('output-format'), $supportedFormats)) {
$output->writeln('<error>Wrong output format</error>');
$output->writeln('Supported formats are '.implode(', ', $supportedFormats).'.');
$output->error(array('Wrong output format', 'Supported formats are '.implode(', ', $supportedFormats).'.'));

return 1;
}
$kernel = $this->getContainer()->get('kernel');

// Define Root Path to App folder
$rootPath = $kernel->getRootDir();
$currentName = "app folder";
$currentName = 'app folder';

// Override with provided Bundle info
if (null !== $input->getArgument('bundle')) {
Expand All @@ -106,20 +107,22 @@ protected function execute(InputInterface $input, OutputInterface $output)
}
}

$output->title('Symfony translation update command');

// get bundle directory
$translationsPath = $rootPath.'/Resources/translations';
$output->writeln(sprintf('Generating "<info>%s</info>" translation files for "<info>%s</info>"', $input->getArgument('locale'), $currentName));
$output->text(sprintf('Generating "<info>%s</info>" translation files for "<info>%s</info>"', $input->getArgument('locale'), $currentName));

// load any messages from templates
$extractedCatalogue = new MessageCatalogue($input->getArgument('locale'));
$output->writeln('Parsing templates');
$output->text('Parsing templates');
$extractor = $this->getContainer()->get('translation.extractor');
$extractor->setPrefix($input->getOption('prefix'));
$extractor->extract($rootPath.'/Resources/views/', $extractedCatalogue);

// load any existing messages from the translation files
$currentCatalogue = new MessageCatalogue($input->getArgument('locale'));
$output->writeln('Loading translation files');
$output->text('Loading translation files');
$loader = $this->getContainer()->get('translation.loader');
$loader->loadMessages($translationsPath, $currentCatalogue);

Expand All @@ -130,26 +133,27 @@ protected function execute(InputInterface $input, OutputInterface $output)

// Exit if no messages found.
if (!count($operation->getDomains())) {
$output->writeln("\n<comment>No translation found.</comment>");
$output->warning('No translation found.');

return;
}

// show compiled list of messages
if ($input->getOption('dump-messages') === true) {
$output->newLine();
foreach ($operation->getDomains() as $domain) {
$output->writeln(sprintf("\nDisplaying messages for domain <info>%s</info>:\n", $domain));
$output->section(sprintf('Displaying messages for domain <info>%s</info>:', $domain));
$newKeys = array_keys($operation->getNewMessages($domain));
$allKeys = array_keys($operation->getMessages($domain));
foreach (array_diff($allKeys, $newKeys) as $id) {
$output->writeln($id);
}
foreach ($newKeys as $id) {
$output->writeln(sprintf('<fg=green>%s</>', $id));
}
foreach (array_keys($operation->getObsoleteMessages($domain)) as $id) {
$output->writeln(sprintf('<fg=red>%s</>', $id));
}
$output->listing(array_merge(
array_diff($allKeys, $newKeys),
array_map(function ($id) {
return sprintf('<fg=green>%s</>', $id);
}, $newKeys),
array_map(function($id) {
return sprintf('<fg=red>%s</>', $id);
}, array_keys($operation->getObsoleteMessages($domain)))
));
}

if ($input->getOption('output-format') == 'xlf') {
Expand All @@ -163,8 +167,11 @@ protected function execute(InputInterface $input, OutputInterface $output)

// save the files
if ($input->getOption('force') === true) {
$output->writeln('Writing files');
$output->text('Writing files');
$writer->writeTranslations($operation->getResult(), $input->getOption('output-format'), array('path' => $translationsPath, 'default_locale' => $this->getContainer()->getParameter('kernel.default_locale')));
}

$output->newLine();
$output->success('Success');
}
}
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/FrameworkBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"require-dev": {
"symfony/phpunit-bridge": "~2.7",
"symfony/browser-kit": "~2.4",
"symfony/console": "~2.6",
"symfony/console": "~2.7",
"symfony/css-selector": "~2.0,>=2.0.5",
"symfony/dom-crawler": "~2.0,>=2.0.5",
"symfony/finder": "~2.0,>=2.0.5",
Expand Down