[FrameworkBundle] Applied new styles to translation commands

This commit is contained in:
ogizanagi 2015-05-09 16:43:35 +02:00
parent e60f715920
commit 812fedcdc6
3 changed files with 37 additions and 40 deletions

View File

@ -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;
@ -85,8 +85,9 @@ EOF
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$output = new SymfonyStyle($input, $output);
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');
@ -106,7 +107,7 @@ EOF
$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));
}
}
}
@ -135,13 +136,13 @@ EOF
// 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;
}
@ -161,19 +162,12 @@ EOF
}
}
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) {
@ -206,15 +200,11 @@ EOF
$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)

View File

@ -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;
@ -67,9 +68,10 @@ EOF
*/
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;
}
@ -78,8 +80,7 @@ EOF
$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;
}
@ -87,7 +88,7 @@ EOF
// 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')) {
@ -106,20 +107,22 @@ EOF
}
}
$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);
@ -130,26 +133,27 @@ EOF
// 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') {
@ -163,8 +167,11 @@ EOF
// 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');
}
}

View File

@ -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",