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:
commit
197396089f
@ -11,12 +11,14 @@
|
||||
|
||||
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\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
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\Translator;
|
||||
|
||||
@ -48,6 +50,7 @@ class TranslationDebugCommand extends ContainerAwareCommand
|
||||
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-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')
|
||||
->setHelp(<<<EOF
|
||||
@ -75,6 +78,10 @@ You can display information about app translations in a specific locale:
|
||||
|
||||
<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
|
||||
)
|
||||
;
|
||||
@ -91,40 +98,45 @@ EOF
|
||||
|
||||
$locale = $input->getArgument('locale');
|
||||
$domain = $input->getOption('domain');
|
||||
/** @var TranslationLoader $loader */
|
||||
$loader = $this->getContainer()->get('translation.loader');
|
||||
/** @var Kernel $kernel */
|
||||
$kernel = $this->getContainer()->get('kernel');
|
||||
|
||||
// Define Root Path to App folder
|
||||
$rootPath = $kernel->getRootDir();
|
||||
$rootPaths = array($kernel->getRootDir());
|
||||
|
||||
// Override with provided Bundle info
|
||||
if (null !== $input->getArgument('bundle')) {
|
||||
try {
|
||||
$rootPath = $kernel->getBundle($input->getArgument('bundle'))->getPath();
|
||||
$rootPaths = array($kernel->getBundle($input->getArgument('bundle'))->getPath());
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
// 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)) {
|
||||
throw new \InvalidArgumentException(sprintf('<error>"%s" is neither an enabled bundle nor a directory.</error>', $rootPath));
|
||||
if (!is_dir($rootPaths[0])) {
|
||||
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
|
||||
$translationsPath = $rootPath.'/Resources/translations';
|
||||
|
||||
$output->writeln(sprintf('Translations in <info>%s</info>', $translationsPath));
|
||||
|
||||
// Extract used messages
|
||||
$extractedCatalogue = new MessageCatalogue($locale);
|
||||
if (is_dir($rootPath.'/Resources/views')) {
|
||||
$this->getContainer()->get('translation.extractor')->extract($rootPath.'/Resources/views', $extractedCatalogue);
|
||||
}
|
||||
$extractedCatalogue = $this->extractMessages($locale, $rootPath);
|
||||
|
||||
// Load defined messages
|
||||
$currentCatalogue = new MessageCatalogue($locale);
|
||||
if (is_dir($translationsPath)) {
|
||||
$loader->loadMessages($translationsPath, $currentCatalogue);
|
||||
}
|
||||
$currentCatalogue = $this->loadCurrentMessages($locale, $translationsPath, $loader);
|
||||
|
||||
// Merge defined and extracted messages to get all message ids
|
||||
$mergeOperation = new MergeOperation($extractedCatalogue, $currentCatalogue);
|
||||
@ -143,24 +155,11 @@ EOF
|
||||
|
||||
$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;
|
||||
}
|
||||
|
||||
$fallbackCatalogue = new MessageCatalogue($fallbackLocale);
|
||||
$loader->loadMessages($translationsPath, $fallbackCatalogue);
|
||||
$fallbackCatalogues[] = $fallbackCatalogue;
|
||||
}
|
||||
}
|
||||
|
||||
// Load the fallback catalogues
|
||||
$fallbackCatalogues = $this->loadFallbackCatalogues($locale, $translationsPath, $loader);
|
||||
if (class_exists('Symfony\Component\Console\Helper\Table')) {
|
||||
$table = new Table($output);
|
||||
} else {
|
||||
@ -215,6 +214,8 @@ EOF
|
||||
} else {
|
||||
$table->render($output);
|
||||
}
|
||||
$output->writeln('');
|
||||
}
|
||||
}
|
||||
|
||||
private function formatState($state)
|
||||
@ -263,4 +264,63 @@ EOF
|
||||
|
||||
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