Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[FrameworkBundle] Update translation commands to work with default paths #25065

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,17 @@ class TranslationDebugCommand extends ContainerAwareCommand
private $translator;
private $reader;
private $extractor;
private $defaultTransPath;
private $defaultViewsPath;

/**
* @param TranslatorInterface $translator
* @param TranslationReaderInterface $reader
* @param ExtractorInterface $extractor
* @param string $defaultTransPath
* @param string $defaultViewsPath
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

string|null

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've removed the block as it does not help in any way.

*/
public function __construct($translator = null, TranslationReaderInterface $reader = null, ExtractorInterface $extractor = null)
public function __construct($translator = null, TranslationReaderInterface $reader = null, ExtractorInterface $extractor = null, $defaultTransPath = null, $defaultViewsPath = null)
{
if (!$translator instanceof TranslatorInterface) {
@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);
Expand All @@ -66,6 +70,8 @@ public function __construct($translator = null, TranslationReaderInterface $read
$this->translator = $translator;
$this->reader = $reader;
$this->extractor = $extractor;
$this->defaultTransPath = $defaultTransPath;
$this->defaultViewsPath = $defaultViewsPath;
}

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

// Define Root Path to App folder
$transPaths = array($kernel->getRootDir().'/Resources/');
// Define Root Paths
$transPaths = array($kernel->getRootDir().'/Resources/translations');
if ($this->defaultTransPath) {
$transPaths[] = $this->defaultTransPath;
}
$viewsPaths = array($kernel->getRootDir().'/Resources/views');
if ($this->defaultViewsPath) {
$viewsPaths[] = $this->defaultViewsPath;
}

// Override with provided Bundle info
if (null !== $input->getArgument('bundle')) {
try {
$bundle = $kernel->getBundle($input->getArgument('bundle'));
$transPaths = array(
$bundle->getPath().'/Resources/',
sprintf('%s/Resources/%s/', $kernel->getRootDir(), $bundle->getName()),
);
$transPaths = array($bundle->getPath().'/Resources/translations');
if ($this->defaultTransPath) {
$transPaths[] = $this->defaultTransPath.'/'.$bundle->getName();
}
$transPaths[] = sprintf('%s/Resources/%s/translations', $kernel->getRootDir(), $bundle->getName());
$viewsPaths = array($bundle->getPath().'/Resources/views');
if ($this->defaultViewsPath) {
$viewsPaths[] = $this->defaultViewsPath.'/bundles/'.$bundle->getName();
}
$viewsPaths[] = sprintf('%s/Resources/%s/views', $kernel->getRootDir(), $bundle->getName());
} catch (\InvalidArgumentException $e) {
// such a bundle does not exist, so treat the argument as path
$transPaths = array($input->getArgument('bundle').'/Resources/');
$transPaths = array($input->getArgument('bundle').'/Resources/translations');
$viewsPaths = array($input->getArgument('bundle').'/Resources/views');

if (!is_dir($transPaths[0])) {
throw new \InvalidArgumentException(sprintf('"%s" is neither an enabled bundle nor a directory.', $transPaths[0]));
}
}
} elseif ($input->getOption('all')) {
foreach ($kernel->getBundles() as $bundle) {
$transPaths[] = $bundle->getPath().'/Resources/';
$transPaths[] = sprintf('%s/Resources/%s/', $kernel->getRootDir(), $bundle->getName());
$transPaths[] = $bundle->getPath().'/Resources/translations';
if ($this->defaultTransPath) {
$transPaths[] = $this->defaultTransPath.'/'.$bundle->getName();
}
$transPaths[] = sprintf('%s/Resources/%s/translations', $kernel->getRootDir(), $bundle->getName());
$viewsPaths[] = $bundle->getPath().'/Resources/views';
if ($this->defaultViewsPath) {
$viewsPaths[] = $this->defaultViewsPath.'/bundles/'.$bundle->getName();
}
$viewsPaths[] = sprintf('%s/Resources/%s/views', $kernel->getRootDir(), $bundle->getName());
}
}

// Extract used messages
$extractedCatalogue = $this->extractMessages($locale, $transPaths);
$extractedCatalogue = $this->extractMessages($locale, $viewsPaths);

// Load defined messages
$currentCatalogue = $this->loadCurrentMessages($locale, $transPaths);
Expand Down Expand Up @@ -310,7 +338,6 @@ private function extractMessages($locale, $transPaths)
{
$extractedCatalogue = new MessageCatalogue($locale);
foreach ($transPaths as $path) {
$path = $path.'views';
if (is_dir($path)) {
$this->extractor->extract($path, $extractedCatalogue);
}
Expand All @@ -329,7 +356,6 @@ private function loadCurrentMessages($locale, $transPaths)
{
$currentCatalogue = new MessageCatalogue($locale);
foreach ($transPaths as $path) {
$path = $path.'translations';
if (is_dir($path)) {
$this->reader->read($path, $currentCatalogue);
}
Expand All @@ -355,7 +381,6 @@ private function loadFallbackCatalogues($locale, $transPaths)

$fallbackCatalogue = new MessageCatalogue($fallbackLocale);
foreach ($transPaths as $path) {
$path = $path.'translations';
if (is_dir($path)) {
$this->reader->read($path, $fallbackCatalogue);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Bundle\FrameworkBundle\Command;

use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Translation\Catalogue\TargetOperation;
use Symfony\Component\Translation\Catalogue\MergeOperation;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -39,14 +40,18 @@ class TranslationUpdateCommand extends ContainerAwareCommand
private $reader;
private $extractor;
private $defaultLocale;
private $defaultTransPath;
private $defaultViewsPath;

/**
* @param TranslationWriterInterface $writer
* @param TranslationReaderInterface $reader
* @param ExtractorInterface $extractor
* @param string $defaultLocale
* @param string $defaultTransPath
* @param string $defaultViewsPath
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

string|null

*/
public function __construct($writer = null, TranslationReaderInterface $reader = null, ExtractorInterface $extractor = null, $defaultLocale = null)
public function __construct($writer = null, TranslationReaderInterface $reader = null, ExtractorInterface $extractor = null, $defaultLocale = null, $defaultTransPath = null, $defaultViewsPath = null)
{
if (!$writer instanceof TranslationWriterInterface) {
@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);
Expand All @@ -62,6 +67,8 @@ public function __construct($writer = null, TranslationReaderInterface $reader =
$this->reader = $reader;
$this->extractor = $extractor;
$this->defaultLocale = $defaultLocale;
$this->defaultTransPath = $defaultTransPath;
$this->defaultViewsPath = $defaultViewsPath;
}

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

return 1;
}
/** @var KernelInterface $kernel */
$kernel = $this->getApplication()->getKernel();

// Define Root Path to App folder
$transPaths = array($kernel->getRootDir().'/Resources/');
// Define Root Paths
$transPaths = array($kernel->getRootDir().'/Resources/translations');
if ($this->defaultTransPath) {
$transPaths[] = $this->defaultTransPath;
}
$viewsPaths = array($kernel->getRootDir().'/Resources/views');
if ($this->defaultViewsPath) {
$viewsPaths[] = $this->defaultViewsPath;
}
$currentName = 'app folder';

// Override with provided Bundle info
if (null !== $input->getArgument('bundle')) {
try {
$foundBundle = $kernel->getBundle($input->getArgument('bundle'));
$transPaths = array(
$foundBundle->getPath().'/Resources/',
sprintf('%s/Resources/%s/', $kernel->getRootDir(), $foundBundle->getName()),
);
$transPaths = array($foundBundle->getPath().'/Resources/translations');
if ($this->defaultTransPath) {
$transPaths[] = $this->defaultTransPath.'/'.$foundBundle->getName();
}
$transPaths[] = sprintf('%s/Resources/%s/translations', $kernel->getRootDir(), $foundBundle->getName());
$viewsPaths = array($foundBundle->getPath().'/Resources/views');
if ($this->defaultViewsPath) {
$viewsPaths[] = $this->defaultViewsPath.'/bundles/'.$foundBundle->getName();
}
$viewsPaths[] = sprintf('%s/Resources/%s/views', $kernel->getRootDir(), $foundBundle->getName());
$currentName = $foundBundle->getName();
} catch (\InvalidArgumentException $e) {
// such a bundle does not exist, so treat the argument as path
$transPaths = array($input->getArgument('bundle').'/Resources/');
$transPaths = array($input->getArgument('bundle').'/Resources/translations');
$viewsPaths = array($input->getArgument('bundle').'/Resources/views');
$currentName = $transPaths[0];

if (!is_dir($transPaths[0])) {
Expand All @@ -188,8 +210,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$prefix = '';
}
$this->extractor->setPrefix($prefix);
foreach ($transPaths as $path) {
$path .= 'views';
foreach ($viewsPaths as $path) {
if (is_dir($path)) {
$this->extractor->extract($path, $extractedCatalogue);
}
Expand All @@ -199,7 +220,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
$currentCatalogue = new MessageCatalogue($input->getArgument('locale'));
$errorIo->comment('Loading translation files...');
foreach ($transPaths as $path) {
$path .= 'translations';
if (is_dir($path)) {
$this->reader->read($path, $currentCatalogue);
}
Expand Down Expand Up @@ -267,14 +287,13 @@ protected function execute(InputInterface $input, OutputInterface $output)

$bundleTransPath = false;
foreach ($transPaths as $path) {
$path .= 'translations';
if (is_dir($path)) {
$bundleTransPath = $path;
}
}

if (!$bundleTransPath) {
$bundleTransPath = end($transPaths).'translations';
$bundleTransPath = end($transPaths);
}

$this->writer->write($operation->getResult(), $input->getOption('output-format'), array('path' => $bundleTransPath, 'default_locale' => $this->defaultLocale));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@
<argument type="service" id="translator" />
<argument type="service" id="translation.reader" />
<argument type="service" id="translation.extractor" />
<argument>%translator.default_path%</argument>
<argument /> <!-- %twig.default_path% -->
<tag name="console.command" command="debug:translation" />
</service>

Expand All @@ -86,6 +88,8 @@
<argument type="service" id="translation.reader" />
<argument type="service" id="translation.extractor" />
<argument>%kernel.default_locale%</argument>
<argument>%translator.default_path%</argument>
<argument /> <!-- %twig.default_path% -->
<tag name="console.command" command="translation:update" />
</service>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,21 @@ public function testDebugDefaultDirectory()
$this->assertRegExp('/unused/', $tester->getDisplay());
}

public function testDebugDefaultRootDirectory()
{
$this->fs->remove($this->translationDir);
$this->fs = new Filesystem();
$this->translationDir = sys_get_temp_dir().'/'.uniqid('sf2_translation', true);
$this->fs->mkdir($this->translationDir.'/translations');
$this->fs->mkdir($this->translationDir.'/templates');

$tester = $this->createCommandTester(array('foo' => 'foo'), array('bar' => 'bar'));
$tester->execute(array('locale' => 'en'));

$this->assertRegExp('/missing/', $tester->getDisplay());
$this->assertRegExp('/unused/', $tester->getDisplay());
}

public function testDebugCustomDirectory()
{
$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\KernelInterface')->getMock();
Expand Down Expand Up @@ -100,6 +115,8 @@ protected function setUp()
$this->translationDir = sys_get_temp_dir().'/'.uniqid('sf2_translation', true);
$this->fs->mkdir($this->translationDir.'/Resources/translations');
$this->fs->mkdir($this->translationDir.'/Resources/views');
$this->fs->mkdir($this->translationDir.'/translations');
$this->fs->mkdir($this->translationDir.'/templates');
}

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

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

$application = new Application($kernel);
$application->add($command);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ public function testDumpMessagesAndClean()
$this->assertRegExp('/1 message was successfully extracted/', $tester->getDisplay());
}

public function testDumpMessagesAndCleanInRootDirectory()
{
$this->fs->remove($this->translationDir);
$this->translationDir = sys_get_temp_dir().'/'.uniqid('sf2_translation', true);
$this->fs->mkdir($this->translationDir.'/translations');
$this->fs->mkdir($this->translationDir.'/templates');

$tester = $this->createCommandTester(array('messages' => array('foo' => 'foo')));
$tester->execute(array('command' => 'translation:update', 'locale' => 'en', '--dump-messages' => true, '--clean' => true));
$this->assertRegExp('/foo/', $tester->getDisplay());
$this->assertRegExp('/1 message was successfully extracted/', $tester->getDisplay());
}

public function testDumpTwoMessagesAndClean()
{
$tester = $this->createCommandTester(array('messages' => array('foo' => 'foo', 'bar' => 'bar')));
Expand All @@ -55,6 +68,18 @@ public function testWriteMessages()
$this->assertRegExp('/Translation files were successfully updated./', $tester->getDisplay());
}

public function testWriteMessagesInRootDirectory()
{
$this->fs->remove($this->translationDir);
$this->translationDir = sys_get_temp_dir().'/'.uniqid('sf2_translation', true);
$this->fs->mkdir($this->translationDir.'/translations');
$this->fs->mkdir($this->translationDir.'/templates');

$tester = $this->createCommandTester(array('messages' => array('foo' => 'foo')));
$tester->execute(array('command' => 'translation:update', 'locale' => 'en', '--force' => true));
$this->assertRegExp('/Translation files were successfully updated./', $tester->getDisplay());
}

public function testWriteMessagesForSpecificDomain()
{
$tester = $this->createCommandTester(array('messages' => array('foo' => 'foo'), 'mydomain' => array('bar' => 'bar')));
Expand All @@ -68,6 +93,8 @@ protected function setUp()
$this->translationDir = sys_get_temp_dir().'/'.uniqid('sf2_translation', true);
$this->fs->mkdir($this->translationDir.'/Resources/translations');
$this->fs->mkdir($this->translationDir.'/Resources/views');
$this->fs->mkdir($this->translationDir.'/translations');
$this->fs->mkdir($this->translationDir.'/templates');
}

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

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

$application = new Application($kernel);
$application->add($command);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ class TranslatorPass implements CompilerPassInterface
private $translatorServiceId;
private $readerServiceId;
private $loaderTag;
private $debugCommandServiceId;
private $updateCommandServiceId;

public function __construct($translatorServiceId = 'translator.default', $readerServiceId = 'translation.loader', $loaderTag = 'translation.loader')
public function __construct($translatorServiceId = 'translator.default', $readerServiceId = 'translation.loader', $loaderTag = 'translation.loader', $debugCommandServiceId = 'console.command.translation_debug', $updateCommandServiceId = 'console.command.translation_update')
{
if ('translation.loader' === $readerServiceId && 2 > func_num_args()) {
@trigger_error('The default value for $readerServiceId will change in 4.0 to "translation.reader".', E_USER_DEPRECATED);
Expand All @@ -31,6 +33,8 @@ public function __construct($translatorServiceId = 'translator.default', $reader
$this->translatorServiceId = $translatorServiceId;
$this->readerServiceId = $readerServiceId;
$this->loaderTag = $loaderTag;
$this->debugCommandServiceId = $debugCommandServiceId;
$this->updateCommandServiceId = $updateCommandServiceId;
}

public function process(ContainerBuilder $container)
Expand Down Expand Up @@ -75,5 +79,10 @@ public function process(ContainerBuilder $container)
->replaceArgument(0, ServiceLocatorTagPass::register($container, $loaderRefs))
->replaceArgument(3, $loaders)
;

if ($container->hasParameter('twig.default_path')) {
$container->getDefinition($this->debugCommandServiceId)->replaceArgument(4, $container->getParameter('twig.default_path'));
$container->getDefinition($this->updateCommandServiceId)->replaceArgument(5, $container->getParameter('twig.default_path'));
}
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.