[FrameworkBundle] fixed edge cases for translation:debug and tweaked the output
This commit is contained in:
parent
597a3107bc
commit
f039bde3d8
@ -12,11 +12,13 @@
|
|||||||
namespace Symfony\Bundle\FrameworkBundle\Command;
|
namespace Symfony\Bundle\FrameworkBundle\Command;
|
||||||
|
|
||||||
use Symfony\Component\Translation\Catalogue\MergeOperation;
|
use Symfony\Component\Translation\Catalogue\MergeOperation;
|
||||||
|
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\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\Translation\MessageCatalogue;
|
use Symfony\Component\Translation\MessageCatalogue;
|
||||||
|
use Symfony\Component\Translation\Translator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Helps finding unused or missing translation messages in a given locale
|
* Helps finding unused or missing translation messages in a given locale
|
||||||
@ -46,10 +48,11 @@ class TranslationDebugCommand extends ContainerAwareCommand
|
|||||||
))
|
))
|
||||||
->setDescription('Displays translation messages informations')
|
->setDescription('Displays translation messages informations')
|
||||||
->setHelp(<<<EOF
|
->setHelp(<<<EOF
|
||||||
The <info>%command.name%</info> command helps finding unused or missing translation messages and
|
The <info>%command.name%</info> command helps finding unused or missing translation
|
||||||
comparing them with the fallback ones by inspecting the templates and translation files of a given bundle.
|
messages and comparing them with the fallback ones by inspecting the
|
||||||
|
templates and translation files of a given bundle.
|
||||||
|
|
||||||
You can display informations about a bundle translations in a specific locale:
|
You can display information about bundle translations in a specific locale:
|
||||||
|
|
||||||
<info>php %command.full_name% en AcmeDemoBundle</info>
|
<info>php %command.full_name% en AcmeDemoBundle</info>
|
||||||
|
|
||||||
@ -81,12 +84,13 @@ EOF
|
|||||||
|
|
||||||
// Extract used messages
|
// Extract used messages
|
||||||
$extractedCatalogue = new MessageCatalogue($locale);
|
$extractedCatalogue = new MessageCatalogue($locale);
|
||||||
$this->getContainer()->get('translation.extractor')
|
$this->getContainer()->get('translation.extractor')->extract($bundle->getPath().'/Resources/views', $extractedCatalogue);
|
||||||
->extract($bundle->getPath().'/Resources/views/', $extractedCatalogue);
|
|
||||||
|
|
||||||
// Load defined messages
|
// Load defined messages
|
||||||
$currentCatalogue = new MessageCatalogue($locale);
|
$currentCatalogue = new MessageCatalogue($locale);
|
||||||
$loader->loadMessages($bundle->getPath().'/Resources/translations', $currentCatalogue);
|
if (is_dir($bundle->getPath().'/Resources/translations')) {
|
||||||
|
$loader->loadMessages($bundle->getPath().'/Resources/translations', $currentCatalogue);
|
||||||
|
}
|
||||||
|
|
||||||
// Merge defined and extracted messages to get all message ids
|
// Merge defined and extracted messages to get all message ids
|
||||||
$mergeOperation = new MergeOperation($extractedCatalogue, $currentCatalogue);
|
$mergeOperation = new MergeOperation($extractedCatalogue, $currentCatalogue);
|
||||||
@ -110,32 +114,28 @@ EOF
|
|||||||
|
|
||||||
// Load the fallback catalogues
|
// Load the fallback catalogues
|
||||||
$fallbackCatalogues = array();
|
$fallbackCatalogues = array();
|
||||||
foreach ($this->getContainer()->get('translator')->getFallbackLocales() as $fallbackLocale) {
|
$translator = $this->getContainer()->get('translator');
|
||||||
if ($fallbackLocale === $locale) {
|
if ($translator instanceof Translator) {
|
||||||
continue;
|
foreach ($translator->getFallbackLocales() as $fallbackLocale) {
|
||||||
}
|
if ($fallbackLocale === $locale) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
$fallbackCatalogue = new MessageCatalogue($fallbackLocale);
|
$fallbackCatalogue = new MessageCatalogue($fallbackLocale);
|
||||||
$loader->loadMessages($bundle->getPath().'/Resources/translations', $fallbackCatalogue);
|
$loader->loadMessages($bundle->getPath().'/Resources/translations', $fallbackCatalogue);
|
||||||
$fallbackCatalogues[] = $fallbackCatalogue;
|
$fallbackCatalogues[] = $fallbackCatalogue;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Display legend
|
/** @var \Symfony\Component\Console\Helper\Table $table */
|
||||||
$output->writeln(sprintf('Legend: %s Missing message %s Unused message %s Equals fallback message',
|
$table = new Table($output);
|
||||||
$this->formatState(self::MESSAGE_MISSING),
|
|
||||||
$this->formatState(self::MESSAGE_UNUSED),
|
|
||||||
$this->formatState(self::MESSAGE_EQUALS_FALLBACK)
|
|
||||||
));
|
|
||||||
|
|
||||||
/** @var \Symfony\Component\Console\Helper\TableHelper $tableHelper */
|
|
||||||
$tableHelper = $this->getHelperSet()->get('table');
|
|
||||||
|
|
||||||
// Display header line
|
// Display header line
|
||||||
$headers = array('State(s)', 'Id', sprintf('Message Preview (%s)', $locale));
|
$headers = array('State(s)', '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());
|
||||||
}
|
}
|
||||||
$tableHelper->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) {
|
||||||
@ -157,9 +157,9 @@ EOF
|
|||||||
}
|
}
|
||||||
|
|
||||||
foreach ($fallbackCatalogues as $fallbackCatalogue) {
|
foreach ($fallbackCatalogues as $fallbackCatalogue) {
|
||||||
if ($fallbackCatalogue->defines($messageId, $domain)
|
if ($fallbackCatalogue->defines($messageId, $domain) && $value === $fallbackCatalogue->get($messageId, $domain)) {
|
||||||
&& $value === $fallbackCatalogue->get($messageId, $domain)) {
|
|
||||||
$states[] = self::MESSAGE_EQUALS_FALLBACK;
|
$states[] = self::MESSAGE_EQUALS_FALLBACK;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -169,25 +169,31 @@ EOF
|
|||||||
$row[] = $this->sanitizeString($fallbackCatalogue->get($messageId, $domain));
|
$row[] = $this->sanitizeString($fallbackCatalogue->get($messageId, $domain));
|
||||||
}
|
}
|
||||||
|
|
||||||
$tableHelper->addRow($row);
|
$table->addRow($row);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$tableHelper->render($output);
|
$table->render();
|
||||||
|
|
||||||
|
$output->writeln('');
|
||||||
|
$output->writeln('<info>Legend:</info>');
|
||||||
|
$output->writeln(sprintf(' %s Missing message', $this->formatState(self::MESSAGE_MISSING)));
|
||||||
|
$output->writeln(sprintf(' %s Unused message', $this->formatState(self::MESSAGE_UNUSED)));
|
||||||
|
$output->writeln(sprintf(' %s Same as the fallback message', $this->formatState(self::MESSAGE_EQUALS_FALLBACK)));
|
||||||
}
|
}
|
||||||
|
|
||||||
private function formatState($state)
|
private function formatState($state)
|
||||||
{
|
{
|
||||||
if (self::MESSAGE_MISSING === $state) {
|
if (self::MESSAGE_MISSING === $state) {
|
||||||
return '<fg=red;options=bold>x</fg=red;options=bold>';
|
return '<fg=red>x</>';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (self::MESSAGE_UNUSED === $state) {
|
if (self::MESSAGE_UNUSED === $state) {
|
||||||
return '<fg=yellow;options=bold>o</fg=yellow;options=bold>';
|
return '<fg=yellow>o</>';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (self::MESSAGE_EQUALS_FALLBACK === $state) {
|
if (self::MESSAGE_EQUALS_FALLBACK === $state) {
|
||||||
return '<fg=green;options=bold>=</fg=green;options=bold>';
|
return '<fg=green>=</>';
|
||||||
}
|
}
|
||||||
|
|
||||||
return $state;
|
return $state;
|
||||||
@ -208,16 +214,16 @@ EOF
|
|||||||
return sprintf('<fg=cyan;options=bold>%s</fg=cyan;options=bold>', $id);
|
return sprintf('<fg=cyan;options=bold>%s</fg=cyan;options=bold>', $id);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function sanitizeString($string, $lenght = 40)
|
private function sanitizeString($string, $length = 40)
|
||||||
{
|
{
|
||||||
$string = trim(preg_replace('/\s+/', ' ', $string));
|
$string = trim(preg_replace('/\s+/', ' ', $string));
|
||||||
|
|
||||||
if (function_exists('mb_strlen') && false !== $encoding = mb_detect_encoding($string)) {
|
if (function_exists('mb_strlen') && false !== $encoding = mb_detect_encoding($string)) {
|
||||||
if (mb_strlen($string, $encoding) > $lenght) {
|
if (mb_strlen($string, $encoding) > $length) {
|
||||||
return mb_substr($string, 0, $lenght - 3, $encoding).'...';
|
return mb_substr($string, 0, $length - 3, $encoding).'...';
|
||||||
}
|
}
|
||||||
} elseif (strlen($string) > $lenght) {
|
} elseif (strlen($string) > $length) {
|
||||||
return substr($string, 0, $lenght - 3).'...';
|
return substr($string, 0, $length - 3).'...';
|
||||||
}
|
}
|
||||||
|
|
||||||
return $string;
|
return $string;
|
||||||
|
Reference in New Issue
Block a user