Skip to content

Commit 396a441

Browse files
mvrhovNyholm
authored andcommitted
Extract/update only for one bundle (#166)
1 parent 0709136 commit 396a441

File tree

6 files changed

+74
-3
lines changed

6 files changed

+74
-3
lines changed

Command/BundleTrait.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the PHP Translation package.
5+
*
6+
* (c) PHP Translation team <tobias.nyholm@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Translation\Bundle\Command;
13+
14+
use Symfony\Component\Console\Input\InputInterface;
15+
use Translation\Bundle\Model\Configuration;
16+
17+
trait BundleTrait
18+
{
19+
private function configureBundleDirs(InputInterface $input, Configuration $config)
20+
{
21+
if ($bundleName = $input->getOption('bundle')) {
22+
if ('@' === $bundleName[0]) {
23+
if (false === $pos = strpos($bundleName, '/')) {
24+
$bundleName = substr($bundleName, 1);
25+
} else {
26+
$bundleName = substr($bundleName, 1, $pos - 2);
27+
}
28+
}
29+
30+
$bundle = $this->getApplication()
31+
->getKernel()
32+
->getBundle($bundleName)
33+
;
34+
35+
$config->reconfigureBundleDirs($bundle->getPath(), $bundle->getPath().'/Resources/translations');
36+
}
37+
}
38+
}

Command/DeleteObsoleteCommand.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Console\Helper\ProgressBar;
1616
use Symfony\Component\Console\Input\InputArgument;
1717
use Symfony\Component\Console\Input\InputInterface;
18+
use Symfony\Component\Console\Input\InputOption;
1819
use Symfony\Component\Console\Output\OutputInterface;
1920
use Symfony\Component\Console\Question\ConfirmationQuestion;
2021

@@ -23,13 +24,17 @@
2324
*/
2425
class DeleteObsoleteCommand extends ContainerAwareCommand
2526
{
27+
use BundleTrait;
28+
2629
protected function configure()
2730
{
2831
$this
2932
->setName('translation:delete-obsolete')
3033
->setDescription('Delete all translations marked as obsolete.')
3134
->addArgument('configuration', InputArgument::OPTIONAL, 'The configuration to use', 'default')
32-
->addArgument('locale', InputArgument::OPTIONAL, 'The locale ot use. If omitted, we use all configured locales.', null);
35+
->addArgument('locale', InputArgument::OPTIONAL, 'The locale ot use. If omitted, we use all configured locales.', null)
36+
->addOption('bundle', 'b', InputOption::VALUE_REQUIRED, 'The bundle you want remove translations from.')
37+
;
3338
}
3439

3540
protected function execute(InputInterface $input, OutputInterface $output)
@@ -43,6 +48,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
4348

4449
$catalogueManager = $container->get('php_translation.catalogue_manager');
4550
$config = $container->get('php_translation.configuration_manager')->getConfiguration($configName);
51+
$this->configureBundleDirs($input, $config);
4652
$catalogueManager->load($container->get('php_translation.catalogue_fetcher')->getCatalogues($config, $locales));
4753

4854
$storage = $container->get('php_translation.storage.'.$configName);

Command/DownloadCommand.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,16 @@
2525
*/
2626
class DownloadCommand extends ContainerAwareCommand
2727
{
28+
use BundleTrait;
29+
2830
protected function configure()
2931
{
3032
$this
3133
->setName('translation:download')
3234
->setDescription('Replace local messages with messages from remote')
3335
->addArgument('configuration', InputArgument::OPTIONAL, 'The configuration to use', 'default')
3436
->addOption('cache', null, InputOption::VALUE_NONE, 'Clear the cache if the translations have changed')
37+
->addOption('bundle', 'b', InputOption::VALUE_REQUIRED, 'The bundle you want update translations from.')
3538
;
3639
}
3740

@@ -44,6 +47,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
4447
$storage = $container->get('php_translation.storage.'.$configName);
4548
/** @var Configuration $configuration */
4649
$configuration = $this->getContainer()->get('php_translation.configuration.'.$configName);
50+
$this->configureBundleDirs($input, $configuration);
4751

4852
if ($input->getOption('cache')) {
4953
$translationsDirectory = $configuration->getOutputDir();

Command/ExtractCommand.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,18 @@
2626
*/
2727
class ExtractCommand extends ContainerAwareCommand
2828
{
29+
use BundleTrait;
30+
2931
protected function configure()
3032
{
3133
$this
3234
->setName('translation:extract')
3335
->setDescription('Extract translations from source code.')
3436
->addArgument('configuration', InputArgument::OPTIONAL, 'The configuration to use', 'default')
3537
->addArgument('locale', InputArgument::OPTIONAL, 'The locale ot use. If omitted, we use all configured locales.', false)
36-
->addOption('hide-errors', null, InputOption::VALUE_NONE, 'If we should print error or not');
38+
->addOption('hide-errors', null, InputOption::VALUE_NONE, 'If we should print error or not')
39+
->addOption('bundle', 'b', InputOption::VALUE_REQUIRED, 'The bundle you want extract translations from.')
40+
;
3741
}
3842

3943
protected function execute(InputInterface $input, OutputInterface $output)
@@ -51,6 +55,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
5155
$catalogues = $container->get('php_translation.catalogue_fetcher')
5256
->getCatalogues($config, $locales);
5357

58+
$this->configureBundleDirs($input, $config);
5459
$finder = $this->getConfiguredFinder($config);
5560

5661
$result = $importer->extractToCatalogues($finder, $catalogues, [

Command/StatusCommand.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,18 @@
2323
*/
2424
class StatusCommand extends ContainerAwareCommand
2525
{
26+
use BundleTrait;
27+
2628
protected function configure()
2729
{
2830
$this
2931
->setName('translation:status')
3032
->setDescription('Show status about your translations.')
3133
->addArgument('configuration', InputArgument::OPTIONAL, 'The configuration to use', 'default')
3234
->addArgument('locale', InputArgument::OPTIONAL, 'The locale ot use. If omitted, we use all configured locales.', false)
33-
->addOption('json', null, InputOption::VALUE_NONE, 'If we should output in Json format');
35+
->addOption('json', null, InputOption::VALUE_NONE, 'If we should output in Json format')
36+
->addOption('bundle', 'b', InputOption::VALUE_REQUIRED, 'The translations for bundle you want to check.')
37+
;
3438
}
3539

3640
protected function execute(InputInterface $input, OutputInterface $output)
@@ -39,6 +43,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
3943
$counter = $container->get('php_translation.catalogue_counter');
4044
$config = $container->get('php_translation.configuration_manager')
4145
->getConfiguration($input->getArgument('configuration'));
46+
$this->configureBundleDirs($input, $config);
4247

4348
$locales = [];
4449
if ($inputLocale = $input->getArgument('locale')) {

Model/Configuration.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,4 +217,17 @@ public function getXliffVersion()
217217
{
218218
return $this->xliffVersion;
219219
}
220+
221+
/**
222+
* Reconfigures the directories so we can use one configuration for extracting
223+
* the messages only from one bundle.
224+
*
225+
* @param string $bundleDir
226+
* @param string $outputDir
227+
*/
228+
public function reconfigureBundleDirs($bundleDir, $outputDir)
229+
{
230+
$this->dirs = is_array($bundleDir) ? $bundleDir : [$bundleDir];
231+
$this->outputDir = $outputDir;
232+
}
220233
}

0 commit comments

Comments
 (0)