8000 Update translation commands to work with default paths · symfony/symfony@3a76018 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3a76018

Browse files
committed
Update translation commands to work with default paths
1 parent b40c84d commit 3a76018

File tree

3 files changed

+75
-27
lines changed

3 files changed

+75
-27
lines changed

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

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,17 @@ class TranslationDebugCommand extends ContainerAwareCommand
4545
private $translator;
4646
private $reader;
4747
private $extractor;
48+
private $defaultTransPath;
49+
private $defaultViewsPath;
4850

4951
/**
5052
* @param TranslatorInterface $translator
5153
* @param TranslationReaderInterface $reader
5254
* @param ExtractorInterface $extractor
55+
* @param string $defaultTransPath
56+
* @param string $defaultViewsPath
5357
*/
54-
public function __construct($translator = null, TranslationReaderInterface $reader = null, ExtractorInterface $extractor = null)
58+
public function __construct($translator = null, TranslationReaderInterface $reader = null, ExtractorInterface $extractor = null, $defaultTransPath = null, $defaultViewsPath = null)
5559
{
5660
if (!$translator instanceof TranslatorInterface) {
5761
@trigger_error(sprintf('%s() expects an instance of "%s" as first argument since version 3.4. Not passing it is deprecated and will throw a TypeError in 4.0.', __METHOD__, TranslatorInterface::class), E_USER_DEPRECATED);
@@ -66,6 +70,8 @@ public function __construct($translator = null, TranslationReaderInterface $read
6670
$this->translator = $translator;
6771
$this->reader = $reader;
6872
$this->extractor = $extractor;
73+
$this->defaultTransPath = $defaultTransPath;
74+
$this->defaultViewsPath = $defaultViewsPath;
6975
}
7076

7177
/**
@@ -153,34 +159,56 @@ protected function execute(InputInterface $input, OutputInterface $output)
153159
/** @var KernelInterface $kernel */
154160
$kernel = $this->getApplication()->getKernel();
155161

156-
// Define Root Path to App folder
157-
$transPaths = array($kernel->getRootDir().'/Resources/');
162+
// Define Root Paths
163+
$transPaths = array($kernel->getRootDir().'/Resources/translations');
164+
if ($this->defaultTransPath) {
165+
$transPaths[] = $this->defaultTransPath;
166+
}
167+
$viewsPaths = array($kernel->getRootDir().'/Resources/views');
168+
if (< 67E6 span class=pl-c1>$this->defaultViewsPath) {
169+
$viewsPaths[] = $this->defaultViewsPath;
170+
}
158171

159172
// Override with provided Bundle info
160173
if (null !== $input->getArgument('bundle')) {
161174
try {
162175
$bundle = $kernel->getBundle($input->getArgument('bundle'));
163-
$transPaths = array(
164-
$bundle->getPath().'/Resources/',
165-
sprintf('%s/Resources/%s/', $kernel->getRootDir(), $bundle->getName()),
166-
);
176+
$transPaths = array($bundle->getPath().'/Resources/translations');
177+
if ($this->defaultTransPath) {
178+
$transPaths[] = $this->defaultTransPath.'/'.$bundle->getName();
179+
}
180+
$transPaths[] = sprintf('%s/Resources/%s/translations', $kernel->getRootDir(), $bundle->getName());
181+
$viewsPaths = array($bundle->getPath().'/Resources/views');
182+
if ($this->defaultViewsPath) {
183+
$viewsPaths[] = $this->defaultViewsPath.'/bundles/'.$bundle->getName();
184+
}
185+
$viewsPaths[] = sprintf('%s/Resources/%s/views', $kernel->getRootDir(), $bundle->getName());
167186
} catch (\InvalidArgumentException $e) {
168187
// such a bundle does not exist, so treat the argument as path
169-
$transPaths = array($input->getArgument('bundle').'/Resources/');
188+
$transPaths = array($input->getArgument('bundle').'/Resources/translations');
189+
$viewsPaths = array($input->getArgument('bundle').'/Resources/views');
170190

171191
if (!is_dir($transPaths[0])) {
172192
throw new \InvalidArgumentException(sprintf('"%s" is neither an enabled bundle nor a directory.', $transPaths[0]));
173193
}
174194
}
175195
} elseif ($input->getOption('all')) {
176196
foreach ($kernel->getBundles() as $bundle) {
177-
$transPaths[] = $bundle->getPath().'/Resources/';
178-
$transPaths[] = sprintf('%s/Resources/%s/', $kernel->getRootDir(), $bundle->getName());
197+
$transPaths[] = $bundle->getPath().'/Resources/translations';
198+
if ($this->defaultTransPath) {
199+
$transPaths[] = $this->defaultTransPath.'/'.$bundle->getName();
200+
}
201+
$transPaths[] = sprintf('%s/Resources/%s/translations', $kernel->getRootDir(), $bundle->getName());
202+
$viewsPaths[] = $bundle->getPath().'/Resources/views';
203+
if ($this->defaultViewsPath) {
204+
$viewsPaths[] = $this->defaultViewsPath.'/bundles/'.$bundle->getName();
205+
}
206+
$viewsPaths[] = sprintf('%s/Resources/%s/views', $kernel->getRootDir(), $bundle->getName());
179207
}
180208
}
181209

182210
// Extract used messages
183-
$extractedCatalogue = $this->extractMessages($locale, $transPaths);
211+
$extractedCatalogue = $this->extractMessages($locale, $viewsPaths);
184212

185213
// Load defined messages
186214
$currentCatalogue = $this->loadCurrentMessages($locale, $transPaths);
@@ -310,7 +338,6 @@ private function extractMessages($locale, $transPaths)
310338
{
311339
$extractedCatalogue = new MessageCatalogue($locale);
312340
foreach ($transPaths as $path) {
313-
$path = $path.'views';
314341
if (is_dir($path)) {
315342
$this->extractor->extract($path, $extractedCatalogue);
316343
}
@@ -329,7 +356,6 @@ private function loadCurrentMessages($locale, $transPaths)
329356
{
330357
$currentCatalogue = new MessageCatalogue($locale);
331358
foreach ($transPaths as $path) {
332-
$path = $path.'translations';
333359
if (is_dir($path)) {
334360
$this->reader->read($path, $currentCatalogue);
335361
}
@@ -355,7 +381,6 @@ private function loadFallbackCatalogues($locale, $transPaths)
355381

356382
$fallbackCatalogue = new MessageCatalogue($fallbackLocale);
357383
foreach ($transPaths as $path) {
358-
$path = $path.'translations';
359384
if (is_dir($path)) {
360385
$this->reader->read($path, $fallbackCatalogue);
361386
}

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

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Bundle\FrameworkBundle\Command;
1313

1414
use Symfony\Component\Console\Style\SymfonyStyle;
15+
use Symfony\Component\HttpKernel\KernelInterface;
1516
use Symfony\Component\Translation\Catalogue\TargetOperation;
1617
use Symfony\Component\Translation\Catalogue\MergeOperation;
1718
use Symfony\Component\Console\Input\InputInterface;
@@ -39,14 +40,18 @@ class TranslationUpdateCommand extends ContainerAwareCommand
3940
private $reader;
4041
private $extractor;
4142
private $defaultLocale;
43+
private $defaultTransPath;
44+
private $defaultViewsPath;
4245

4346
/**
4447
* @param TranslationWriterInterface $writer
4548
* @param TranslationReaderInterface $reader
4649
* @param ExtractorInterface $extractor
4750
* @param string $defaultLocale
51+
* @param string $defaultTransPath
52+
* @param string $defaultViewsPath
4853
*/
49-
public function __construct($writer = null, TranslationReaderInterface $reader = null, ExtractorInterface $extractor = null, $defaultLocale = null)
54+
public function __construct($writer = null, TranslationReaderInterface $reader = null, ExtractorInterface $extractor = null, $defaultLocale = null, $defaultTransPath = null, $defaultViewsPath = null)
5055
{
51 F42D 56
if (!$writer instanceof TranslationWriterInterface) {
5257
@trigger_error(sprintf('%s() expects an instance of "%s" as first argument since version 3.4. Not passing it is deprecated and will throw a TypeError in 4.0.', __METHOD__, TranslationWriterInterface::class), E_USER_DEPRECATED);
@@ -62,6 +67,8 @@ public function __construct($writer = null, TranslationReaderInterface $reader =
6267
$this->reader = $reader;
6368
$this->extractor = $extractor;
6469
$this->defaultLocale = $defaultLocale;
70+
$this->defaultTransPath = $defaultTransPath;
71+
$this->defaultViewsPath = $defaultViewsPath;
6572
}
6673

6774
/**
@@ -149,24 +156,39 @@ protected function execute(InputInterface $input, OutputInterface $output)
149156

150157
return 1;
151158
}
159+
/** @var KernelInterface $kernel */
152160
$kernel = $this->getApplication()->getKernel();
153161

154-
// Define Root Path to App folder
155-
$transPaths = array($kernel->getRootDir().'/Resources/');
162+
// Define Root Paths
163+
$transPaths = array($kernel->getRootDir().'/Resources/translations');
164+
if ($this->defaultTransPath) {
165+
$transPaths[] = $this->defaultTransPath;
166+
}
167+
$viewsPaths = array($kernel->getRootDir().'/Resources/views');
168+
if ($this->defaultViewsPath) {
169+
$viewsPaths[] = $this->defaultViewsPath;
170+
}
156171
$currentName = 'app folder';
157172

158173
// Override with provided Bundle info
159174
if (null !== $input->getArgument('bundle')) {
160175
try {
161176
$foundBundle = $kernel->getBundle($input->getArgument('bundle'));
162-
$transPaths = array(
163-
$foundBundle->getPath().'/Resources/',
164-
sprintf('%s/Resources/%s/', $kernel->getRootDir(), $foundBundle->getName()),
165-
);
177+
$transPaths = array($foundBundle->getPath().'/Resources/translations');
178+
if ($this->defaultTransPath) {
179+
$transPaths[] = $this->defaultTransPath.'/'.$foundBundle->getName();
180+
}
181+
$transPaths[] = sprintf('%s/Resources/%s/translations', $kernel->getRootDir(), $foundBundle->getName());
182+
$viewsPaths = array($foundBundle->getPath().'/Resources/views');
183+
if ($this->defaultViewsPath) {
184+
$viewsPaths[] = $this->defaultViewsPath.'/bundles/'.$foundBundle->getName();
185+
}
186+
$viewsPaths[] = sprintf('%s/Resources/%s/views', $kernel->getRootDir(), $foundBundle->getName());
166187
$currentName = $foundBundle->getName();
167188
} catch (\InvalidArgumentException $e) {
168189
// such a bundle does not exist, so treat the argument as path
169-
$transPaths = array($input->getArgument('bundle').'/Resources/');
190+
$transPaths = array($input->getArgument('bundle').'/Resources/translations');
191+
$viewsPaths = array($input->getArgument('bundle').'/Resources/views');
170192
$currentName = $transPaths[0];
171193

172194
if (!is_dir($transPaths[0])) {
@@ -188,8 +210,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
188210
$prefix = '';
189211
}
190212
$this->extractor->setPrefix($prefix);
191-
foreach ($transPaths as $path) {
192-
$path .= 'views';
213+
foreach ($viewsPaths as $path) {
193214
if (is_dir($path)) {
194215
$this->extractor->extract($path, $extractedCatalogue);
195216
}
@@ -199,7 +220,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
199220
$currentCatalogue = new MessageCatalogue($input->getArgument('locale'));
200221
$errorIo->comment('Loading translation files...');
201222
foreach ($transPaths as $path) {
202-
$path .= 'translations';
203223
if (is_dir($path)) {
204224
$this->reader->read($path, $currentCatalogue);
205225
}
@@ -267,14 +287,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
267287

268288
$bundleTransPath = false;
269289
foreach ($transPaths as $path) {
270-
$path .= 'translations';
271290
if (is_dir($path)) {
272291
$bundleTransPath = $path;
273292
}
274293
}
275294

276295
if (!$bundleTransPath) {
277-
$bundleTransPath = end($transPaths).'translations';
296+
$bundleTransPath = end($transPaths);
278297
}
279298

280299
$this->writer->write($operation->getResult(), $input->getOption('output-format'), array('path' => $bundleTransPath, 'default_locale' => $this->defaultLocale));

src/Symfony/Bundle/FrameworkBundle/Resources/config/console.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@
7878
<argument type="service" id="translator" />
7979
<argument type="service" id="translation.reader" />
8080
<argument type="service" id="translation.extractor" />
81+
<argument>%translator.default_path%</argument>
82+
<argument on-invalid="null">%twig.default_path%</argument>
8183
<tag name="console.command" command="debug:translation" />
8284
</service>
8385

@@ -86,6 +88,8 @@
8688
<argument type="service" id="translation.reader" />
8789
<argument type="service" id="translation.extractor" />
8890
<argument>%kernel.default_locale%</argument>
91+
<argument>%translator.default_path%</argument>
92+
<argument on-invalid="null">%twig.default_path%</argument>
8993
<tag name="console.command" command="translation:update" />
9094
</service>
9195

0 commit comments

Comments
 (0)
0