minor #14595 [FrameworkBundle] Applied new styles to translation commands (ogizanagi)

This PR was merged into the 2.7 branch.

Discussion
----------

[FrameworkBundle] Applied new styles to translation commands

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | Partially #14138
| License       | MIT
| Doc PR        | -

## Translation debug

_Only the table layout and deprecation note is changed_

![screenshot 2015-05-09 a 17 09 17](https://cloud.githubusercontent.com/assets/2211145/7550802/45f9a53c-f670-11e4-8e33-6ec9de973c99.PNG)
![screenshot 2015-05-09 a 17 13 47](https://cloud.githubusercontent.com/assets/2211145/7551396/d57523e2-f686-11e4-907c-8b4183183ee1.PNG)

## Translation update

### Before:

![screenshot 2015-05-09 a 17 15 51](https://cloud.githubusercontent.com/assets/2211145/7550791/06cee3a4-f670-11e4-80f1-320e2768290c.PNG)

### After:

![screenshot 2015-05-09 a 17 03 59](https://cloud.githubusercontent.com/assets/2211145/7550797/45f3baaa-f670-11e4-9beb-ad6c6b10442c.PNG)
![screenshot 2015-05-09 a 17 04 35](https://cloud.githubusercontent.com/assets/2211145/7550799/45f7ed64-f670-11e4-9b77-9b602ea2920b.PNG)
![screenshot 2015-05-09 a 17 05 06](https://cloud.githubusercontent.com/assets/2211145/7550800/45f97904-f670-11e4-8b60-c73095b74800.PNG)
![screenshot 2015-05-09 a 17 06 07](https://cloud.githubusercontent.com/assets/2211145/7550798/45f6508a-f670-11e4-9c21-7247691df985.PNG)

Commits
-------

812fedc [FrameworkBundle] Applied new styles to translation commands
This commit is contained in:
Fabien Potencier 2015-05-20 10:22:46 +02:00
commit c7e05388a0
2 changed files with 36 additions and 39 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');
}
}