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

Skip to content

Commit dc72866

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

File tree

6 files changed

+131
-30
lines changed

6 files changed

+131
-30
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 */
F438
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 ($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
{
5156
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 /> <!-- %twig.default_path% -->
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 /> <!-- %twig.default_path% -->
8993
<tag name="console.command" command="translation:update" />
9094
</service>
9195

src/Symfony/Bundle/FrameworkBundle/Tests/Command/TranslationDebugCommandTest.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,21 @@ public function testDebugDefaultDirectory()
6464
$this->assertRegExp('/unused/', $tester->getDisplay());
6565
}
6666

67+
public function testDebugDefaultRootDirectory()
68+
{
69+
$this->fs->remove($this->translationDir);
70+
$this->fs = new Filesystem();
71+
$this->translationDir = sys_get_temp_dir().'/'.uniqid('sf2_translation', true);
72+
$this->fs->mkdir($this->translationDir.'/translations');
73+
$this->fs->mkdir($this->translationDir.'/templates');
74+
75+
$tester = $this->createCommandTester(array('foo' => 'foo'), array('bar' => 'bar'));
76+
$tester->execute(array('locale' => 'en'));
77+
78+
$this->assertRegExp('/missing/', $tester->getDisplay());
79+
$this->assertRegExp('/unused/', $tester->getDisplay());
80+
}
81+
6782
public function testDebugCustomDirectory()
6883
{
6984
$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\KernelInterface')->getMock();
@@ -100,6 +115,8 @@ protected function setUp()
100115
$this->translationDir = sys_get_temp_dir().'/'.uniqid('sf2_translation', true);
101116
$this->fs->mkdir($this->translationDir.'/Resources/translations');
102117
$this->fs->mkdir($this->translationDir.'/Resources/views');
118+
$this->fs->mkdir($this->translationDir.'/translations');
119+
$this->fs->mkdir($this->translationDir.'/templates');
103120
}
104121

105122
protected function tearDown()
@@ -174,7 +191,7 @@ private function createCommandTester($extractedMessages = array(), $loadedMessag
174191
->method('getContainer')
175192
->will($this->returnValue($this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock()));
176193

177-
$command = new TranslationDebugCommand($translator, $loader, $extractor);
194+
$command = new TranslationDebugCommand($translator, $loader, $extractor, $this->translationDir.'/translations', $this->translationDir.'/templates');
178195

179196
$application = new Application($kernel);
180197
$application->add($command);

src/Symfony/Bundle/FrameworkBundle/Tests/Command/TranslationUpdateCommandTest.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,19 @@ public function testDumpMessagesAndClean()
3131
$this->assertRegExp('/1 message was successfully extracted/', $tester->getDisplay());
3232
}
3333

34+
public function testDumpMessagesAndCleanInRootDirectory()
35+
{
36+
$this->fs->remove($this->translationDir);
37+
$this->translationDir = sys_get_temp_dir().'/'.uniqid('sf2_translation', true);
38+
$this->fs->mkdir($this->translationDir.'/translations');
39+
$this->fs->mkdir($this->translationDir.'/templates');
40+
41+
$tester = $this->createCommandTester(array('messages' => array('foo' => 'foo')));
42+
$tester->execute(array('command' => 'translation:update', 'locale' => 'en', '--dump-messages' => true, '--clean' => true));
43+
$this->assertRegExp('/foo/', $tester->getDisplay());
44+
$this->assertRegExp('/1 message was successfully extracted/', $tester->getDisplay());
45+
}
46+
3447
public function testDumpTwoMessagesAndClean()
3548
{
3649
$tester = $this->createCommandTester(array('messages' => array('foo' => 'foo', 'bar' => 'bar')));
@@ -55,6 +68,18 @@ public function testWriteMessages()
5568
$this->assertRegExp('/Translation files were successfully updated./', $tester->getDisplay());
5669
}
5770

71+
public function testWriteMessagesInRootDirectory()
72+
{
73+
$this->fs->remove($this->translationDir);
74+
$this->translationDir = sys_get_temp_dir().'/'.uniqid('sf2_translation', true);
75+
$this->fs->mkdir($this->translationDir.'/translations');
76+
$this->fs->mkdir($this->translationDir.'/templates');
77+
78+
$tester = $this->createCommandTester(array('messages' => array('foo' => 'foo')));
79+
$tester->execute(array('command' => 'translation:update', 'locale' => 'en', '--force' => true));
80+
$this->assertRegExp('/Translation files were successfully updated./', $tester->getDisplay());
81+
}
82+
5883
public function testWriteMessagesForSpecificDomain()
5984
{
6085
$tester = $this->createCommandTester(array('messages' => array('foo' => 'foo'), 'mydomain' => array('bar' => 'bar')));
@@ -68,6 +93,8 @@ protected function setUp()
6893
$this->translationDir = sys_get_temp_dir().'/'.uniqid('sf2_translation', true);
6994
$this->fs->mkdir($this->translationDir.'/Resources/translations');
7095
$this->fs->mkdir($this->translationDir.'/Resources/views');
96+
$this->fs->mkdir($this->translationDir.'/translations');
97+
$this->fs->mkdir($this->translationDir.'/templates');
7198
}
7299

73100
protected function tearDown()
@@ -152,7 +179,7 @@ private function createCommandTester($extractedMessages = array(), $loadedMessag
152179
->method('getContainer')
153180
->will($this->returnValue($this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerInterface')->getMock()));
154181

155-
$command = new TranslationUpdateCommand($writer, $loader, $extractor, 'en');
182+
$command = new TranslationUpdateCommand($writer, $loader, $extractor, 'en', $this->translationDir.'/translations', $this->translationDir.'/templates');
156183

157184
$application = new Application($kernel);
158185
$application->add($command);

src/Symfony/Component/Translation/DependencyInjection/TranslatorPass.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ class TranslatorPass implements CompilerPassInterface
2121
private $translatorServiceId;
2222
private $readerServiceId;
2323
private $loaderTag;
24+
private $debugCommandServiceId;
25+
private $updateCommandServiceId;
2426

25-
public function __construct($translatorServiceId = 'translator.default', $readerServiceId = 'translation.loader', $loaderTag = 'translation.loader')
27+
public function __construct($translatorServiceId = 'translator.default', $readerServiceId = 'translation.loader', $loaderTag = 'translation.loader', $debugCommandServiceId = 'console.command.translation_debug', $updateCommandServiceId = 'console.command.translation_update')
2628
{
2729
if ('translation.loader' === $readerServiceId && 2 > func_num_args()) {
2830
@trigger_error('The default value for $readerServiceId will change in 4.0 to "translation.reader".', E_USER_DEPRECATED);
@@ -31,6 +33,8 @@ public function __construct($translatorServiceId = 'translator.default', $reader
3133
$this->translatorServiceId = $translatorServiceId;
3234
$this->readerServiceId = $readerServiceId;
3335
$this->loaderTag = $loaderTag;
36+
$this->debugCommandServiceId = $debugCommandServiceId;
37+
$this->updateCommandServiceId = $updateCommandServiceId;
3438
}
3539

3640
public function process(ContainerBuilder $container)
@@ -75,5 +79,10 @@ public function process(ContainerBuilder $container)
7579
->replaceArgument(0, ServiceLocatorTagPass::register($container, $loaderRefs))
7680
->replaceArgument(3, $loaders)
7781
;
82+
83+
if ($container->hasParameter('twig.default_path')) {
84+
$container->getDefinition($this->debugCommandServiceId)->replaceArgument(4, $container->getParameter('twig.default_path'));
85+
$container->getDefinition($this->updateCommandServiceId)->replaceArgument(5, $container->getParameter('twig.default_path'));
86+
}
7887
}
7988
}

0 commit comments

Comments
 (0)
0