Skip to content

Commit cc13ae4

Browse files
committed
feature #13443 [Translation][Command][FrameworkBundle] Enable translation debugging in directories (xelaris)
This PR was merged into the 2.7 branch. Discussion ---------- [Translation][Command][FrameworkBundle] Enable translation debugging in directories | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #12273 | License | MIT | Doc PR | This PR follows up #13340 and enables not only to inspect Bundles, but also directories, like the `app/` directory. Additionally it harmonizes the 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. Commits ------- 2662244 [FrameworkBundle] Enable translation debugging in directories
2 parents 1d5f786 + 2662244 commit cc13ae4

File tree

2 files changed

+50
-13
lines changed

2 files changed

+50
-13
lines changed

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

Lines changed: 34 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,39 @@ 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+
if (is_dir($rootPath.'/Resources/views')) {
120+
$this->getContainer()->get('translation.extractor')->extract($rootPath.'/Resources/views', $extractedCatalogue);
121+
}
96122

97123
// Load defined messages
98124
$currentCatalogue = new MessageCatalogue($locale);
99-
if (is_dir($bundle->getPath().'/Resources/translations')) {
100-
$loader->loadMessages($bundle->getPath().'/Resources/translations', $currentCatalogue);
125+
if (is_dir($translationsPath)) {
126+
$loader->loadMessages($translationsPath, $currentCatalogue);
101127
}
102128

103129
// Merge defined and extracted messages to get all message ids
@@ -130,7 +156,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
130156
}
131157

132158
$fallbackCatalogue = new MessageCatalogue($fallbackLocale);
133-
$loader->loadMessages($bundle->getPath().'/Resources/translations', $fallbackCatalogue);
159+
$loader->loadMessages($translationsPath, $fallbackCatalogue);
134160
$fallbackCatalogues[] = $fallbackCatalogue;
135161
}
136162
}

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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy