8000 [FrameworkBundle] Enable translation debugging in directories · symfony/symfony@e9e7e07 · GitHub
[go: up one dir, main page]

Skip to content

Commit e9e7e07

Browse files
committed
[FrameworkBundle] Enable translation debugging in directories
Harmonize TranslationDebugCommand and TranslationUpdateCommand to expect an optional bundle name or a directory and fall back to kernel root dir if none of them is given.
1 parent fef2bd4 commit e9e7e07

File tree

2 files changed

+48
-13
lines changed

2 files changed

+48
-13
lines changed

src/Symfony/Bundle/FrameworkBundle/Command/TranslationDebugCommand.php

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,16 @@ protected function configure()
4444
))
4545
->setDefinition(array(
4646
new InputArgument('locale', InputArgument::REQUIRED, 'The locale'),
47-
new InputArgument('bundle', InputArgument::REQUIRED, 'The bundle name'),
47+
new InputArgument('bundle', InputArgument::OPTIONAL, 'The bundle name or directory where to load the messages, defaults to app/Resources folder'),
4848
new InputOption('domain', null, InputOption::VALUE_OPTIONAL, 'The messages domain'),
4949
new InputOption('only-missing', null, InputOption::VALUE_NONE, 'Displays only missing messages'),
5050
new InputOption('only-unused', null, InputOption::VALUE_NONE, 'Displays only unused messages'),
5151
))
52-
->setDescription('Displays translation messages informations')
52+
->setDescription('Displays translation messages information')
5353
->setHelp(<<<EOF
5454
The <info>%command.name%</info> command helps finding unused or missing translation
5555
messages and comparing them with the fallback ones by inspecting the
56-
templates and translation files of a given bundle.
56+
templates and translation files of a given bundle or the app folder.
5757
5858
You can display information about bundle translations in a specific locale:
5959
@@ -71,6 +71,10 @@ protected function configure()
7171
7272
<info>php %command.full_name% --only-unused en AcmeDemoBundle</info>
7373
74+
You can display information about app translations in a specific locale:
75+
76+
<info>php %command.full_name% en</info>
77+
7478
EOF
7579
)
7680
;
@@ -87,17 +91,37 @@ protected function execute(InputInterface $input, OutputInterface $output)
8791

8892
$locale = $input->getArgument('locale');
8993
$domain = $input->getOption('domain');
90-
$bundle = $this->getContainer()->get('kernel')->getBundle($input->getArgument('bundle'));
9194
$loader = $this->getContainer()->get('translation.loader');
95+
$kernel = $this->getContainer()->get('kernel');
96+
97+
// Define Root Path to App folder
98+
$rootPath = $kernel->getRootDir();
99+
100+
// Override with provided Bundle info
101+
if (null !== $input->getArgument('bundle')) {
102+
try {
103+
$rootPath = $kernel->getBundle($input->getArgument('bundle'))->getPath();
104+
} catch (\InvalidArgumentException $e) {
105+
// such a bundle does not exist, so treat the argument as path
106+
$rootPath = $input->getArgument('bundle');
107+
108+
if (!is_dir($rootPath)) {
109+
throw new \InvalidArgumentException(sprintf('<error>"%s" is neither an enabled bundle nor a directory.</error>', $rootPath));
110+
}
111+
}
112+
}
113+
114+
// get bundle directory
115+
$translationsPath = $rootPath.'/Resources/translations';
92116

93117
// Extract used messages
94118
$extractedCatalogue = new MessageCatalogue($locale);
95-
$this->getContainer()->get('translation.extractor')->extract($bundle->getPath().'/Resources/views', $extractedCatalogue);
119+
$this->getContainer()->get('translation.extractor')->extract($rootPath.'/Resources/views', $extractedCatalogue);
96120

97121
// Load defined messages
98122
$currentCatalogue = new MessageCatalogue($locale);
99-
if (is_dir($bundle->getPath().'/Resources/translations')) {
100-
$loader->loadMessages($bundle->getPath().'/Resources/translations', $currentCatalogue);
123+
if (is_dir($translationsPath)) {
124+
$loader->loadMessages($translationsPath, $currentCatalogue);
101125
}
102126

103127
// Merge defined and extracted messages to get all message ids
@@ -130,7 +154,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
130154
}
131155

132156
$fallbackCatalogue = new MessageCatalogue($fallbackLocale);
133-
$loader->loadMessages($bundle->getPath().'/Resources/translations', $fallbackCatalogue);
157+
$loader->loadMessages($translationsPath, $fallbackCatalogue);
134158
$fallbackCatalogues[] = $fallbackCatalogue;
135159
}
136160
}

src/Symfony/Bundle/FrameworkBundle/Command/TranslationUpdateCommand.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ protected function configure()
3535
->setName('translation:update')
3636
->setDefinition(array(
3737
new InputArgument('locale', InputArgument::REQUIRED, 'The locale'),
38-
new InputArgument('bundle', InputArgument::OPTIONAL, 'The bundle where to load the messages, defaults to app/Resources folder', null),
38+
new InputArgument('bundle', InputArgument::OPTIONAL, 'The bundle name or directory where to load the messages, defaults to app/Resources folder'),
3939
new InputOption('prefix', null, InputOption::VALUE_OPTIONAL, 'Override the default prefix', '__'),
4040
new InputOption('output-format', null, InputOption::VALUE_OPTIONAL, 'Override the default output format', 'yml'),
4141
new InputOption('dump-messages', null, InputOption::VALUE_NONE, 'Should the messages be dumped in the console'),
@@ -83,16 +83,27 @@ protected function execute(InputInterface $input, OutputInterface $output)
8383

8484
return 1;
8585
}
86+
$kernel = $this->getContainer()->get('kernel');
8687

8788
// Define Root Path to App folder
88-
$rootPath = $this->getApplication()->getKernel()->getRootDir();
89+
$rootPath = $kernel->getRootDir();
8990
$currentName = "app folder";
9091

9192
// Override with provided Bundle info
9293
if (null !== $input->getArgument('bundle')) {
93-
$foundBundle = $this->getApplication()->getKernel()->getBundle($input->getArgument('bundle'));
94-
$rootPath = $foundBundle->getPath();
95-
$currentName = $foundBundle->getName();
94+
try {
95+
$foundBundle = $kernel->getBundle($input->getArgument('bundle'));
96+
$rootPath = $foundBundle->getPath();
97+
$currentName = $foundBundle->getName();
98+
} catch (\InvalidArgumentException $e) {
99+
// such a bundle does not exist, so treat the argument as path
100+
$rootPath = $input->getArgument('bundle');
101+
$currentName = $rootPath;
102+
103+
if (!is_dir($rootPath)) {
104+
throw new \InvalidArgumentException(sprintf('<error>"%s" is neither an enabled bundle nor a directory.</error>', $rootPath));
105+
}
106+
}
96107
}
97108

98109
// get bundle directory

0 commit comments

Comments
 (0)
0