feature #14320 [Translation] added an --all option to the debug:translation command #14237 (sgehrig)

This PR was submitted for the 2.7 branch but it was merged into the 2.8 branch instead (closes #14320).

Discussion
----------

[Translation] added an --all option to the debug:translation command #14237

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

- [ ] submit changes to the documentation

This PR adds an `--all` option the `debug:translation` command to enable debugging translations in all registered bundles *AND* the app directory.

Commits
-------

0e7fe79 added the --all option to the debug:translation command
This commit is contained in:
Abdellatif Ait boudad 2015-04-29 17:32:00 +00:00
commit 197396089f

View File

@ -11,12 +11,14 @@
namespace Symfony\Bundle\FrameworkBundle\Command; namespace Symfony\Bundle\FrameworkBundle\Command;
use Symfony\Component\Translation\Catalogue\MergeOperation; use Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader;
use Symfony\Component\Console\Helper\Table; use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Translation\Catalogue\MergeOperation;
use Symfony\Component\Translation\MessageCatalogue; use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Translation\Translator; use Symfony\Component\Translation\Translator;
@ -48,6 +50,7 @@ class TranslationDebugCommand extends ContainerAwareCommand
new InputOption('domain', null, InputOption::VALUE_OPTIONAL, 'The messages domain'), new InputOption('domain', null, InputOption::VALUE_OPTIONAL, 'The messages domain'),
new InputOption('only-missing', null, InputOption::VALUE_NONE, 'Displays only missing messages'), new InputOption('only-missing', null, InputOption::VALUE_NONE, 'Displays only missing messages'),
new InputOption('only-unused', null, InputOption::VALUE_NONE, 'Displays only unused messages'), new InputOption('only-unused', null, InputOption::VALUE_NONE, 'Displays only unused messages'),
new InputOption('all', null, InputOption::VALUE_NONE, 'Load messages from all registered bundles'),
)) ))
->setDescription('Displays translation messages information') ->setDescription('Displays translation messages information')
->setHelp(<<<EOF ->setHelp(<<<EOF
@ -75,6 +78,10 @@ You can display information about app translations in a specific locale:
<info>php %command.full_name% en</info> <info>php %command.full_name% en</info>
You can display information about translations in all registered bundles in a specific locale:
<info>php %command.full_name% --all en</info>
EOF EOF
) )
; ;
@ -91,129 +98,123 @@ EOF
$locale = $input->getArgument('locale'); $locale = $input->getArgument('locale');
$domain = $input->getOption('domain'); $domain = $input->getOption('domain');
/** @var TranslationLoader $loader */
$loader = $this->getContainer()->get('translation.loader'); $loader = $this->getContainer()->get('translation.loader');
/** @var Kernel $kernel */
$kernel = $this->getContainer()->get('kernel'); $kernel = $this->getContainer()->get('kernel');
// Define Root Path to App folder // Define Root Path to App folder
$rootPath = $kernel->getRootDir(); $rootPaths = array($kernel->getRootDir());
// Override with provided Bundle info // Override with provided Bundle info
if (null !== $input->getArgument('bundle')) { if (null !== $input->getArgument('bundle')) {
try { try {
$rootPath = $kernel->getBundle($input->getArgument('bundle'))->getPath(); $rootPaths = array($kernel->getBundle($input->getArgument('bundle'))->getPath());
} catch (\InvalidArgumentException $e) { } catch (\InvalidArgumentException $e) {
// such a bundle does not exist, so treat the argument as path // such a bundle does not exist, so treat the argument as path
$rootPath = $input->getArgument('bundle'); $rootPaths = array($input->getArgument('bundle'));
if (!is_dir($rootPath)) { if (!is_dir($rootPaths[0])) {
throw new \InvalidArgumentException(sprintf('<error>"%s" is neither an enabled bundle nor a directory.</error>', $rootPath)); throw new \InvalidArgumentException(
sprintf('<error>"%s" is neither an enabled bundle nor a directory.</error>', $rootPaths[0])
);
} }
} }
} elseif ($input->getOption('all')) {
foreach ($kernel->getBundles() as $bundle) {
$rootPaths[] = $bundle->getPath();
}
} }
// get bundle directory foreach ($rootPaths as $rootPath) {
$translationsPath = $rootPath.'/Resources/translations'; // get bundle directory
$translationsPath = $rootPath.'/Resources/translations';
// Extract used messages $output->writeln(sprintf('Translations in <info>%s</info>', $translationsPath));
$extractedCatalogue = new MessageCatalogue($locale);
if (is_dir($rootPath.'/Resources/views')) {
$this->getContainer()->get('translation.extractor')->extract($rootPath.'/Resources/views', $extractedCatalogue);
}
// Load defined messages // Extract used messages
$currentCatalogue = new MessageCatalogue($locale); $extractedCatalogue = $this->extractMessages($locale, $rootPath);
if (is_dir($translationsPath)) {
$loader->loadMessages($translationsPath, $currentCatalogue);
}
// Merge defined and extracted messages to get all message ids // Load defined messages
$mergeOperation = new MergeOperation($extractedCatalogue, $currentCatalogue); $currentCatalogue = $this->loadCurrentMessages($locale, $translationsPath, $loader);
$allMessages = $mergeOperation->getResult()->all($domain);
if (null !== $domain) {
$allMessages = array($domain => $allMessages);
}
// 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);
// Merge defined and extracted messages to get all message ids
$mergeOperation = new MergeOperation($extractedCatalogue, $currentCatalogue);
$allMessages = $mergeOperation->getResult()->all($domain);
if (null !== $domain) { if (null !== $domain) {
$outputMessage .= sprintf(' <info>and domain "%s"</info>', $domain); $allMessages = array($domain => $allMessages);
} }
$output->writeln($outputMessage); // 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);
return; if (null !== $domain) {
} $outputMessage .= sprintf(' <info>and domain "%s"</info>', $domain);
// Load the fallback catalogues
$fallbackCatalogues = array();
$translator = $this->getContainer()->get('translator');
if ($translator instanceof Translator) {
foreach ($translator->getFallbackLocales() as $fallbackLocale) {
if ($fallbackLocale === $locale) {
continue;
} }
$fallbackCatalogue = new MessageCatalogue($fallbackLocale); $output->writeln($outputMessage);
$loader->loadMessages($translationsPath, $fallbackCatalogue);
$fallbackCatalogues[] = $fallbackCatalogue; continue;
} }
}
if (class_exists('Symfony\Component\Console\Helper\Table')) { // Load the fallback catalogues
$table = new Table($output); $fallbackCatalogues = $this->loadFallbackCatalogues($locale, $translationsPath, $loader);
} else { if (class_exists('Symfony\Component\Console\Helper\Table')) {
$table = $this->getHelperSet()->get('table'); $table = new Table($output);
} } else {
$table = $this->getHelperSet()->get('table');
}
// Display header line // Display header line
$headers = array('State', 'Domain', 'Id', sprintf('Message Preview (%s)', $locale)); $headers = array('State', 'Domain', 'Id', sprintf('Message Preview (%s)', $locale));
foreach ($fallbackCatalogues as $fallbackCatalogue) { foreach ($fallbackCatalogues as $fallbackCatalogue) {
$headers[] = sprintf('Fallback Message Preview (%s)', $fallbackCatalogue->getLocale()); $headers[] = sprintf('Fallback Message Preview (%s)', $fallbackCatalogue->getLocale());
} }
$table->setHeaders($headers); $table->setHeaders($headers);
// Iterate all message ids and determine their state // Iterate all message ids and determine their state
foreach ($allMessages as $domain => $messages) { foreach ($allMessages as $domain => $messages) {
foreach (array_keys($messages) as $messageId) { foreach (array_keys($messages) as $messageId) {
$value = $currentCatalogue->get($messageId, $domain); $value = $currentCatalogue->get($messageId, $domain);
$states = array(); $states = array();
if ($extractedCatalogue->defines($messageId, $domain)) { if ($extractedCatalogue->defines($messageId, $domain)) {
if (!$currentCatalogue->defines($messageId, $domain)) { if (!$currentCatalogue->defines($messageId, $domain)) {
$states[] = self::MESSAGE_MISSING; $states[] = self::MESSAGE_MISSING;
}
} elseif ($currentCatalogue->defines($messageId, $domain)) {
$states[] = self::MESSAGE_UNUSED;
} }
} elseif ($currentCatalogue->defines($messageId, $domain)) {
$states[] = self::MESSAGE_UNUSED;
}
if (!in_array(self::MESSAGE_UNUSED, $states) && true === $input->getOption('only-unused') if (!in_array(self::MESSAGE_UNUSED, $states) && true === $input->getOption('only-unused')
|| !in_array(self::MESSAGE_MISSING, $states) && true === $input->getOption('only-missing')) { || !in_array(self::MESSAGE_MISSING, $states) && true === $input->getOption('only-missing')) {
continue; continue;
}
foreach ($fallbackCatalogues as $fallbackCatalogue) {
if ($fallbackCatalogue->defines($messageId, $domain) && $value === $fallbackCatalogue->get($messageId, $domain)) {
$states[] = self::MESSAGE_EQUALS_FALLBACK;
break;
} }
}
$row = array($this->formatStates($states), $domain, $this->formatId($messageId), $this->sanitizeString($value)); foreach ($fallbackCatalogues as $fallbackCatalogue) {
foreach ($fallbackCatalogues as $fallbackCatalogue) { if ($fallbackCatalogue->defines($messageId, $domain) && $value === $fallbackCatalogue->get($messageId, $domain)) {
$row[] = $this->sanitizeString($fallbackCatalogue->get($messageId, $domain)); $states[] = self::MESSAGE_EQUALS_FALLBACK;
}
$table->addRow($row); break;
}
}
$row = array($this->formatStates($states), $domain, $this->formatId($messageId), $this->sanitizeString($value));
foreach ($fallbackCatalogues as $fallbackCatalogue) {
$row[] = $this->sanitizeString($fallbackCatalogue->get($messageId, $domain));
}
$table->addRow($row);
}
} }
}
if (class_exists('Symfony\Component\Console\Helper\Table')) { if (class_exists('Symfony\Component\Console\Helper\Table')) {
$table->render(); $table->render();
} else { } else {
$table->render($output); $table->render($output);
}
$output->writeln('');
} }
} }
@ -263,4 +264,63 @@ EOF
return $string; return $string;
} }
/**
* @param string $locale
* @param string $rootPath
*
* @return MessageCatalogue
*/
private function extractMessages($locale, $rootPath)
{
$extractedCatalogue = new MessageCatalogue($locale);
if (is_dir($rootPath.'/Resources/views')) {
$this->getContainer()->get('translation.extractor')->extract($rootPath.'/Resources/views', $extractedCatalogue);
}
return $extractedCatalogue;
}
/**
* @param string $locale
* @param string $translationsPath
* @param TranslationLoader $loader
*
* @return MessageCatalogue
*/
private function loadCurrentMessages($locale, $translationsPath, TranslationLoader $loader)
{
$currentCatalogue = new MessageCatalogue($locale);
if (is_dir($translationsPath)) {
$loader->loadMessages($translationsPath, $currentCatalogue);
}
return $currentCatalogue;
}
/**
* @param string $locale
* @param string $translationsPath
* @param TranslationLoader $loader
*
* @return MessageCatalogue[]
*/
private function loadFallbackCatalogues($locale, $translationsPath, TranslationLoader $loader)
{
$fallbackCatalogues = array();
$translator = $this->getContainer()->get('translator');
if ($translator instanceof Translator) {
foreach ($translator->getFallbackLocales() as $fallbackLocale) {
if ($fallbackLocale === $locale) {
continue;
}
$fallbackCatalogue = new MessageCatalogue($fallbackLocale);
$loader->loadMessages($translationsPath, $fallbackCatalogue);
$fallbackCatalogues[] = $fallbackCatalogue;
}
}
return $fallbackCatalogues;
}
} }