added the --all option to the debug:translation command
This commit is contained in:
parent
7e29d2c999
commit
0e7fe7916f
@ -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,40 +98,45 @@ 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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
foreach ($rootPaths as $rootPath) {
|
||||||
// get bundle directory
|
// get bundle directory
|
||||||
$translationsPath = $rootPath.'/Resources/translations';
|
$translationsPath = $rootPath.'/Resources/translations';
|
||||||
|
|
||||||
|
$output->writeln(sprintf('Translations in <info>%s</info>', $translationsPath));
|
||||||
|
|
||||||
// Extract used messages
|
// Extract used messages
|
||||||
$extractedCatalogue = new MessageCatalogue($locale);
|
$extractedCatalogue = $this->extractMessages($locale, $rootPath);
|
||||||
if (is_dir($rootPath.'/Resources/views')) {
|
|
||||||
$this->getContainer()->get('translation.extractor')->extract($rootPath.'/Resources/views', $extractedCatalogue);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Load defined messages
|
// Load defined messages
|
||||||
$currentCatalogue = new MessageCatalogue($locale);
|
$currentCatalogue = $this->loadCurrentMessages($locale, $translationsPath, $loader);
|
||||||
if (is_dir($translationsPath)) {
|
|
||||||
$loader->loadMessages($translationsPath, $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);
|
||||||
@ -143,24 +155,11 @@ EOF
|
|||||||
|
|
||||||
$output->writeln($outputMessage);
|
$output->writeln($outputMessage);
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Load the fallback catalogues
|
|
||||||
$fallbackCatalogues = array();
|
|
||||||
$translator = $this->getContainer()->get('translator');
|
|
||||||
if ($translator instanceof Translator) {
|
|
||||||
foreach ($translator->getFallbackLocales() as $fallbackLocale) {
|
|
||||||
if ($fallbackLocale === $locale) {
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
$fallbackCatalogue = new MessageCatalogue($fallbackLocale);
|
// Load the fallback catalogues
|
||||||
$loader->loadMessages($translationsPath, $fallbackCatalogue);
|
$fallbackCatalogues = $this->loadFallbackCatalogues($locale, $translationsPath, $loader);
|
||||||
$fallbackCatalogues[] = $fallbackCatalogue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (class_exists('Symfony\Component\Console\Helper\Table')) {
|
if (class_exists('Symfony\Component\Console\Helper\Table')) {
|
||||||
$table = new Table($output);
|
$table = new Table($output);
|
||||||
} else {
|
} else {
|
||||||
@ -215,6 +214,8 @@ EOF
|
|||||||
} else {
|
} else {
|
||||||
$table->render($output);
|
$table->render($output);
|
||||||
}
|
}
|
||||||
|
$output->writeln('');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function formatState($state)
|
private function formatState($state)
|
||||||
@ -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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user